Skip to content

Commit 1beeb8a

Browse files
committed
Resolve relative paths in ProcessImportSection function
1 parent 8d76059 commit 1beeb8a

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

internal/exec/stack_processor_utils.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,11 +1743,26 @@ func FindComponentDependenciesLegacy(
17431743
return unique, nil
17441744
}
17451745

1746+
// resolveRelativePath checks if a path is relative to the current directory and if so,
1747+
// resolves it relative to the current file's directory
1748+
func resolveRelativePath(path string, currentFilePath string) string {
1749+
// Get the first element of the path
1750+
firstElement := filepath.Clean(strings.Split(path, string(filepath.Separator))[0])
1751+
1752+
// Check if the path starts with "." or ".."
1753+
if firstElement == "." || firstElement == ".." {
1754+
baseDir := filepath.Dir(currentFilePath)
1755+
result := filepath.ToSlash(filepath.Clean(filepath.Join(baseDir, path)))
1756+
return result
1757+
}
1758+
return path
1759+
}
1760+
17461761
// ProcessImportSection processes the `import` section in stack manifests
17471762
// The `import` section can contain:
17481763
// 1. Project-relative paths (e.g. "mixins/region/us-east-2")
1749-
// 2. Paths relative to the current file (starting with "./" or "../")
1750-
// 3. StackImport structs containing either of the above path types
1764+
// 2. Paths relative to the current stack file (e.g. "./_defaults")
1765+
// 3. StackImport structs containing either of the above path types (e.g. "path: mixins/region/us-east-2")
17511766
func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.StackImport, error) {
17521767
stackImports, ok := stackMap[cfg.ImportSectionName]
17531768

@@ -1773,15 +1788,7 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
17731788
var importObj schema.StackImport
17741789
err := mapstructure.Decode(imp, &importObj)
17751790
if err == nil {
1776-
// Handle paths relative to current file (starting with ./ or ../)
1777-
if strings.HasPrefix(importObj.Path, "./") || strings.HasPrefix(importObj.Path, "../") {
1778-
// Get the directory of the current file
1779-
baseDir := filepath.Dir(filePath)
1780-
// Join the base directory with the relative path
1781-
importObj.Path = filepath.Join(baseDir, importObj.Path)
1782-
// Clean the path to resolve any .. segments
1783-
importObj.Path = filepath.Clean(importObj.Path)
1784-
}
1791+
importObj.Path = resolveRelativePath(importObj.Path, filePath)
17851792
result = append(result, importObj)
17861793
continue
17871794
}
@@ -1795,13 +1802,7 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
17951802
return nil, fmt.Errorf("invalid empty import in the file '%s'", filePath)
17961803
}
17971804

1798-
// Handle paths relative to current file (starting with ./ or ../)
1799-
if strings.HasPrefix(s, "./") || strings.HasPrefix(s, "../") {
1800-
baseDir := filepath.Dir(filePath)
1801-
s = filepath.Join(baseDir, s)
1802-
s = filepath.Clean(s)
1803-
}
1804-
1805+
s = resolveRelativePath(s, filePath)
18051806
result = append(result, schema.StackImport{Path: s})
18061807
}
18071808

0 commit comments

Comments
 (0)