From b4f6da32990a2eefd46648cd3447f154e2ac2bc7 Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Tue, 8 Oct 2024 10:51:46 +0800 Subject: [PATCH] remove windows file path prefix in ast node Signed-off-by: he1pa <18012015693@163.com> --- kclvm/api/src/service/service_impl.rs | 3 +- kclvm/ast/src/ast.rs | 6 ++-- kclvm/cmd/src/tests.rs | 1 + kclvm/config/src/modfile.rs | 6 ++-- kclvm/config/src/tests.rs | 6 +++- kclvm/driver/src/lib.rs | 47 ++++++++++++++++----------- kclvm/parser/src/lib.rs | 3 +- kclvm/parser/src/tests.rs | 24 +++++++++++--- kclvm/tools/src/LSP/src/from_lsp.rs | 3 +- kclvm/tools/src/LSP/src/hover.rs | 5 ++- 10 files changed, 69 insertions(+), 35 deletions(-) diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index e06aedf70..88ed0de7b 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -203,11 +203,12 @@ impl KclvmServiceImpl { /// use kclvm_api::service::service_impl::KclvmServiceImpl; /// use kclvm_api::gpyrpc::*; /// use std::path::Path; + /// use kclvm_utils::path::PathPrefix; /// /// let serv = KclvmServiceImpl::default(); /// let args = &LoadPackageArgs { /// parse_args: Some(ParseProgramArgs { - /// paths: vec![Path::new(".").join("src").join("testdata").join("parse").join("main.k").canonicalize().unwrap().display().to_string()], + /// paths: vec![Path::new(".").join("src").join("testdata").join("parse").join("main.k").canonicalize().unwrap().display().to_string().adjust_canonicalization()], /// ..Default::default() /// }), /// resolve_ast: true, diff --git a/kclvm/ast/src/ast.rs b/kclvm/ast/src/ast.rs index 53eac4b08..581899afc 100644 --- a/kclvm/ast/src/ast.rs +++ b/kclvm/ast/src/ast.rs @@ -33,6 +33,7 @@ //! in the compiler and regenerate the walker code. //! :copyright: Copyright The KCL Authors. All rights reserved. +use kclvm_utils::path::PathPrefix; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use std::collections::HashMap; @@ -178,7 +179,7 @@ impl Node { Self { id: AstIndex::default(), node, - filename, + filename: filename.adjust_canonicalization(), line, column, end_line, @@ -202,7 +203,8 @@ impl Node { let filename = kclvm_utils::path::convert_windows_drive_letter(&format!( "{}", lo.file.name.prefer_remapped() - )); + )) + .adjust_canonicalization(); Self { id: AstIndex::default(), node, diff --git a/kclvm/cmd/src/tests.rs b/kclvm/cmd/src/tests.rs index 743812ad0..016b04876 100644 --- a/kclvm/cmd/src/tests.rs +++ b/kclvm/cmd/src/tests.rs @@ -172,6 +172,7 @@ fn test_external_cmd_invalid() { } #[test] +#[cfg(not(windows))] // All the unit test cases in [`test_run_command`] can not be executed concurrently. fn test_run_command() { test_run_command_with_import(); diff --git a/kclvm/config/src/modfile.rs b/kclvm/config/src/modfile.rs index 27c99dc3e..fa54ebafc 100644 --- a/kclvm/config/src/modfile.rs +++ b/kclvm/config/src/modfile.rs @@ -175,8 +175,10 @@ pub fn load_mod_lock_file>(path: P) -> Result { /// If the user root directory cannot be found, an empty string will be returned. pub fn get_vendor_home() -> String { match env::var(KCL_PKG_PATH) { - Ok(path) => path, - Err(_) => create_default_vendor_home().unwrap_or_default(), + Ok(path) => path.adjust_canonicalization(), + Err(_) => create_default_vendor_home() + .unwrap_or_default() + .adjust_canonicalization(), } } diff --git a/kclvm/config/src/tests.rs b/kclvm/config/src/tests.rs index 73f190a0e..cb5ef38fe 100644 --- a/kclvm/config/src/tests.rs +++ b/kclvm/config/src/tests.rs @@ -1,3 +1,4 @@ +use kclvm_utils::path::PathPrefix; use kclvm_version as version; use std::{ collections::HashMap, @@ -27,7 +28,10 @@ fn test_vendor_home() { .join("kpm") .canonicalize() .unwrap(); - assert_eq!(get_vendor_home(), kpm_home.display().to_string()) + assert_eq!( + get_vendor_home(), + kpm_home.display().to_string().adjust_canonicalization() + ) } #[test] diff --git a/kclvm/driver/src/lib.rs b/kclvm/driver/src/lib.rs index e46b10bcd..423685513 100644 --- a/kclvm/driver/src/lib.rs +++ b/kclvm/driver/src/lib.rs @@ -38,31 +38,40 @@ pub fn lookup_compile_workspace( file: &str, load_pkg: bool, ) -> CompileUnitOptions { - let mut default_res: CompileUnitOptions = (vec![], None, None); - let mut load_opt = kclvm_parser::LoadProgramOptions::default(); - let metadata = fill_pkg_maps_for_k_file(tool, file.into(), &mut load_opt).unwrap_or(None); - let path = Path::new(file); - if let Some(ext) = path.extension() { - if load_pkg { - if let Some(parent) = path.parent() { - if let Ok(files) = get_kcl_files(parent, false) { - default_res = (files, Some(load_opt), metadata); + fn default_res(tool: &dyn Toolchain, file: &str, load_pkg: bool) -> CompileUnitOptions { + let mut default_res: CompileUnitOptions = (vec![], None, None); + let mut load_opt = kclvm_parser::LoadProgramOptions::default(); + let metadata = fill_pkg_maps_for_k_file(tool, file.into(), &mut load_opt).unwrap_or(None); + let path = Path::new(file); + if let Some(ext) = path.extension() { + if load_pkg { + if let Some(parent) = path.parent() { + if let Ok(files) = get_kcl_files(parent, false) { + default_res = (files, Some(load_opt), metadata); + } + } + } else { + if ext == KCL_FILE_EXTENSION && path.is_file() { + default_res = (vec![file.to_string()], Some(load_opt), metadata); } - } - } else { - if ext == KCL_FILE_EXTENSION && path.is_file() { - default_res = (vec![file.to_string()], Some(load_opt), metadata); } } + default_res } + match lookup_compile_unit_path(file) { Ok(CompileUnitPath::SettingFile(dir)) => { let settings_files = lookup_setting_files(&dir); let files = if settings_files.is_empty() { - default_res.0.iter().map(|s| s.as_str()).collect() + default_res(tool, file, load_pkg) + .0 + .iter() + .map(|s| s.clone()) + .collect() } else { vec![] }; + let files: Vec<&str> = files.iter().map(|s| s.as_str()).collect(); let settings_files: Vec<&str> = settings_files.iter().map(|f| f.to_str().unwrap()).collect(); match build_settings_pathbuf(&files, Some(settings_files), None) { @@ -83,12 +92,12 @@ pub fn lookup_compile_workspace( let metadata = fill_pkg_maps_for_k_file(tool, file.into(), &mut load_opt).unwrap_or(None); if files.is_empty() { - default_res + default_res(tool, file, load_pkg) } else { (files, Some(load_opt), metadata) } } - Err(_) => default_res, + Err(_) => default_res(tool, file, load_pkg), } } Ok(CompileUnitPath::ModFile(dir)) => match load_mod_file(&dir) { @@ -101,12 +110,12 @@ pub fn lookup_compile_workspace( load_opt.work_dir = work_dir.clone(); (files, Some(load_opt), metadata) } else { - default_res + default_res(tool, file, load_pkg) } } - Err(_) => default_res, + Err(_) => default_res(tool, file, load_pkg), }, - Ok(CompileUnitPath::NotFound) | Err(_) => default_res, + Ok(CompileUnitPath::NotFound) | Err(_) => default_res(tool, file, load_pkg), } } diff --git a/kclvm/parser/src/lib.rs b/kclvm/parser/src/lib.rs index 86f6d5898..01b31bcbe 100644 --- a/kclvm/parser/src/lib.rs +++ b/kclvm/parser/src/lib.rs @@ -23,6 +23,7 @@ use kclvm_config::modfile::{get_vendor_home, KCL_FILE_EXTENSION, KCL_FILE_SUFFIX use kclvm_error::diagnostic::{Errors, Range}; use kclvm_error::{ErrorKind, Message, Position, Style}; use kclvm_sema::plugin::PLUGIN_MODULE_PREFIX; +use kclvm_utils::path::PathPrefix; use kclvm_utils::pkgpath::parse_external_pkg_name; use kclvm_utils::pkgpath::rm_external_pkg_name; @@ -191,7 +192,7 @@ pub fn parse_file_with_session( // Parser let mut p = parser::Parser::new(&sess, stream); let mut m = p.parse_module(); - m.filename = filename.to_string(); + m.filename = filename.to_string().adjust_canonicalization(); m.pkg = kclvm_ast::MAIN_PKG.to_string(); m.name = kclvm_ast::MAIN_PKG.to_string(); diff --git a/kclvm/parser/src/tests.rs b/kclvm/parser/src/tests.rs index 68e4e41e6..b797ed0e1 100644 --- a/kclvm/parser/src/tests.rs +++ b/kclvm/parser/src/tests.rs @@ -202,7 +202,8 @@ pub fn test_vendor_home() { .canonicalize() .unwrap() .display() - .to_string(); + .to_string() + .adjust_canonicalization(); env::set_var(KCL_PKG_PATH, vendor); assert_eq!(get_vendor_home(), vendor.to_string()); } @@ -215,7 +216,8 @@ fn set_vendor_home() -> String { .canonicalize() .unwrap() .display() - .to_string(); + .to_string() + .adjust_canonicalization(); env::set_var(KCL_PKG_PATH, vendor); debug_assert_eq!(get_vendor_home(), vendor.to_string()); vendor.to_string() @@ -292,7 +294,11 @@ pub fn test_import_vendor() { let test_fn = |test_case_name: &&str, pkgs: &Vec<&str>, module_cache: Option| { - let test_case_path = dir.join(test_case_name).display().to_string(); + let test_case_path = dir + .join(test_case_name) + .display() + .to_string() + .adjust_canonicalization(); let m = load_program(sess.clone(), &[&test_case_path], None, module_cache) .unwrap() .program; @@ -338,7 +344,11 @@ pub fn test_import_vendor_without_kclmod() { .unwrap(); test_cases.into_iter().for_each(|(test_case_name, pkgs)| { - let test_case_path = dir.join(test_case_name).display().to_string(); + let test_case_path = dir + .join(test_case_name) + .display() + .to_string() + .adjust_canonicalization(); let m = load_program(sess.clone(), &[&test_case_path], None, None) .unwrap() .program; @@ -574,7 +584,11 @@ fn test_import_vendor_by_external_arguments() { dep_name.to_string(), external_dir.join(dep_name).display().to_string(), ); - let test_case_path = dir.join(test_case_name).display().to_string(); + let test_case_path = dir + .join(test_case_name) + .display() + .to_string() + .adjust_canonicalization(); let m = load_program(sess.clone(), &[&test_case_path], None, module_cache) .unwrap() .program; diff --git a/kclvm/tools/src/LSP/src/from_lsp.rs b/kclvm/tools/src/LSP/src/from_lsp.rs index 8f129b0aa..5d1e8ecf0 100644 --- a/kclvm/tools/src/LSP/src/from_lsp.rs +++ b/kclvm/tools/src/LSP/src/from_lsp.rs @@ -1,6 +1,7 @@ use std::ops::Range; use kclvm_error::Position as KCLPos; +use kclvm_utils::path::PathPrefix; use lsp_types::{Position, Url}; use ra_ap_vfs::AbsPathBuf; @@ -17,7 +18,7 @@ pub(crate) fn abs_path(uri: &Url) -> anyhow::Result { // The position in lsp protocol is different with position in ast node whose line number is 1 based. pub(crate) fn kcl_pos(file: &str, pos: Position) -> KCLPos { KCLPos { - filename: kclvm_utils::path::convert_windows_drive_letter(file), + filename: kclvm_utils::path::convert_windows_drive_letter(file).adjust_canonicalization(), line: (pos.line + 1) as u64, column: Some(pos.character as u64), } diff --git a/kclvm/tools/src/LSP/src/hover.rs b/kclvm/tools/src/LSP/src/hover.rs index dc3bbe205..b94cc7bb2 100644 --- a/kclvm/tools/src/LSP/src/hover.rs +++ b/kclvm/tools/src/LSP/src/hover.rs @@ -259,7 +259,6 @@ mod tests { use super::hover; #[test] - #[bench_test] fn schema_doc_hover_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -286,12 +285,12 @@ mod tests { "schema Person:\n name: str\n age: int".to_string() ); } else { - unreachable!("Wrong type"); + unreachable!("test error"); } if let MarkedString::String(s) = vec[2].clone() { assert_eq!(s, "hover doc test"); } else { - unreachable!("Wrong type"); + unreachable!("test error"); } } _ => unreachable!("test error"),