Skip to content

fix(paths): respect desired path on case-insensitive file systems #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils/get-relative-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ function getIsFsCaseSensitive() {
return isCaseSensitiveFilesystem;
}

function getMatchPortion(from: string, to: string) {
/**
* @private The export is only for unit tests
*/
export function getMatchPortion(from: string, to: string) {
const lowerFrom = from.toLocaleLowerCase();
const lowerTo = to.toLocaleLowerCase();

Expand All @@ -58,7 +61,7 @@ function getMatchPortion(from: string, to: string) {
i++;
}

return from.slice(0, i);
return to.slice(0, i);
}

// endregion
Expand Down
18 changes: 18 additions & 0 deletions test/tests/get-match-portion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getMatchPortion } from '../../src/utils/get-relative-path'

describe(`getMatchPortion`, () => {
it("works in a simple case", () => {
expect(getMatchPortion("/foo/bar", "/foo/quux")).toBe("/foo/");
});

// We use the function getMatchPortion to generate a new path for “to”, so let’s preserve
// the case where possible.
// Otherwise we are introducing inconsistency for our users, who may have had import from Foo,
// their file is named Foo, but we rewrite the path to foo.
// Although the file is still accessible in the file system, other tools might reasonably
// complain about the unexpected case mismatch.
it("prioritizes the casing of the “to” parameter", () => {
expect(getMatchPortion("/foo/bar", "/foO/quux")).toBe("/foO/");
expect(getMatchPortion("/foo/bar", "/foo/Bonk")).toBe("/foo/B");
});
});
Loading