Skip to content

Commit bfe0211

Browse files
committed
fix(paths): respect desired path on case-insensitive file systems
1 parent 90d5e9d commit bfe0211

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/utils/get-relative-path.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ function getIsFsCaseSensitive() {
4646
return isCaseSensitiveFilesystem;
4747
}
4848

49-
function getMatchPortion(from: string, to: string) {
49+
/**
50+
* @private The export is only for unit tests
51+
*/
52+
export function getMatchPortion(from: string, to: string) {
5053
const lowerFrom = from.toLocaleLowerCase();
5154
const lowerTo = to.toLocaleLowerCase();
5255

@@ -58,7 +61,7 @@ function getMatchPortion(from: string, to: string) {
5861
i++;
5962
}
6063

61-
return from.slice(0, i);
64+
return to.slice(0, i);
6265
}
6366

6467
// endregion

test/tests/get-match-portion.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getMatchPortion } from '../../src/utils/get-relative-path'
2+
3+
describe(`getMatchPortion`, () => {
4+
it("works in a simple case", () => {
5+
expect(getMatchPortion("/foo/bar", "/foo/quux")).toBe("/foo/");
6+
});
7+
8+
// We use the function getMatchPortion to generate a new path for “to”, so let’s preserve
9+
// the case where possible.
10+
// Otherwise we are introducing inconsistency for our users, who may have had import from Foo,
11+
// their file is named Foo, but we rewrite the path to foo.
12+
// Although the file is still accessible in the file system, other tools might reasonably
13+
// complain about the unexpected case mismatch.
14+
it("prioritizes the casing of the “to” parameter", () => {
15+
expect(getMatchPortion("/foo/bar", "/foO/quux")).toBe("/foO/");
16+
expect(getMatchPortion("/foo/bar", "/foo/Bonk")).toBe("/foo/B");
17+
});
18+
});

0 commit comments

Comments
 (0)