This is an automated email from the ASF dual-hosted git repository.

sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 12388a643a53785aaec047625d483eb0e82777a0
Author: Szymon Janc <[email protected]>
AuthorDate: Mon Oct 19 17:47:43 2026 +0200

    newt/config: Fix panic on relative import with no project loaded
    
    Relative $import paths were always resolved against the project
    base. newtrc.yml is read before a project is loaded, so a relative
    import in ~/.newt/newtrc.yml dereferenced a nil project and newt
    crashed at startup of every command.
    
    When no project is loaded, resolve relative imports against the
    directory of the importing file, and report an error for imports
    referring to a repository ("@repo/...").
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/config/config.go      | 20 ++++++++---
 newt/config/config_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/newt/config/config.go b/newt/config/config.go
index 1fe59ba2..99bb3d24 100644
--- a/newt/config/config.go
+++ b/newt/config/config.go
@@ -147,14 +147,24 @@ func readLineage(path string) ([]FileEntry, error) {
        // Recursively process imports, accumulating file info in `entries`.
        var iter func(path string, parent *util.FileInfo) error
        iter = func(path string, parent *util.FileInfo) error {
-               // Relative paths are relative to the project base.
+               // Relative paths are relative to the project base.  If no 
project
+               // is loaded (e.g., when reading newtrc), imports are relative 
to
+               // the importing file.
                if !filepath.IsAbs(path) {
                        proj := interfaces.GetProject()
-                       newPath, err := proj.ResolvePath(proj.Path(), path)
-                       if err != nil {
-                               return err
+                       if proj != nil {
+                               newPath, err := proj.ResolvePath(proj.Path(), 
path)
+                               if err != nil {
+                                       return err
+                               }
+                               path = newPath
+                       } else if parent != nil {
+                               if strings.HasPrefix(path, "@") {
+                                       return parent.ErrTree(util.FmtNewtError(
+                                               "cannot import %s: no project 
loaded", path))
+                               }
+                               path = filepath.Join(filepath.Dir(parent.Path), 
path)
                        }
-                       path = newPath
                }
 
                // Only operate on absolute paths to ensure each file has a 
unique ID.
diff --git a/newt/config/config_test.go b/newt/config/config_test.go
new file mode 100644
index 00000000..1642a338
--- /dev/null
+++ b/newt/config/config_test.go
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package config
+
+import (
+       "os"
+       "path/filepath"
+       "strings"
+       "testing"
+
+       "mynewt.apache.org/newt/newt/interfaces"
+)
+
+func configWriteFile(t *testing.T, path string, content string) {
+       t.Helper()
+       if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
+               t.Fatal(err)
+       }
+       if err := os.WriteFile(path, []byte(content), 0644); err != nil {
+               t.Fatal(err)
+       }
+}
+
+// Relative imports must not require a loaded project (e.g. newtrc is read
+// before the project).  They are resolved relative to the importing file.
+func TestReadFileImportNoProject(t *testing.T) {
+       interfaces.SetProject(nil)
+
+       dir := t.TempDir()
+       top := filepath.Join(dir, "newtrc.yml")
+       configWriteFile(t, top, "$import:\n    - sub/foo.yml\nkey1: top\n")
+       configWriteFile(t, filepath.Join(dir, "sub", "foo.yml"),
+               "$import:\n    - bar.yml\nkey2: foo\n")
+       configWriteFile(t, filepath.Join(dir, "sub", "bar.yml"), "key3: bar\n")
+
+       yc, err := ReadFile(top)
+       if err != nil {
+               t.Fatalf("unexpected error: %v", err)
+       }
+
+       for k, exp := range map[string]string{
+               "key1": "top",
+               "key2": "foo",
+               "key3": "bar",
+       } {
+               v, err := yc.GetValString(k, nil)
+               if err != nil {
+                       t.Fatalf("unexpected error reading %s: %v", k, err)
+               }
+               if v != exp {
+                       t.Errorf("%s: expected %q, got %q", k, exp, v)
+               }
+       }
+}
+
+func TestReadFileRepoImportNoProject(t *testing.T) {
+       interfaces.SetProject(nil)
+
+       dir := t.TempDir()
+       top := filepath.Join(dir, "newtrc.yml")
+       configWriteFile(t, top, "$import:\n    - \"@foo/bar.yml\"\n")
+
+       _, err := ReadFile(top)
+       if err == nil {
+               t.Fatalf("expected error")
+       }
+       if !strings.Contains(err.Error(), "no project") {
+               t.Fatalf("unexpected error: %v", err)
+       }
+}

Reply via email to