Skip to content

Commit 9454207

Browse files
authored
Rewatch: simplify getting bsc path (#7634)
* Rewatch: simplify getting bsc path * CHANGELOG * Fix Windows
1 parent 5547386 commit 9454207

File tree

11 files changed

+38
-140
lines changed

11 files changed

+38
-140
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Remove obsolete jsx options. https://github.com/rescript-lang/rescript/pull/7633
2020
- Remove obsolete option `-bs-unsafe-empty-array`. https://github.com/rescript-lang/rescript/pull/7635
2121
- Clean up `config.ml`. https://github.com/rescript-lang/rescript/pull/7636
22+
- Rewatch: simplify getting bsc path. https://github.com/rescript-lang/rescript/pull/7634
2223

2324
# 12.0.0-beta.1
2425

cli/rescript.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,14 @@
33
// @ts-check
44

55
import * as child_process from "node:child_process";
6-
import { bsc_exe, rescript_exe } from "./common/bins.js";
6+
import { rescript_exe } from "./common/bins.js";
77

88
const args = process.argv.slice(2);
99

10-
const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));
11-
1210
try {
13-
if (firstPositionalArgIndex !== -1) {
14-
const subcommand = args[firstPositionalArgIndex];
15-
const subcommandWithArgs = args.slice(firstPositionalArgIndex);
16-
17-
if (
18-
subcommand === "build" ||
19-
subcommand === "watch" ||
20-
subcommand === "clean"
21-
) {
22-
child_process.execFileSync(
23-
rescript_exe,
24-
[...subcommandWithArgs, "--bsc-path", bsc_exe],
25-
{
26-
stdio: "inherit",
27-
},
28-
);
29-
} else {
30-
child_process.execFileSync(rescript_exe, [...args], {
31-
stdio: "inherit",
32-
});
33-
}
34-
} else {
35-
// no subcommand means build subcommand
36-
child_process.execFileSync(rescript_exe, [...args, "--bsc-path", bsc_exe], {
37-
stdio: "inherit",
38-
});
39-
}
11+
child_process.execFileSync(rescript_exe, args, {
12+
stdio: "inherit",
13+
});
4014
} catch (err) {
4115
if (err.status !== undefined) {
4216
process.exit(err.status); // Pass through the exit code

rewatch/src/build.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,12 @@ pub fn initialize_build(
114114
filter: &Option<regex::Regex>,
115115
show_progress: bool,
116116
path: &Path,
117-
bsc_path: &Option<PathBuf>,
118117
build_dev_deps: bool,
119118
snapshot_output: bool,
120119
) -> Result<BuildState> {
121120
let project_root = helpers::get_abs_path(path);
122121
let workspace_root = helpers::get_workspace_root(&project_root);
123-
let bsc_path = match bsc_path {
124-
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
125-
None => helpers::get_bsc(&project_root, &workspace_root),
126-
};
122+
let bsc_path = helpers::get_bsc();
127123
let root_config_name = packages::read_package_name(&project_root)?;
128124

129125
if !snapshot_output && show_progress {
@@ -478,7 +474,6 @@ pub fn build(
478474
show_progress: bool,
479475
no_timing: bool,
480476
create_sourcedirs: bool,
481-
bsc_path: Option<PathBuf>,
482477
build_dev_deps: bool,
483478
snapshot_output: bool,
484479
) -> Result<BuildState> {
@@ -493,7 +488,6 @@ pub fn build(
493488
filter,
494489
show_progress,
495490
path,
496-
&bsc_path,
497491
build_dev_deps,
498492
snapshot_output,
499493
)

rewatch/src/build/clean.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
331331
});
332332
}
333333

334-
pub fn clean(
335-
path: &Path,
336-
show_progress: bool,
337-
bsc_path: Option<PathBuf>,
338-
snapshot_output: bool,
339-
) -> Result<()> {
334+
pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result<()> {
340335
let project_root = helpers::get_abs_path(path);
341336
let workspace_root = helpers::get_workspace_root(&project_root);
342337
let packages = packages::make(
@@ -349,10 +344,7 @@ pub fn clean(
349344
true,
350345
)?;
351346
let root_config_name = packages::read_package_name(&project_root)?;
352-
let bsc_path = match bsc_path {
353-
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
354-
None => helpers::get_bsc(&project_root, &workspace_root),
355-
};
347+
let bsc_path = helpers::get_bsc();
356348

357349
let timing_clean_compiler_assets = Instant::now();
358350
if !snapshot_output && show_progress {

rewatch/src/cli.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ pub struct DevArg {
7777
pub dev: bool,
7878
}
7979

80-
#[derive(Args, Debug, Clone)]
81-
pub struct BscPathArg {
82-
/// Custom path to bsc
83-
#[arg(long)]
84-
pub bsc_path: Option<String>,
85-
}
86-
8780
#[derive(Args, Debug, Clone, Copy)]
8881
pub struct SnapshotOutputArg {
8982
/// simple output for snapshot testing
@@ -114,9 +107,6 @@ pub struct BuildArgs {
114107

115108
#[command(flatten)]
116109
pub snapshot_output: SnapshotOutputArg,
117-
118-
#[command(flatten)]
119-
pub bsc_path: BscPathArg,
120110
}
121111

122112
#[derive(Args, Clone, Debug)]
@@ -138,9 +128,6 @@ pub struct WatchArgs {
138128

139129
#[command(flatten)]
140130
pub snapshot_output: SnapshotOutputArg,
141-
142-
#[command(flatten)]
143-
pub bsc_path: BscPathArg,
144131
}
145132

146133
#[derive(Subcommand, Clone, Debug)]
@@ -154,9 +141,6 @@ pub enum Command {
154141
#[command(flatten)]
155142
folder: FolderArg,
156143

157-
#[command(flatten)]
158-
bsc_path: BscPathArg,
159-
160144
#[command(flatten)]
161145
snapshot_output: SnapshotOutputArg,
162146
},
@@ -231,14 +215,6 @@ impl Deref for DevArg {
231215
}
232216
}
233217

234-
impl Deref for BscPathArg {
235-
type Target = Option<String>;
236-
237-
fn deref(&self) -> &Self::Target {
238-
&self.bsc_path
239-
}
240-
}
241-
242218
impl Deref for SnapshotOutputArg {
243219
type Target = bool;
244220

rewatch/src/helpers.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -180,44 +180,24 @@ pub fn create_path_for_path(path: &Path) {
180180
fs::DirBuilder::new().recursive(true).create(path).unwrap();
181181
}
182182

183-
fn get_bin_dir() -> PathBuf {
184-
let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) {
185-
("macos", "aarch64") => "darwin-arm64",
186-
("macos", _) => "darwin-x64",
187-
("linux", "aarch64") => "linux-arm64",
188-
("linux", _) => "linux-x64",
189-
("windows", "aarch64") => "win-arm64",
190-
("windows", _) => "win32-x64",
191-
_ => panic!("Unsupported architecture"),
192-
};
193-
194-
Path::new("node_modules")
195-
.join("@rescript")
196-
.join(subfolder)
197-
.join("bin")
183+
pub fn get_bin_dir() -> PathBuf {
184+
let current_exe_path = std::env::current_exe().expect("Could not get current executable path");
185+
current_exe_path
186+
.parent()
187+
.expect("Could not get parent directory of current executable")
188+
.to_path_buf()
198189
}
199190

200-
pub fn get_bsc(root_path: &Path, workspace_root: &Option<PathBuf>) -> PathBuf {
201-
let bin_dir = get_bin_dir();
191+
pub fn get_bsc() -> PathBuf {
192+
let bsc_path = match std::env::var("RESCRIPT_BSC_EXE") {
193+
Ok(val) => PathBuf::from(val),
194+
Err(_) => get_bin_dir().join("bsc.exe"),
195+
};
202196

203-
match (
204-
root_path
205-
.join(&bin_dir)
206-
.join("bsc.exe")
207-
.canonicalize()
208-
.map(StrippedVerbatimPath::to_stripped_verbatim_path),
209-
workspace_root.as_ref().map(|workspace_root| {
210-
workspace_root
211-
.join(&bin_dir)
212-
.join("bsc.exe")
213-
.canonicalize()
214-
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
215-
}),
216-
) {
217-
(Ok(path), _) => path,
218-
(_, Some(Ok(path))) => path,
219-
_ => panic!("Could not find bsc.exe"),
220-
}
197+
bsc_path
198+
.canonicalize()
199+
.expect("Could not get bsc path")
200+
.to_stripped_verbatim_path()
221201
}
222202

223203
pub fn get_rescript_legacy(root_path: &Path, workspace_root: Option<PathBuf>) -> PathBuf {

rewatch/src/main.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use anyhow::Result;
22
use clap::Parser;
33
use log::LevelFilter;
44
use regex::Regex;
5-
use std::{
6-
io::Write,
7-
path::{Path, PathBuf},
8-
};
5+
use std::{io::Write, path::Path};
96

107
use rewatch::{build, cli, cmd, lock, watcher};
118

@@ -45,7 +42,6 @@ fn main() -> Result<()> {
4542
show_progress,
4643
build_args.no_timing,
4744
*build_args.create_sourcedirs,
48-
build_args.bsc_path.as_ref().map(PathBuf::from),
4945
*build_args.dev,
5046
*build_args.snapshot_output,
5147
) {
@@ -75,25 +71,18 @@ fn main() -> Result<()> {
7571
(*watch_args.after_build).clone(),
7672
*watch_args.create_sourcedirs,
7773
*watch_args.dev,
78-
(*watch_args.bsc_path).clone(),
7974
*watch_args.snapshot_output,
8075
);
8176

8277
Ok(())
8378
}
8479
cli::Command::Clean {
8580
folder,
86-
bsc_path,
8781
snapshot_output,
8882
} => {
8983
let _lock = get_lock(&folder);
9084

91-
build::clean::clean(
92-
Path::new(&folder as &str),
93-
show_progress,
94-
bsc_path.as_ref().map(PathBuf::from),
95-
*snapshot_output,
96-
)
85+
build::clean::clean(Path::new(&folder as &str), show_progress, *snapshot_output)
9786
}
9887
cli::Command::Legacy { legacy_args } => {
9988
let code = build::pass_through_legacy(legacy_args);

rewatch/src/watcher.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::queue::*;
1111
use futures_timer::Delay;
1212
use notify::event::ModifyKind;
1313
use notify::{Config, Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
14-
use std::path::{Path, PathBuf};
14+
use std::path::Path;
1515
use std::sync::Arc;
1616
use std::sync::Mutex;
1717
use std::time::{Duration, Instant};
@@ -63,19 +63,11 @@ async fn async_watch(
6363
after_build: Option<String>,
6464
create_sourcedirs: bool,
6565
build_dev_deps: bool,
66-
bsc_path: Option<PathBuf>,
6766
snapshot_output: bool,
6867
) -> notify::Result<()> {
69-
let mut build_state = build::initialize_build(
70-
None,
71-
filter,
72-
show_progress,
73-
path,
74-
&bsc_path,
75-
build_dev_deps,
76-
snapshot_output,
77-
)
78-
.expect("Can't initialize build");
68+
let mut build_state =
69+
build::initialize_build(None, filter, show_progress, path, build_dev_deps, snapshot_output)
70+
.expect("Can't initialize build");
7971
let mut needs_compile_type = CompileType::Incremental;
8072
// create a mutex to capture if ctrl-c was pressed
8173
let ctrlc_pressed = Arc::new(Mutex::new(false));
@@ -259,7 +251,6 @@ async fn async_watch(
259251
filter,
260252
show_progress,
261253
path,
262-
&bsc_path,
263254
build_dev_deps,
264255
snapshot_output,
265256
)
@@ -308,7 +299,6 @@ pub fn start(
308299
after_build: Option<String>,
309300
create_sourcedirs: bool,
310301
build_dev_deps: bool,
311-
bsc_path: Option<String>,
312302
snapshot_output: bool,
313303
) {
314304
futures::executor::block_on(async {
@@ -323,7 +313,6 @@ pub fn start(
323313
.expect("Could not start watcher");
324314

325315
let path = Path::new(folder);
326-
let bsc_path_buf = bsc_path.map(PathBuf::from);
327316

328317
if let Err(e) = async_watch(
329318
consumer,
@@ -333,7 +322,6 @@ pub fn start(
333322
after_build,
334323
create_sourcedirs,
335324
build_dev_deps,
336-
bsc_path_buf,
337325
snapshot_output,
338326
)
339327
.await

rewatch/tests/get_bin_paths.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @ts-check
2+
import { bsc_exe } from '../../cli/common/bins.js';
3+
4+
console.log(`RESCRIPT_BSC_EXE='${bsc_exe}'`);

rewatch/tests/suite-ci.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ cd $(dirname $0)
88
# Get rewatch executable location from the first argument or use default
99
if [ -n "$1" ]; then
1010
REWATCH_EXECUTABLE="$1"
11-
BSC_PATH=""
1211
else
1312
REWATCH_EXECUTABLE="../target/release/rewatch"
14-
BSC_PATH="--bsc-path ../../_build/install/default/bin/bsc"
13+
eval $(node ./get_bin_paths.js)
1514
fi
15+
1616
export REWATCH_EXECUTABLE
17-
export BSC_PATH
17+
export RESCRIPT_BSC_EXE
1818

1919
source ./utils.sh
2020

0 commit comments

Comments
 (0)