diff --git a/kclvm/tools/src/LSP/src/tests.rs b/kclvm/tools/src/LSP/src/tests.rs index cb2912c9c..8adcd8018 100644 --- a/kclvm/tools/src/LSP/src/tests.rs +++ b/kclvm/tools/src/LSP/src/tests.rs @@ -898,6 +898,8 @@ fn compile_unit_cache_test() { let compile_unit_cache = KCLCompileUnitCache::default(); let start = Instant::now(); let _ = compile_unit_with_cache(&Some(Arc::clone(&compile_unit_cache)), &path.to_string()); + + assert!(compile_unit_cache.read().get(&path.to_string()).is_some()); let first_compile_time = start.elapsed(); let start = Instant::now(); diff --git a/kclvm/tools/src/LSP/src/util.rs b/kclvm/tools/src/LSP/src/util.rs index d7d997cc1..ba8901403 100644 --- a/kclvm/tools/src/LSP/src/util.rs +++ b/kclvm/tools/src/LSP/src/util.rs @@ -73,10 +73,14 @@ pub(crate) fn compile_unit_with_cache( ) -> (Vec, Option) { match compile_unit_cache { Some(cache) => { - let map = cache.read(); + let mut map = cache.write(); match map.get(file) { Some(compile_unit) => compile_unit.clone(), - None => lookup_compile_unit(file, true), + None => { + let res = lookup_compile_unit(file, true); + map.insert(file.to_string(), res.clone()); + res + } } } None => lookup_compile_unit(file, true), @@ -89,12 +93,6 @@ pub(crate) fn compile_with_params( // Lookup compile unit (kcl.mod or kcl.yaml) from the entry file. let (mut files, opt) = compile_unit_with_cache(¶ms.compile_unit_cache, ¶ms.file); - if let Some(cache) = params.compile_unit_cache { - cache - .write() - .insert(params.file.clone(), (files.clone(), opt.clone())); - } - if !files.contains(¶ms.file) { files.push(params.file.clone()); }