Skip to content

Rewatch: simplify getting bsc path #7634

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

Merged
merged 3 commits into from
Jul 12, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Remove obsolete jsx options. https://github.com/rescript-lang/rescript/pull/7633
- Remove obsolete option `-bs-unsafe-empty-array`. https://github.com/rescript-lang/rescript/pull/7635
- Clean up `config.ml`. https://github.com/rescript-lang/rescript/pull/7636
- Rewatch: simplify getting bsc path. https://github.com/rescript-lang/rescript/pull/7634

# 12.0.0-beta.1

Expand Down
34 changes: 4 additions & 30 deletions cli/rescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,14 @@
// @ts-check

import * as child_process from "node:child_process";
import { bsc_exe, rescript_exe } from "./common/bins.js";
import { rescript_exe } from "./common/bins.js";

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

const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));

try {
if (firstPositionalArgIndex !== -1) {
const subcommand = args[firstPositionalArgIndex];
const subcommandWithArgs = args.slice(firstPositionalArgIndex);

if (
subcommand === "build" ||
subcommand === "watch" ||
subcommand === "clean"
) {
child_process.execFileSync(
rescript_exe,
[...subcommandWithArgs, "--bsc-path", bsc_exe],
{
stdio: "inherit",
},
);
} else {
child_process.execFileSync(rescript_exe, [...args], {
stdio: "inherit",
});
}
} else {
// no subcommand means build subcommand
child_process.execFileSync(rescript_exe, [...args, "--bsc-path", bsc_exe], {
stdio: "inherit",
});
}
child_process.execFileSync(rescript_exe, args, {
stdio: "inherit",
});
} catch (err) {
if (err.status !== undefined) {
process.exit(err.status); // Pass through the exit code
Expand Down
8 changes: 1 addition & 7 deletions rewatch/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,12 @@ pub fn initialize_build(
filter: &Option<regex::Regex>,
show_progress: bool,
path: &Path,
bsc_path: &Option<PathBuf>,
build_dev_deps: bool,
snapshot_output: bool,
) -> Result<BuildState> {
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
let bsc_path = match bsc_path {
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
None => helpers::get_bsc(&project_root, &workspace_root),
};
let bsc_path = helpers::get_bsc();
let root_config_name = packages::read_package_name(&project_root)?;

if !snapshot_output && show_progress {
Expand Down Expand Up @@ -478,7 +474,6 @@ pub fn build(
show_progress: bool,
no_timing: bool,
create_sourcedirs: bool,
bsc_path: Option<PathBuf>,
build_dev_deps: bool,
snapshot_output: bool,
) -> Result<BuildState> {
Expand All @@ -493,7 +488,6 @@ pub fn build(
filter,
show_progress,
path,
&bsc_path,
build_dev_deps,
snapshot_output,
)
Expand Down
12 changes: 2 additions & 10 deletions rewatch/src/build/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
});
}

pub fn clean(
path: &Path,
show_progress: bool,
bsc_path: Option<PathBuf>,
snapshot_output: bool,
) -> Result<()> {
pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result<()> {
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
let packages = packages::make(
Expand All @@ -349,10 +344,7 @@ pub fn clean(
true,
)?;
let root_config_name = packages::read_package_name(&project_root)?;
let bsc_path = match bsc_path {
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
None => helpers::get_bsc(&project_root, &workspace_root),
};
let bsc_path = helpers::get_bsc();

let timing_clean_compiler_assets = Instant::now();
if !snapshot_output && show_progress {
Expand Down
24 changes: 0 additions & 24 deletions rewatch/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ pub struct DevArg {
pub dev: bool,
}

#[derive(Args, Debug, Clone)]
pub struct BscPathArg {
/// Custom path to bsc
#[arg(long)]
pub bsc_path: Option<String>,
}

#[derive(Args, Debug, Clone, Copy)]
pub struct SnapshotOutputArg {
/// simple output for snapshot testing
Expand Down Expand Up @@ -114,9 +107,6 @@ pub struct BuildArgs {

#[command(flatten)]
pub snapshot_output: SnapshotOutputArg,

#[command(flatten)]
pub bsc_path: BscPathArg,
}

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

#[command(flatten)]
pub snapshot_output: SnapshotOutputArg,

#[command(flatten)]
pub bsc_path: BscPathArg,
}

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

#[command(flatten)]
bsc_path: BscPathArg,

#[command(flatten)]
snapshot_output: SnapshotOutputArg,
},
Expand Down Expand Up @@ -231,14 +215,6 @@ impl Deref for DevArg {
}
}

impl Deref for BscPathArg {
type Target = Option<String>;

fn deref(&self) -> &Self::Target {
&self.bsc_path
}
}

impl Deref for SnapshotOutputArg {
type Target = bool;

Expand Down
50 changes: 15 additions & 35 deletions rewatch/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,44 +180,24 @@ pub fn create_path_for_path(path: &Path) {
fs::DirBuilder::new().recursive(true).create(path).unwrap();
}

fn get_bin_dir() -> PathBuf {
let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) {
("macos", "aarch64") => "darwin-arm64",
("macos", _) => "darwin-x64",
("linux", "aarch64") => "linux-arm64",
("linux", _) => "linux-x64",
("windows", "aarch64") => "win-arm64",
("windows", _) => "win32-x64",
_ => panic!("Unsupported architecture"),
};

Path::new("node_modules")
.join("@rescript")
.join(subfolder)
.join("bin")
pub fn get_bin_dir() -> PathBuf {
let current_exe_path = std::env::current_exe().expect("Could not get current executable path");
current_exe_path
.parent()
.expect("Could not get parent directory of current executable")
.to_path_buf()
}

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

match (
root_path
.join(&bin_dir)
.join("bsc.exe")
.canonicalize()
.map(StrippedVerbatimPath::to_stripped_verbatim_path),
workspace_root.as_ref().map(|workspace_root| {
workspace_root
.join(&bin_dir)
.join("bsc.exe")
.canonicalize()
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
}),
) {
(Ok(path), _) => path,
(_, Some(Ok(path))) => path,
_ => panic!("Could not find bsc.exe"),
}
bsc_path
.canonicalize()
.expect("Could not get bsc path")
.to_stripped_verbatim_path()
}

pub fn get_rescript_legacy(root_path: &Path, workspace_root: Option<PathBuf>) -> PathBuf {
Expand Down
15 changes: 2 additions & 13 deletions rewatch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use anyhow::Result;
use clap::Parser;
use log::LevelFilter;
use regex::Regex;
use std::{
io::Write,
path::{Path, PathBuf},
};
use std::{io::Write, path::Path};

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

Expand Down Expand Up @@ -45,7 +42,6 @@ fn main() -> Result<()> {
show_progress,
build_args.no_timing,
*build_args.create_sourcedirs,
build_args.bsc_path.as_ref().map(PathBuf::from),
*build_args.dev,
*build_args.snapshot_output,
) {
Expand Down Expand Up @@ -75,25 +71,18 @@ fn main() -> Result<()> {
(*watch_args.after_build).clone(),
*watch_args.create_sourcedirs,
*watch_args.dev,
(*watch_args.bsc_path).clone(),
*watch_args.snapshot_output,
);

Ok(())
}
cli::Command::Clean {
folder,
bsc_path,
snapshot_output,
} => {
let _lock = get_lock(&folder);

build::clean::clean(
Path::new(&folder as &str),
show_progress,
bsc_path.as_ref().map(PathBuf::from),
*snapshot_output,
)
build::clean::clean(Path::new(&folder as &str), show_progress, *snapshot_output)
}
cli::Command::Legacy { legacy_args } => {
let code = build::pass_through_legacy(legacy_args);
Expand Down
20 changes: 4 additions & 16 deletions rewatch/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::queue::*;
use futures_timer::Delay;
use notify::event::ModifyKind;
use notify::{Config, Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -63,19 +63,11 @@ async fn async_watch(
after_build: Option<String>,
create_sourcedirs: bool,
build_dev_deps: bool,
bsc_path: Option<PathBuf>,
snapshot_output: bool,
) -> notify::Result<()> {
let mut build_state = build::initialize_build(
None,
filter,
show_progress,
path,
&bsc_path,
build_dev_deps,
snapshot_output,
)
.expect("Can't initialize build");
let mut build_state =
build::initialize_build(None, filter, show_progress, path, build_dev_deps, snapshot_output)
.expect("Can't initialize build");
let mut needs_compile_type = CompileType::Incremental;
// create a mutex to capture if ctrl-c was pressed
let ctrlc_pressed = Arc::new(Mutex::new(false));
Expand Down Expand Up @@ -259,7 +251,6 @@ async fn async_watch(
filter,
show_progress,
path,
&bsc_path,
build_dev_deps,
snapshot_output,
)
Expand Down Expand Up @@ -308,7 +299,6 @@ pub fn start(
after_build: Option<String>,
create_sourcedirs: bool,
build_dev_deps: bool,
bsc_path: Option<String>,
snapshot_output: bool,
) {
futures::executor::block_on(async {
Expand All @@ -323,7 +313,6 @@ pub fn start(
.expect("Could not start watcher");

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

if let Err(e) = async_watch(
consumer,
Expand All @@ -333,7 +322,6 @@ pub fn start(
after_build,
create_sourcedirs,
build_dev_deps,
bsc_path_buf,
snapshot_output,
)
.await
Expand Down
4 changes: 4 additions & 0 deletions rewatch/tests/get_bin_paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-check
import { bsc_exe } from '../../cli/common/bins.js';

console.log(`RESCRIPT_BSC_EXE='${bsc_exe}'`);
6 changes: 3 additions & 3 deletions rewatch/tests/suite-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ cd $(dirname $0)
# Get rewatch executable location from the first argument or use default
if [ -n "$1" ]; then
REWATCH_EXECUTABLE="$1"
BSC_PATH=""
else
REWATCH_EXECUTABLE="../target/release/rewatch"
BSC_PATH="--bsc-path ../../_build/install/default/bin/bsc"
eval $(node ./get_bin_paths.js)
fi

export REWATCH_EXECUTABLE
export BSC_PATH
export RESCRIPT_BSC_EXE

source ./utils.sh

Expand Down
4 changes: 2 additions & 2 deletions rewatch/tests/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ overwrite() { echo -e "\r\033[1A\033[0K$@"; }
success() { echo -e "- ✅ \033[32m$1\033[0m"; }
error() { echo -e "- 🛑 \033[31m$1\033[0m"; }
bold() { echo -e "\033[1m$1\033[0m"; }
rewatch() { RUST_BACKTRACE=1 $REWATCH_EXECUTABLE $@ $BSC_PATH; }
rewatch_bg() { RUST_BACKTRACE=1 nohup $REWATCH_EXECUTABLE $@ $BSC_PATH; }
rewatch() { RUST_BACKTRACE=1 $REWATCH_EXECUTABLE $@; }
rewatch_bg() { RUST_BACKTRACE=1 nohup $REWATCH_EXECUTABLE $@; }

# Detect if running on Windows
is_windows() {
Expand Down