Repository: kaspar030/laze (language: python)

Issue:
bug: `${relpath}` from imported definitions sometimes empty in project root
... should be `.`


Discussion / hints:

Fix patch:
diff --git a/src/data.rs b/src/data.rs
index 3175c985..29be9923 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -447,6 +447,7 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
         defaults: Option<&Module>,
     ) -> Module {
         let relpath = filename.parent().unwrap();
+
         let name = match name {
             Some(name) => name.clone(),
             None => if let Some(import_root) = import_root {
@@ -456,7 +457,7 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
                     .strip_prefix(import_root.path())
                     .unwrap()
             } else {
-                relpath
+                &relpath
             }
             .to_string(),
         };
@@ -468,7 +469,11 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
 
         module.is_binary = is_binary;
         module.defined_in = Some(filename.to_path_buf());
-        module.relpath = Some(Utf8PathBuf::from(&relpath));
+        module.relpath = Some(if relpath.eq("") {
+            Utf8PathBuf::from(".")
+        } else {
+            Utf8PathBuf::from(&relpath)
+        });
 
         module
     }
@@ -482,8 +487,6 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
         defaults: Option<&Module>,
         build_dir: &Utf8Path,
     ) -> Result<Module, Error> {
-        let relpath = filename.parent().unwrap().to_string();
-
         let mut m = init_module(
             &module.name,
             context,
@@ -642,6 +645,8 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
             m.allowlist = module.allowlist.clone();
         }
 
+        let relpath = m.relpath.as_ref().unwrap().clone();
+
         m.download = module.download.clone();
         let srcdir = if let Some(download) = &m.download {
             let srcdir = download.srcdir(build_dir, &m);
@@ -656,7 +661,11 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
 
             srcdir
         } else {
-            Utf8PathBuf::from(relpath.clone())
+            if relpath != "." {
+                relpath.clone()
+            } else {
+                "".into()
+            }
         };
 
         m.build = module.build.clone();
@@ -675,7 +684,8 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
 
         // populate "early env"
         m.env_early
-            .insert("relpath".into(), EnvKey::Single(relpath.clone()));
+            .insert("relpath".into(), EnvKey::Single(relpath.to_string()));
+
         m.env_early.insert(
             "root".into(),
             EnvKey::Single(match import_root {
@@ -693,7 +703,7 @@ pub fn load(filename: &Utf8Path, build_dir: &Utf8Path) -> Result<(ContextBag, Fi
 
         if is_binary {
             m.env_global
-                .insert("appdir".into(), EnvKey::Single(relpath));
+                .insert("appdir".into(), EnvKey::Single(relpath.to_string()));
         }
 
         Ok(m)
diff --git a/src/main.rs b/src/main.rs
index 7c90af2a..26aaa508 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -203,6 +203,11 @@ fn try_main() -> Result<i32> {
 
     let (project_root, project_file) = determine_project_root(&cwd)?;
     let start_relpath = pathdiff::diff_utf8_paths(&cwd, &project_root).unwrap();
+    let start_relpath = if start_relpath.eq("") {
+        ".".into()
+    } else {
+        start_relpath
+    };
 
     println!(
         "laze: project root: {project_root} relpath: {start_relpath} project_file: {project_file}",


Test patch:
diff --git a/src/tests/40_import_root_cwd/build_expected/build-global.ninja b/src/tests/40_import_root_cwd/build_expected/build-global.ninja
new file mode 100644
index 00000000..ca7e0423
--- /dev/null
+++ b/src/tests/40_import_root_cwd/build_expected/build-global.ninja
@@ -0,0 +1,18 @@
+builddir = build
+build ALWAYS: phony
+rule CC_12906185565894242289
+  command = echo CC ${in} root=. relpath=. relroot=.. LOCAL_RELPATH=. > ${out}
+  description = CC
+
+build build/objects/foo.1350336948704932060.o: $
+    CC_12906185565894242289 $
+    foo.c
+
+rule LINK_11962182955076852389
+  command = echo LINK ${out} root= relpath=. relroot=.. > ${out} && cat ${in} >> ${out}
+  description = LINK
+
+build build/single_builder/app/app.elf: $
+    LINK_11962182955076852389 $
+    build/objects/foo.1350336948704932060.o
+
diff --git a/src/tests/40_import_root_cwd/build_expected/objects/foo.1350336948704932060.o b/src/tests/40_import_root_cwd/build_expected/objects/foo.1350336948704932060.o
new file mode 100644
index 00000000..be5158b1
--- /dev/null
+++ b/src/tests/40_import_root_cwd/build_expected/objects/foo.1350336948704932060.o
@@ -0,0 +1,1 @@
+CC foo.c root=. relpath=. relroot=.. LOCAL_RELPATH=.
diff --git a/src/tests/40_import_root_cwd/build_expected/single_builder/app/app.elf b/src/tests/40_import_root_cwd/build_expected/single_builder/app/app.elf
new file mode 100644
index 00000000..5e1d1521
--- /dev/null
+++ b/src/tests/40_import_root_cwd/build_expected/single_builder/app/app.elf
@@ -0,0 +1,2 @@
+LINK build/single_builder/app/app.elf root= relpath=. relroot=..
+CC foo.c root=. relpath=. relroot=.. LOCAL_RELPATH=.
diff --git a/src/tests/40_import_root_cwd/foo.c b/src/tests/40_import_root_cwd/foo.c
new file mode 100644
index 00000000..257cc564
--- /dev/null
+++ b/src/tests/40_import_root_cwd/foo.c
@@ -0,0 +1,1 @@
+foo
diff --git a/src/tests/40_import_root_cwd/imported.yml b/src/tests/40_import_root_cwd/imported.yml
new file mode 100644
index 00000000..6276cba4
--- /dev/null
+++ b/src/tests/40_import_root_cwd/imported.yml
@@ -0,0 +1,13 @@
+builders:
+  - name: single_builder
+    rules:
+        - name: CC
+          in: 'c'
+          out: 'o'
+          cmd: 'echo CC ${in} root=${root} relpath=${relpath} relroot=${relroot} LOCAL_RELPATH=${LOCAL_RELPATH} > ${out}'
+        - name: LINK
+          in: 'o'
+          cmd: echo LINK ${out} root=${root} relpath=${relpath} relroot=${relroot} > ${out} && cat ${in} >> ${out}
+
+    env:
+      bindir: build/${builder}/${app}
diff --git a/src/tests/40_import_root_cwd/laze-project.yml b/src/tests/40_import_root_cwd/laze-project.yml
new file mode 100644
index 00000000..bb3d4e8a
--- /dev/null
+++ b/src/tests/40_import_root_cwd/laze-project.yml
@@ -0,0 +1,12 @@
+imports:
+  - git:
+       url: ./testrepo
+       commit: HEAD
+
+apps:
+  - name: app
+    sources:
+      - foo.c
+    env:
+      local:
+        LOCAL_RELPATH: ${relpath}
diff --git a/src/tests/40_import_root_cwd/test.sh b/src/tests/40_import_root_cwd/test.sh
new file mode 100755
index 00000000..610a5c4f
--- /dev/null
+++ b/src/tests/40_import_root_cwd/test.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+rm -Rf testrepo
+git init testrepo
+cp imported.yml testrepo/laze.yml
+git -C testrepo add .
+git -C testrepo commit -m ...
+
+. ../test-common.sh
+
+cleanup
+
+${LAZE} build -g
+
+clean_temp_files
+rm -Rf testrepo
+rm -Rf build/imports
+
+diff -r build build_expected
+
+echo TEST_OK
+
+cleanup