Skip to content

Commit a90ccc1

Browse files
committed
feat: handle paths via source_file instead of cmt_file
1 parent fdf07c7 commit a90ccc1

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

compiler/gentype/GenTypeMain.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ let process_cmt_file cmt =
138138
if !Debug.basic then Log_.item "Cmt %s\n" cmt;
139139
let cmt_file = cmt |> Paths.get_cmt_file in
140140
if cmt_file <> "" then
141-
let output_file = cmt |> Paths.get_output_file ~config in
142-
let output_file_relative = cmt |> Paths.get_output_file_relative ~config in
143141
let file_name = cmt |> Paths.get_module_name in
144142
let is_interface = Filename.check_suffix cmt_file ".cmti" in
145143
let input_cmt, has_gentype_annotations =
@@ -155,6 +153,10 @@ let process_cmt_file cmt =
155153
| true -> ".resi"
156154
| false -> ".res")
157155
in
156+
let output_file = source_file |> Paths.get_output_file ~config in
157+
let output_file_relative =
158+
source_file |> Paths.get_output_file_relative ~config
159+
in
158160
let resolver =
159161
ModuleResolver.create_lazy_resolver ~config
160162
~extensions:[".res"; ".shim.ts"] ~exclude_file:(fun fname ->

compiler/gentype/Paths.ml

+31-5
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,43 @@ let find_name_space cmt =
2929
cmt |> Filename.basename |> (Filename.chop_extension [@doesNotRaise])
3030
|> keep_after_dash
3131

32-
let get_output_file_relative ~config cmt =
33-
(cmt |> handle_namespace) ^ ModuleExtension.ts_input_file_suffix ~config
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])
3436

35-
let get_output_file ~(config : Config.t) cmt =
36-
Filename.concat config.project_root (get_output_file_relative ~config cmt)
37+
let get_output_file_relative ~config source_path =
38+
if Filename.is_relative source_path then
39+
(source_path |> handle_namespace)
40+
^ ModuleExtension.ts_input_file_suffix ~config
41+
else
42+
let relative_path =
43+
remove_project_root_from_absolute_path ~config source_path
44+
in
45+
(relative_path |> handle_namespace)
46+
^ ModuleExtension.ts_input_file_suffix ~config
47+
48+
let get_output_file ~(config : Config.t) sourcePath =
49+
if Filename.is_relative sourcePath then
50+
(* assuming a relative path from the project root *)
51+
Filename.concat config.project_root
52+
(get_output_file_relative ~config sourcePath)
53+
else
54+
(* we want to place the output beside the source file *)
55+
let relative_path =
56+
remove_project_root_from_absolute_path ~config sourcePath
57+
in
58+
Filename.concat config.project_root
59+
(get_output_file_relative ~config relative_path)
3760

3861
let get_module_name cmt =
3962
cmt |> handle_namespace |> Filename.basename |> ModuleName.from_string_unsafe
4063

4164
let get_cmt_file cmt =
42-
let path_cmt = Filename.concat (Sys.getcwd ()) cmt in
65+
let path_cmt =
66+
if Filename.is_relative cmt then Filename.concat (Sys.getcwd ()) cmt
67+
else cmt
68+
in
4369
let cmt_file =
4470
if Filename.check_suffix path_cmt ".cmt" then
4571
let path_cmt_lower_case =

0 commit comments

Comments
 (0)