From e1827be8f2435581e9913e97406064cd0d7c6607 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Sat, 3 May 2025 16:38:14 +0200 Subject: [PATCH 01/14] support out of source --- src/build/compile.rs | 46 ++++++++++++++++++++++++--------- src/build/packages.rs | 60 +++++++++++++++++++++++++++++++++++++++++-- src/build/parse.rs | 4 +-- src/config.rs | 25 ++++++++++++++++++ src/helpers.rs | 8 ++++-- 5 files changed, 124 insertions(+), 19 deletions(-) diff --git a/src/build/compile.rs b/src/build/compile.rs index 3a13fbe..dc870e7 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -430,23 +430,43 @@ pub fn compiler_args( false => vec![], }; + let package_name_arg = vec!["-bs-package-name".to_string(), config.name.to_owned()]; + let implementation_args = if is_interface { debug!("Compiling interface file: {}", &module_name); vec![] } else { debug!("Compiling file: {}", &module_name); - - vec![ - "-bs-package-name".to_string(), - config.name.to_owned(), - "-bs-package-output".to_string(), - format!( - "{}:{}:{}", - root_config.get_module(), - Path::new(file_path).parent().unwrap().to_str().unwrap(), - root_config.get_suffix() - ), - ] + let specs = config.get_package_specs(); + + specs + .iter() + .map(|spec| { + return vec![ + "-bs-package-output".to_string(), + format!( + "{}:{}:{}", + root_config.get_module(), + if spec.in_source { + Path::new(file_path) + .parent() + .unwrap() + .to_str() + .unwrap() + .to_string() + } else { + format!( + "lib/{}/{}", + spec.get_out_of_source_dir(), + Path::new(file_path).parent().unwrap().to_str().unwrap() + ) + }, + root_config.get_suffix() + ), + ]; + }) + .flatten() + .collect() }; vec![ @@ -463,6 +483,7 @@ pub fn compiler_args( // this is the default // we should probably parse the right ones from the package config // vec!["-w".to_string(), "a".to_string()], + package_name_arg, implementation_args, // vec![ // "-I".to_string(), @@ -588,6 +609,7 @@ fn compile_file( &Some(packages), build_dev_deps, ); + let to_mjs = Command::new(bsc_path) .current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap()) .args(to_mjs_args) diff --git a/src/build/packages.rs b/src/build/packages.rs index d458ccc..600f37e 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -67,6 +67,14 @@ pub fn get_build_path(canonical_path: &str) -> String { format!("{}/lib/bs", canonical_path) } +pub fn get_js_path(canonical_path: &str) -> String { + format!("{}/lib/js", canonical_path) +} + +pub fn get_es6_path(canonical_path: &str) -> String { + format!("{}/lib/es6", canonical_path) +} + pub fn get_ocaml_build_path(canonical_path: &str) -> String { format!("{}/lib/ocaml", canonical_path) } @@ -80,6 +88,14 @@ impl Package { get_build_path(&self.path) } + pub fn get_js_path(&self) -> String { + get_js_path(&self.path) + } + + pub fn get_es6_path(&self) -> String { + get_es6_path(&self.path) + } + pub fn get_mlmap_path(&self) -> String { self.get_build_path() + "/" @@ -594,8 +610,48 @@ pub fn parse_packages(build_state: &mut BuildState) { } let build_path_abs = package.get_build_path(); let bs_build_path = package.get_ocaml_build_path(); - helpers::create_build_path(&build_path_abs); - helpers::create_build_path(&bs_build_path); + helpers::create_path(&build_path_abs); + helpers::create_path(&bs_build_path); + let root_config = build_state + .get_package(&build_state.root_config_name) + .expect("cannot find root config"); + + root_config.config.get_package_specs().iter().for_each(|spec| { + if !spec.in_source { + // we don't want to calculate this if we don't have out of source specs + // we do this twice, but we almost never have multiple package specs + // so this optimization is less important + let relative_dirs: AHashSet = match &package.source_files { + Some(source_files) => source_files + .keys() + .map(|source_file| { + Path::new(source_file) + .parent() + .expect("parent dir not found") + .to_owned() + }) + .collect(), + _ => AHashSet::new(), + }; + if spec.is_common_js() { + helpers::create_path(&package.get_js_path()); + relative_dirs.iter().for_each(|path_buf| { + helpers::create_path_for_path(&Path::join( + &PathBuf::from(package.get_js_path()), + path_buf, + )) + }) + } else { + helpers::create_path(&package.get_es6_path()); + relative_dirs.iter().for_each(|path_buf| { + helpers::create_path_for_path(&Path::join( + &PathBuf::from(package.get_es6_path()), + path_buf, + )) + }) + } + } + }); package.namespace.to_suffix().iter().for_each(|namespace| { // generate the mlmap "AST" file for modules that have a namespace configured diff --git a/src/build/parse.rs b/src/build/parse.rs index 023a329..b4fddd7 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -325,9 +325,7 @@ fn generate_ast( ); // generate the dir of the ast_path (it mirrors the source file dir) - helpers::create_build_path( - &(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy()), - ); + helpers::create_path(&(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy())); /* Create .ast */ let result = if let Some(res_to_ast) = Some( diff --git a/src/config.rs b/src/config.rs index 53e1353..572a50a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -111,6 +111,23 @@ pub struct PackageSpec { pub suffix: Option, } +impl PackageSpec { + pub fn get_out_of_source_dir(&self) -> String { + match self.module.as_str() { + "commonjs" => "js", + _ => "es6", + } + .to_string() + } + + pub fn is_common_js(&self) -> bool { + match self.module.as_str() { + "commonjs" => true, + _ => false, + } + } +} + #[derive(Deserialize, Debug, Clone)] #[serde(untagged)] pub enum Error { @@ -448,6 +465,14 @@ impl Config { None => vec![], } } + + pub fn get_package_specs(&self) -> Vec { + match self.package_specs.clone() { + None => vec![], + Some(OneOrMore::Single(spec)) => vec![spec], + Some(OneOrMore::Multiple(vec)) => vec, + } + } } #[cfg(test)] diff --git a/src/helpers.rs b/src/helpers.rs index e6d4096..c4a7343 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -139,13 +139,17 @@ pub fn contains_ascii_characters(str: &str) -> bool { false } -pub fn create_build_path(build_path: &str) { +pub fn create_path(path: &str) { fs::DirBuilder::new() .recursive(true) - .create(PathBuf::from(build_path.to_string())) + .create(PathBuf::from(path.to_string())) .unwrap(); } +pub fn create_path_for_path(path: &Path) { + fs::DirBuilder::new().recursive(true).create(path).unwrap(); +} + pub fn get_bsc(root_path: &str, workspace_root: Option) -> String { let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) { ("macos", "aarch64") => "darwinarm64", From 3e5fed92cd2d7d411155e956097fc154105c9308 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Sun, 4 May 2025 11:36:38 +0200 Subject: [PATCH 02/14] fix --- src/build.rs | 2 +- src/build/compile.rs | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/build.rs b/src/build.rs index b4754f6..47c632c 100644 --- a/src/build.rs +++ b/src/build.rs @@ -138,7 +138,7 @@ pub fn initialize_build( 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) => bsc_path, + Some(bsc_path) => helpers::get_abs_path(&bsc_path), None => helpers::get_bsc(&project_root, workspace_root.to_owned()), }; let root_config_name = packages::read_package_name(&project_root)?; diff --git a/src/build/compile.rs b/src/build/compile.rs index dc870e7..de599f2 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -12,7 +12,7 @@ use anyhow::anyhow; use console::style; use log::{debug, trace}; use rayon::prelude::*; -use std::path::Path; +use std::path::{Component, Path, PathBuf}; use std::process::Command; use std::time::SystemTime; @@ -446,7 +446,7 @@ pub fn compiler_args( "-bs-package-output".to_string(), format!( "{}:{}:{}", - root_config.get_module(), + spec.module, if spec.in_source { Path::new(file_path) .parent() @@ -456,9 +456,13 @@ pub fn compiler_args( .to_string() } else { format!( - "lib/{}/{}", - spec.get_out_of_source_dir(), - Path::new(file_path).parent().unwrap().to_str().unwrap() + "lib/{}", + Path::join( + Path::new(&spec.get_out_of_source_dir()), + Path::new(file_path).parent().unwrap() + ) + .to_str() + .unwrap() ) }, root_config.get_suffix() From 15a83b5e49577fd557c5ad381e6f04f401055728 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Sun, 4 May 2025 12:15:10 +0200 Subject: [PATCH 03/14] do everything proper from the package specs --- src/build/clean.rs | 29 ++++++++++++++++------ src/build/compile.rs | 43 ++++++++++++++++++--------------- src/build/read_compile_state.rs | 7 +++++- src/config.rs | 35 +++++++-------------------- 4 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/build/clean.rs b/src/build/clean.rs index 0cc4142..5913cbf 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -70,16 +70,31 @@ pub fn clean_mjs_files(build_state: &BuildState) { .packages .get(&build_state.root_config_name) .expect("Could not find root package"); - Some(( - std::path::PathBuf::from(package.path.to_string()) - .join(&source_file.implementation.path) - .to_string_lossy() - .to_string(), - root_package.config.get_suffix(), - )) + + Some( + root_package + .config + .get_package_specs() + .iter() + .filter_map(|spec| { + if spec.in_source { + Some(( + std::path::PathBuf::from(package.path.to_string()) + .join(&source_file.implementation.path) + .to_string_lossy() + .to_string(), + spec.get_suffix(), + )) + } else { + None + } + }) + .collect::>(), + ) } _ => None, }) + .flatten() .collect::>(); rescript_file_locations diff --git a/src/build/compile.rs b/src/build/compile.rs index de599f2..c8addac 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -465,7 +465,7 @@ pub fn compiler_args( .unwrap() ) }, - root_config.get_suffix() + spec.get_suffix() ), ]; }) @@ -725,26 +725,31 @@ fn compile_file( } // copy js file - match &module.source_type { - SourceType::SourceFile(SourceFile { - implementation: Implementation { path, .. }, - .. - }) => { - let source = helpers::get_source_file_from_rescript_file( - &std::path::Path::new(&package.path).join(path), - &root_package.config.get_suffix(), - ); - let destination = helpers::get_source_file_from_rescript_file( - &std::path::Path::new(&package.get_build_path()).join(path), - &root_package.config.get_suffix(), - ); - - if source.exists() { - let _ = std::fs::copy(&source, &destination).expect("copying source file failed"); + root_package.config.get_package_specs().iter().for_each(|spec| { + if spec.in_source { + match &module.source_type { + SourceType::SourceFile(SourceFile { + implementation: Implementation { path, .. }, + .. + }) => { + let source = helpers::get_source_file_from_rescript_file( + &std::path::Path::new(&package.path).join(path), + &spec.get_suffix(), + ); + let destination = helpers::get_source_file_from_rescript_file( + &std::path::Path::new(&package.get_build_path()).join(path), + &spec.get_suffix(), + ); + + if source.exists() { + let _ = + std::fs::copy(&source, &destination).expect("copying source file failed"); + } + } + _ => (), } } - _ => (), - } + }); if helpers::contains_ascii_characters(&err) { if package.is_pinned_dep || package.is_local_dep { diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index 27fd1a7..54da8df 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -103,7 +103,12 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { last_modified: last_modified.to_owned(), ast_file_path, is_root: *package_is_root, - suffix: root_package.config.get_suffix(), + suffix: root_package + .config + .get_package_specs() + .first() + .map(|spec| spec.get_suffix()) + .unwrap(), }, ); let _ = ast_rescript_file_locations.insert(res_file_path); diff --git a/src/config.rs b/src/config.rs index 572a50a..dcf4b3e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -126,6 +126,10 @@ impl PackageSpec { _ => false, } } + + pub fn get_suffix(&self) -> String { + self.suffix.to_owned().unwrap_or(".js".to_string()) + } } #[derive(Deserialize, Debug, Clone)] @@ -434,31 +438,6 @@ impl Config { } } - pub fn get_module(&self) -> String { - match &self.package_specs { - Some(OneOrMore::Single(PackageSpec { module, .. })) => module.to_string(), - Some(OneOrMore::Multiple(vec)) => match vec.first() { - Some(PackageSpec { module, .. }) => module.to_string(), - None => "commonjs".to_string(), - }, - _ => "commonjs".to_string(), - } - } - - pub fn get_suffix(&self) -> String { - match &self.package_specs { - Some(OneOrMore::Single(PackageSpec { suffix, .. })) => suffix.to_owned(), - Some(OneOrMore::Multiple(vec)) => match vec.first() { - Some(PackageSpec { suffix, .. }) => suffix.to_owned(), - None => None, - }, - - _ => None, - } - .or(self.suffix.to_owned()) - .unwrap_or(".js".to_string()) - } - pub fn get_gentype_arg(&self) -> Vec { match &self.gentype_config { Some(_) => vec!["-bs-gentype".to_string()], @@ -468,7 +447,11 @@ impl Config { pub fn get_package_specs(&self) -> Vec { match self.package_specs.clone() { - None => vec![], + None => vec![PackageSpec { + module: "commonjs".to_string(), + in_source: true, + suffix: Some(".js".to_string()), + }], Some(OneOrMore::Single(spec)) => vec![spec], Some(OneOrMore::Multiple(vec)) => vec, } From 3abf0f09d81417869f020a643513bfa32c4ef1dc Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Sun, 4 May 2025 12:15:27 +0200 Subject: [PATCH 04/14] remove warnings --- src/build/compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/compile.rs b/src/build/compile.rs index c8addac..4c87c73 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -12,7 +12,7 @@ use anyhow::anyhow; use console::style; use log::{debug, trace}; use rayon::prelude::*; -use std::path::{Component, Path, PathBuf}; +use std::path::Path; use std::process::Command; use std::time::SystemTime; From 7cea36e9102cfe9e980bcd367e050c7b74f8e4c7 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Sun, 4 May 2025 18:06:57 +0200 Subject: [PATCH 05/14] fix bug --- src/build/compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/compile.rs b/src/build/compile.rs index 4c87c73..9000422 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -437,7 +437,7 @@ pub fn compiler_args( vec![] } else { debug!("Compiling file: {}", &module_name); - let specs = config.get_package_specs(); + let specs = root_config.get_package_specs(); specs .iter() From 86e02145fe68b1dc44106f34b41ca1cbf22465cb Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Mon, 5 May 2025 09:14:55 +0200 Subject: [PATCH 06/14] add alias for config --- src/config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index dcf4b3e..631681f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -203,13 +203,13 @@ pub struct Config { pub suffix: Option, #[serde(rename = "pinned-dependencies")] pub pinned_dependencies: Option>, - #[serde(rename = "bs-dependencies")] + #[serde(rename = "dependencies", alias = "bs-dependencies")] pub bs_dependencies: Option>, - #[serde(rename = "bs-dev-dependencies")] + #[serde(rename = "bs-dev-dependencies", alias = "dev-dependencies")] pub bs_dev_dependencies: Option>, #[serde(rename = "ppx-flags")] pub ppx_flags: Option>>, - #[serde(rename = "bsc-flags")] + #[serde(rename = "bsc-flags", alias = "compiler-flags")] pub bsc_flags: Option>>, pub reason: Option, pub namespace: Option, From 19b8e37ac904e4ab6cd5a6836e82ff15d151f064 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Mon, 5 May 2025 09:15:15 +0200 Subject: [PATCH 07/14] Fix tests --- src/build/clean.rs | 2 +- src/build/compile.rs | 6 +++--- src/build/read_compile_state.rs | 5 +---- src/config.rs | 17 +++++++++++++---- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/build/clean.rs b/src/build/clean.rs index 5913cbf..0fdf11a 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -83,7 +83,7 @@ pub fn clean_mjs_files(build_state: &BuildState) { .join(&source_file.implementation.path) .to_string_lossy() .to_string(), - spec.get_suffix(), + root_package.config.get_suffix(spec), )) } else { None diff --git a/src/build/compile.rs b/src/build/compile.rs index 9000422..73260ec 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -465,7 +465,7 @@ pub fn compiler_args( .unwrap() ) }, - spec.get_suffix() + root_config.get_suffix(spec), ), ]; }) @@ -734,11 +734,11 @@ fn compile_file( }) => { let source = helpers::get_source_file_from_rescript_file( &std::path::Path::new(&package.path).join(path), - &spec.get_suffix(), + &root_package.config.get_suffix(spec), ); let destination = helpers::get_source_file_from_rescript_file( &std::path::Path::new(&package.get_build_path()).join(path), - &spec.get_suffix(), + &root_package.config.get_suffix(spec), ); if source.exists() { diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index 54da8df..4bfc3e2 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -105,10 +105,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { is_root: *package_is_root, suffix: root_package .config - .get_package_specs() - .first() - .map(|spec| spec.get_suffix()) - .unwrap(), + .get_suffix(root_package.config.get_package_specs().first().unwrap()), }, ); let _ = ast_rescript_file_locations.insert(res_file_path); diff --git a/src/config.rs b/src/config.rs index 631681f..60b118d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -127,8 +127,8 @@ impl PackageSpec { } } - pub fn get_suffix(&self) -> String { - self.suffix.to_owned().unwrap_or(".js".to_string()) + pub fn get_suffix(&self) -> Option { + self.suffix.to_owned() } } @@ -456,6 +456,12 @@ impl Config { Some(OneOrMore::Multiple(vec)) => vec, } } + + pub fn get_suffix(&self, spec: &PackageSpec) -> String { + spec.get_suffix() + .or(self.suffix.clone()) + .unwrap_or(".js".to_string()) + } } #[cfg(test)] @@ -476,8 +482,11 @@ mod tests { "#; let config = serde_json::from_str::(json).unwrap(); - assert_eq!(config.get_suffix(), ".mjs"); - assert_eq!(config.get_module(), "es6"); + let specs = config.get_package_specs(); + assert_eq!(specs.len(), 1); + let spec = specs.first().unwrap(); + assert_eq!(spec.module, "es6"); + assert_eq!(config.get_suffix(spec), ".mjs"); } #[test] From f49667b61cab4408e564333cf062ccff89eb53df Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Mon, 5 May 2025 09:30:44 +0200 Subject: [PATCH 08/14] write more tests for the bsconfig aliases --- src/config.rs | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/src/config.rs b/src/config.rs index 60b118d..7bcbda2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -607,6 +607,134 @@ mod tests { ); } + #[test] + fn test_get_suffix() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs" + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!( + config.get_suffix(&config.get_package_specs().first().unwrap()), + ".mjs" + ); + } + + #[test] + fn test_dependencies() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "bs-dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!(config.bs_dependencies, Some(vec!["@testrepo/main".to_string()])); + } + + #[test] + fn test_dependencies_alias() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!(config.bs_dependencies, Some(vec!["@testrepo/main".to_string()])); + } + + #[test] + fn test_dev_dependencies() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "bs-dev-dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!( + config.bs_dev_dependencies, + Some(vec!["@testrepo/main".to_string()]) + ); + } + + #[test] + fn test_dev_dependencies_alias() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "dev-dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!( + config.bs_dev_dependencies, + Some(vec!["@testrepo/main".to_string()]) + ); + } + #[test] fn test_check_if_rescript11_or_higher() { assert_eq!(check_if_rescript11_or_higher("11.0.0"), Ok(true)); From 5142449ef33b05b495d7b85193df11bea8fcfeda Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Mon, 5 May 2025 10:38:07 +0200 Subject: [PATCH 09/14] add upstream script etc --- .gitignore | 1 + .gitmodules | 3 --- sync_upstream.sh | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) delete mode 100644 .gitmodules create mode 100755 sync_upstream.sh diff --git a/.gitignore b/.gitignore index 0592392..e68f4e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .DS_Store +/docs diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 9e2c168..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "walnut_monorepo"] - path = walnut_monorepo - url = git@github.com:teamwalnut/walnut_monorepo.git diff --git a/sync_upstream.sh b/sync_upstream.sh new file mode 100755 index 0000000..b58c312 --- /dev/null +++ b/sync_upstream.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +rm -rf testrepo/node_modules +cp -rf . ../rescript/rewatch +cd testrepo +yarn install +cd .. +rm -rf ../rescript/rewatch/.git +rm -rf ../rescript/rewatch/docs +rm -rf ../rescript/rewatch/.github +cd ../rescript/rewatch +rm .tmuxinator.yaml +rm package.json +rm release.sh +rm sync_upstream.sh +# reset yarn.lock to the most recent commited version +git checkout -- testrepo/yarn.lock +cd testrepo +yarn install From cd9a16ecc5a6f653ca95c048a113d83a425e30c6 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Thu, 8 May 2025 22:18:46 +0200 Subject: [PATCH 10/14] downstream rescript changes --- src/build.rs | 2 +- src/build/clean.rs | 2 +- src/main.rs | 1 + src/watcher.rs | 34 ++++++++++++++++--- testrepo/package.json | 3 ++ testrepo/packages/dep01/package.json | 3 +- testrepo/packages/dep02/package.json | 5 +-- testrepo/packages/main/package.json | 3 +- testrepo/packages/new-namespace/package.json | 5 +-- testrepo/yarn.lock | 2 +- tests/lock.sh | 15 ++++---- tests/snapshots/remove-file.txt | 4 +-- .../rename-file-internal-dep-namespace.txt | 4 +-- tests/snapshots/rename-file-internal-dep.txt | 4 +-- tests/suite-ci.sh | 30 +++++++++++----- tests/utils.sh | 3 +- tests/watch.sh | 4 +-- 17 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/build.rs b/src/build.rs index 47c632c..15f02de 100644 --- a/src/build.rs +++ b/src/build.rs @@ -72,7 +72,7 @@ pub fn get_compiler_args( rescript_version } else { let bsc_path = match bsc_path { - Some(bsc_path) => bsc_path, + Some(bsc_path) => helpers::get_abs_path(&bsc_path), None => helpers::get_bsc(&package_root, workspace_root.to_owned()), }; helpers::get_rescript_version(&bsc_path) diff --git a/src/build/clean.rs b/src/build/clean.rs index 0fdf11a..24b3d2c 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -351,7 +351,7 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option) -> Resul )?; let root_config_name = packages::read_package_name(&project_root)?; let bsc_path = match bsc_path { - Some(bsc_path) => bsc_path, + Some(bsc_path) => helpers::get_abs_path(&bsc_path), None => helpers::get_bsc(&project_root, workspace_root.to_owned()), }; diff --git a/src/main.rs b/src/main.rs index 4e4d4ad..ebfa29e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,6 +149,7 @@ fn main() -> Result<()> { args.after_build, args.create_sourcedirs, args.dev, + args.bsc_path, ); Ok(()) diff --git a/src/watcher.rs b/src/watcher.rs index 3846e2f..98431b0 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -54,9 +54,18 @@ async fn async_watch( after_build: Option, create_sourcedirs: bool, build_dev_deps: bool, + bsc_path: Option, ) -> notify::Result<()> { - let mut build_state = build::initialize_build(None, filter, show_progress, path, None, build_dev_deps) - .expect("Can't initialize build"); + let mut build_state = build::initialize_build( + None, + filter, + show_progress, + path, + None, + build_dev_deps, + bsc_path.clone(), + ) + .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)); @@ -91,6 +100,20 @@ async fn async_watch( } for event in events { + // if there is a file named rewatch.lock in the events path, we can quit the watcher + if let Some(_) = event.paths.iter().find(|path| path.ends_with("rewatch.lock")) { + match event.kind { + EventKind::Remove(_) => { + if show_progress { + println!("\nExiting... (lockfile removed)"); + } + clean::cleanup_after_build(&build_state); + return Ok(()); + } + _ => (), + } + } + let paths = event .paths .iter() @@ -214,9 +237,8 @@ async fn async_watch( } CompileType::Full => { let timing_total = Instant::now(); - build_state = - build::initialize_build(None, filter, show_progress, path, None, build_dev_deps) - .expect("Can't initialize build"); + build_state = build::initialize_build(None, filter, show_progress, path, None) + .expect("Can't initialize build"); let _ = build::incremental_build( &mut build_state, None, @@ -260,6 +282,7 @@ pub fn start( after_build: Option, create_sourcedirs: bool, build_dev_deps: bool, + bsc_path: Option, ) { futures::executor::block_on(async { let queue = Arc::new(FifoQueue::>::new()); @@ -280,6 +303,7 @@ pub fn start( after_build, create_sourcedirs, build_dev_deps, + bsc_path, ) .await { diff --git a/testrepo/package.json b/testrepo/package.json index c09461a..7f50287 100644 --- a/testrepo/package.json +++ b/testrepo/package.json @@ -11,6 +11,9 @@ "packages/with-dev-deps" ] }, + "dependencies": { + "rescript": "11.1.4" + }, "scripts": { "build": "../target/release/rewatch build .", "build:rescript": "rescript build -with-deps", diff --git a/testrepo/packages/dep01/package.json b/testrepo/packages/dep01/package.json index e60d809..9209fbb 100644 --- a/testrepo/packages/dep01/package.json +++ b/testrepo/packages/dep01/package.json @@ -12,7 +12,6 @@ "author": "", "license": "MIT", "dependencies": { - "@testrepo/dep02": "*", - "rescript": "*" + "@testrepo/dep02": "*" } } diff --git a/testrepo/packages/dep02/package.json b/testrepo/packages/dep02/package.json index 8dc05ab..ee0c567 100644 --- a/testrepo/packages/dep02/package.json +++ b/testrepo/packages/dep02/package.json @@ -10,8 +10,5 @@ "rescript" ], "author": "", - "license": "MIT", - "dependencies": { - "rescript": "*" - } + "license": "MIT" } diff --git a/testrepo/packages/main/package.json b/testrepo/packages/main/package.json index 5bf3dca..10954e0 100644 --- a/testrepo/packages/main/package.json +++ b/testrepo/packages/main/package.json @@ -12,7 +12,6 @@ "author": "", "license": "MIT", "dependencies": { - "@testrepo/dep01": "*", - "rescript": "*" + "@testrepo/dep01": "*" } } diff --git a/testrepo/packages/new-namespace/package.json b/testrepo/packages/new-namespace/package.json index 1fb4e26..8e30fe2 100644 --- a/testrepo/packages/new-namespace/package.json +++ b/testrepo/packages/new-namespace/package.json @@ -5,8 +5,5 @@ "rescript" ], "author": "", - "license": "MIT", - "dependencies": { - "rescript": "*" - } + "license": "MIT" } diff --git a/testrepo/yarn.lock b/testrepo/yarn.lock index cc5d022..eca9571 100644 --- a/testrepo/yarn.lock +++ b/testrepo/yarn.lock @@ -7,7 +7,7 @@ resolved "https://registry.yarnpkg.com/@rescript/core/-/core-1.6.1.tgz#159670c94d64a2b8236f46be2bf09a007b1ece08" integrity sha512-vyb5k90ck+65Fgui+5vCja/mUfzKaK3kOPT4Z6aAJdHLH1eljEi1zKhXroCiCtpNLSWp8k4ulh1bdB5WS0hvqA== -rescript@*: +rescript@*, rescript@11.1.4: version "11.1.4" resolved "https://registry.yarnpkg.com/rescript/-/rescript-11.1.4.tgz#9a42ebc4fc5363707e39cef5b3188160b63bee42" integrity sha512-0bGU0bocihjSC6MsE3TMjHjY0EUpchyrREquLS8VsZ3ohSMD+VHUEwimEfB3kpBI1vYkw3UFZ3WD8R28guz/Vw== diff --git a/tests/lock.sh b/tests/lock.sh index 019e616..4522a7a 100755 --- a/tests/lock.sh +++ b/tests/lock.sh @@ -14,20 +14,21 @@ else fi exit_watcher() { - # we need to kill the parent process (rewatch) - kill $(pgrep -P $!); + # Try to find child process, if not found just kill the process directly + rm lib/rewatch.lock } -rewatch watch &>/dev/null & -success "Watcher Started" +rewatch_bg watch > /dev/null 2>&1 & sleep 1 -if rewatch watch 2>&1 | grep 'Could not start Rewatch:' &> /dev/null; +if rewatch watch | grep 'Could not start Rewatch:' &> /dev/null; then + # rm output.txt success "Lock is correctly set" exit_watcher else + # rm output.txt error "Not setting lock correctly" exit_watcher exit 1 @@ -36,7 +37,7 @@ fi sleep 1 touch tmp.txt -rewatch watch &> tmp.txt & +rewatch_bg watch > tmp.txt 2>&1 & success "Watcher Started" sleep 1 @@ -51,4 +52,4 @@ else exit_watcher fi -rm tmp.txt +rm tmp.txt \ No newline at end of file diff --git a/tests/snapshots/remove-file.txt b/tests/snapshots/remove-file.txt index d17ec13..7fdacf2 100644 --- a/tests/snapshots/remove-file.txt +++ b/tests/snapshots/remove-file.txt @@ -17,8 +17,8 @@ The module or file Dep02 can't be found. - If it's a third-party dependency: - - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? - - Did you include the file's directory to the "sources" in bsconfig.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in rescript.json? + - Did you include the file's directory to the "sources" in rescript.json? diff --git a/tests/snapshots/rename-file-internal-dep-namespace.txt b/tests/snapshots/rename-file-internal-dep-namespace.txt index 72fedd6..a5836f9 100644 --- a/tests/snapshots/rename-file-internal-dep-namespace.txt +++ b/tests/snapshots/rename-file-internal-dep-namespace.txt @@ -15,8 +15,8 @@ The module or file Other_module can't be found. - If it's a third-party dependency: - - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? - - Did you include the file's directory to the "sources" in bsconfig.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in rescript.json? + - Did you include the file's directory to the "sources" in rescript.json? Hint: Did you mean Other_module2? diff --git a/tests/snapshots/rename-file-internal-dep.txt b/tests/snapshots/rename-file-internal-dep.txt index bb0e0f1..9dc7b4a 100644 --- a/tests/snapshots/rename-file-internal-dep.txt +++ b/tests/snapshots/rename-file-internal-dep.txt @@ -17,8 +17,8 @@ The module or file InternalDep can't be found. - If it's a third-party dependency: - - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? - - Did you include the file's directory to the "sources" in bsconfig.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in rescript.json? + - Did you include the file's directory to the "sources" in rescript.json? diff --git a/tests/suite-ci.sh b/tests/suite-ci.sh index bcec567..6097074 100755 --- a/tests/suite-ci.sh +++ b/tests/suite-ci.sh @@ -2,23 +2,35 @@ # Make sure we are in the right directory cd $(dirname $0) +# Get rewatch executable location from the first argument or use default +if [ -n "$1" ]; then + REWATCH_EXECUTABLE="$1" +else + REWATCH_EXECUTABLE="../target/release/rewatch" +fi +export REWATCH_EXECUTABLE + source ./utils.sh -bold "Check if build exists" -if test -f ../target/release/rewatch; -then - success "Build exists" -else - error "Build does not exist. Exiting..." - exit 1 -fi +bold "Rescript version" +(cd ../testrepo && ./node_modules/.bin/rescript -v) + +# we need to reset the yarn.lock and package.json to the original state +# so there is not diff in git. The CI will install new ReScript package +bold "Reset package.json and yarn.lock" +git checkout ../testrepo/yarn.lock &> /dev/null +git checkout ../testrepo/package.json &> /dev/null +success "Reset package.json and yarn.lock" bold "Make sure the testrepo is clean" -if git diff --exit-code ../testrepo &> /dev/null; +if git diff --exit-code ../testrepo &> diff.txt; then + rm diff.txt success "Testrepo has no changes" else error "Testrepo is not clean to start with" + cat diff.txt + rm diff.txt exit 1 fi diff --git a/tests/utils.sh b/tests/utils.sh index e02b280..f2ee211 100644 --- a/tests/utils.sh +++ b/tests/utils.sh @@ -3,7 +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 ../target/release/rewatch --no-timing=true $@; } +rewatch() { RUST_BACKTRACE=1 $REWATCH_EXECUTABLE --no-timing=true $@; } +rewatch_bg() { RUST_BACKTRACE=1 nohup $REWATCH_EXECUTABLE --no-timing=true $@; } replace() { if [[ $OSTYPE == 'darwin'* ]]; diff --git a/tests/watch.sh b/tests/watch.sh index 20546d8..e3a24c0 100755 --- a/tests/watch.sh +++ b/tests/watch.sh @@ -13,10 +13,10 @@ fi exit_watcher() { # we need to kill the parent process (rewatch) - kill $(pgrep -P $!); + rm lib/rewatch.lock } -rewatch watch &>/dev/null & +rewatch_bg watch > /dev/null 2>&1 & success "Watcher Started" echo 'Js.log("added-by-test")' >> ./packages/main/src/Main.res From 190b67983bf1e387748a241e27be01e90f77aee2 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Thu, 8 May 2025 22:19:25 +0200 Subject: [PATCH 11/14] fix snapshot --- tests/snapshots/remove-file.txt | 4 ++-- tests/snapshots/rename-file-internal-dep-namespace.txt | 4 ++-- tests/snapshots/rename-file-internal-dep.txt | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/snapshots/remove-file.txt b/tests/snapshots/remove-file.txt index 7fdacf2..d17ec13 100644 --- a/tests/snapshots/remove-file.txt +++ b/tests/snapshots/remove-file.txt @@ -17,8 +17,8 @@ The module or file Dep02 can't be found. - If it's a third-party dependency: - - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in rescript.json? - - Did you include the file's directory to the "sources" in rescript.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? + - Did you include the file's directory to the "sources" in bsconfig.json? diff --git a/tests/snapshots/rename-file-internal-dep-namespace.txt b/tests/snapshots/rename-file-internal-dep-namespace.txt index a5836f9..72fedd6 100644 --- a/tests/snapshots/rename-file-internal-dep-namespace.txt +++ b/tests/snapshots/rename-file-internal-dep-namespace.txt @@ -15,8 +15,8 @@ The module or file Other_module can't be found. - If it's a third-party dependency: - - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in rescript.json? - - Did you include the file's directory to the "sources" in rescript.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? + - Did you include the file's directory to the "sources" in bsconfig.json? Hint: Did you mean Other_module2? diff --git a/tests/snapshots/rename-file-internal-dep.txt b/tests/snapshots/rename-file-internal-dep.txt index 9dc7b4a..bb0e0f1 100644 --- a/tests/snapshots/rename-file-internal-dep.txt +++ b/tests/snapshots/rename-file-internal-dep.txt @@ -17,8 +17,8 @@ The module or file InternalDep can't be found. - If it's a third-party dependency: - - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in rescript.json? - - Did you include the file's directory to the "sources" in rescript.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? + - Did you include the file's directory to the "sources" in bsconfig.json? From 1bf1632eaced9b1a309b7a96a196ff458809e8e2 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Thu, 8 May 2025 22:25:15 +0200 Subject: [PATCH 12/14] fix rebase --- src/watcher.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/watcher.rs b/src/watcher.rs index 98431b0..6b99db7 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -61,9 +61,8 @@ async fn async_watch( filter, show_progress, path, - None, - build_dev_deps, bsc_path.clone(), + build_dev_deps, ) .expect("Can't initialize build"); let mut needs_compile_type = CompileType::Incremental; @@ -237,8 +236,15 @@ async fn async_watch( } CompileType::Full => { let timing_total = Instant::now(); - build_state = build::initialize_build(None, filter, show_progress, path, None) - .expect("Can't initialize build"); + build_state = build::initialize_build( + None, + filter, + show_progress, + path, + bsc_path.clone(), + build_dev_deps, + ) + .expect("Can't initialize build"); let _ = build::incremental_build( &mut build_state, None, From 80a14e485ad58124eb0995adc2aede659cf8110c Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Thu, 8 May 2025 22:42:23 +0200 Subject: [PATCH 13/14] fix clean error --- src/build/clean.rs | 4 ++-- src/build/packages.rs | 10 ++++++---- src/main.rs | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/build/clean.rs b/src/build/clean.rs index 24b3d2c..ba53e4f 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -338,7 +338,7 @@ pub fn cleanup_after_build(build_state: &BuildState) { }); } -pub fn clean(path: &str, show_progress: bool, bsc_path: Option) -> Result<()> { +pub fn clean(path: &str, show_progress: bool, bsc_path: Option, build_dev_deps: bool) -> Result<()> { let project_root = helpers::get_abs_path(path); let workspace_root = helpers::get_workspace_root(&project_root); let packages = packages::make( @@ -347,7 +347,7 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option) -> Resul &workspace_root, show_progress, // Always clean dev dependencies - true, + build_dev_deps, )?; let root_config_name = packages::read_package_name(&project_root)?; let bsc_path = match bsc_path { diff --git a/src/build/packages.rs b/src/build/packages.rs index 600f37e..d85f93f 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -510,17 +510,19 @@ pub fn get_source_files( }; let path_dir = Path::new(&source.dir); - if (build_dev_deps && type_ == &Some("dev".to_string())) || type_ != &Some("dev".to_string()) { - match read_folders(filter, package_dir, path_dir, recurse) { + match (build_dev_deps, type_) { + (false, Some(type_)) if type_ == "dev" => (), + _ => match read_folders(filter, package_dir, path_dir, recurse) { Ok(files) => map.extend(files), + Err(_e) => log::error!( "Could not read folder: {:?}. Specified in dependency: {}, located {:?}...", path_dir.to_path_buf().into_os_string(), package_name, package_dir ), - } - } + }, + }; map } diff --git a/src/main.rs b/src/main.rs index ebfa29e..3ff4768 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,7 @@ fn main() -> Result<()> { std::process::exit(1) } lock::Lock::Aquired(_) => match command { - Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path), + Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path, args.dev), Command::Build => { match build::build( &filter, From 90d97729dedae60c116b23880826882acf62902d Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Thu, 8 May 2025 22:46:16 +0200 Subject: [PATCH 14/14] fix test --- tests/compile.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/compile.sh b/tests/compile.sh index 65d599b..dd39bf5 100755 --- a/tests/compile.sh +++ b/tests/compile.sh @@ -91,7 +91,7 @@ else exit 1 fi -rewatch clean &> /dev/null +rewatch clean --dev &> /dev/null file_count=$(find ./packages/with-dev-deps -name *.mjs | wc -l) if [ "$file_count" -eq 0 ]; then