Skip to content

Commit

Permalink
fix: path canonicalization for parse file API (#1708)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Oct 22, 2024
1 parent d6c8653 commit fc64537
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,7 +180,8 @@ impl KclvmServiceImpl {
/// assert_eq!(result.deps.len(), 2);
/// ```
pub fn parse_file(&self, args: &ParseFileArgs) -> anyhow::Result<ParseFileResult> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/parser/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion kclvm/parser/src/file_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Pkg {
pub type PkgMap = HashMap<PkgFile, Pkg>;

/// A graph of files, where each file depends on zero or more other files.
#[derive(Default)]
#[derive(Default, Debug)]
pub struct PkgFileGraph {
graph: StableDiGraph<PkgFile, ()>,
path_to_node_index: IndexMap<PkgFile, petgraph::graph::NodeIndex>,
Expand Down

0 comments on commit fc64537

Please sign in to comment.