diff --git a/rust/kcl-lib/src/execution/exec_ast.rs b/rust/kcl-lib/src/execution/exec_ast.rs index 40ccfb992..67549bb61 100644 --- a/rust/kcl-lib/src/execution/exec_ast.rs +++ b/rust/kcl-lib/src/execution/exec_ast.rs @@ -156,44 +156,41 @@ impl ExecutorContext { exec_state: &mut ExecState, ) -> Result<(), KclError> { for statement in &program.body { - match statement { - BodyItem::ImportStatement(import_stmt) => { - let path_str = import_stmt.path.to_string(); + if let BodyItem::ImportStatement(import_stmt) = statement { + let path_str = import_stmt.path.to_string(); - if modules.contains_key(&path_str) { - // don't waste our time if we've already loaded the - // module. - continue; - } - - let source_range = SourceRange::from(import_stmt); - let attrs = &import_stmt.outer_attrs; - let module_id = self - .open_module(&import_stmt.path, attrs, exec_state, source_range) - .await?; - - let Some(module) = exec_state.get_module(module_id) else { - crate::log::log("we got back a module id that doesn't exist"); - unreachable!(); - }; - - let progn = { - // this dance is to avoid taking out a mut borrow - // below on exec_state after borrowing here. As a - // result, we need to clone (ugh) the program for - // now. - let ModuleRepr::Kcl(ref progn, _) = module.repr else { - // not a kcl file, we can skip this - continue; - }; - progn.clone() - }; - - modules.insert(path_str, progn.clone().inner); - - self.preload_all_modules(modules, &progn, exec_state).await?; + if modules.contains_key(&path_str) { + // don't waste our time if we've already loaded the + // module. + continue; } - _ => {} + + let source_range = SourceRange::from(import_stmt); + let attrs = &import_stmt.outer_attrs; + let module_id = self + .open_module(&import_stmt.path, attrs, exec_state, source_range) + .await?; + + let Some(module) = exec_state.get_module(module_id) else { + crate::log::log("we got back a module id that doesn't exist"); + unreachable!(); + }; + + let progn = { + // this dance is to avoid taking out a mut borrow + // below on exec_state after borrowing here. As a + // result, we need to clone (ugh) the program for + // now. + let ModuleRepr::Kcl(ref progn, _) = module.repr else { + // not a kcl file, we can skip this + continue; + }; + progn.clone() + }; + + modules.insert(path_str, progn.clone().inner); + + self.preload_all_modules(modules, &progn, exec_state).await?; }; } Ok(()) @@ -2571,8 +2568,6 @@ a = foo() #[tokio::test(flavor = "multi_thread")] async fn load_all_modules() { - let mut universe = HashMap::>::new(); - // program a.kcl let programa_kcl = r#" export a = 1 @@ -2598,7 +2593,7 @@ import c from 'c.kcl' d = b + c "#; - let main = crate::parsing::parse_str(&main_kcl, ModuleId::default()) + let main = crate::parsing::parse_str(main_kcl, ModuleId::default()) .parse_errs_as_err() .unwrap(); diff --git a/rust/kcl-lib/src/execution/mod.rs b/rust/kcl-lib/src/execution/mod.rs index 655b6556a..162a0ae73 100644 --- a/rust/kcl-lib/src/execution/mod.rs +++ b/rust/kcl-lib/src/execution/mod.rs @@ -735,12 +735,10 @@ impl ExecutorContext { for module in modules { let program = universe.get(&module).unwrap().clone(); - let module = module.clone(); - let mut exec_state = exec_state.clone(); + let exec_state = exec_state.clone(); let exec_ctxt = self.clone(); set.spawn(async move { - let module = module; let mut exec_state = exec_state; let exec_ctxt = exec_ctxt; let program = program; @@ -761,7 +759,7 @@ impl ExecutorContext { set.join_all().await; } - self.inner_run(&program, exec_state, false).await + self.inner_run(program, exec_state, false).await } /// Perform the execution of a program. Accept all possible parameters and