Skip to content

Commit

Permalink
fix: fix builtin 'modpath()' returns empty (kcl-lang#1824)
Browse files Browse the repository at this point in the history
* fix: fix builtin 'modpath()' returns empty

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: make fmt

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: canonicalize the path in windows

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: make fmt

Signed-off-by: zongz <zongzhe1024@163.com>

---------

Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe authored Jan 14, 2025
1 parent 9bd64f2 commit 829b262
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
15 changes: 7 additions & 8 deletions kclvm/parser/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,27 +355,26 @@ pub fn get_compile_entries_from_paths(
result.push_entry(entry);
}

let pkg_root = if result
let main_pkg_paths_count = result
.get_unique_normal_paths_by_name(kclvm_ast::MAIN_PKG)
.len()
== 1
&& opts.work_dir.is_empty()
{
.len();

let pkg_root = if main_pkg_paths_count == 1 {
// If the 'kcl.mod' can be found only once, the package root path will be the path of the 'kcl.mod'.
result
.get_unique_normal_paths_by_name(kclvm_ast::MAIN_PKG)
.get(0)
.unwrap()
.to_string()
} else if !opts.work_dir.is_empty() {
} else if main_pkg_paths_count > 1 && !opts.work_dir.is_empty() {
// If the 'kcl.mod' can be found more than once, the package root path will be the 'work_dir'.
if let Some(root_work_dir) = get_pkg_root(&opts.work_dir) {
root_work_dir
} else {
"".to_string()
opts.work_dir.to_string()
}
} else {
"".to_string()
opts.work_dir.to_string()
};
result.root_path = pkg_root.clone();
// Replace the '${KCL_MOD}' of all the paths with package name '__main__'.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import file

a = file.modpath()
24 changes: 24 additions & 0 deletions kclvm/runner/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use kclvm_parser::load_program;
use kclvm_parser::ParseSession;
#[cfg(feature = "llvm")]
use kclvm_sema::resolver::resolve_program;
use kclvm_utils::path::PathPrefix;
use serde_json::Value;
#[cfg(feature = "llvm")]
use std::fs::create_dir_all;
Expand Down Expand Up @@ -711,3 +712,26 @@ fn test_compile_with_symbolic_link() {
"{\"The_first_kcl_program\": \"Hello World!\", \"b\": 1}"
);
}

#[test]
fn test_kcl_issue_1799() {
let main_test_path = PathBuf::from("./src/test_issues/github.com/kcl-lang/kcl/1799/main.k");
let mut args = ExecProgramArgs::default();
args.k_filename_list
.push(main_test_path.display().to_string());
args.work_dir = Some(".".to_string());
let res = exec_program(Arc::new(ParseSession::default()), &args);
assert!(res.is_ok());
assert_eq!(
res.as_ref().unwrap().yaml_result,
format!(
"a: {}",
main_test_path
.parent()
.unwrap()
.canonicalize()
.unwrap()
.adjust_canonicalization()
)
);
}

0 comments on commit 829b262

Please sign in to comment.