From fc6453726066c6c1c768507f22f90f9f6a029973 Mon Sep 17 00:00:00 2001 From: Peefy Date: Tue, 22 Oct 2024 17:26:23 +0800 Subject: [PATCH] fix: path canonicalization for parse file API (#1708) Signed-off-by: peefy --- kclvm/api/src/service/service_impl.rs | 5 +++-- kclvm/parser/src/entry.rs | 2 +- kclvm/parser/src/file_graph.rs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index f523199fa..53e175400 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -9,7 +9,7 @@ use kcl_language_server::rename; use kclvm_config::settings::build_settings_pathbuf; use kclvm_loader::option::list_options; use kclvm_loader::{load_packages_with_cache, LoadPackageOptions}; -use kclvm_parser::entry::get_normalized_k_files_from_paths; +use kclvm_parser::entry::{canonicalize_input_file, get_normalized_k_files_from_paths}; use kclvm_parser::load_program; use kclvm_parser::parse_single_file; use kclvm_parser::KCLModuleCache; @@ -180,7 +180,8 @@ impl KclvmServiceImpl { /// assert_eq!(result.deps.len(), 2); /// ``` pub fn parse_file(&self, args: &ParseFileArgs) -> anyhow::Result { - let result = parse_single_file(&args.path, transform_str_para(&args.source))?; + let file = canonicalize_input_file(&args.path, ""); + let result = parse_single_file(&file, transform_str_para(&args.source))?; let ast_json = serde_json::to_string(&result.module)?; Ok(ParseFileResult { diff --git a/kclvm/parser/src/entry.rs b/kclvm/parser/src/entry.rs index 56d9ca927..1fa951624 100644 --- a/kclvm/parser/src/entry.rs +++ b/kclvm/parser/src/entry.rs @@ -527,7 +527,7 @@ fn is_ignored_file(filename: &str) -> bool { } /// Normalize the input file with the working directory and replace ${KCL_MOD} with the module root path. -fn canonicalize_input_file(file: &str, work_dir: &str) -> String { +pub fn canonicalize_input_file(file: &str, work_dir: &str) -> String { let path = std::path::Path::new(file); let is_absolute = path.is_absolute(); // If the input file or path is a relative path and it is not a absolute path in the KCL module VFS, diff --git a/kclvm/parser/src/file_graph.rs b/kclvm/parser/src/file_graph.rs index 9d146d8d7..b06aa8e73 100644 --- a/kclvm/parser/src/file_graph.rs +++ b/kclvm/parser/src/file_graph.rs @@ -29,7 +29,7 @@ pub struct Pkg { pub type PkgMap = HashMap; /// A graph of files, where each file depends on zero or more other files. -#[derive(Default)] +#[derive(Default, Debug)] pub struct PkgFileGraph { graph: StableDiGraph, path_to_node_index: IndexMap,