Skip to content
Closed
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: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ jobs:
working-directory: ${{ steps.tmp-dir.outputs.path }}

- name: Test installation
run: npx rescript -h && npx rescript legacy build && cat src/Test.res.js
run: npx rescript -h && npx rescript build && cat src/Test.res.js && npx rescript clean && npx rescript legacy build
shell: bash
working-directory: ${{ steps.tmp-dir.outputs.path }}

Expand Down Expand Up @@ -633,7 +633,7 @@ jobs:
working-directory: ${{ steps.tmp-dir.outputs.path }}

- name: Test installation
run: pnpm rescript -h && pnpm rescript legacy build && cat src/Test.res.js
run: pnpm rescript -h && pnpm rescript build && cat src/Test.res.js && pnpm rescript clean && pnpm rescript legacy build
shell: bash
working-directory: ${{ steps.tmp-dir.outputs.path }}

Expand Down
2 changes: 1 addition & 1 deletion analysis/src/ModuleResolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let ( /+ ) = Filename.concat
let rec resolveNodeModulePath ~startPath name =
if name = "@rescript/runtime" then
(* Hack: we need a reliable way to resolve modules in monorepos. *)
Some Config.runtime_module_path
Some !Runtime_package.path
else
let scope = Filename.dirname name in
let name = Filename.basename name in
Expand Down
4 changes: 4 additions & 0 deletions cli/bsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import { execFileSync } from "node:child_process";

import { bsc_exe } from "./common/bins.js";
import { runtimePath } from "./common/runtime.js";

const delegate_args = process.argv.slice(2);
if (!delegate_args.includes("-runtime-path")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this happens when we run bunx bsc directly. It makes me wonder if we could just reuse this and pass it to Rewatch via an environment variable.

I'm not against oxc_resolver per se, but doesn't this create two different mechanisms for resolving the -runtime-path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the runtime path to rewatch via an env var or command line flag was my original intention, but then I thought it was better if rewatch can remain self-contained here so that you can just invoke the binary directly and it does the resolution itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my experience with calling rewatch directly is that I need to set RESCRIPT_BSC_EXE, so it seems a bit weird that we do this for the runtime but not the compiler.

delegate_args.push("-runtime-path", runtimePath);
}

