Skip to content

Commit 34cb6b8

Browse files
authored
Ensure runtime is passed to mlmap generation (#7888)
* Ensure runtime is passed to mlmap generation * Remove unnecessary specifier check * Add runtime to bsb * Add changelog
1 parent 5c0e9dd commit 34cb6b8

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#### :bug: Bug fix
2424

25+
- Include `-runtime-path` as bsc argument when generating `.mlmap` file. https://github.com/rescript-lang/rescript/pull/7888
26+
2527
#### :memo: Documentation
2628

2729
#### :nail_care: Polish

cli/common/runtime.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ async function resolvePackageInDeno(pkgName) {
4242
}
4343

4444
async function resolvePackageRoot(pkgName) {
45-
const specifier =
46-
typeof globalThis.Deno !== "undefined"
47-
? `npm:${pkgName}/package.json`
48-
: `${pkgName}/package.json`;
45+
const specifier = `${pkgName}/package.json`;
4946

5047
if (typeof import.meta.resolve === "function") {
5148
const url = import.meta.resolve(specifier);

compiler/bsb/bsb_ninja_rule.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
206206
let mi, mi_dev = aux ~read_cmi:`is_cmi ~postbuild:None ~name:"mi" in
207207
let build_package =
208208
define
209-
~command:(bsc ^ " -w -49 -color always -no-alias-deps $i")
209+
~command:
210+
(bsc ^ " -w -49 -color always -no-alias-deps -runtime-path "
211+
^ !Runtime_package.path ^ " $i")
210212
~restat:() "build_package"
211213
in
212214
{

rewatch/src/build/compile.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ pub fn compile(
336336
Ok((compile_errors, compile_warnings, num_compiled_modules))
337337
}
338338

339-
fn get_runtime_path_args(package_config: &Config, project_context: &ProjectContext) -> Result<Vec<String>> {
339+
pub fn get_runtime_path_args(
340+
package_config: &Config,
341+
project_context: &ProjectContext,
342+
) -> Result<Vec<String>> {
340343
match std::env::var("RESCRIPT_RUNTIME") {
341344
Ok(runtime_path) => Ok(vec!["-runtime-path".to_string(), runtime_path]),
342345
Err(_) => match helpers::try_package_path(package_config, project_context, "@rescript/runtime") {

rewatch/src/build/namespaces.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
use crate::build::compile::get_runtime_path_args;
12
use crate::build::packages;
23
use crate::helpers::StrippedVerbatimPath;
4+
use crate::project_context::ProjectContext;
35
use ahash::AHashSet;
6+
use anyhow::{Result, anyhow};
47
use std::fs::File;
58
use std::io::Write;
69
use std::path::Path;
710
use std::path::PathBuf;
811
use std::process::Command;
9-
1012
// Namespaces work like the following: The build system will generate a file
1113
// called `MyModule.mlmap` which contains all modules that are in the namespace
1214
//
@@ -51,20 +53,47 @@ pub fn gen_mlmap(
5153
path
5254
}
5355

54-
pub fn compile_mlmap(package: &packages::Package, namespace: &str, bsc_path: &Path) {
56+
pub fn compile_mlmap(
57+
project_context: &ProjectContext,
58+
package: &packages::Package,
59+
namespace: &str,
60+
bsc_path: &Path,
61+
) -> Result<()> {
5562
let build_path_abs = package.get_build_path();
5663
let mlmap_name = format!("{namespace}.mlmap");
57-
let args = vec!["-w", "-49", "-color", "always", "-no-alias-deps", &mlmap_name];
64+
let mut args: Vec<String> = vec![];
65+
// include `-runtime-path` arg
66+
args.extend(get_runtime_path_args(&package.config, project_context)?);
67+
// remaining flags
68+
args.extend([
69+
"-w".to_string(),
70+
"-49".to_string(),
71+
"-color".to_string(),
72+
"always".to_string(),
73+
"-no-alias-deps".to_string(),
74+
]);
75+
args.push(mlmap_name.clone());
5876

59-
let _ = Command::new(bsc_path)
77+
let output = Command::new(bsc_path)
6078
.current_dir(
6179
build_path_abs
6280
.canonicalize()
6381
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
6482
.ok()
6583
.unwrap(),
6684
)
67-
.args(args)
68-
.output()
69-
.expect("err");
85+
.args(&args)
86+
.output()?;
87+
88+
if !output.status.success() {
89+
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
90+
return Err(anyhow!(
91+
"Failed to compile namespace mlmap {} in {}: {}",
92+
namespace,
93+
build_path_abs.to_string_lossy(),
94+
stderr
95+
));
96+
}
97+
98+
Ok(())
7099
}

rewatch/src/build/parse.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::process::Command;
1616
pub fn generate_asts(
1717
build_state: &mut BuildState,
1818
inc: impl Fn() + std::marker::Sync,
19-
) -> Result<String, String> {
19+
) -> anyhow::Result<String> {
2020
let mut has_failure = false;
2121
let mut stderr = "".to_string();
2222

@@ -192,7 +192,15 @@ pub fn generate_asts(
192192
// specific to compiling mlmaps
193193
let compile_path = package.get_mlmap_compile_path();
194194
let mlmap_hash = helpers::compute_file_hash(Path::new(&compile_path));
195-
namespaces::compile_mlmap(package, module_name, &build_state.bsc_path);
195+
if let Err(err) = namespaces::compile_mlmap(
196+
&build_state.project_context,
197+
package,
198+
module_name,
199+
&build_state.bsc_path,
200+
) {
201+
has_failure = true;
202+
stderr.push_str(&format!("{}\n", err));
203+
}
196204
let mlmap_hash_after = helpers::compute_file_hash(Path::new(&compile_path));
197205

198206
let suffix = package
@@ -232,7 +240,11 @@ pub fn generate_asts(
232240
}
233241
});
234242

235-
if has_failure { Err(stderr) } else { Ok(stderr) }
243+
if has_failure {
244+
Err(anyhow!(stderr))
245+
} else {
246+
Ok(stderr)
247+
}
236248
}
237249

238250
pub fn parser_args(

0 commit comments

Comments
 (0)