|
1 | 1 | let version = "4.06.1+BS" |
2 | 2 |
|
3 | | -(* FIXME: Unreliable resolution *) |
| 3 | +exception Cannot_find_standard_library of string |
| 4 | + |
| 5 | +(* This resolves the location of the standard library starting from the location of bsc.exe. |
| 6 | + Unfortunately it is rather hacky, as we have to detect and handle several different package layouts here. *) |
4 | 7 | let standard_library = |
5 | | - 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" |
| 8 | + let build_path rest path = |
| 9 | + String.concat Filename.dir_sep (List.rev rest @ path) |
| 10 | + in |
| 11 | + match |
| 12 | + Sys.executable_name |> Filename.dirname |
| 13 | + |> String.split_on_char Filename.dir_sep.[0] |
| 14 | + |> List.rev |
| 15 | + with |
| 16 | + (* 1. Packages installed via pnpm |
| 17 | + - bin: node_modules/.pnpm/@[email protected]/node_modules/@rescript/darwin-arm64/bin |
| 18 | + - stdlib: node_modules/rescript/lib/ocaml (symlink) |
| 19 | + *) |
| 20 | + | "bin" :: _platform :: "@rescript" :: "node_modules" :: _package :: ".pnpm" |
| 21 | + :: "node_modules" :: rest -> |
| 22 | + build_path rest ["node_modules"; "rescript"; "lib"; "ocaml"] |
| 23 | + (* 2. Packages installed via npm |
| 24 | + - bin: node_modules/@rescript/{platform}/bin |
| 25 | + - stdlib: node_modules/rescript/lib/ocaml |
| 26 | + *) |
| 27 | + | "bin" :: _platform :: "@rescript" :: "node_modules" :: rest -> |
| 28 | + build_path rest ["node_modules"; "rescript"; "lib"; "ocaml"] |
| 29 | + (* 3. Local development in the `rescript` repo |
| 30 | + - bin: <repo>/packages/@rescript/{platform}/bin |
| 31 | + - stdlib: <repo>/lib/ocaml |
| 32 | + *) |
| 33 | + | "bin" :: _platform :: "@rescript" :: "packages" :: rest -> |
| 34 | + build_path rest ["lib"; "ocaml"] |
| 35 | + | _ -> |
| 36 | + raise (Cannot_find_standard_library "Unrecognized node_modules structure") |
21 | 37 |
|
22 | 38 | let standard_library_default = standard_library |
23 | 39 |
|
|
0 commit comments