try {
execFileSync(bsc_exe, delegate_args, { stdio: "inherit" });
Expand Down
10 changes: 10 additions & 0 deletions cli/common/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-check
import { fileURLToPath } from "node:url";
import * as path from "node:path";

const runtimePackageJsonUrl = await import.meta.resolve(
"@rescript/runtime/package.json",
);
const runtimePackageJsonPath = fileURLToPath(runtimePackageJsonUrl);

export const runtimePath = path.dirname(runtimePackageJsonPath);
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.
8 changes: 7 additions & 1 deletion cli/common/bsb.js → cli/rescript-legacy/bsb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { createServer } from "node:http";
import * as os from "node:os";
import * as path from "node:path";

import { rescript_legacy_exe } from "./bins.js";
import { rescript_legacy_exe } from "../common/bins.js";
import { runtimePath } from "../common/runtime.js";
import { WebSocket } from "./minisocket.js";

const cwd = process.cwd();
Expand Down Expand Up @@ -49,6 +50,11 @@ function acquireBuild(args, options) {
if (ownerProcess) {
return null;
}

if (args[0] === "build" && !args.includes("-runtime-path")) {
args.push("-runtime-path", runtimePath);
}

try {
ownerProcess = child_process.spawn(rescript_legacy_exe, args, {
stdio: "inherit",
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
File renamed without changes.
7 changes: 5 additions & 2 deletions compiler/bsb/bsb_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ let usage_b (buf : Ext_buffer.t) ~usage (speclist : t) =
else (
buf +> "\nOptions:\n";
let max_col = ref 0 in
Ext_array.iter speclist (fun (key, _, _) ->
if String.length key > !max_col then max_col := String.length key);
Ext_array.iter speclist (fun (key, _, doc) ->
if
(not (Ext_string.starts_with doc "*internal*"))
&& String.length key > !max_col
then max_col := String.length key);
Ext_array.iter speclist (fun (key, _, doc) ->
if not (Ext_string.starts_with doc "*internal*") then (
buf +> " ";
Expand Down
2 changes: 1 addition & 1 deletion compiler/bsb/bsb_exception.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let print (fmt : Format.formatter) (x : error) =
modname
| Package_not_found name ->
let name = Bsb_pkg_types.to_string name in
if Ext_string.equal name Bs_version.package_name then
if Ext_string.equal name Runtime_package.name then
Format.fprintf fmt
"File \"rescript.json\", line 1\n\
@{<error>Error:@} package @{<error>%s@} is not found\n\
Expand Down
2 changes: 2 additions & 0 deletions compiler/bsb/bsb_ninja_rule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
string =
Ext_buffer.clear buf;
Ext_buffer.add_string buf bsc;
Ext_buffer.add_string buf (" -runtime-path " ^ !Runtime_package.path);
Ext_buffer.add_string buf ns_flag;
if read_cmi = `yes then Ext_buffer.add_string buf " -bs-read-cmi";
(* The include order matters below *)
Expand Down Expand Up @@ -139,6 +140,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
let mk_ast =
Ext_buffer.clear buf;
Ext_buffer.add_string buf bsc;
Ext_buffer.add_string buf (" -runtime-path " ^ !Runtime_package.path);
Ext_buffer.add_char_string buf ' ' warnings;
(match ppx_files with
| [] -> ()
Expand Down
5 changes: 5 additions & 0 deletions compiler/bsb_exe/rescript_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ let install_target () =
let eid = Bsb_unix.run_command_execv install_command in
if eid <> 0 then Bsb_unix.command_fatal_error install_command eid

let setup_runtime_path path = Runtime_package.path := path

let build_subcommand ~start argv argv_len =
let i = Ext_array.rfind_with_index argv Ext_string.equal separator in

Expand Down Expand Up @@ -141,6 +143,9 @@ let build_subcommand ~start argv argv_len =
unit_set_spec no_deps_mode,
"*internal* Needed for watcher to build without dependencies on file \
change" );
( "-runtime-path",
string_call setup_runtime_path,
"*internal* Set the path of the runtime package (@rescript/runtime)" );
( "-warn-error",
string_call (fun s -> warning_as_error := Some s),
"Warning numbers and whether to turn them into errors, e.g., \
Expand Down
5 changes: 5 additions & 0 deletions compiler/bsc/rescript_compiler_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ let set_abs_input_name sourcefile =
sourcefile
let setup_outcome_printer () = Lazy.force Res_outcome_printer.setup

let setup_runtime_path path = Runtime_package.path := path

let process_file sourcefile ?kind ppf =
(* This is a better default then "", it will be changed later
The {!Location.input_name} relies on that we write the binary ast
Expand Down Expand Up @@ -402,6 +404,9 @@ let command_line_flags : (string * Bsc_args.spec * string) array =
"<list> Enable or disable error status for warnings according\n\
to <list>. See option -w for the syntax of <list>.\n\
Default setting is " ^ Bsc_warnings.defaults_warn_error );
( "-runtime-path",
string_call setup_runtime_path,
"*internal* Set the path of the runtime package (@rescript/runtime)" );
( "-make-runtime",
unit_call Js_packages_state.make_runtime,
"*internal* make runtime library" );
Expand Down
1 change: 0 additions & 1 deletion compiler/common/bs_version.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
let version = "12.0.0-beta.10"
let header = "// Generated by ReScript, PLEASE EDIT WITH CARE"
let package_name = "@rescript/runtime"
2 changes: 0 additions & 2 deletions compiler/common/bs_version.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@
val version : string

val header : string

val package_name : string
2 changes: 1 addition & 1 deletion compiler/core/js_name_of_module_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let get_runtime_module_path
(*Invariant: the package path to rescript, it is used to
calculate relative js path
*)
(Config.runtime_module_path // dep_path // js_file)
(!Runtime_package.path // dep_path // js_file)

(* [output_dir] is decided by the command line argument *)
let string_of_module_id
Expand Down
8 changes: 4 additions & 4 deletions compiler/core/js_packages_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ let runtime_dir_of_module_system (ms : module_system) =
| Esmodule | Es6_global -> "es6"

let runtime_package_path (ms : module_system) js_file =
Bs_version.package_name // "lib" // runtime_dir_of_module_system ms // js_file
Runtime_package.name // "lib" // runtime_dir_of_module_system ms // js_file

type t = {name: package_name; module_systems: package_info list}

Expand Down Expand Up @@ -85,8 +85,8 @@ let map (x : t) cb = Ext_list.map x.module_systems cb

(* we don't want force people to use package *)

(**
TODO: not allowing user to provide such specific package name
(**
TODO: not allowing user to provide such specific package name
For empty package, [-bs-package-output] does not make sense
it is only allowed to generate commonjs file in the same directory
*)
Expand Down Expand Up @@ -163,7 +163,7 @@ let query_package_infos ({name; module_systems} : t)
with
| Some k ->
let rel_path = k.path in
let pkg_rel_path = Bs_version.package_name // rel_path in
let pkg_rel_path = Runtime_package.name // rel_path in
Package_found {rel_path; pkg_rel_path; suffix = k.suffix}
| None -> Package_not_found)

Expand Down
10 changes: 6 additions & 4 deletions compiler/core/res_compmisc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

let init_path () =
let dirs = !Clflags.include_dirs in
let exp_dirs =
List.map (Misc.expand_directory Config.standard_library) dirs
let stdlib_dir =
let ( // ) = Filename.concat in
!Runtime_package.path // "lib" // "ocaml"
in
let dirs = !Clflags.include_dirs in
let exp_dirs = List.map (Misc.expand_directory stdlib_dir) dirs in
Config.load_path :=
if !Js_config.no_stdlib then exp_dirs
else List.rev_append exp_dirs [Config.standard_library];
else List.rev_append exp_dirs [stdlib_dir];
Env.reset_cache ()

(* Return the initial environment in which compilation proceeds. *)
Expand Down
37 changes: 0 additions & 37 deletions compiler/ext/config.ml
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
(* This resolves the location of the standard library starting from the location of bsc.exe
(@rescript/{platform}/bin/bsc.exe), handling different supported package layouts. *)
let runtime_module_path =
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/@[email protected]/node_modules/@rescript/darwin-arm64/bin
- runtime: node_modules/.pnpm/node_modules/@rescript/runtime (symlink)
*)
| "bin" :: _platform :: "@rescript" :: "node_modules" :: _package :: ".pnpm"
:: "node_modules" :: rest ->
build_path rest
["node_modules"; ".pnpm"; "node_modules"; "@rescript"; "runtime"]
(* 2. Packages installed via npm
- bin: node_modules/@rescript/{platform}/bin
- runtime: node_modules/@rescript/runtime
*)
| "bin" :: _platform :: "@rescript" :: "node_modules" :: rest ->
build_path rest ["node_modules"; "@rescript"; "runtime"]
(* 3. Several other cases that can occur in local development, e.g.
- bin: <repo>/packages/@rescript/{platform}/bin, <repo>/_build/install/default/bin
- runtime: <repo>/packages/@rescript/runtime
*)
| _ :: _ :: _ :: _ :: rest ->
build_path rest ["packages"; "@rescript"; "runtime"]
| _ -> ""

let standard_library =
let ( // ) = Filename.concat in
runtime_module_path // "lib" // "ocaml"

let cmi_magic_number = "Caml1999I022"

and ast_impl_magic_number = "Caml1999M022"
Expand Down
6 changes: 0 additions & 6 deletions compiler/ext/config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@

(* System configuration *)

(* The directory containing the runtime module (@rescript/runtime) *)
val runtime_module_path : string

(* The directory containing the runtime artifacts (@rescript/runtime/lib/ocaml) *)
val standard_library : string

(* Directories in the search path for .cmi and .cmo files *)
val load_path : string list ref

Expand Down
29 changes: 29 additions & 0 deletions compiler/ext/runtime_package.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let name = "@rescript/runtime"

(* Simple default approach to find the runtime package path. This will not work with all package managers/layouts. *)
let default_path =
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 npm
- bin: node_modules/@rescript/{platform}/bin
- runtime: node_modules/@rescript/runtime
*)
| "bin" :: _platform :: "@rescript" :: "node_modules" :: rest ->
build_path rest ["node_modules"; "@rescript"; "runtime"]
(* 2. Several other cases that can occur in local development, e.g.
- bin: <repo>/packages/@rescript/{platform}/bin, <repo>/_build/install/default/bin
- runtime: <repo>/packages/@rescript/runtime
*)
| _ :: _ :: _ :: _ :: rest ->
build_path rest ["packages"; "@rescript"; "runtime"]
| _ -> ""

(* To support pnpm and other package managers/layouts, we determine the path on the JS side and pass it in
via -runtime-path to override the default. *)
let path = ref default_path
3 changes: 3 additions & 0 deletions compiler/ext/runtime_package.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val name : string

val path : string ref
7 changes: 4 additions & 3 deletions packages/artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
"README.md",
"cli/bsc.js",
"cli/bstracing.js",
"cli/common/args.js",
"cli/common/bins.js",
"cli/common/bsb.js",
"cli/common/minisocket.js",
"cli/common/runtime.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-legacy/minisocket.js",
"cli/rescript-tools.js",
"cli/rescript.js",
"docs/docson/build-schema.json",
Expand Down
Loading
Loading