From 0d7bf09e0ea53aa5be1bd3931328f18a2fa7cb3a Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 12 Jul 2025 07:40:30 +0200 Subject: [PATCH 1/4] Move all rescript legacy js sources to rescript-legacy folder --- cli/rescript-legacy.js | 2 +- cli/{common => rescript-legacy}/args.js | 0 cli/{common => rescript-legacy}/bsb.js | 2 +- cli/rescript-legacy/dump.js | 2 +- cli/rescript-legacy/format.js | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename cli/{common => rescript-legacy}/args.js (100%) rename cli/{common => rescript-legacy}/bsb.js (99%) diff --git a/cli/rescript-legacy.js b/cli/rescript-legacy.js index c464f468a6..6aa38be7ea 100755 --- a/cli/rescript-legacy.js +++ b/cli/rescript-legacy.js @@ -10,7 +10,7 @@ import * as fs from "node:fs"; import * as tty from "node:tty"; import { bsc_exe, rescript_legacy_exe } from "./common/bins.js"; -import * as bsb from "./common/bsb.js"; +import * as bsb from "./rescript-legacy/bsb.js"; const cwd = process.cwd(); process.env.BSB_PROJECT_ROOT = cwd; diff --git a/cli/common/args.js b/cli/rescript-legacy/args.js similarity index 100% rename from cli/common/args.js rename to cli/rescript-legacy/args.js diff --git a/cli/common/bsb.js b/cli/rescript-legacy/bsb.js similarity index 99% rename from cli/common/bsb.js rename to cli/rescript-legacy/bsb.js index b38d462887..2705cb858c 100644 --- a/cli/common/bsb.js +++ b/cli/rescript-legacy/bsb.js @@ -7,7 +7,7 @@ import * as os from "node:os"; import * as path from "node:path"; import { WebSocket } from "#lib/minisocket"; -import { rescript_legacy_exe } from "./bins.js"; +import { rescript_legacy_exe } from "../common/bins.js"; const cwd = process.cwd(); const lockFileName = path.join(cwd, ".bsb.lock"); diff --git a/cli/rescript-legacy/dump.js b/cli/rescript-legacy/dump.js index 3acf52a251..5eceb22b31 100644 --- a/cli/rescript-legacy/dump.js +++ b/cli/rescript-legacy/dump.js @@ -3,7 +3,7 @@ import * as child_process from "node:child_process"; import * as path from "node:path"; -import * as arg from "#cli/args"; +import * as arg from "./args.js"; const dump_usage = `Usage: rescript dump [target] \`rescript dump\` dumps the information for the target diff --git a/cli/rescript-legacy/format.js b/cli/rescript-legacy/format.js index 9fc1759b8d..fd8deac31c 100644 --- a/cli/rescript-legacy/format.js +++ b/cli/rescript-legacy/format.js @@ -8,7 +8,7 @@ import * as os from "node:os"; import * as path from "node:path"; import { promisify } from "node:util"; -import * as arg from "#cli/args"; +import * as arg from "./args.js"; const asyncExecFile = promisify(child_process.execFile); From b2f9b78343fb656e94c2e31e289b9bbaa2fcf7ec Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 12 Jul 2025 07:06:25 +0200 Subject: [PATCH 2/4] Reliably determine standard lib dir # Conflicts: # cli/rescript.js # rewatch/tests/suite-ci.sh --- cli/bsc.js | 6 +++++- cli/common/stdlib.js | 10 ++++++++++ cli/rescript-legacy/bsb.js | 2 ++ cli/rescript-tools.js | 6 +++++- cli/rescript.js | 2 ++ compiler/ext/config.ml | 35 ++++++----------------------------- lib_dev/process.js | 2 ++ rewatch/tests/suite-ci.sh | 4 +--- 8 files changed, 33 insertions(+), 34 deletions(-) create mode 100644 cli/common/stdlib.js diff --git a/cli/bsc.js b/cli/bsc.js index 711ccc17dd..be20cb1c7d 100755 --- a/cli/bsc.js +++ b/cli/bsc.js @@ -5,11 +5,15 @@ import { execFileSync } from "node:child_process"; import { bsc_exe } from "./common/bins.js"; +import { stdlibDir } from "./common/stdlib.js"; const delegate_args = process.argv.slice(2); try { - execFileSync(bsc_exe, delegate_args, { stdio: "inherit" }); + execFileSync(bsc_exe, delegate_args, { + stdio: "inherit", + env: { ...process.env, RESCRIPT_STDLIB: stdlibDir }, + }); } catch (e) { if (e.code === "ENOENT") { console.error(String(e)); diff --git a/cli/common/stdlib.js b/cli/common/stdlib.js new file mode 100644 index 0000000000..94f56c6f21 --- /dev/null +++ b/cli/common/stdlib.js @@ -0,0 +1,10 @@ +// @ts-check +import * as path from "node:path"; +import { fileURLToPath } from "node:url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const rescriptDir = path.dirname(path.dirname(__dirname)); + +export const stdlibDir = path.join(rescriptDir, "lib", "ocaml"); diff --git a/cli/rescript-legacy/bsb.js b/cli/rescript-legacy/bsb.js index 2705cb858c..df228f5a91 100644 --- a/cli/rescript-legacy/bsb.js +++ b/cli/rescript-legacy/bsb.js @@ -8,6 +8,7 @@ import * as path from "node:path"; import { WebSocket } from "#lib/minisocket"; import { rescript_legacy_exe } from "../common/bins.js"; +import { stdlibDir } from "../common/stdlib.js"; const cwd = process.cwd(); const lockFileName = path.join(cwd, ".bsb.lock"); @@ -52,6 +53,7 @@ function acquireBuild(args, options) { try { ownerProcess = child_process.spawn(rescript_legacy_exe, args, { stdio: "inherit", + env: { ...process.env, RESCRIPT_STDLIB: stdlibDir }, ...options, }); fs.writeFileSync(lockFileName, ownerProcess.pid.toString(), { diff --git a/cli/rescript-tools.js b/cli/rescript-tools.js index a5f8e6f0eb..7d6bc63c61 100755 --- a/cli/rescript-tools.js +++ b/cli/rescript-tools.js @@ -5,7 +5,11 @@ import * as child_process from "node:child_process"; import { rescript_tools_exe } from "./common/bins.js"; +import { stdlibDir } from "./common/stdlib.js"; const args = process.argv.slice(2); -child_process.spawnSync(rescript_tools_exe, args, { stdio: "inherit" }); +child_process.spawnSync(rescript_tools_exe, args, { + stdio: "inherit", + env: { ...process.env, RESCRIPT_STDLIB: stdlibDir }, +}); diff --git a/cli/rescript.js b/cli/rescript.js index 25a900abfd..25ac12c220 100755 --- a/cli/rescript.js +++ b/cli/rescript.js @@ -4,12 +4,14 @@ import * as child_process from "node:child_process"; import { rescript_exe } from "./common/bins.js"; +import { stdlibDir } from "./common/stdlib.js"; const args = process.argv.slice(2); try { child_process.execFileSync(rescript_exe, args, { stdio: "inherit", + env: { ...process.env, RESCRIPT_STDLIB: stdlibDir }, }); } catch (err) { if (err.status !== undefined) { diff --git a/compiler/ext/config.ml b/compiler/ext/config.ml index c6e1619d4f..2191bee664 100644 --- a/compiler/ext/config.ml +++ b/compiler/ext/config.ml @@ -1,33 +1,10 @@ -(* This resolves the location of the standard library starting from the location of bsc.exe, - handling different supported package layouts. *) +(* To determine the standard library path in a reliable way that works with all package managers / + package directory layouts, we need to do it on the JS side, see cli/common/stdlib.js. + We pass the directory to the compiler exe via the environment variable RESCRIPT_STDLIB. *) let standard_library = - let build_path rest path = - String.concat Filename.dir_sep (List.rev_append rest path) - in - match - Sys.executable_name |> Filename.dirname - |> String.split_on_char Filename.dir_sep.[0] - |> List.rev - with - (* 1. Packages installed via pnpm - - bin: node_modules/.pnpm/@rescript+darwin-arm64@12.0.0-alpha.13/node_modules/@rescript/darwin-arm64/bin - - stdlib: node_modules/rescript/lib/ocaml (symlink) - *) - | "bin" :: _platform :: "@rescript" :: "node_modules" :: _package :: ".pnpm" - :: "node_modules" :: rest -> - build_path rest ["node_modules"; "rescript"; "lib"; "ocaml"] - (* 2. Packages installed via npm - - bin: node_modules/@rescript/{platform}/bin - - stdlib: node_modules/rescript/lib/ocaml - *) - | "bin" :: _platform :: "@rescript" :: "node_modules" :: rest -> - build_path rest ["node_modules"; "rescript"; "lib"; "ocaml"] - (* 3. Several other cases that can occur in local development, e.g. - - bin: /packages/@rescript/{platform}/bin, /_build/install/default/bin - - stdlib: /lib/ocaml - *) - | _ :: _ :: _ :: _ :: rest -> build_path rest ["lib"; "ocaml"] - | _ -> "" + match Sys.getenv_opt "RESCRIPT_STDLIB" with + | Some path -> path + | None -> "" let cmi_magic_number = "Caml1999I022" diff --git a/lib_dev/process.js b/lib_dev/process.js index 0dbddd4881..3b45a29dd9 100644 --- a/lib_dev/process.js +++ b/lib_dev/process.js @@ -2,6 +2,7 @@ import * as child_process from "node:child_process"; import * as fs from "node:fs/promises"; import * as path from "node:path"; import { bsc_exe, rescript_legacy_exe } from "#cli/bins"; +import { stdlibDir } from "#cli/stdlib"; /** * @typedef {{ @@ -54,6 +55,7 @@ export function setup(cwd = process.cwd()) { cwd, shell: process.platform === "win32", stdio: ["ignore", "pipe", "pipe"], + env: { ...process.env, RESCRIPT_STDLIB: stdlibDir }, ...options, }); diff --git a/rewatch/tests/suite-ci.sh b/rewatch/tests/suite-ci.sh index 24ae688424..3e53e2c877 100755 --- a/rewatch/tests/suite-ci.sh +++ b/rewatch/tests/suite-ci.sh @@ -15,12 +15,10 @@ fi export REWATCH_EXECUTABLE export RESCRIPT_BSC_EXE +export RESCRIPT_STDLIB=$(realpath ../../lib/ocaml) source ./utils.sh -bold "Rescript version" -(cd ../testrepo && ./node_modules/.bin/rescript -v) - # we need to reset the yarn.lock and package.json to the original state # so there is not diff in git. The CI will install new ReScript package bold "Reset package.json and yarn.lock" From e4f2d4953bf2ce73bd0841a67d8febe62ff0cb36 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 12 Jul 2025 08:31:37 +0200 Subject: [PATCH 3/4] CHANGELOG # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aa338c251..6588f751fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 12.0.0-beta.2 (Unreleased) +#### :bug: Bug fix + +- Reliably determine standard lib dir. This should fix remaining cases where the stdlib is not found when using pnpm. https://github.com/rescript-lang/rescript/pull/7637 + #### :nail_care: Polish - Add missing backtick and spaces to `Belt.Map.map` doc comment. https://github.com/rescript-lang/rescript/pull/7632 From 56bb638445c795a25eedfeb508a283cf86aa832e Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 12 Jul 2025 08:56:19 +0200 Subject: [PATCH 4/4] Update artifact list --- packages/artifacts.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/artifacts.txt b/packages/artifacts.txt index bd4dc88787..095b169df3 100644 --- a/packages/artifacts.txt +++ b/packages/artifacts.txt @@ -6,10 +6,11 @@ LICENSE README.md cli/bsc.js cli/bstracing.js -cli/common/args.js cli/common/bins.js -cli/common/bsb.js +cli/common/stdlib.js cli/rescript-legacy.js +cli/rescript-legacy/args.js +cli/rescript-legacy/bsb.js cli/rescript-legacy/dump.js cli/rescript-legacy/format.js cli/rescript-tools.js