Skip to content

Commit

Permalink
canonicalization windows file path
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Sep 25, 2024
1 parent 1088e60 commit 1dede3e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
13 changes: 8 additions & 5 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::service::capi::*;
use crate::{call, gpyrpc::*};
use kclvm_utils::path::PathPrefix;
use once_cell::sync::Lazy;
use prost::Message;
use serde::de::DeserializeOwned;
Expand All @@ -9,7 +10,6 @@ use std::fs;
use std::os::raw::c_char;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

const TEST_DATA_PATH: &str = "./src/testdata";
static TEST_MUTEX: Lazy<Mutex<i32>> = Lazy::new(|| Mutex::new(0i32));

Expand Down Expand Up @@ -102,12 +102,15 @@ fn test_c_api_get_schema_type_mapping() {
"get-schema-type-mapping.response.json",
|r| {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let root = root.to_str().unwrap();
for (_, s_ty) in &mut r.schema_type_mapping {
let filename = match s_ty.filename.strip_prefix(root) {
Some(f) => f.to_string(),
None => s_ty.filename.clone(),
let filename = {
let filename = s_ty.filename.adjust_canonicalization();
match filename.strip_prefix(root.to_str().unwrap()) {
Some(f) => f.to_string(),
None => s_ty.filename.clone(),
}
};

s_ty.filename = filename.replace('.', "").replace('/', "").replace('\\', "")
}
},
Expand Down
1 change: 1 addition & 0 deletions kclvm/error/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use indexmap::IndexSet;
use kclvm_span::Loc;
use kclvm_utils::path::PathPrefix;
use std::fmt;
use std::hash::Hash;

Expand Down
4 changes: 3 additions & 1 deletion kclvm/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use kclvm_ast::token::{CommentKind, Token, TokenKind};
use kclvm_ast::token_stream::{Cursor, TokenStream};
use kclvm_error::ParseErrorMessage;
use kclvm_span::symbol::Symbol;
use kclvm_utils::path::PathPrefix;

/// The parser is built on top of the [`kclvm_parser::lexer`], and ordering KCL tokens
/// [`kclvm_ast::token`] to KCL ast nodes [`kclvm_ast::ast`].
Expand Down Expand Up @@ -86,7 +87,8 @@ impl<'a> Parser<'a> {
let filename = kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
));
))
.adjust_canonicalization();

(
filename,
Expand Down
14 changes: 4 additions & 10 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ fn test_resolve_program_cycle_reference_fail() {
"Module 'file1' imported but unused",
"Module 'file2' imported but unused",
];
for diag in &scope.handler.diagnostics {
println!("{:?}", diag.messages[0].message);
}
assert_eq!(scope.handler.diagnostics.len(), err_messages.len());
for (diag, msg) in scope.handler.diagnostics.iter().zip(err_messages.iter()) {
assert_eq!(diag.messages[0].message, msg.to_string(),);
Expand Down Expand Up @@ -534,7 +531,7 @@ fn test_pkg_scope() {
)
.unwrap()
.program;
println!("{:?}", program.pkgs.get("pkg").unwrap());

let scope = resolve_program(&mut program);

assert_eq!(scope.scope_map.len(), 2);
Expand Down Expand Up @@ -870,12 +867,9 @@ fn test_pkg_asname() {
.program;
let scope = resolve_program(&mut program);
let diags = scope.handler.diagnostics;
for diag in diags {
println!("{:?}", diag);
}
// assert_eq!(diags.len(), 4);
// assert_eq!(diags[0].messages[0].message, "name 'pkg' is not defined");
// assert_eq!(diags[1].messages[0].message, "name 'subpkg' is not defined");
assert_eq!(diags.len(), 4);
assert_eq!(diags[0].messages[0].message, "name 'pkg' is not defined");
assert_eq!(diags[1].messages[0].message, "name 'subpkg' is not defined");
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions kclvm/tools/src/LSP/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use kclvm_ast::ast::Program;
use kclvm_driver::{lookup_compile_workspace, toolchain};
use kclvm_error::Diagnostic;
use kclvm_parser::{
entry::get_normalized_k_files_from_paths, file_graph, load_program, parse_kcl_program,
ASTCache, KCLModuleCache, LoadProgramOptions, ParseSessionRef,
entry::get_normalized_k_files_from_paths, load_program, KCLModuleCache, LoadProgramOptions,
ParseSessionRef,
};
use kclvm_sema::{
advanced_resolver::AdvancedResolver,
Expand Down
3 changes: 3 additions & 0 deletions kclvm/utils/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ where
/// ```
fn adjust_canonicalization(&self) -> String {
const VERBATIM_PREFIX: &str = r#"\\?\"#;
const ESCAPE_VERBATIM_PREFIX: &str = r#"\\\\?\\"#;
let p = self.as_ref().display().to_string();
if p.starts_with(VERBATIM_PREFIX) {
p[VERBATIM_PREFIX.len()..].to_string()
} else if p.starts_with(ESCAPE_VERBATIM_PREFIX) {
p[ESCAPE_VERBATIM_PREFIX.len()..].to_string()
} else {
p
}
Expand Down

0 comments on commit 1dede3e

Please sign in to comment.