Skip to content

Commit 373c19a

Browse files
committed
Reliably determine standard lib dir
1 parent c791ffb commit 373c19a

File tree

8 files changed

+47
-43
lines changed

8 files changed

+47
-43
lines changed

cli/bsc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import { execFileSync } from "node:child_process";
66

77
import { bsc_exe } from "./common/bins.js";
8+
import { stdlibDir } from "./common/stdlib.js";
89

910
const delegate_args = process.argv.slice(2);
1011

1112
try {
12-
execFileSync(bsc_exe, delegate_args, { stdio: "inherit" });
13+
execFileSync(bsc_exe, delegate_args, {
14+
stdio: "inherit",
15+
env: { ...process.env, RESCRIPT_STDLIB: stdlibDir },
16+
});
1317
} catch (e) {
1418
if (e.code === "ENOENT") {
1519
console.error(String(e));

cli/common/stdlib.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @ts-check
2+
import * as path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = path.dirname(__filename);
7+
8+
const rescriptDir = path.dirname(path.dirname(__dirname));
9+
10+
export const stdlibDir = path.join(rescriptDir, "lib", "ocaml");

cli/rescript-legacy/bsb.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as path from "node:path";
88
import { WebSocket } from "#lib/minisocket";
99

1010
import { rescript_legacy_exe } from "../common/bins.js";
11+
import { stdlibDir } from "../common/stdlib.js";
1112

1213
const cwd = process.cwd();
1314
const lockFileName = path.join(cwd, ".bsb.lock");
@@ -52,6 +53,7 @@ function acquireBuild(args, options) {
5253
try {
5354
ownerProcess = child_process.spawn(rescript_legacy_exe, args, {
5455
stdio: "inherit",
56+
env: { ...process.env, RESCRIPT_STDLIB: stdlibDir },
5557
...options,
5658
});
5759
fs.writeFileSync(lockFileName, ownerProcess.pid.toString(), {

cli/rescript-tools.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import * as child_process from "node:child_process";
66

77
import { rescript_tools_exe } from "./common/bins.js";
8+
import { stdlibDir } from "./common/stdlib.js";
89

910
const args = process.argv.slice(2);
1011

11-
child_process.spawnSync(rescript_tools_exe, args, { stdio: "inherit" });
12+
child_process.spawnSync(rescript_tools_exe, args, {
13+
stdio: "inherit",
14+
env: { ...process.env, RESCRIPT_STDLIB: stdlibDir },
15+
});

cli/rescript.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
import * as child_process from "node:child_process";
66
import { bsc_exe, rescript_exe } from "./common/bins.js";
7+
import { stdlibDir } from "./common/stdlib.js";
8+
9+
/**
10+
* @type {import("node:child_process").ExecFileSyncOptions}
11+
*/
12+
const options = {
13+
stdio: "inherit",
14+
env: { ...process.env, RESCRIPT_STDLIB: stdlibDir },
15+
};
716

817
const args = process.argv.slice(2);
918

@@ -22,20 +31,18 @@ try {
2231
child_process.execFileSync(
2332
rescript_exe,
2433
[...subcommandWithArgs, "--bsc-path", bsc_exe],
25-
{
26-
stdio: "inherit",
27-
},
34+
options,
2835
);
2936
} else {
30-
child_process.execFileSync(rescript_exe, [...args], {
31-
stdio: "inherit",
32-
});
37+
child_process.execFileSync(rescript_exe, args, options);
3338
}
3439
} else {
3540
// no subcommand means build subcommand
36-
child_process.execFileSync(rescript_exe, [...args, "--bsc-path", bsc_exe], {
37-
stdio: "inherit",
38-
});
41+
child_process.execFileSync(
42+
rescript_exe,
43+
[...args, "--bsc-path", bsc_exe],
44+
options,
45+
);
3946
}
4047
} catch (err) {
4148
if (err.status !== undefined) {

compiler/ext/config.ml

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
1-
(* This resolves the location of the standard library starting from the location of bsc.exe,
2-
handling different supported package layouts. *)
1+
(* To determine the standard library path in a reliable way that works with all package managers /
2+
package directory layouts, we need to do it on the JS side, see cli/common/stdlib.js.
3+
We pass the directory to the compiler exe via the environment variable RESCRIPT_STDLIB. *)
34
let standard_library =
4-
let build_path rest path =
5-
String.concat Filename.dir_sep (List.rev_append rest path)
6-
in
7-
match
8-
Sys.executable_name |> Filename.dirname
9-
|> String.split_on_char Filename.dir_sep.[0]
10-
|> List.rev
11-
with
12-
(* 1. Packages installed via pnpm
13-
- bin: node_modules/.pnpm/@rescript+darwin-arm64@12.0.0-alpha.13/node_modules/@rescript/darwin-arm64/bin
14-
- stdlib: node_modules/rescript/lib/ocaml (symlink)
15-
*)
16-
| "bin" :: _platform :: "@rescript" :: "node_modules" :: _package :: ".pnpm"
17-
:: "node_modules" :: rest ->
18-
build_path rest ["node_modules"; "rescript"; "lib"; "ocaml"]
19-
(* 2. Packages installed via npm
20-
- bin: node_modules/@rescript/{platform}/bin
21-
- stdlib: node_modules/rescript/lib/ocaml
22-
*)
23-
| "bin" :: _platform :: "@rescript" :: "node_modules" :: rest ->
24-
build_path rest ["node_modules"; "rescript"; "lib"; "ocaml"]
25-
(* 3. Several other cases that can occur in local development, e.g.
26-
- bin: <repo>/packages/@rescript/{platform}/bin, <repo>/_build/install/default/bin
27-
- stdlib: <repo>/lib/ocaml
28-
*)
29-
| _ :: _ :: _ :: _ :: rest -> build_path rest ["lib"; "ocaml"]
30-
| _ -> ""
5+
match Sys.getenv_opt "RESCRIPT_STDLIB" with
6+
| Some path -> path
7+
| None -> ""
318

329
let cmi_magic_number = "Caml1999I022"
3310

lib_dev/process.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as child_process from "node:child_process";
22
import * as fs from "node:fs/promises";
33
import * as path from "node:path";
44
import { bsc_exe, rescript_legacy_exe } from "#cli/bins";
5+
import { stdlibDir } from "#cli/stdlib";
56

67
/**
78
* @typedef {{
@@ -54,6 +55,7 @@ export function setup(cwd = process.cwd()) {
5455
cwd,
5556
shell: process.platform === "win32",
5657
stdio: ["ignore", "pipe", "pipe"],
58+
env: { ...process.env, RESCRIPT_STDLIB: stdlibDir },
5759
...options,
5860
});
5961

rewatch/tests/suite-ci.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ else
1515
fi
1616
export REWATCH_EXECUTABLE
1717
export BSC_PATH
18+
export RESCRIPT_STDLIB=$(realpath ../../lib/ocaml)
1819

1920
source ./utils.sh
2021

21-
bold "Rescript version"
22-
(cd ../testrepo && ./node_modules/.bin/rescript -v)
23-
2422
# we need to reset the yarn.lock and package.json to the original state
2523
# so there is not diff in git. The CI will install new ReScript package
2624
bold "Reset package.json and yarn.lock"

0 commit comments

Comments
 (0)