Skip to content

Commit c7ff1b6

Browse files
committed
fix: consider file seperators
1 parent 964460d commit c7ff1b6

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

jscomp/gentype/Paths.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ let findNameSpace cmt =
2828
cmt |> Filename.basename |> (Filename.chop_extension [@doesNotRaise])
2929
|> keepAfterDash
3030

31-
let remove_project_root_from_absolute_path ~(config : Config.t) source_path =
32-
let i = String.length config.projectRoot + 1 in
33-
let n = String.length source_path - i in
34-
(String.sub source_path i n [@doesNotRaise])
31+
let removePathPrefix ~prefix path =
32+
let normalizedPrefix = Filename.concat prefix "" in
33+
let prefixLen = String.length normalizedPrefix in
34+
let pathLen = String.length path in
35+
let isPrefix =
36+
prefixLen <= pathLen && String.sub path 0 prefixLen = normalizedPrefix
37+
in
38+
if isPrefix then String.sub path prefixLen (pathLen - prefixLen) else path
3539

3640
let appendSuffix ~config sourcePath =
3741
(sourcePath |> handleNamespace) ^ ModuleExtension.tsInputFileSuffix ~config
@@ -40,7 +44,7 @@ let getOutputFileRelative ~config sourcePath =
4044
if Filename.is_relative sourcePath then appendSuffix ~config sourcePath
4145
else
4246
let relative_path =
43-
remove_project_root_from_absolute_path ~config sourcePath
47+
removePathPrefix ~prefix:config.projectRoot sourcePath
4448
in
4549
appendSuffix ~config relative_path
4650

@@ -54,7 +58,7 @@ let getOutputFile ~(config : Config.t) sourcePath =
5458
else
5559
(* for absolute paths we want to place the output beside the source file *)
5660
let relative_path =
57-
remove_project_root_from_absolute_path ~config sourcePath
61+
removePathPrefix ~prefix:config.projectRoot sourcePath
5862
in
5963
computeAbsoluteOutputFilePath ~config relative_path
6064

0 commit comments

Comments
 (0)