Skip to content

Commit

Permalink
remove windows file path prefix in ast node
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Oct 8, 2024
1 parent e3ab00d commit b4f6da3
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 35 deletions.
3 changes: 2 additions & 1 deletion kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -178,7 +179,7 @@ impl<T> Node<T> {
Self {
id: AstIndex::default(),
node,
filename,
filename: filename.adjust_canonicalization(),
line,
column,
end_line,
Expand All @@ -202,7 +203,8 @@ impl<T> Node<T> {
let filename = kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
));
))
.adjust_canonicalization();
Self {
id: AstIndex::default(),
node,
Expand Down
1 change: 1 addition & 0 deletions kclvm/cmd/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions kclvm/config/src/modfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ pub fn load_mod_lock_file<P: AsRef<Path>>(path: P) -> Result<ModLockFile> {
/// 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(),
}
}

Expand Down
6 changes: 5 additions & 1 deletion kclvm/config/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use kclvm_utils::path::PathPrefix;
use kclvm_version as version;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -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]
Expand Down
47 changes: 28 additions & 19 deletions kclvm/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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),
}
}

Expand Down
3 changes: 2 additions & 1 deletion kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down
24 changes: 19 additions & 5 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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()
Expand Down Expand Up @@ -292,7 +294,11 @@ pub fn test_import_vendor() {

let test_fn =
|test_case_name: &&str, pkgs: &Vec<&str>, module_cache: Option<KCLModuleCache>| {
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion kclvm/tools/src/LSP/src/from_lsp.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,7 +18,7 @@ pub(crate) fn abs_path(uri: &Url) -> anyhow::Result<AbsPathBuf> {
// 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),
}
Expand Down
5 changes: 2 additions & 3 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand All @@ -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"),
Expand Down

0 comments on commit b4f6da3

Please sign in to comment.