diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index 387173d32..c3c45b85a 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -5,10 +5,8 @@ use std::string::String; use crate::gpyrpc::*; -use anyhow::anyhow; use kcl_language_server::rename; use kclvm_config::settings::build_settings_pathbuf; -use kclvm_driver::canonicalize_input_files; use kclvm_loader::option::list_options; use kclvm_loader::{load_packages_with_cache, LoadPackageOptions}; use kclvm_parser::load_program; @@ -860,16 +858,7 @@ impl KclvmServiceImpl { ) -> anyhow::Result { let settings_files = args.files.iter().map(|f| f.as_str()).collect::>(); let settings_pathbuf = build_settings_pathbuf(&[], Some(settings_files), None)?; - let files = if !settings_pathbuf.settings().input().is_empty() { - canonicalize_input_files( - &settings_pathbuf.settings().input(), - args.work_dir.clone(), - false, - ) - .map_err(|e| anyhow!(e))? - } else { - vec![] - }; + let files = settings_pathbuf.settings().input(); Ok(settings_pathbuf .settings() .clone() diff --git a/kclvm/driver/src/lib.rs b/kclvm/driver/src/lib.rs index 1fa2f08aa..2eaba78ee 100644 --- a/kclvm/driver/src/lib.rs +++ b/kclvm/driver/src/lib.rs @@ -11,9 +11,8 @@ use glob::glob; use kclvm_config::{ modfile::{ get_pkg_root, load_mod_file, KCL_FILE_EXTENSION, KCL_FILE_SUFFIX, KCL_MOD_FILE, - KCL_MOD_PATH_ENV, KCL_WORK_FILE, + KCL_WORK_FILE, }, - path::ModRelativePath, settings::{build_settings_pathbuf, DEFAULT_SETTING_FILE}, workfile::load_work_file, }; @@ -59,77 +58,6 @@ pub fn expand_input_files(k_files: &[String]) -> Vec { res } -/// Normalize input files with the working directory and replace ${KCL_MOD} with the module root path. -pub fn canonicalize_input_files( - k_files: &[String], - work_dir: String, - check_exist: bool, -) -> Result, String> { - let mut kcl_paths = Vec::::new(); - // The first traversal changes the relative path to an absolute path - for file in k_files.iter() { - let path = Path::new(file); - - let is_absolute = path.is_absolute(); - let is_exist_maybe_symlink = path.exists(); - // If the input file or path is a relative path and it is not a absolute path in the KCL module VFS, - // join with the work directory path and convert it to a absolute path. - let path = ModRelativePath::from(file.to_string()); - let abs_path = if !is_absolute && !path.is_relative_path().map_err(|err| err.to_string())? { - let filepath = Path::new(&work_dir).join(file); - match filepath.canonicalize() { - Ok(path) => Some(path.adjust_canonicalization()), - Err(_) => { - if check_exist { - return Err(format!( - "Cannot find the kcl file, please check the file path {}", - file - )); - } - Some(filepath.to_string_lossy().to_string()) - } - } - } else { - None - }; - // If the input file or path is a symlink, convert it to a real path. - let real_path = if is_exist_maybe_symlink { - match PathBuf::from(file.to_string()).canonicalize() { - Ok(real_path) => Some(String::from(real_path.to_str().unwrap())), - Err(_) => { - if check_exist { - return Err(format!( - "Cannot find the kcl file, please check the file path {}", - file - )); - } - Some(file.to_string()) - } - } - } else { - None - }; - - kcl_paths.push(abs_path.unwrap_or(real_path.unwrap_or(file.to_string()))); - } - - // Get the root path of the project - let pkgroot = kclvm_config::modfile::get_pkg_root_from_paths(&kcl_paths, work_dir)?; - - // The second traversal replaces ${KCL_MOD} with the project root path - kcl_paths = kcl_paths - .iter() - .map(|file| { - if file.contains(KCL_MOD_PATH_ENV) { - file.replace(KCL_MOD_PATH_ENV, pkgroot.as_str()) - } else { - file.clone() - } - }) - .collect(); - Ok(kcl_paths) -} - /// Get compile workspace(files and options) from a single file input. /// 1. Lookup entry files in kcl.yaml /// 2. Lookup entry files in kcl.mod diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 07844ad8b..632f7c5ee 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -9,24 +9,9 @@ use walkdir::WalkDir; use crate::arguments::parse_key_value_pair; use crate::toolchain::Toolchain; use crate::toolchain::{fill_pkg_maps_for_k_file, CommandToolchain, NativeToolchain}; -use crate::{canonicalize_input_files, expand_input_files, get_pkg_list}; +use crate::{expand_input_files, get_pkg_list}; use crate::{lookup_the_nearest_file_dir, toolchain}; -#[test] -fn test_canonicalize_input_files() { - let input_files = vec!["file1.k".to_string(), "file2.k".to_string()]; - let work_dir = ".".to_string(); - let expected_files = vec![ - Path::new(".").join("file1.k").to_string_lossy().to_string(), - Path::new(".").join("file2.k").to_string_lossy().to_string(), - ]; - assert_eq!( - canonicalize_input_files(&input_files, work_dir.clone(), false).unwrap(), - expected_files - ); - assert!(canonicalize_input_files(&input_files, work_dir, true).is_err()); -} - #[test] fn test_expand_input_files_with_kcl_mod() { let path = PathBuf::from("src/test_data/expand_file_pattern");