From 2a8765c1e993d4f56294c5122f4921afe7f96c21 Mon Sep 17 00:00:00 2001 From: Anthony Bullard Date: Tue, 24 Dec 2024 18:29:04 -0600 Subject: [PATCH 1/3] #7407: Register package dependent packages\n\nCloses #7407 --- crates/compiler/load_internal/src/file.rs | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/compiler/load_internal/src/file.rs b/crates/compiler/load_internal/src/file.rs index 0531048b20..04d56ff7d5 100644 --- a/crates/compiler/load_internal/src/file.rs +++ b/crates/compiler/load_internal/src/file.rs @@ -1342,7 +1342,31 @@ fn handle_root_type<'a>( Ok((header_output, RootType::Module { main_path })) } - App { .. } | Package { .. } | Platform { .. } => Ok((header_output, RootType::Main)), + Package { .. } => { + let main_path = opt_main_path.or_else(|| find_main_roc_recursively(src_dir)); + + let cache_dir = roc_cache_dir.as_persistent_path(); + + if let (Some(main_path), Some(cache_dir)) = (main_path.clone(), cache_dir) { + let mut messages = Vec::with_capacity(4); + messages.push(header_output.msg); + load_packages_from_main( + arena, + src_dir.clone(), + main_path, + &mut messages, + Arc::clone(&arc_modules), + Arc::clone(&ident_ids_by_module), + Arc::clone(&arc_shorthands), + cache_dir, + )?; + + header_output.msg = Msg::Many(messages); + } + + Ok((header_output, RootType::Main)) + } + App { .. } | Platform { .. } => Ok((header_output, RootType::Main)), } } else { Ok((header_output, RootType::Main)) From 8a726d23ed6b355e3ea8e69c0e98784dbe0505f3 Mon Sep 17 00:00:00 2001 From: Anthony Bullard Date: Thu, 26 Dec 2024 10:03:46 -0600 Subject: [PATCH 2/3] DRY up code --- crates/compiler/load_internal/src/file.rs | 37 ++++++++--------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/crates/compiler/load_internal/src/file.rs b/crates/compiler/load_internal/src/file.rs index 04d56ff7d5..51706d12b5 100644 --- a/crates/compiler/load_internal/src/file.rs +++ b/crates/compiler/load_internal/src/file.rs @@ -1317,8 +1317,13 @@ fn handle_root_type<'a>( use HeaderType::*; + let use_main = match header_type { + Package { .. } | App { .. } | Platform { .. } => true, + Module { .. } | Builtin { .. } | Hosted { .. } => false, + }; + match header_type { - Module { .. } | Builtin { .. } | Hosted { .. } => { + Module { .. } | Builtin { .. } | Hosted { .. } | Package { .. } => { let main_path = opt_main_path.or_else(|| find_main_roc_recursively(src_dir)); let cache_dir = roc_cache_dir.as_persistent_path(); @@ -1340,31 +1345,13 @@ fn handle_root_type<'a>( header_output.msg = Msg::Many(messages); } - Ok((header_output, RootType::Module { main_path })) - } - Package { .. } => { - let main_path = opt_main_path.or_else(|| find_main_roc_recursively(src_dir)); + let root_type = if use_main { + RootType::Main + } else { + RootType::Module { main_path } + }; - let cache_dir = roc_cache_dir.as_persistent_path(); - - if let (Some(main_path), Some(cache_dir)) = (main_path.clone(), cache_dir) { - let mut messages = Vec::with_capacity(4); - messages.push(header_output.msg); - load_packages_from_main( - arena, - src_dir.clone(), - main_path, - &mut messages, - Arc::clone(&arc_modules), - Arc::clone(&ident_ids_by_module), - Arc::clone(&arc_shorthands), - cache_dir, - )?; - - header_output.msg = Msg::Many(messages); - } - - Ok((header_output, RootType::Main)) + Ok((header_output, root_type)) } App { .. } | Platform { .. } => Ok((header_output, RootType::Main)), } From d0ec7e2762ab503c0f4d877b7c67092829406a46 Mon Sep 17 00:00:00 2001 From: Anthony Bullard Date: Thu, 26 Dec 2024 10:32:39 -0600 Subject: [PATCH 3/3] Add unit test --- .../compiler/load_internal/tests/test_load.rs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/crates/compiler/load_internal/tests/test_load.rs b/crates/compiler/load_internal/tests/test_load.rs index a514d0466d..d623f0854f 100644 --- a/crates/compiler/load_internal/tests/test_load.rs +++ b/crates/compiler/load_internal/tests/test_load.rs @@ -2131,3 +2131,51 @@ fn roc_file_no_extension() { assert_eq!(err, expected, "\n{}", err); } + +#[test] +fn roc_package_depends_on_other_package() { + let modules = vec![ + ( + "main", + indoc!( + r#" + package [Module] { other: "other/main.roc" } + "# + ), + ), + ( + "Module.roc", + indoc!( + r#" + module [foo] + + import other.OtherMod + + foo = OtherMod.say "hello" + "# + ), + ), + ( + "other/main.roc", + indoc!( + r#" + package [OtherMod] {} + "# + ), + ), + ( + "other/OtherMod.roc", + indoc!( + r#" + module [say] + + say = \msg -> "$(msg), world!" + "# + ), + ), + ]; + + let result = multiple_modules("roc_package_depends_on_other_package", modules); + + assert!(result.is_ok()); +}