Skip to content

Commit

Permalink
Fix getRelativePath behaviour on case-insensitive filesystems
Browse files Browse the repository at this point in the history
Typically when resolving an import in file `A/a.ts` "from" file `B/b.ts` e.g.

  import x from "../B/b"

the function will be called as `getRelativePath("A", "b")`. Note the `"b"` path
has been lowercased by tsc at some point and that `"A"` may not exist on disk
at this point as it's being generated.

The previous logic would fail on `realpathSync.native(from)` because `from =
"A"` does not exist on disk yet and would not fix the casing of `to`. As such
`"../b"` would be returned instead of the correct `"../B"`. The simplest fix is
to swap the realpath calls which is what we do here, as typically `to` will
exist (it's what is being imported) and `from` may not.
  • Loading branch information
sammko committed Apr 26, 2024
1 parent ea7dad5 commit caba646
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/get-relative-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function getMatchPortion(from: string, to: string) {

export function getRelativePath(from: string, to: string) {
try {
from = fs.realpathSync.native(from);
to = fs.realpathSync.native(to);
from = fs.realpathSync.native(from);
} catch {
if (!getIsFsCaseSensitive()) {
const matchPortion = getMatchPortion(from, to);
Expand Down

0 comments on commit caba646

Please sign in to comment.