Skip to content

Reliably determine standard lib dir #7637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

#### :house: Internal

- Remove internal/unused `-bs-v` flag. https://github.com/rescript-lang/rescript/pull/7627
Expand Down
6 changes: 5 additions & 1 deletion cli/bsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions cli/common/stdlib.js
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 1 addition & 1 deletion cli/rescript-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion cli/common/bsb.js → cli/rescript-legacy/bsb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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";
import { stdlibDir } from "../common/stdlib.js";

const cwd = process.cwd();
const lockFileName = path.join(cwd, ".bsb.lock");
Expand Down Expand Up @@ -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(), {
Expand Down
2 changes: 1 addition & 1 deletion cli/rescript-legacy/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <options> [target]
\`rescript dump\` dumps the information for the target
Expand Down
2 changes: 1 addition & 1 deletion cli/rescript-legacy/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion cli/rescript-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
2 changes: 2 additions & 0 deletions cli/rescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 6 additions & 29 deletions compiler/ext/config.ml
Original file line number Diff line number Diff line change
@@ -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: <repo>/packages/@rescript/{platform}/bin, <repo>/_build/install/default/bin
- stdlib: <repo>/lib/ocaml
*)
| _ :: _ :: _ :: _ :: rest -> build_path rest ["lib"; "ocaml"]
| _ -> ""
match Sys.getenv_opt "RESCRIPT_STDLIB" with
| Some path -> path
| None -> ""

let cmi_magic_number = "Caml1999I022"

Expand Down
2 changes: 2 additions & 0 deletions lib_dev/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {{
Expand Down Expand Up @@ -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,
});

Expand Down
5 changes: 3 additions & 2 deletions packages/artifacts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions rewatch/tests/suite-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down