Skip to content

Commit

Permalink
remove fn canonicalize_input_files()
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Sep 12, 2024
1 parent 64bd05d commit 603979d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 94 deletions.
11 changes: 4 additions & 7 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ 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::entry::fix_path;
use kclvm_parser::load_program;
use kclvm_parser::parse_file;
use kclvm_parser::KCLModuleCache;
Expand Down Expand Up @@ -861,12 +861,9 @@ impl KclvmServiceImpl {
let settings_files = args.files.iter().map(|f| f.as_str()).collect::<Vec<&str>>();
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))?
let mut opts = LoadProgramOptions::default();
opts.work_dir = args.work_dir.clone();
fix_path(&settings_pathbuf.settings().input(), &opts).map_err(|e| anyhow!(e))?
} else {
vec![]
};
Expand Down
71 changes: 0 additions & 71 deletions kclvm/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,77 +59,6 @@ pub fn expand_input_files(k_files: &[String]) -> Vec<String> {
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<Vec<String>, String> {
let mut kcl_paths = Vec::<String>::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
Expand Down
17 changes: 1 addition & 16 deletions kclvm/driver/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 603979d

Please sign in to comment.