Skip to content

Commit c017659

Browse files
committed
fix: relative path handling
1 parent 81a25e1 commit c017659

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

src/utils/fileSystemHandler.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@ export const ensureFileExists = async (filePath: string): Promise<void> => {
2323
};
2424

2525
/**
26-
* Traverse up a file path and search for the given file name.
26+
* Traverse up a file path and search for the given file name. Always returns an absolute path.
2727
*
2828
* @param start File or folder path to start searching from
2929
* @param fileName File name to search for
3030
*/
3131
export function searchUp(start: SourcePath, fileName: string): string | undefined {
32-
const filePath = path.join(start, fileName);
32+
const absoluteStart = path.isAbsolute(start) ? start : path.join(process.cwd(), start);
33+
const filePath = path.join(absoluteStart, fileName);
3334
if (fs.existsSync(filePath)) {
3435
return filePath;
3536
}
3637

37-
const normalizedStart = path.normalize(start);
38-
const parent = path.dirname(normalizedStart);
38+
const normalizedAbsoluteStart = path.normalize(absoluteStart);
39+
const parent = path.dirname(normalizedAbsoluteStart);
3940

4041
// If we're at root, stop (don't try to go up with ..)
41-
if (parent === normalizedStart || normalizedStart === path.parse(normalizedStart).root) {
42+
if (parent === normalizedAbsoluteStart || normalizedAbsoluteStart === path.parse(normalizedAbsoluteStart).root) {
4243
return;
4344
}
4445

test/nuts/local/searchUp/searchUp.nut.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ describe('searchUp nut test', () => {
6666
);
6767
expect(path.isAbsolute(relativeStartPath)).to.be.false;
6868
const result = searchUp(relativeStartPath, 'target.txt');
69-
const expected = path.relative(
70-
session.project.dir,
71-
path.join(session.project.dir, 'level1', 'level2', 'target.txt')
72-
);
69+
const expected = path.join(session.project.dir, 'level1', 'level2', 'target.txt');
7370

7471
expect(result).to.equal(expected);
7572
});
@@ -81,7 +78,7 @@ describe('searchUp nut test', () => {
8178
);
8279
expect(path.isAbsolute(relativeStartPath)).to.be.false;
8380
const result = searchUp(relativeStartPath, '.gitignore');
84-
const expected = path.relative(session.project.dir, path.join(session.project.dir, '.gitignore'));
81+
const expected = path.join(session.project.dir, '.gitignore');
8582

8683
expect(result).to.equal(expected);
8784
});
@@ -95,7 +92,7 @@ describe('searchUp nut test', () => {
9592
const absoluteStartPath = path.resolve(session.project.dir, relativeStartPath);
9693
fs.writeFileSync(path.join(absoluteStartPath, 'localFile.txt'), 'local content');
9794
const result = searchUp(relativeStartPath, 'localFile.txt');
98-
const expected = path.relative(session.project.dir, path.join(absoluteStartPath, 'localFile.txt'));
95+
const expected = path.join(absoluteStartPath, 'localFile.txt');
9996

10097
expect(result).to.equal(expected);
10198
});
@@ -120,10 +117,7 @@ describe('searchUp nut test', () => {
120117
const absoluteFilePath = path.resolve(session.project.dir, relativeFilePath);
121118
fs.writeFileSync(absoluteFilePath, 'content');
122119
const result = searchUp(relativeFilePath, 'target.txt');
123-
const expected = path.relative(
124-
session.project.dir,
125-
path.join(session.project.dir, 'level1', 'level2', 'target.txt')
126-
);
120+
const expected = path.join(session.project.dir, 'level1', 'level2', 'target.txt');
127121

128122
expect(result).to.equal(expected);
129123
});

0 commit comments

Comments
 (0)