Skip to content

Commit 893ab5d

Browse files
authored
Use rescript.json dir as working dir for js-post-build (#8195)
* Use rescript.json working dir. * Add changelog
1 parent a40570b commit 893ab5d

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- `Int.fromString` and `Float.fromString` use stricter number parsing and no longer uses an explicit radix argument, but instead supports parsing hexadecimal, binary and exponential notation.
2020
- Remove `external-stdlib` configuration option from `rescript.json`. This option was rarely used and is no longer supported.
2121
- `js-post-build` now passes the correct output file path based on `in-source` configuration: when `in-source: true`, the path next to the source file is passed; when `in-source: false`, the path in the `lib/<module>/` directory is passed. Additionally, stdout and stderr from the post-build command are now logged. https://github.com/rescript-lang/rescript/pull/8190
22+
- `js-post-build` command now runs in the directory containing the `rescript.json` where it is defined, instead of the unpredictable build invocation directory. This provides consistent behavior in monorepos. https://github.com/rescript-lang/rescript/pull/8195
2223

2324
#### :eyeglasses: Spec Compliance
2425

docs/docson/build-schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
"properties": {
126126
"cmd": {
127127
"type": "string",
128-
"description": "Shell command to run after each JS file is compiled. The absolute path to the output JS file is appended as an argument. The path respects the `in-source` setting."
128+
"description": "Shell command to run after each JS file is compiled. The absolute path to the output JS file is appended as an argument. The path respects the `in-source` setting. The command runs in the directory containing the rescript.json where it is defined."
129129
}
130130
}
131131
},
@@ -454,7 +454,7 @@
454454
},
455455
"js-post-build": {
456456
"$ref": "#/definitions/js-post-build",
457-
"description": "Post-processing hook. The build system will invoke `cmd <absolute-path-to-js-file>` after each JS file is compiled. The path respects the `in-source` setting: when true, the path is next to the source file; when false, the path is in the `lib/<module>/` directory. The command runs with the same working directory as the build process (typically the project root)."
457+
"description": "Post-processing hook. The build system will invoke `cmd <absolute-path-to-js-file>` after each JS file is compiled. The path respects the `in-source` setting: when true, the path is next to the source file; when false, the path is in the `lib/<module>/` directory. The command runs in the directory containing the rescript.json where it is defined."
458458
},
459459
"package-specs": {
460460
"$ref": "#/definitions/package-specs",

rewatch/src/build/compile.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,27 @@ use std::sync::OnceLock;
2222
use std::time::SystemTime;
2323

2424
/// Execute js-post-build command for a compiled JavaScript file.
25-
/// Unlike bsb which passes relative paths, rewatch passes absolute paths for clarity.
26-
fn execute_post_build_command(cmd: &str, js_file_path: &Path) -> Result<()> {
25+
/// The command runs in the directory containing the rescript.json that defines it.
26+
/// The absolute path to the JS file is passed as an argument.
27+
fn execute_post_build_command(cmd: &str, js_file_path: &Path, working_dir: &Path) -> Result<()> {
2728
let full_command = format!("{} {}", cmd, js_file_path.display());
2829

29-
debug!("Executing js-post-build: {}", full_command);
30+
debug!(
31+
"Executing js-post-build: {} (in {})",
32+
full_command,
33+
working_dir.display()
34+
);
3035

3136
let output = if cfg!(target_os = "windows") {
32-
Command::new("cmd").args(["/C", &full_command]).output()
37+
Command::new("cmd")
38+
.args(["/C", &full_command])
39+
.current_dir(working_dir)
40+
.output()
3341
} else {
34-
Command::new("sh").args(["-c", &full_command]).output()
42+
Command::new("sh")
43+
.args(["-c", &full_command])
44+
.current_dir(working_dir)
45+
.output()
3546
};
3647

3748
match output {
@@ -886,7 +897,8 @@ fn compile_file(
886897

887898
if js_file.exists() {
888899
// Fail the build if post-build command fails (matches bsb behavior with &&)
889-
execute_post_build_command(&js_post_build.cmd, &js_file)?;
900+
// Run in the package's directory (where rescript.json is defined)
901+
execute_post_build_command(&js_post_build.cmd, &js_file, &package.path)?;
890902
}
891903
}
892904
}

0 commit comments

Comments
 (0)