diff --git a/CHANGELOG.md b/CHANGELOG.md index 21dc19ecfc..c53e769d37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cli/rescript.js b/cli/rescript.js index 64f0ca982e..25a900abfd 100755 --- a/cli/rescript.js +++ b/cli/rescript.js @@ -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 diff --git a/rewatch/src/build.rs b/rewatch/src/build.rs index ed9c39d442..4545e3f0f3 100644 --- a/rewatch/src/build.rs +++ b/rewatch/src/build.rs @@ -114,16 +114,12 @@ pub fn initialize_build( filter: &Option, show_progress: bool, path: &Path, - bsc_path: &Option, build_dev_deps: bool, snapshot_output: bool, ) -> Result { 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 { @@ -478,7 +474,6 @@ pub fn build( show_progress: bool, no_timing: bool, create_sourcedirs: bool, - bsc_path: Option, build_dev_deps: bool, snapshot_output: bool, ) -> Result { @@ -493,7 +488,6 @@ pub fn build( filter, show_progress, path, - &bsc_path, build_dev_deps, snapshot_output, ) diff --git a/rewatch/src/build/clean.rs b/rewatch/src/build/clean.rs index 8a0bfb5999..da1543b2f9 100644 --- a/rewatch/src/build/clean.rs +++ b/rewatch/src/build/clean.rs @@ -331,12 +331,7 @@ pub fn cleanup_after_build(build_state: &BuildState) { }); } -pub fn clean( - path: &Path, - show_progress: bool, - bsc_path: Option, - 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( @@ -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 { diff --git a/rewatch/src/cli.rs b/rewatch/src/cli.rs index 8661b0ff1b..6053d8cca2 100644 --- a/rewatch/src/cli.rs +++ b/rewatch/src/cli.rs @@ -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, -} - #[derive(Args, Debug, Clone, Copy)] pub struct SnapshotOutputArg { /// simple output for snapshot testing @@ -114,9 +107,6 @@ pub struct BuildArgs { #[command(flatten)] pub snapshot_output: SnapshotOutputArg, - - #[command(flatten)] - pub bsc_path: BscPathArg, } #[derive(Args, Clone, Debug)] @@ -138,9 +128,6 @@ pub struct WatchArgs { #[command(flatten)] pub snapshot_output: SnapshotOutputArg, - - #[command(flatten)] - pub bsc_path: BscPathArg, } #[derive(Subcommand, Clone, Debug)] @@ -154,9 +141,6 @@ pub enum Command { #[command(flatten)] folder: FolderArg, - #[command(flatten)] - bsc_path: BscPathArg, - #[command(flatten)] snapshot_output: SnapshotOutputArg, }, @@ -231,14 +215,6 @@ impl Deref for DevArg { } } -impl Deref for BscPathArg { - type Target = Option; - - fn deref(&self) -> &Self::Target { - &self.bsc_path - } -} - impl Deref for SnapshotOutputArg { type Target = bool; diff --git a/rewatch/src/helpers.rs b/rewatch/src/helpers.rs index ce75919e7b..acfd70f388 100644 --- a/rewatch/src/helpers.rs +++ b/rewatch/src/helpers.rs @@ -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 { - 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 { diff --git a/rewatch/src/main.rs b/rewatch/src/main.rs index ebf04fa911..fb3f6c3516 100644 --- a/rewatch/src/main.rs +++ b/rewatch/src/main.rs @@ -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}; @@ -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, ) { @@ -75,7 +71,6 @@ fn main() -> Result<()> { (*watch_args.after_build).clone(), *watch_args.create_sourcedirs, *watch_args.dev, - (*watch_args.bsc_path).clone(), *watch_args.snapshot_output, ); @@ -83,17 +78,11 @@ fn main() -> Result<()> { } 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); diff --git a/rewatch/src/watcher.rs b/rewatch/src/watcher.rs index bb22a9545c..f9fb7f6398 100644 --- a/rewatch/src/watcher.rs +++ b/rewatch/src/watcher.rs @@ -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}; @@ -63,19 +63,11 @@ async fn async_watch( after_build: Option, create_sourcedirs: bool, build_dev_deps: bool, - bsc_path: Option, 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)); @@ -259,7 +251,6 @@ async fn async_watch( filter, show_progress, path, - &bsc_path, build_dev_deps, snapshot_output, ) @@ -308,7 +299,6 @@ pub fn start( after_build: Option, create_sourcedirs: bool, build_dev_deps: bool, - bsc_path: Option, snapshot_output: bool, ) { futures::executor::block_on(async { @@ -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, @@ -333,7 +322,6 @@ pub fn start( after_build, create_sourcedirs, build_dev_deps, - bsc_path_buf, snapshot_output, ) .await diff --git a/rewatch/tests/get_bin_paths.js b/rewatch/tests/get_bin_paths.js new file mode 100644 index 0000000000..31a6e384fe --- /dev/null +++ b/rewatch/tests/get_bin_paths.js @@ -0,0 +1,4 @@ +// @ts-check +import { bsc_exe } from '../../cli/common/bins.js'; + +console.log(`RESCRIPT_BSC_EXE='${bsc_exe}'`); diff --git a/rewatch/tests/suite-ci.sh b/rewatch/tests/suite-ci.sh index fd5f11544b..24ae688424 100755 --- a/rewatch/tests/suite-ci.sh +++ b/rewatch/tests/suite-ci.sh @@ -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 diff --git a/rewatch/tests/utils.sh b/rewatch/tests/utils.sh index 44a22ba0e3..bef51f9fce 100644 --- a/rewatch/tests/utils.sh +++ b/rewatch/tests/utils.sh @@ -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() {