Skip to content

Commit 3b6ad82

Browse files
committed
fix: consider file seperators
1 parent f62fd61 commit 3b6ad82

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

compiler/gentype/Paths.ml

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

32-
let remove_project_root_from_absolute_path ~(config : Config.t) source_path =
33-
let i = String.length config.project_root + 1 in
34-
let n = String.length source_path - i in
35-
(String.sub source_path i n [@doesNotRaise])
32+
let remove_path_prefix ~prefix path =
33+
let normalized_prefix = Filename.concat prefix "" in
34+
let prefix_len = String.length normalized_prefix in
35+
let path_len = String.length path in
36+
let is_prefix =
37+
prefix_len <= path_len && String.sub path 0 prefix_len = normalized_prefix
38+
in
39+
if is_prefix then String.sub path prefix_len (path_len - prefix_len) else path
3640

3741
let append_suffix ~config source_path =
3842
(source_path |> handle_namespace)
@@ -42,7 +46,7 @@ let get_output_file_relative ~(config : Config.t) source_path =
4246
if Filename.is_relative source_path then append_suffix ~config source_path
4347
else
4448
let relative_path =
45-
remove_project_root_from_absolute_path ~config source_path
49+
remove_path_prefix ~prefix:config.project_root source_path
4650
in
4751
append_suffix ~config relative_path
4852

@@ -56,7 +60,7 @@ let get_output_file ~(config : Config.t) sourcePath =
5660
else
5761
(* for absolute paths we want to place the output beside the source file *)
5862
let relative_path =
59-
remove_project_root_from_absolute_path ~config sourcePath
63+
remove_path_prefix ~prefix:config.project_root sourcePath
6064
in
6165
compute_absolute_output_file_path ~config relative_path
6266

0 commit comments

Comments
 (0)