From 0eb833d215832108298a012e60d60ab506483188 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Fri, 12 Jan 2024 14:25:27 +0400 Subject: [PATCH 1/6] Read non-hoisted deps --- src/build/packages.rs | 63 +++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/src/build/packages.rs b/src/build/packages.rs index 419390c..db3c07f 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -43,7 +43,7 @@ struct Dependency { bsconfig: bsconfig::T, path: String, is_pinned: bool, - dependencies: Vec + dependencies: Vec, } #[derive(Debug, Clone)] @@ -201,6 +201,7 @@ fn read_bsconfig(package_dir: &str) -> bsconfig::T { fn read_dependencies<'a>( registered_dependencies_set: &'a mut AHashSet, parent_bsconfig: bsconfig::T, + parent_path: String, ) -> Vec { return parent_bsconfig .bs_dependencies @@ -219,19 +220,25 @@ fn read_dependencies<'a>( // Read all bsconfig files in parallel instead of blocking .par_iter() .map(|package_name| { - let path = - match PathBuf::from(helpers::get_relative_package_path(package_name, false)).canonicalize() { - Ok(dir) => dir.to_string_lossy().to_string(), - Err(e) => { - print!( - "{} {} Error building package tree (are node_modules up-to-date?)... \n More details: {}", - style("[1/2]").bold().dim(), - CROSS, - e.to_string() - ); - std::process::exit(2) + let path_buf = + match PathBuf::from(format!("{}/node_modules/{}", parent_path, package_name)).canonicalize() { + Ok(p) => p, + Err(_) => { + match PathBuf::from(format!("node_modules/{}", package_name)).canonicalize() { + Ok(p) => p, + Err(e) => { + print!( + "{} {} Error building package tree (are node_modules up-to-date?)... \n More details: {}", + style("[1/2]").bold().dim(), + CROSS, + e.to_string() + ); + std::process::exit(2) + } + } } }; + let path = path_buf.to_string_lossy().to_string(); let bsconfig = read_bsconfig(&path); let is_pinned = parent_bsconfig @@ -240,7 +247,7 @@ fn read_dependencies<'a>( .map(|p| p.contains(&bsconfig.name)) .unwrap_or(false); - let dependencies = read_dependencies(&mut registered_dependencies_set.clone(),bsconfig.to_owned()); + let dependencies = read_dependencies(&mut registered_dependencies_set.clone(),bsconfig.to_owned(), path.to_owned()); Dependency { name: package_name.to_owned(), @@ -249,8 +256,7 @@ fn read_dependencies<'a>( is_pinned, dependencies, } - }) - .collect::>(); + }).collect::>(); } fn flatten_dependencies(dependencies: Vec) -> Vec { @@ -263,12 +269,7 @@ fn flatten_dependencies(dependencies: Vec) -> Vec { flattened } -fn make_package ( - bsconfig: bsconfig::T, - package_dir: &str, - is_pinned_dep: bool, - is_root: bool -) -> Package { +fn make_package(bsconfig: bsconfig::T, package_dir: &str, is_pinned_dep: bool, is_root: bool) -> Package { let source_folders = match bsconfig.sources.to_owned() { bsconfig::OneOrMore::Single(source) => get_source_dirs(source, None), bsconfig::OneOrMore::Multiple(sources) => { @@ -332,14 +333,24 @@ fn read_packages(project_root: &str) -> AHashMap { // Store all packages and completely deduplicate them let mut map: AHashMap = AHashMap::new(); - map.insert(root_bsconfig.name.to_owned(), make_package(root_bsconfig.to_owned(), project_root, false, true)); + map.insert( + root_bsconfig.name.to_owned(), + make_package(root_bsconfig.to_owned(), project_root, false, true), + ); let mut registered_dependencies_set: AHashSet = AHashSet::new(); - let dependencies = flatten_dependencies(read_dependencies(&mut registered_dependencies_set, root_bsconfig.to_owned())); + let dependencies = flatten_dependencies(read_dependencies( + &mut registered_dependencies_set, + root_bsconfig.to_owned(), + project_root.to_string(), + )); dependencies.iter().for_each(|d| { - if !map.contains_key(&d.name) { - map.insert(d.name.to_owned(), make_package(d.bsconfig.to_owned(), &d.path, d.is_pinned, false)); - } + if !map.contains_key(&d.name) { + map.insert( + d.name.to_owned(), + make_package(d.bsconfig.to_owned(), &d.path, d.is_pinned, false), + ); + } }); return map; From e170bcfc21133187c3efc9d65eef5a5c3a403ef9 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Fri, 12 Jan 2024 17:51:13 +0400 Subject: [PATCH 2/6] Remove unused helpers --- src/helpers.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 8eeebc2..6c1b817 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -43,13 +43,6 @@ impl LexicalAbsolute for Path { } } -pub fn get_relative_package_path(package_name: &str, is_root: bool) -> String { - match is_root { - true => "".to_string(), - false => format!("node_modules/{}", package_name), - } -} - pub fn get_build_path(root: &str, package_name: &str, is_root: bool) -> String { match is_root { true => format!("{}/lib/ocaml", root), @@ -64,13 +57,6 @@ pub fn get_bs_build_path(root: &str, package_name: &str, is_root: bool) -> Strin } } -pub fn get_path(root: &str, package_name: &str, file: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/{}", root, file), - false => format!("{}/node_modules/{}/{}", root, package_name, file), - } -} - pub fn get_node_modules_path(root: &str) -> String { format!("{}/node_modules", root) } From 268423107a9aaae6d45dc18166e776b37cc0697d Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Mon, 15 Jan 2024 14:08:49 +0400 Subject: [PATCH 3/6] Support getting bs build path for non-hoisted packages --- src/build/clean.rs | 41 +++++---------- src/build/compile.rs | 91 +++++++++------------------------ src/build/logs.rs | 29 +++++++---- src/build/packages.rs | 31 +++++------ src/build/parse.rs | 8 +-- src/build/read_compile_state.rs | 4 +- src/helpers.rs | 13 +---- 7 files changed, 78 insertions(+), 139 deletions(-) diff --git a/src/build/clean.rs b/src/build/clean.rs index 2c1df97..0e1ccac 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -20,10 +20,10 @@ fn remove_ast(source_file: &str, package_name: &str, root_path: &str, is_root: b )); } -fn remove_iast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) { +fn remove_iast(source_file: &str, package: &packages::Package, root_path: &str, is_root: bool) { let _ = std::fs::remove_file(helpers::get_compiler_asset( source_file, - package_name, + &package.name, &packages::Namespace::NoNamespace, root_path, "iast", @@ -41,7 +41,7 @@ fn remove_mjs_file(source_file: &str, suffix: &bsconfig::Suffix) { fn remove_compile_asset( source_file: &str, - package_name: &str, + package: &packages::Package, namespace: &packages::Namespace, root_path: &str, is_root: bool, @@ -49,7 +49,7 @@ fn remove_compile_asset( ) { let _ = std::fs::remove_file(helpers::get_compiler_asset( source_file, - package_name, + &package.name, namespace, root_path, extension, @@ -57,17 +57,15 @@ fn remove_compile_asset( )); let _ = std::fs::remove_file(helpers::get_bs_compiler_asset( source_file, - package_name, + package, namespace, - root_path, extension, - is_root, )); } pub fn remove_compile_assets( source_file: &str, - package_name: &str, + package: &packages::Package, namespace: &packages::Namespace, root_path: &str, is_root: bool, @@ -75,14 +73,7 @@ pub fn remove_compile_assets( // optimization // only issue cmti if htere is an interfacce file for extension in &["cmj", "cmi", "cmt", "cmti"] { - remove_compile_asset( - source_file, - package_name, - namespace, - root_path, - is_root, - extension, - ); + remove_compile_asset(source_file, package, namespace, root_path, is_root, extension); } } @@ -99,7 +90,7 @@ pub fn clean_mjs_files(build_state: &BuildState) { .get(&build_state.root_config_name) .expect("Could not find root package"); Some(( - std::path::PathBuf::from(package.package_dir.to_string()) + std::path::PathBuf::from(package.path.to_string()) .join(source_file.implementation.path.to_string()) .to_string_lossy() .to_string(), @@ -154,9 +145,10 @@ pub fn cleanup_previous_build( .ast_modules .get(&res_file_location.to_string()) .expect("Could not find module name for ast file"); + let package = build_state.get_package(package_name).unwrap(); remove_compile_assets( res_file_location, - package_name, + package, package_namespace, &build_state.project_root, *is_root, @@ -165,12 +157,7 @@ pub fn cleanup_previous_build( &res_file_location, &suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs), ); - remove_iast( - res_file_location, - package_name, - &build_state.project_root, - *is_root, - ); + remove_iast(res_file_location, package, &build_state.project_root, *is_root); remove_ast( res_file_location, package_name, @@ -356,7 +343,7 @@ pub fn cleanup_after_build(build_state: &BuildState) { SourceType::SourceFile(source_file) => { remove_iast( &source_file.implementation.path, - &module.package_name, + package, &build_state.project_root, package.is_root, ); @@ -381,7 +368,7 @@ pub fn cleanup_after_build(build_state: &BuildState) { // unecessary mark all the dependents as dirty, when there is no change in the interface remove_compile_asset( &source_file.implementation.path, - &module.package_name, + package, &package.namespace, &build_state.project_root, package.is_root, @@ -420,7 +407,7 @@ pub fn clean(path: &str) { let path = std::path::Path::new(&path_str); let _ = std::fs::remove_dir_all(path); - let path_str = helpers::get_bs_build_path(&project_root, &package.name, package.is_root); + let path_str = package.get_bs_build_path(); let path = std::path::Path::new(&path_str); let _ = std::fs::remove_dir_all(path); }); diff --git a/src/build/compile.rs b/src/build/compile.rs index 9cde3ec..1e04d00 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -287,23 +287,13 @@ pub fn compile( match result { Ok(Some(err)) => { source_file.implementation.compile_state = CompileState::Warning; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(&build_state.project_root, package.is_root, package, &err); compile_warnings.push_str(&err); } Ok(None) => (), Err(err) => { source_file.implementation.compile_state = CompileState::Error; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(&build_state.project_root, package.is_root, package, &err); compile_errors.push_str(&err); } }; @@ -311,24 +301,14 @@ pub fn compile( Some(Ok(Some(err))) => { source_file.interface.as_mut().unwrap().compile_state = CompileState::Warning; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(&build_state.project_root, package.is_root, package, &err); compile_warnings.push_str(&err); } Some(Ok(None)) => (), Some(Err(err)) => { source_file.interface.as_mut().unwrap().compile_state = CompileState::Error; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(&build_state.project_root, package.is_root, package, &err); compile_errors.push_str(&err); } _ => (), @@ -550,50 +530,34 @@ fn compile_file( if !is_interface { let _ = std::fs::copy( build_path_abs.to_string() + "/" + &module_name + ".cmi", - std::path::Path::new(&helpers::get_bs_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(dir) - // because editor tooling doesn't support namespace entries yet - // we just remove the @ for now. This makes sure the editor support - // doesn't break - .join(module_name.to_owned().replace("@", "") + ".cmi"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + // because editor tooling doesn't support namespace entries yet + // we just remove the @ for now. This makes sure the editor support + // doesn't break + .join(module_name.to_owned().replace("@", "") + ".cmi"), ); let _ = std::fs::copy( build_path_abs.to_string() + "/" + &module_name + ".cmj", - std::path::Path::new(&helpers::get_bs_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(dir) - .join(module_name.to_owned() + ".cmj"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + .join(module_name.to_owned() + ".cmj"), ); let _ = std::fs::copy( build_path_abs.to_string() + "/" + &module_name + ".cmt", - std::path::Path::new(&helpers::get_bs_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(dir) - // because editor tooling doesn't support namespace entries yet - // we just remove the @ for now. This makes sure the editor support - // doesn't break - .join(module_name.to_owned().replace("@", "") + ".cmt"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + // because editor tooling doesn't support namespace entries yet + // we just remove the @ for now. This makes sure the editor support + // doesn't break + .join(module_name.to_owned().replace("@", "") + ".cmt"), ); } else { let _ = std::fs::copy( build_path_abs.to_string() + "/" + &module_name + ".cmti", - std::path::Path::new(&helpers::get_bs_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(dir) - .join(module_name.to_owned() + ".cmti"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + .join(module_name.to_owned() + ".cmti"), ); } match &module.source_type { @@ -609,18 +573,13 @@ fn compile_file( // editor tools expects the source file in lib/bs for finding the current package // and in lib/ocaml when referencing modules in other packages let _ = std::fs::copy( - std::path::Path::new(&package.package_dir).join(path), - std::path::Path::new(&helpers::get_bs_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(path), + std::path::Path::new(&package.path).join(path), + std::path::Path::new(&package.get_bs_build_path()).join(path), ) .expect("copying source file failed"); let _ = std::fs::copy( - std::path::Path::new(&package.package_dir).join(path), + std::path::Path::new(&package.path).join(path), std::path::Path::new(&helpers::get_build_path( root_path, &package.name, diff --git a/src/build/logs.rs b/src/build/logs.rs index 2c71e98..5acac29 100644 --- a/src/build/logs.rs +++ b/src/build/logs.rs @@ -7,15 +7,22 @@ use regex::Regex; use std::fs::File; use std::io::prelude::*; +use super::packages; + enum Location { Bs, Ocaml, } -fn get_log_file_path(project_root: &str, subfolder: Location, name: &str, is_root: bool) -> String { +fn get_log_file_path( + project_root: &str, + subfolder: Location, + package: &packages::Package, + is_root: bool, +) -> String { let build_folder = match subfolder { - Location::Bs => helpers::get_bs_build_path(project_root, name, is_root), - Location::Ocaml => helpers::get_build_path(project_root, name, is_root), + Location::Bs => package.get_bs_build_path(), + Location::Ocaml => helpers::get_build_path(project_root, &package.name, is_root), }; build_folder.to_owned() + "/.compiler.log" @@ -49,7 +56,7 @@ pub fn initialize(project_root: &str, packages: &AHashMap) { let _ = File::create(get_log_file_path( project_root, Location::Bs, - name, + package, package.is_root, )) .map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time()))) @@ -57,14 +64,14 @@ pub fn initialize(project_root: &str, packages: &AHashMap) { }) } -pub fn append(project_root: &str, is_root: bool, name: &str, str: &str) { +pub fn append(project_root: &str, is_root: bool, package: &packages::Package, str: &str) { File::options() .append(true) - .open(get_log_file_path(project_root, Location::Bs, name, is_root)) - .map(|file| write_to_log_file(file, &name, str)) + .open(get_log_file_path(project_root, Location::Bs, package, is_root)) + .map(|file| write_to_log_file(file, &package.name, str)) .expect( &("Cannot write compilerlog: ".to_owned() - + &get_log_file_path(project_root, Location::Bs, name, is_root)), + + &get_log_file_path(project_root, Location::Bs, package, is_root)), ); } @@ -75,14 +82,14 @@ pub fn finalize(project_root: &str, packages: &AHashMap) { .open(get_log_file_path( project_root, Location::Bs, - name, + package, package.is_root, )) .map(|file| write_to_log_file(file, &name, &format!("#Done({})\n", helpers::get_system_time()))); let _ = std::fs::copy( - get_log_file_path(project_root, Location::Bs, name, package.is_root), - get_log_file_path(project_root, Location::Ocaml, name, package.is_root), + get_log_file_path(project_root, Location::Bs, package, package.is_root), + get_log_file_path(project_root, Location::Ocaml, package, package.is_root), ); }) } diff --git a/src/build/packages.rs b/src/build/packages.rs index db3c07f..bad3d17 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -56,12 +56,18 @@ pub struct Package { pub namespace: Namespace, pub modules: Option>, // canonicalized dir of the package - pub package_dir: String, + pub path: String, pub dirs: Option>, pub is_pinned_dep: bool, pub is_root: bool, } +impl Package { + pub fn get_bs_build_path(&self) -> String { + format!("{}/lib/bs", self.path) + } +} + impl PartialEq for Package { fn eq(&self, other: &Self) -> bool { self.name == other.name @@ -269,7 +275,7 @@ fn flatten_dependencies(dependencies: Vec) -> Vec { flattened } -fn make_package(bsconfig: bsconfig::T, package_dir: &str, is_pinned_dep: bool, is_root: bool) -> Package { +fn make_package(bsconfig: bsconfig::T, package_path: &str, is_pinned_dep: bool, is_root: bool) -> Package { let source_folders = match bsconfig.sources.to_owned() { bsconfig::OneOrMore::Single(source) => get_source_dirs(source, None), bsconfig::OneOrMore::Multiple(sources) => { @@ -321,7 +327,7 @@ fn make_package(bsconfig: bsconfig::T, package_dir: &str, is_pinned_dep: bool, i }, }, modules: None, - package_dir: package_dir.to_string(), + path: package_path.to_string(), dirs: None, is_pinned_dep: is_pinned_dep, is_root, @@ -417,7 +423,7 @@ fn extend_with_children( value .source_folders .par_iter() - .map(|source| get_source_files(Path::new(&value.package_dir), &filter, source)) + .map(|source| get_source_files(Path::new(&value.path), &filter, source)) .collect::>>() .into_iter() .for_each(|source| map.extend(source)); @@ -464,14 +470,7 @@ pub fn make(filter: &Option, root_folder: &str) -> AHashMap dirs.iter().for_each(|dir| { - let _ = std::fs::create_dir_all( - std::path::Path::new(&helpers::get_bs_build_path( - root_folder, - &package.name, - package.is_root, - )) - .join(dir), - ); + let _ = std::fs::create_dir_all(std::path::Path::new(&package.get_bs_build_path()).join(dir)); }), None => (), }); @@ -496,11 +495,7 @@ pub fn parse_packages(build_state: &mut BuildState) { } let build_path_abs = helpers::get_build_path(&build_state.project_root, &package.bsconfig.name, package.is_root); - let bs_build_path = helpers::get_bs_build_path( - &build_state.project_root, - &package.bsconfig.name, - package.is_root, - ); + let bs_build_path = package.get_bs_build_path(); helpers::create_build_path(&build_path_abs); helpers::create_build_path(&bs_build_path); @@ -877,7 +872,7 @@ mod test { source_files: None, namespace: Namespace::Namespace(String::from("Package1")), modules: None, - package_dir: String::from("./something"), + path: String::from("./something"), dirs: None, is_pinned_dep: false, is_root: false, diff --git a/src/build/parse.rs b/src/build/parse.rs index bd704d4..d4b7deb 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -137,7 +137,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(&build_state.project_root, package.is_root, package, &err); stderr.push_str(&err); } } @@ -149,7 +149,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(&build_state.project_root, package.is_root, package, &err); has_failure = true; stderr.push_str(&err); } @@ -168,7 +168,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(&build_state.project_root, package.is_root, package, &err); stderr.push_str(&err); } } @@ -184,7 +184,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(&build_state.project_root, package.is_root, package, &err); has_failure = true; stderr.push_str(&err); } diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index 6b13777..eaeb5fb 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -20,7 +20,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { let package = build_state.packages.get(&module.package_name).unwrap(); Some( - PathBuf::from(&package.package_dir) + PathBuf::from(&package.path) .canonicalize() .expect("Could not canonicalize") .join(source_file.implementation.path.to_owned()) @@ -39,7 +39,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { .filter_map(|module| { let package = build_state.packages.get(&module.package_name).unwrap(); module.get_interface().as_ref().map(|interface| { - PathBuf::from(&package.package_dir) + PathBuf::from(&package.path) .canonicalize() .expect("Could not canonicalize") .join(interface.path.to_owned()) diff --git a/src/helpers.rs b/src/helpers.rs index 6c1b817..f97d030 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -50,13 +50,6 @@ pub fn get_build_path(root: &str, package_name: &str, is_root: bool) -> String { } } -pub fn get_bs_build_path(root: &str, package_name: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/lib/bs", root), - false => format!("{}/node_modules/{}/lib/bs", root, package_name), - } -} - pub fn get_node_modules_path(root: &str) -> String { format!("{}/node_modules", root) } @@ -185,11 +178,9 @@ pub fn canonicalize_string_path(path: &str) -> Option { pub fn get_bs_compiler_asset( source_file: &str, - package_name: &str, + package: &packages::Package, namespace: &packages::Namespace, - root_path: &str, extension: &str, - is_root: bool, ) -> String { let namespace = match extension { "ast" | "iast" => &packages::Namespace::NoNamespace, @@ -198,7 +189,7 @@ pub fn get_bs_compiler_asset( let dir = std::path::Path::new(&source_file).parent().unwrap(); - std::path::Path::new(&get_bs_build_path(root_path, &package_name, is_root)) + std::path::Path::new(&package.get_bs_build_path()) .join(dir) .join(file_path_to_compiler_asset_basename(source_file, namespace) + extension) .to_str() From 5e3a11abf7d1fdd6aaf62ba62f28daaa69085134 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Mon, 15 Jan 2024 14:30:21 +0400 Subject: [PATCH 4/6] Support get_build_path for non-hoisted set up --- src/build.rs | 6 ++-- src/build/clean.rs | 59 ++++++++------------------------- src/build/compile.rs | 41 ++++++++--------------- src/build/deps.rs | 14 ++------ src/build/logs.rs | 44 ++++++++---------------- src/build/namespaces.rs | 5 ++- src/build/packages.rs | 10 +++--- src/build/parse.rs | 18 ++++------ src/build/read_compile_state.rs | 7 +--- src/helpers.rs | 43 ++++++------------------ 10 files changed, 70 insertions(+), 177 deletions(-) diff --git a/src/build.rs b/src/build.rs index b26ec0a..7252625 100644 --- a/src/build.rs +++ b/src/build.rs @@ -89,7 +89,7 @@ pub fn build(filter: &Option, path: &str, no_timing: bool) -> Resu let _ = stdout().flush(); let mut build_state = BuildState::new(project_root, root_config_name, packages); packages::parse_packages(&mut build_state); - logs::initialize(&build_state.project_root, &build_state.packages); + logs::initialize(&build_state.packages); let timing_source_files_elapsed = timing_source_files.elapsed(); println!( "{}\r{} {}Found source files in {:.2}s", @@ -150,7 +150,7 @@ pub fn build(filter: &Option, path: &str, no_timing: bool) -> Resu print!("{}", &err); } Err(err) => { - logs::finalize(&build_state.project_root, &build_state.packages); + logs::finalize(&build_state.packages); println!( "{}\r{} {}Error parsing source files in {:.2}s", LINE_CLEAR, @@ -195,7 +195,7 @@ pub fn build(filter: &Option, path: &str, no_timing: bool) -> Resu ); let compile_duration = start_compiling.elapsed(); - logs::finalize(&build_state.project_root, &build_state.packages); + logs::finalize(&build_state.packages); pb.finish(); clean::cleanup_after_build(&build_state); if compile_errors.len() > 0 { diff --git a/src/build/clean.rs b/src/build/clean.rs index 0e1ccac..c2a9c47 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -9,25 +9,21 @@ use rayon::prelude::*; use std::io::Write; use std::time::Instant; -fn remove_ast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) { +fn remove_ast(source_file: &str, package: &packages::Package) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_name, &packages::Namespace::NoNamespace, - root_path, "ast", - is_root, )); } -fn remove_iast(source_file: &str, package: &packages::Package, root_path: &str, is_root: bool) { +fn remove_iast(source_file: &str, package: &packages::Package) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - &package.name, &packages::Namespace::NoNamespace, - root_path, "iast", - is_root, )); } @@ -43,17 +39,14 @@ fn remove_compile_asset( source_file: &str, package: &packages::Package, namespace: &packages::Namespace, - root_path: &str, - is_root: bool, + extension: &str, ) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - &package.name, namespace, - root_path, extension, - is_root, )); let _ = std::fs::remove_file(helpers::get_bs_compiler_asset( source_file, @@ -67,13 +60,11 @@ pub fn remove_compile_assets( source_file: &str, package: &packages::Package, namespace: &packages::Namespace, - root_path: &str, - is_root: bool, ) { // optimization // only issue cmti if htere is an interfacce file for extension in &["cmj", "cmi", "cmt", "cmti"] { - remove_compile_asset(source_file, package, namespace, root_path, is_root, extension); + remove_compile_asset(source_file, package, namespace, extension); } } @@ -138,7 +129,6 @@ pub fn cleanup_previous_build( package_name, namespace: package_namespace, ast_file_path, - is_root, suffix, .. } = compile_assets_state @@ -146,24 +136,13 @@ pub fn cleanup_previous_build( .get(&res_file_location.to_string()) .expect("Could not find module name for ast file"); let package = build_state.get_package(package_name).unwrap(); - remove_compile_assets( - res_file_location, - package, - package_namespace, - &build_state.project_root, - *is_root, - ); + remove_compile_assets(res_file_location, package, package_namespace); remove_mjs_file( &res_file_location, &suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs), ); - remove_iast(res_file_location, package, &build_state.project_root, *is_root); - remove_ast( - res_file_location, - package_name, - &build_state.project_root, - *is_root, - ); + remove_iast(res_file_location, package); + remove_ast(res_file_location, package); match helpers::get_extension(ast_file_path).as_str() { "iast" => Some(module_name.to_owned()), "ast" => None, @@ -341,18 +320,8 @@ pub fn cleanup_after_build(build_state: &BuildState) { if failed_to_parse(module) { match &module.source_type { SourceType::SourceFile(source_file) => { - remove_iast( - &source_file.implementation.path, - package, - &build_state.project_root, - package.is_root, - ); - remove_ast( - &source_file.implementation.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); + remove_iast(&source_file.implementation.path, package); + remove_ast(&source_file.implementation.path, package); } _ => (), } @@ -370,8 +339,6 @@ pub fn cleanup_after_build(build_state: &BuildState) { &source_file.implementation.path, package, &package.namespace, - &build_state.project_root, - package.is_root, "cmt", ); } @@ -403,7 +370,7 @@ pub fn clean(path: &str) { ); std::io::stdout().flush().unwrap(); - let path_str = helpers::get_build_path(&project_root, &package.name, package.is_root); + let path_str = package.get_build_path(); let path = std::path::Path::new(&path_str); let _ = std::fs::remove_dir_all(path); diff --git a/src/build/compile.rs b/src/build/compile.rs index 1e04d00..69ca9af 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -142,12 +142,10 @@ pub fn compile( } SourceType::SourceFile(source_file) => { let cmi_path = helpers::get_compiler_asset( + package, &source_file.implementation.path, - &module.package_name, &package.namespace, - &build_state.project_root, "cmi", - package.is_root, ); let cmi_digest = helpers::compute_file_hash(&cmi_path); @@ -164,12 +162,7 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_iast_path( - &path, - &package.name, - &build_state.project_root, - package.is_root, - ), + &helpers::get_iast_path(package, &path), module, &build_state.project_root, &rescript_version, @@ -182,12 +175,7 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_ast_path( - &source_file.implementation.path, - &package.name, - &build_state.project_root, - package.is_root, - ), + &helpers::get_ast_path(package, &source_file.implementation.path), module, &build_state.project_root, &rescript_version, @@ -287,13 +275,13 @@ pub fn compile( match result { Ok(Some(err)) => { source_file.implementation.compile_state = CompileState::Warning; - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); compile_warnings.push_str(&err); } Ok(None) => (), Err(err) => { source_file.implementation.compile_state = CompileState::Error; - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); compile_errors.push_str(&err); } }; @@ -301,14 +289,14 @@ pub fn compile( Some(Ok(Some(err))) => { source_file.interface.as_mut().unwrap().compile_state = CompileState::Warning; - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); compile_warnings.push_str(&err); } Some(Ok(None)) => (), Some(Err(err)) => { source_file.interface.as_mut().unwrap().compile_state = CompileState::Error; - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); compile_errors.push_str(&err); } _ => (), @@ -355,7 +343,7 @@ fn compile_file( version: &str, is_interface: bool, ) -> Result, String> { - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags); let normal_deps = package @@ -378,10 +366,11 @@ fn compile_file( .concat() .into_iter() .map(|x| { + // TODO: Add support for non-hoisted set up + let hoisted_path = format!("{}/node_modules/{}/lib/ocaml", root_path, &x); vec![ "-I".to_string(), - helpers::canonicalize_string_path(&helpers::get_build_path(root_path, &x, package.is_root)) - .unwrap(), + helpers::canonicalize_string_path(&hoisted_path).unwrap(), ] }) .collect::>>(); @@ -580,12 +569,8 @@ fn compile_file( let _ = std::fs::copy( std::path::Path::new(&package.path).join(path), - std::path::Path::new(&helpers::get_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(std::path::Path::new(path).file_name().unwrap()), + std::path::Path::new(&package.get_build_path()) + .join(std::path::Path::new(path).file_name().unwrap()), ) .expect("copying source file failed"); } diff --git a/src/build/deps.rs b/src/build/deps.rs index 1be9be8..4bc0028 100644 --- a/src/build/deps.rs +++ b/src/build/deps.rs @@ -78,12 +78,7 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet let package = build_state .get_package(&module.package_name) .expect("Package not found"); - let ast_path = helpers::get_ast_path( - &source_file.implementation.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); + let ast_path = helpers::get_ast_path(package, &source_file.implementation.path); let mut deps = get_dep_modules( &ast_path, @@ -94,12 +89,7 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet match &source_file.interface { Some(interface) => { - let iast_path = helpers::get_iast_path( - &interface.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); + let iast_path = helpers::get_iast_path(package, &interface.path); deps.extend(get_dep_modules( &iast_path, diff --git a/src/build/logs.rs b/src/build/logs.rs index 5acac29..d20cf46 100644 --- a/src/build/logs.rs +++ b/src/build/logs.rs @@ -14,15 +14,10 @@ enum Location { Ocaml, } -fn get_log_file_path( - project_root: &str, - subfolder: Location, - package: &packages::Package, - is_root: bool, -) -> String { +fn get_log_file_path(subfolder: Location, package: &packages::Package) -> String { let build_folder = match subfolder { Location::Bs => package.get_bs_build_path(), - Location::Ocaml => helpers::get_build_path(project_root, &package.name, is_root), + Location::Ocaml => package.get_build_path(), }; build_folder.to_owned() + "/.compiler.log" @@ -51,45 +46,32 @@ fn write_to_log_file(mut file: File, package_name: &str, content: &str) { } } -pub fn initialize(project_root: &str, packages: &AHashMap) { +pub fn initialize(packages: &AHashMap) { packages.par_iter().for_each(|(name, package)| { - let _ = File::create(get_log_file_path( - project_root, - Location::Bs, - package, - package.is_root, - )) - .map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time()))) - .expect(&("Cannot create compiler log for package ".to_owned() + name)); + let _ = File::create(get_log_file_path(Location::Bs, package)) + .map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time()))) + .expect(&("Cannot create compiler log for package ".to_owned() + name)); }) } -pub fn append(project_root: &str, is_root: bool, package: &packages::Package, str: &str) { +pub fn append(package: &packages::Package, str: &str) { File::options() .append(true) - .open(get_log_file_path(project_root, Location::Bs, package, is_root)) + .open(get_log_file_path(Location::Bs, package)) .map(|file| write_to_log_file(file, &package.name, str)) - .expect( - &("Cannot write compilerlog: ".to_owned() - + &get_log_file_path(project_root, Location::Bs, package, is_root)), - ); + .expect(&("Cannot write compilerlog: ".to_owned() + &get_log_file_path(Location::Bs, package))); } -pub fn finalize(project_root: &str, packages: &AHashMap) { +pub fn finalize(packages: &AHashMap) { packages.par_iter().for_each(|(name, package)| { let _ = File::options() .append(true) - .open(get_log_file_path( - project_root, - Location::Bs, - package, - package.is_root, - )) + .open(get_log_file_path(Location::Bs, package)) .map(|file| write_to_log_file(file, &name, &format!("#Done({})\n", helpers::get_system_time()))); let _ = std::fs::copy( - get_log_file_path(project_root, Location::Bs, package, package.is_root), - get_log_file_path(project_root, Location::Ocaml, package, package.is_root), + get_log_file_path(Location::Bs, package), + get_log_file_path(Location::Ocaml, package), ); }) } diff --git a/src/build/namespaces.rs b/src/build/namespaces.rs index b72b3d7..10a0fd7 100644 --- a/src/build/namespaces.rs +++ b/src/build/namespaces.rs @@ -23,9 +23,8 @@ pub fn gen_mlmap( package: &packages::Package, namespace: &str, depending_modules: &AHashSet, - root_path: &str, ) -> String { - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); // we don't really need to create a digest, because we track if we need to // recompile in a different way but we need to put it in the file for it to // be readable. @@ -51,7 +50,7 @@ pub fn gen_mlmap( } pub fn compile_mlmap(package: &packages::Package, namespace: &str, root_path: &str) { - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); let mlmap_name = format!("{}.mlmap", namespace); let args = vec!["-w", "-49", "-color", "always", "-no-alias-deps", &mlmap_name]; diff --git a/src/build/packages.rs b/src/build/packages.rs index bad3d17..7f9a2e7 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -66,6 +66,10 @@ impl Package { pub fn get_bs_build_path(&self) -> String { format!("{}/lib/bs", self.path) } + + pub fn get_build_path(&self) -> String { + format!("{}/lib/ocaml", self.path) + } } impl PartialEq for Package { @@ -493,8 +497,7 @@ pub fn parse_packages(build_state: &mut BuildState) { Some(package_modules) => build_state.module_names.extend(package_modules), None => (), } - let build_path_abs = - helpers::get_build_path(&build_state.project_root, &package.bsconfig.name, package.is_root); + let build_path_abs = package.get_build_path(); let bs_build_path = package.get_bs_build_path(); helpers::create_build_path(&build_path_abs); helpers::create_build_path(&bs_build_path); @@ -526,8 +529,7 @@ pub fn parse_packages(build_state: &mut BuildState) { .filter(|module_name| helpers::is_non_exotic_module_name(module_name)) .collect::>(); - let mlmap = - namespaces::gen_mlmap(&package, namespace, &depending_modules, &build_state.project_root); + let mlmap = namespaces::gen_mlmap(&package, namespace, &depending_modules); // mlmap will be compiled in the AST generation step // compile_mlmap(&package, namespace, &project_root); diff --git a/src/build/parse.rs b/src/build/parse.rs index d4b7deb..9ff786d 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -32,22 +32,18 @@ pub fn generate_asts( // probably better to do this in a different function // specific to compiling mlmaps let path = helpers::get_mlmap_path( - &build_state.project_root, - &module.package_name, + package, &package .namespace .to_suffix() .expect("namespace should be set for mlmap module"), - package.is_root, ); let compile_path = helpers::get_mlmap_compile_path( - &build_state.project_root, - &module.package_name, + package, &package .namespace .to_suffix() .expect("namespace should be set for mlmap module"), - package.is_root, ); let mlmap_hash = helpers::compute_file_hash(&compile_path); namespaces::compile_mlmap(&package, module_name, &build_state.project_root); @@ -137,7 +133,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); stderr.push_str(&err); } } @@ -149,7 +145,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); has_failure = true; stderr.push_str(&err); } @@ -168,7 +164,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); stderr.push_str(&err); } } @@ -184,7 +180,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, package, &err); + logs::append(package, &err); has_failure = true; stderr.push_str(&err); } @@ -207,7 +203,7 @@ fn generate_ast( version: &str, ) -> Result<(String, Option), String> { let file = &filename.to_string(); - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); let path = PathBuf::from(filename); let ast_extension = path_to_ast_extension(&path); diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index eaeb5fb..0f4f8f2 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -52,12 +52,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { // scan all ast files in all packages for package in build_state.packages.values() { - let read_dir = fs::read_dir(std::path::Path::new(&helpers::get_build_path( - &build_state.project_root, - &package.name, - package.is_root, - ))) - .unwrap(); + let read_dir = fs::read_dir(std::path::Path::new(&package.get_build_path())).unwrap(); for entry in read_dir { match entry { diff --git a/src/helpers.rs b/src/helpers.rs index f97d030..3a7b896 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -43,13 +43,6 @@ impl LexicalAbsolute for Path { } } -pub fn get_build_path(root: &str, package_name: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/lib/ocaml", root), - false => format!("{}/node_modules/{}/lib/ocaml", root, package_name), - } -} - pub fn get_node_modules_path(root: &str) -> String { format!("{}/node_modules", root) } @@ -155,14 +148,12 @@ pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool { } pub fn get_compiler_asset( + package: &packages::Package, source_file: &str, - package_name: &str, namespace: &packages::Namespace, - root_path: &str, extension: &str, - is_root: bool, ) -> String { - get_build_path(root_path, package_name, is_root) + package.get_build_path() + "/" + &file_path_to_compiler_asset_basename(source_file, namespace) + "." @@ -207,34 +198,20 @@ pub fn is_interface_ast_file(file: &str) -> bool { file.ends_with(".iast") } -pub fn get_mlmap_path(root_path: &str, package_name: &str, namespace: &str, is_root: bool) -> String { - get_build_path(root_path, package_name, is_root) + "/" + namespace + ".mlmap" +pub fn get_mlmap_path(package: &packages::Package, namespace: &str) -> String { + package.get_build_path() + "/" + namespace + ".mlmap" } -pub fn get_mlmap_compile_path(root_path: &str, package_name: &str, namespace: &str, is_root: bool) -> String { - get_build_path(root_path, package_name, is_root) + "/" + namespace + ".cmi" +pub fn get_mlmap_compile_path(package: &packages::Package, namespace: &str) -> String { + package.get_build_path() + "/" + namespace + ".cmi" } -pub fn get_ast_path(source_file: &str, package_name: &str, root_path: &str, is_root: bool) -> String { - get_compiler_asset( - source_file, - package_name, - &packages::Namespace::NoNamespace, - root_path, - "ast", - is_root, - ) +pub fn get_ast_path(package: &packages::Package, source_file: &str) -> String { + get_compiler_asset(package, source_file, &packages::Namespace::NoNamespace, "ast") } -pub fn get_iast_path(source_file: &str, package_name: &str, root_path: &str, is_root: bool) -> String { - get_compiler_asset( - source_file, - package_name, - &packages::Namespace::NoNamespace, - root_path, - "iast", - is_root, - ) +pub fn get_iast_path(package: &packages::Package, source_file: &str) -> String { + get_compiler_asset(package, source_file, &packages::Namespace::NoNamespace, "iast") } pub fn read_lines(filename: String) -> io::Result>> { From fe8f04cbda64cc9a1e797a77b641cff5b36afd22 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Mon, 15 Jan 2024 14:42:26 +0400 Subject: [PATCH 5/6] Move some code from helper to the Package impl --- src/build/clean.rs | 1 - src/build/compile.rs | 4 ++-- src/build/deps.rs | 4 ++-- src/build/packages.rs | 28 ++++++++++++++++++++++++++++ src/build/parse.rs | 16 ++-------------- src/helpers.rs | 16 ---------------- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/build/clean.rs b/src/build/clean.rs index c2a9c47..8c5ca88 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -39,7 +39,6 @@ fn remove_compile_asset( source_file: &str, package: &packages::Package, namespace: &packages::Namespace, - extension: &str, ) { let _ = std::fs::remove_file(helpers::get_compiler_asset( diff --git a/src/build/compile.rs b/src/build/compile.rs index 69ca9af..86dafaf 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -162,7 +162,7 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_iast_path(package, &path), + &package.get_iast_path(&path), module, &build_state.project_root, &rescript_version, @@ -175,7 +175,7 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_ast_path(package, &source_file.implementation.path), + &package.get_ast_path(&source_file.implementation.path), module, &build_state.project_root, &rescript_version, diff --git a/src/build/deps.rs b/src/build/deps.rs index 4bc0028..39589c7 100644 --- a/src/build/deps.rs +++ b/src/build/deps.rs @@ -78,7 +78,7 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet let package = build_state .get_package(&module.package_name) .expect("Package not found"); - let ast_path = helpers::get_ast_path(package, &source_file.implementation.path); + let ast_path = package.get_ast_path(&source_file.implementation.path); let mut deps = get_dep_modules( &ast_path, @@ -89,7 +89,7 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet match &source_file.interface { Some(interface) => { - let iast_path = helpers::get_iast_path(package, &interface.path); + let iast_path = package.get_iast_path(&interface.path); deps.extend(get_dep_modules( &iast_path, diff --git a/src/build/packages.rs b/src/build/packages.rs index 7f9a2e7..589f71b 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -70,6 +70,34 @@ impl Package { pub fn get_build_path(&self) -> String { format!("{}/lib/ocaml", self.path) } + + pub fn get_mlmap_path(&self) -> String { + self.get_build_path() + + "/" + + &self + .namespace + .to_suffix() + .expect("namespace should be set for mlmap module") + + ".mlmap" + } + + pub fn get_mlmap_compile_path(&self) -> String { + self.get_build_path() + + "/" + + &self + .namespace + .to_suffix() + .expect("namespace should be set for mlmap module") + + ".cmi" + } + + pub fn get_ast_path(&self, source_file: &str) -> String { + helpers::get_compiler_asset(self, source_file, &packages::Namespace::NoNamespace, "ast") + } + + pub fn get_iast_path(&self, source_file: &str) -> String { + helpers::get_compiler_asset(self, source_file, &packages::Namespace::NoNamespace, "iast") + } } impl PartialEq for Package { diff --git a/src/build/parse.rs b/src/build/parse.rs index 9ff786d..dc54a4a 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -31,20 +31,8 @@ pub fn generate_asts( SourceType::MlMap(_) => { // probably better to do this in a different function // specific to compiling mlmaps - let path = helpers::get_mlmap_path( - package, - &package - .namespace - .to_suffix() - .expect("namespace should be set for mlmap module"), - ); - let compile_path = helpers::get_mlmap_compile_path( - package, - &package - .namespace - .to_suffix() - .expect("namespace should be set for mlmap module"), - ); + let path = package.get_mlmap_path(); + let compile_path = package.get_mlmap_compile_path(); let mlmap_hash = helpers::compute_file_hash(&compile_path); namespaces::compile_mlmap(&package, module_name, &build_state.project_root); let mlmap_hash_after = helpers::compute_file_hash(&compile_path); diff --git a/src/helpers.rs b/src/helpers.rs index 3a7b896..6c39436 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -198,22 +198,6 @@ pub fn is_interface_ast_file(file: &str) -> bool { file.ends_with(".iast") } -pub fn get_mlmap_path(package: &packages::Package, namespace: &str) -> String { - package.get_build_path() + "/" + namespace + ".mlmap" -} - -pub fn get_mlmap_compile_path(package: &packages::Package, namespace: &str) -> String { - package.get_build_path() + "/" + namespace + ".cmi" -} - -pub fn get_ast_path(package: &packages::Package, source_file: &str) -> String { - get_compiler_asset(package, source_file, &packages::Namespace::NoNamespace, "ast") -} - -pub fn get_iast_path(package: &packages::Package, source_file: &str) -> String { - get_compiler_asset(package, source_file, &packages::Namespace::NoNamespace, "iast") -} - pub fn read_lines(filename: String) -> io::Result>> { let file = fs::File::open(filename)?; Ok(io::BufReader::new(file).lines()) From a963b69caddbab3fa9bd8d4e146756905454748e Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Mon, 15 Jan 2024 14:48:31 +0400 Subject: [PATCH 6/6] Always resolve using absolute path --- src/build/packages.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/build/packages.rs b/src/build/packages.rs index 589f71b..43c3804 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -239,7 +239,8 @@ fn read_bsconfig(package_dir: &str) -> bsconfig::T { fn read_dependencies<'a>( registered_dependencies_set: &'a mut AHashSet, parent_bsconfig: bsconfig::T, - parent_path: String, + parent_path: &str, + project_root: &str, ) -> Vec { return parent_bsconfig .bs_dependencies @@ -261,15 +262,16 @@ fn read_dependencies<'a>( let path_buf = match PathBuf::from(format!("{}/node_modules/{}", parent_path, package_name)).canonicalize() { Ok(p) => p, - Err(_) => { - match PathBuf::from(format!("node_modules/{}", package_name)).canonicalize() { + Err(e1) => { + match PathBuf::from(format!("{}/node_modules/{}", project_root, package_name)).canonicalize() { Ok(p) => p, - Err(e) => { + Err(e2) => { print!( - "{} {} Error building package tree (are node_modules up-to-date?)... \n More details: {}", + "{} {} Error building package tree (are node_modules up-to-date?)... \n More details: {}\n{}", style("[1/2]").bold().dim(), CROSS, - e.to_string() + e1.to_string(), + e2.to_string() ); std::process::exit(2) } @@ -285,7 +287,7 @@ fn read_dependencies<'a>( .map(|p| p.contains(&bsconfig.name)) .unwrap_or(false); - let dependencies = read_dependencies(&mut registered_dependencies_set.clone(),bsconfig.to_owned(), path.to_owned()); + let dependencies = read_dependencies(&mut registered_dependencies_set.clone(),bsconfig.to_owned(), &path, &project_root); Dependency { name: package_name.to_owned(), @@ -380,7 +382,8 @@ fn read_packages(project_root: &str) -> AHashMap { let dependencies = flatten_dependencies(read_dependencies( &mut registered_dependencies_set, root_bsconfig.to_owned(), - project_root.to_string(), + project_root, + project_root, )); dependencies.iter().for_each(|d| { if !map.contains_key(&d.name) {