Skip to content

Commit b31c413

Browse files
committed
feat: optimize DependencyMapBuilder for several entry roots
1 parent 63cd092 commit b31c413

5 files changed

Lines changed: 309 additions & 258 deletions

File tree

src/ast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,6 @@ impl Program {
10591059
})
10601060
}
10611061

1062-
// Put this helper function inside your impl block or just above your logic
10631062
fn extract_single_main(items: &[Item]) -> Result<Option<Expression>, Error> {
10641063
let mut main_expr = None;
10651064

src/driver/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ struct CurrentModule {
397397
#[cfg(test)]
398398
pub(crate) mod tests {
399399
use super::*;
400-
use crate::resolution::tests::canon;
401-
use crate::resolution::DependencyMapBuilder;
400+
use crate::resolution::tests::{build_map, canon};
402401
use crate::test_utils::TempWorkspace;
403402

404403
/// Initializes a raw graph environment for testing, explicitly allowing for and capturing failure states.
@@ -437,11 +436,8 @@ pub(crate) mod tests {
437436
let lib_dir = canon(&ws.create_dir("workspace/libs/lib"));
438437

439438
// Set up the dependency map for imports (e.g. `use lib::...`)
440-
let map = DependencyMapBuilder::new(workspace_dir.clone())
441-
.add_dependency(workspace_dir.clone(), "lib".to_string(), lib_dir.clone())
442-
.build()
443-
.expect("Failed to create dependency map");
444-
let map = Arc::new(map);
439+
let map =
440+
Arc::new(build_map(&workspace_dir, &[(&workspace_dir, "lib", &lib_dir)]).unwrap());
445441

446442
let mut root_file_path = None;
447443
let mut root_content = String::new();

src/lib.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ pub trait ArbitraryOfType: Sized {
422422
pub(crate) mod tests {
423423
use crate::ast::ElementsJetHinter;
424424
use crate::parse::ParseFromStr;
425-
use crate::resolution::tests::canon;
425+
use crate::resolution::tests::{build_map, canon};
426426
use crate::resolution::DependencyMapBuilder;
427427
use crate::source::CanonPath;
428428
use crate::test_utils::TempWorkspace;
@@ -471,15 +471,15 @@ pub(crate) mod tests {
471471
{
472472
let parent = prog_path.as_ref().parent().unwrap();
473473
let canon_root = canon(parent);
474-
let mut builder = DependencyMapBuilder::new(canon_root);
474+
let mut builder = DependencyMapBuilder::new();
475475

476476
for (context, alias, target) in dependencies {
477477
let context = canon(context.as_ref());
478478
let target = canon(target.as_ref());
479479

480-
builder = builder.add_dependency(context, alias.into(), target);
480+
builder.add_dependency(context, alias.into(), target);
481481
}
482-
builder.build().unwrap()
482+
builder.build(canon_root).unwrap()
483483
}
484484

485485
pub(crate) struct TestCase<T> {
@@ -871,7 +871,7 @@ pub(crate) mod tests {
871871
let main_path = root.join(MAIN);
872872
let canon_root = CanonPath::canonicalize(&root).unwrap();
873873

874-
let dependency_map = DependencyMapBuilder::new(canon_root).build().unwrap();
874+
let dependency_map = build_map(&canon_root, &[]).unwrap();
875875

876876
TestCase::<TemplateProgram>::template_deps(&main_path, &dependency_map)
877877
.with_arguments(Arguments::default())
@@ -1304,19 +1304,15 @@ mod error_tests {
13041304
use super::*;
13051305

13061306
use crate::ast::ElementsJetHinter;
1307-
use crate::resolution::tests::canon;
1308-
use crate::resolution::DependencyMapBuilder;
1307+
use crate::resolution::tests::{build_map, canon};
13091308
use crate::source::CanonPath;
13101309
use crate::test_utils::TempWorkspace;
13111310

13121311
fn dependency_map(root_dir: &Path, drp: &str, lib_dir: &Path) -> DependencyMap {
13131312
let context = CanonPath::canonicalize(root_dir).unwrap();
13141313
let target = CanonPath::canonicalize(lib_dir).unwrap();
13151314

1316-
DependencyMapBuilder::new(context.clone())
1317-
.add_dependency(context, drp.into(), target)
1318-
.build()
1319-
.unwrap()
1315+
build_map(&context, &[(&context, drp, &target)]).unwrap()
13201316
}
13211317

13221318
fn source_file(path: &Path) -> CanonSourceFile {
@@ -1407,7 +1403,8 @@ mod error_tests {
14071403
#[cfg(test)]
14081404
mod functional_tests {
14091405
use crate::ast::ElementsJetHinter;
1410-
use crate::resolution::{DependencyMap, DependencyMapBuilder};
1406+
use crate::resolution::tests::build_map;
1407+
use crate::resolution::DependencyMap;
14111408
use crate::source::{CanonPath, CanonSourceFile};
14121409
use crate::tests::{flatten_multidep_test, run_dependency_test, run_multidep_test};
14131410
use crate::{Arguments, CompiledProgram};
@@ -1616,12 +1613,9 @@ mod functional_tests {
16161613
let lib_canon = CanonPath::canonicalize(&lib_dir).unwrap();
16171614

16181615
// 3. Set up the dependency maps
1619-
let dependency_map = DependencyMapBuilder::new(root_canon.clone())
1620-
.add_dependency(root_canon.clone(), "lib".to_string(), lib_canon)
1621-
.build()
1622-
.unwrap();
1616+
let dependency_map = build_map(&root_canon, &[(&root_canon, "lib", &lib_canon)]).unwrap();
16231617

1624-
let no_dependency_map = DependencyMapBuilder::new(root_canon).build().unwrap();
1618+
let no_dependency_map = build_map(&root_canon, &[]).unwrap();
16251619

16261620
// Compile both programs reading directly from the file system
16271621
let poisoned = compile_with_deps(&poisoned_main, &dependency_map);

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
135135
.and_then(|p| CanonPath::canonicalize(p).ok())
136136
.ok_or("Failed to determine project root directory from entry file")?;
137137

138-
let mut builder = DependencyMapBuilder::new(canon_root.clone());
138+
let mut builder = DependencyMapBuilder::new();
139139

140140
for arg in dep_args {
141141
let (left_side, path_str) = arg.split_once('=').unwrap_or_else(|| {
@@ -157,11 +157,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
157157
};
158158

159159
let target_path = CanonPath::canonicalize(Path::new(path_str))?;
160-
161-
builder = builder.add_dependency(context_path, alias.to_string(), target_path);
160+
builder.add_dependency(context_path, alias.to_string(), target_path);
162161
}
163162

164-
let dependencies = match builder.build() {
163+
let dependencies = match builder.build(canon_root.clone()) {
165164
Ok(map) => map,
166165
Err(e) => {
167166
eprintln!("Error: {e}");

0 commit comments

Comments
 (0)