diff --git a/crates/moon/src/cli/run.rs b/crates/moon/src/cli/run.rs index 4824c34e5..b78ed86c4 100644 --- a/crates/moon/src/cli/run.rs +++ b/crates/moon/src/cli/run.rs @@ -219,6 +219,8 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res target_dir, } = cli.source_tgt_dir.try_into_package_dirs()?; + let mod_desc = moonutil::common::read_module_desc_file_in_dir(&source_dir)?; + // Run moon install before build let (resolved_env, dir_sync_result) = auto_sync( &source_dir, @@ -259,7 +261,7 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res bail!("{} is not a package", package_path); } - let pkg = moonutil::common::read_package_desc_file_in_dir(&package)?; + let pkg = moonutil::common::read_package_desc_file_in_dir(&mod_desc.name, &package)?; if !pkg.is_main { bail!("`{}` is not a main package", package_path); } diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index f1e4b7235..3d4ca6f69 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -122,12 +122,12 @@ pub fn read_module_from_json(path: &Path) -> Result anyhow::Result { +fn read_package_from_json(module_name: &str, path: &Path) -> anyhow::Result { let file = File::open(path)?; let reader = BufReader::new(file); let j = serde_json_lenient::from_reader(reader).context(format!("Failed to parse {:?}", path))?; - convert_pkg_json_to_package(j) + convert_pkg_json_to_package(module_name, j) } pub fn write_module_json_to_file(m: &MoonModJSON, source_dir: &Path) -> anyhow::Result<()> { @@ -152,11 +152,11 @@ pub fn read_module_desc_file_in_dir(dir: &Path) -> anyhow::Result { Ok(read_module_from_json(&dir.join(MOON_MOD_JSON))?) } -pub fn read_package_desc_file_in_dir(dir: &Path) -> anyhow::Result { +pub fn read_package_desc_file_in_dir(module_name: &str, dir: &Path) -> anyhow::Result { if !dir.join(MOON_PKG_JSON).exists() { bail!("`{:?}` does not exist", dir.join(MOON_PKG_JSON)); } - read_package_from_json(&dir.join(MOON_PKG_JSON)) + read_package_from_json(module_name, &dir.join(MOON_PKG_JSON)) .context(format!("Failed to load {:?}", dir.join(MOON_PKG_JSON))) } diff --git a/crates/moonutil/src/package.rs b/crates/moonutil/src/package.rs index 52c84e6cf..fe685c985 100644 --- a/crates/moonutil/src/package.rs +++ b/crates/moonutil/src/package.rs @@ -28,7 +28,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::{ - common::{FileName, GeneratedTestDriver}, + common::{FileName, GeneratedTestDriver, MOONBITLANG_CORE}, cond_expr::{CompileCondition, CondExpr, RawTargets}, path::{ImportComponent, PathComponent}, }; @@ -333,8 +333,11 @@ pub struct MoonPkg { pub is_main: bool, pub need_link: bool, pub imports: Vec, + pub core_imports: Vec, pub wbtest_imports: Vec, + pub core_wbtest_imports: Vec, pub test_imports: Vec, + pub core_test_imports: Vec, pub link: Option, pub warn_list: Option, @@ -361,7 +364,7 @@ impl Import { } } -pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result { +pub fn convert_pkg_json_to_package(module_name: &str, j: MoonPkgJSON) -> anyhow::Result { let get_imports = |source: Option| -> Vec { let mut imports = vec![]; if let Some(im) = source { @@ -444,6 +447,19 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result { alias_dedup.insert(alias.clone()); } } + let mut imports_with_core = vec![]; + let mut imports_without_core = vec![]; + for im in imports { + let path = match &im { + Import::Simple(p) => p, + Import::Alias { path, alias: _ } => path, + }; + if module_name != MOONBITLANG_CORE && path.starts_with(MOONBITLANG_CORE) { + imports_with_core.push(im); + } else { + imports_without_core.push(im); + } + } // TODO: check on the fly let mut alias_dedup: HashSet = HashSet::new(); @@ -466,6 +482,19 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result { alias_dedup.insert(alias.clone()); } } + let mut wbtest_imports_with_core = vec![]; + let mut wbtest_imports_without_core = vec![]; + for im in wbtest_imports { + let path = match &im { + Import::Simple(p) => p, + Import::Alias { path, alias: _ } => path, + }; + if module_name != MOONBITLANG_CORE && path.starts_with(MOONBITLANG_CORE) { + wbtest_imports_with_core.push(im); + } else { + wbtest_imports_without_core.push(im); + } + } // TODO: check on the fly let mut alias_dedup: HashSet = HashSet::new(); @@ -488,14 +517,30 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result { alias_dedup.insert(alias.clone()); } } + let mut test_imports_with_core = vec![]; + let mut test_imports_without_core = vec![]; + for im in test_imports { + let path = match &im { + Import::Simple(p) => p, + Import::Alias { path, alias: _ } => path, + }; + if module_name != MOONBITLANG_CORE && path.starts_with(MOONBITLANG_CORE) { + test_imports_with_core.push(im); + } else { + test_imports_without_core.push(im); + } + } let result = MoonPkg { name: None, is_main, need_link, - imports, - wbtest_imports, - test_imports, + imports: imports_without_core, + core_imports: imports_with_core, + wbtest_imports: wbtest_imports_without_core, + core_wbtest_imports: wbtest_imports_with_core, + test_imports: test_imports_without_core, + core_test_imports: test_imports_with_core, link: match j.link { None => None, Some(BoolOrLink::Bool(_)) => None, diff --git a/crates/moonutil/src/scan.rs b/crates/moonutil/src/scan.rs index a4dfc7356..6c91324f1 100644 --- a/crates/moonutil/src/scan.rs +++ b/crates/moonutil/src/scan.rs @@ -249,7 +249,7 @@ fn scan_one_package( Ok(imports) }; - let pkg = crate::common::read_package_desc_file_in_dir(pkg_path)?; + let pkg = crate::common::read_package_desc_file_in_dir(&mod_desc.name, pkg_path)?; let rel = pkg_path.strip_prefix(module_source_dir)?; let rel_path = PathComponent::from_path(rel)?;