Skip to content

Commit f1a0030

Browse files
committed
bsc: add -runtime-path option
1 parent 122a3a6 commit f1a0030

File tree

6 files changed

+108
-108
lines changed

6 files changed

+108
-108
lines changed

analysis/src/ModuleResolution.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let ( /+ ) = Filename.concat
33
let rec resolveNodeModulePath ~startPath name =
44
if name = "@rescript/runtime" then
55
(* Hack: we need a reliable way to resolve modules in monorepos. *)
6-
Some Runtime_package.path
6+
Some !Runtime_package.path
77
else
88
let scope = Filename.dirname name in
99
let name = Filename.basename name in

compiler/bsc/rescript_compiler_main.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ let set_abs_input_name sourcefile =
5050
sourcefile
5151
let setup_outcome_printer () = Lazy.force Res_outcome_printer.setup
5252

53+
let setup_runtime_path path = Runtime_package.path := path
54+
5355
let process_file sourcefile ?kind ppf =
5456
(* This is a better default then "", it will be changed later
5557
The {!Location.input_name} relies on that we write the binary ast
@@ -402,6 +404,9 @@ let command_line_flags : (string * Bsc_args.spec * string) array =
402404
"<list> Enable or disable error status for warnings according\n\
403405
to <list>. See option -w for the syntax of <list>.\n\
404406
Default setting is " ^ Bsc_warnings.defaults_warn_error );
407+
( "-runtime-path",
408+
string_call setup_runtime_path,
409+
"*internal* Set the path of the runtime package (@rescript/runtime)" );
405410
( "-make-runtime",
406411
unit_call Js_packages_state.make_runtime,
407412
"*internal* make runtime library" );
Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,182 +17,182 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
(*
25-
let (=) (x : int) (y:float) = assert false
25+
let (=) (x : int) (y:float) = assert false
2626
*)
2727

2828
#ifdef BROWSER
2929

30-
let string_of_module_id_in_browser (x : Lam_module_ident.t) =
30+
let string_of_module_id_in_browser (x : Lam_module_ident.t) =
3131
match x.kind with
3232
| External {name} -> name
33-
| Runtime | Ml ->
33+
| Runtime | Ml ->
3434
"./stdlib/" ^ x.id.name ^ ".js"
3535

36-
let string_of_module_id
36+
let string_of_module_id
3737
(id : Lam_module_ident.t)
3838
~output_dir:(_:string)
3939
(_module_system : Js_packages_info.module_system)
4040
= string_of_module_id_in_browser id
4141

4242
#else
43-
44-
let (//) = Filename.concat
43+
44+
let (//) = Filename.concat
4545

4646

47-
let fix_path_for_windows : string -> string =
47+
let fix_path_for_windows : string -> string =
4848
if Ext_sys.is_windows_or_cygwin then Ext_string.replace_backward_slash
49-
else fun s -> s
49+
else fun s -> s
5050

5151

52-
(* dependency is runtime module *)
53-
let get_runtime_module_path
54-
(dep_module_id : Lam_module_ident.t)
52+
(* dependency is runtime module *)
53+
let get_runtime_module_path
54+
(dep_module_id : Lam_module_ident.t)
5555
(current_package_info : Js_packages_info.t)
56-
(module_system : Js_packages_info.module_system) =
57-
let current_info_query =
56+
(module_system : Js_packages_info.module_system) =
57+
let current_info_query =
5858
Js_packages_info.query_package_infos current_package_info
5959
module_system in
60-
let js_file =
61-
Ext_namespace.js_name_of_modulename dep_module_id.id.name
62-
Upper Literals.suffix_js in
63-
match current_info_query with
60+
let js_file =
61+
Ext_namespace.js_name_of_modulename dep_module_id.id.name
62+
Upper Literals.suffix_js in
63+
match current_info_query with
6464
| Package_not_found -> assert false
65-
| Package_script ->
66-
Js_packages_info.runtime_package_path module_system js_file
67-
| Package_found pkg ->
68-
let dep_path =
69-
"lib" // Js_packages_info.runtime_dir_of_module_system module_system in
70-
if Js_packages_info.is_runtime_package current_package_info then
65+
| Package_script ->
66+
Js_packages_info.runtime_package_path module_system js_file
67+
| Package_found pkg ->
68+
let dep_path =
69+
"lib" // Js_packages_info.runtime_dir_of_module_system module_system in
70+
if Js_packages_info.is_runtime_package current_package_info then
7171
Ext_path.node_rebase_file
7272
~from:pkg.rel_path
73-
~to_:dep_path
73+
~to_:dep_path
7474
js_file
7575
(* TODO: we assume that both [x] and [path] could only be relative path
7676
which is guaranteed by [-bs-package-output]
7777
*)
78-
else
79-
match module_system with
80-
| Commonjs | Esmodule ->
81-
Js_packages_info.runtime_package_path module_system js_file
78+
else
79+
match module_system with
80+
| Commonjs | Esmodule ->
81+
Js_packages_info.runtime_package_path module_system js_file
8282
(* Note we did a post-processing when working on Windows *)
83-
| Es6_global
84-
->
85-
(* lib/ocaml/xx.cmj --
83+
| Es6_global
84+
->
85+
(* lib/ocaml/xx.cmj --
8686
HACKING: FIXME
8787
maybe we can caching relative package path calculation or employ package map *)
8888
(* assert false *)
89-
Ext_path.rel_normalized_absolute_path
89+
Ext_path.rel_normalized_absolute_path
9090
~from:(
91-
Js_packages_info.get_output_dir
91+
Js_packages_info.get_output_dir
9292
current_package_info
9393
~package_dir:(Lazy.force Ext_path.package_dir)
9494
module_system )
95-
(*Invariant: the package path to rescript, it is used to
95+
(*Invariant: the package path to rescript, it is used to
9696
calculate relative js path
9797
*)
98-
(Runtime_package.path // dep_path // js_file)
98+
(!Runtime_package.path // dep_path // js_file)
9999

100100
(* [output_dir] is decided by the command line argument *)
101-
let string_of_module_id
102-
(dep_module_id : Lam_module_ident.t)
101+
let string_of_module_id
102+
(dep_module_id : Lam_module_ident.t)
103103
~(output_dir : string )
104-
(module_system : Js_packages_info.module_system)
104+
(module_system : Js_packages_info.module_system)
105105
: string =
106-
let current_package_info = Js_packages_state.get_packages_info () in
107-
fix_path_for_windows (
108-
match dep_module_id.kind with
106+
let current_package_info = Js_packages_state.get_packages_info () in
107+
fix_path_for_windows (
108+
match dep_module_id.kind with
109109
| External {name} -> name (* the literal string for external package *)
110-
(* This may not be enough,
111-
1. For cross packages, we may need settle
110+
(* This may not be enough,
111+
1. For cross packages, we may need settle
112112
down a single js package
113113
2. We may need es6 path for dead code elimination
114-
But frankly, very few JS packages have no dependency,
115-
so having plugin may sound not that bad
114+
But frankly, very few JS packages have no dependency,
115+
so having plugin may sound not that bad
116116
*)
117-
| Runtime ->
117+
| Runtime ->
118118
get_runtime_module_path dep_module_id current_package_info module_system
119-
| Ml ->
120-
let current_info_query =
121-
Js_packages_info.query_package_infos
119+
| Ml ->
120+
let current_info_query =
121+
Js_packages_info.query_package_infos
122122
current_package_info
123-
module_system
123+
module_system
124124
in
125-
match Lam_compile_env.get_package_path_from_cmj dep_module_id with
126-
| (package_path, dep_package_info, case) ->
125+
match Lam_compile_env.get_package_path_from_cmj dep_module_id with
126+
| (package_path, dep_package_info, case) ->
127127

128128

129-
let dep_info_query =
130-
Js_packages_info.query_package_infos dep_package_info module_system
131-
in
129+
let dep_info_query =
130+
Js_packages_info.query_package_infos dep_package_info module_system
131+
in
132132
match dep_info_query, current_info_query with
133-
| Package_not_found , _ ->
133+
| Package_not_found , _ ->
134134
Bs_exception.error (Missing_ml_dependency dep_module_id.id.name)
135-
| Package_script , Package_found _ ->
135+
| Package_script , Package_found _ ->
136136
Bs_exception.error (Dependency_script_module_dependent_not dep_module_id.id.name)
137137
| (Package_script | Package_found _ ), Package_not_found -> assert false
138138

139-
| Package_found ({suffix} as pkg), Package_script
140-
->
141-
let js_file =
142-
Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in
139+
| Package_found ({suffix} as pkg), Package_script
140+
->
141+
let js_file =
142+
Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in
143143
pkg.pkg_rel_path // js_file
144144
| Package_found ({suffix } as dep_pkg),
145-
Package_found cur_pkg ->
146-
let js_file =
147-
Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in
145+
Package_found cur_pkg ->
146+
let js_file =
147+
Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in
148148

149-
if Js_packages_info.same_package_by_name current_package_info dep_package_info then
149+
if Js_packages_info.same_package_by_name current_package_info dep_package_info then
150150
Ext_path.node_rebase_file
151151
~from:cur_pkg.rel_path
152-
~to_:dep_pkg.rel_path
152+
~to_:dep_pkg.rel_path
153153
js_file
154154
(* TODO: we assume that both [x] and [path] could only be relative path
155155
which is guaranteed by [-bs-package-output]
156156
*)
157-
else
158-
if Js_packages_info.is_runtime_package dep_package_info then
157+
else
158+
if Js_packages_info.is_runtime_package dep_package_info then
159159
get_runtime_module_path dep_module_id current_package_info module_system
160-
else
161-
begin match module_system with
162-
| Commonjs | Esmodule ->
160+
else
161+
begin match module_system with
162+
| Commonjs | Esmodule ->
163163
dep_pkg.pkg_rel_path // js_file
164164
(* Note we did a post-processing when working on Windows *)
165-
| Es6_global
166-
->
167-
begin
168-
Ext_path.rel_normalized_absolute_path
165+
| Es6_global
166+
->
167+
begin
168+
Ext_path.rel_normalized_absolute_path
169169
~from:(
170-
Js_packages_info.get_output_dir
170+
Js_packages_info.get_output_dir
171171
current_package_info
172172
~package_dir:(Lazy.force Ext_path.package_dir)
173-
module_system
173+
module_system
174174
)
175-
(package_path // dep_pkg.rel_path // js_file)
175+
(package_path // dep_pkg.rel_path // js_file)
176176
end
177177
end
178-
| Package_script, Package_script
179-
->
180-
let js_file =
181-
Ext_namespace.js_name_of_modulename dep_module_id.id.name case Literals.suffix_js in
182-
match Config_util.find_opt js_file with
183-
| Some file ->
184-
let basename = Filename.basename file in
185-
let dirname = Filename.dirname file in
178+
| Package_script, Package_script
179+
->
180+
let js_file =
181+
Ext_namespace.js_name_of_modulename dep_module_id.id.name case Literals.suffix_js in
182+
match Config_util.find_opt js_file with
183+
| Some file ->
184+
let basename = Filename.basename file in
185+
let dirname = Filename.dirname file in
186186
Ext_path.node_rebase_file
187187
~from:(
188-
Ext_path.absolute_cwd_path
188+
Ext_path.absolute_cwd_path
189189
output_dir)
190190
~to_:(
191-
Ext_path.absolute_cwd_path
191+
Ext_path.absolute_cwd_path
192192

193193
dirname)
194-
basename
195-
| None ->
194+
basename
195+
| None ->
196196
Bs_exception.error (Js_not_found js_file))
197197

198198
#endif

compiler/core/res_compmisc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
let init_path () =
2626
let stdlib_dir =
2727
let ( // ) = Filename.concat in
28-
Runtime_package.path // "lib" // "ocaml"
28+
!Runtime_package.path // "lib" // "ocaml"
2929
in
3030
let dirs = !Clflags.include_dirs in
3131
let exp_dirs = List.map (Misc.expand_directory stdlib_dir) dirs in

compiler/ext/runtime_package.ml

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
let name = "@rescript/runtime"
22

3-
(* This resolves the location of the standard library starting from the location of bsc.exe
4-
(@rescript/{platform}/bin/bsc.exe), handling different supported package layouts. *)
5-
let path =
3+
(* Simple default approach to find the runtime package path. This will not work with all package managers/layouts. *)
4+
let default_path =
65
let build_path rest path =
76
String.concat Filename.dir_sep (List.rev_append rest path)
87
in
@@ -11,24 +10,20 @@ let path =
1110
|> String.split_on_char Filename.dir_sep.[0]
1211
|> List.rev
1312
with
14-
(* 1. Packages installed via pnpm
15-
- bin: node_modules/.pnpm/@[email protected]/node_modules/@rescript/darwin-arm64/bin
16-
- runtime: node_modules/.pnpm/node_modules/@rescript/runtime (symlink)
17-
*)
18-
| "bin" :: _platform :: "@rescript" :: "node_modules" :: _package :: ".pnpm"
19-
:: "node_modules" :: rest ->
20-
build_path rest
21-
["node_modules"; ".pnpm"; "node_modules"; "@rescript"; "runtime"]
22-
(* 2. Packages installed via npm
13+
(* 1. Packages installed via npm
2314
- bin: node_modules/@rescript/{platform}/bin
2415
- runtime: node_modules/@rescript/runtime
2516
*)
2617
| "bin" :: _platform :: "@rescript" :: "node_modules" :: rest ->
2718
build_path rest ["node_modules"; "@rescript"; "runtime"]
28-
(* 3. Several other cases that can occur in local development, e.g.
19+
(* 2. Several other cases that can occur in local development, e.g.
2920
- bin: <repo>/packages/@rescript/{platform}/bin, <repo>/_build/install/default/bin
3021
- runtime: <repo>/packages/@rescript/runtime
3122
*)
3223
| _ :: _ :: _ :: _ :: rest ->
3324
build_path rest ["packages"; "@rescript"; "runtime"]
3425
| _ -> ""
26+
27+
(* To support pnpm and other package managers/layouts, we determine the path on the JS side and pass it in
28+
via -runtime-path to override the default. *)
29+
let path = ref default_path

compiler/ext/runtime_package.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
val name : string
22

3-
val path : string
3+
val path : string ref

0 commit comments

Comments
 (0)