From caba64662664c216b55bcbbb27c9d33b600ccdcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C4=8Cavoj?= Date: Fri, 26 Apr 2024 09:38:09 +0200 Subject: [PATCH] Fix `getRelativePath` behaviour on case-insensitive filesystems 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. --- src/utils/get-relative-path.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/get-relative-path.ts b/src/utils/get-relative-path.ts index 1c705764..265c6a48 100644 --- a/src/utils/get-relative-path.ts +++ b/src/utils/get-relative-path.ts @@ -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);