Skip to content

Commit d9c565c

Browse files
committed
Fix finding the standard library for pnpm
1 parent d70c7fa commit d9c565c

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

compiler/ext/config.ml

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
let version = "4.06.1+BS"
22

3-
(* FIXME: Unreliable resolution *)
43
let standard_library =
4+
(* This resolves the location of the standard library starting from the location of bsc.exe.
5+
Unfortunately it is rather hacky, as we have to detect and handle several cases here:
6+
1.) Local development in the `rescript` repo
7+
2.) Packages installed via npm
8+
3.) Packages installed via pnpm *)
9+
let bin_dir = Filename.dirname Sys.executable_name in
10+
let dir_sep = Filename.dir_sep in
511
let ( // ) = Filename.concat in
6-
let exe_path = Sys.executable_name in
7-
if Ext_string.contain_substring exe_path ("node_modules" // "@rescript") then
8-
(* node_modules/@rescript/{platform}/bin *)
9-
Filename.dirname exe_path // Filename.parent_dir_name
10-
// Filename.parent_dir_name // Filename.parent_dir_name // "rescript"
11-
// "lib" // "ocaml"
12-
else if Ext_string.contain_substring exe_path ("node_modules" // "rescript")
13-
then
14-
(* node_modules/rescript/{platform} *)
15-
Filename.dirname exe_path // Filename.parent_dir_name // "lib" // "ocaml"
16-
else
17-
(* git repo: rescript/packages/@rescript/{platform}/bin *)
18-
Filename.dirname exe_path // Filename.parent_dir_name
19-
// Filename.parent_dir_name // Filename.parent_dir_name
20-
// Filename.parent_dir_name // "lib" // "ocaml"
12+
13+
(* Find position of /node_modules/ in bin_dir *)
14+
match Ext_string.find ~sub:(dir_sep ^ "node_modules" ^ dir_sep) bin_dir with
15+
| -1 ->
16+
(* 1. Not installed in node_modules => local development in the `rescript` repo
17+
- bin: <repo>/packages/@rescript/{platform}/bin
18+
- stdlib: <repo>/lib/ocaml
19+
*)
20+
let up = Filename.parent_dir_name in
21+
bin_dir // up // up // up // up // "lib" // "ocaml"
22+
| i -> (
23+
(* to account for the / before node_modules *)
24+
let i = i + 1 in
25+
let project_path = String.sub bin_dir 0 i in
26+
let subpath = String.sub bin_dir i (String.length bin_dir - i) in
27+
28+
match subpath |> String.split_on_char dir_sep.[0] with
29+
(* 2. Packages installed via npm
30+
- bin: node_modules/@rescript/{platform}/bin
31+
- stdlib: node_modules/rescript/lib/ocaml
32+
*)
33+
| ["node_modules"; "@rescript"; _platform; "bin"] ->
34+
project_path // "node_modules" // "rescript" // "lib" // "ocaml"
35+
(* 3. Packages installed via pnpm
36+
- bin: node_modules/.pnpm/@[email protected]/node_modules/@rescript/darwin-arm64/bin
37+
- stdlib: node_modules/.pnpm/[email protected]/node_modules/rescript/lib/ocaml
38+
*)
39+
| [
40+
"node_modules";
41+
".pnpm";
42+
platform_package;
43+
"node_modules";
44+
"@rescript";
45+
_platform;
46+
"bin";
47+
] -> (
48+
match platform_package |> String.split_on_char '@' with
49+
| [""; _prefix; version] ->
50+
project_path // "node_modules" // ".pnpm" // ("rescript@" ^ version)
51+
// "node_modules" // "rescript" // "lib" // "ocaml"
52+
| _ -> assert false)
53+
| _ -> assert false)
2154

2255
let standard_library_default = standard_library
2356

0 commit comments

Comments
 (0)