Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: path canonicalization for parse file API #1708

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading