@@ -1743,11 +1743,26 @@ func FindComponentDependenciesLegacy(
1743
1743
return unique , nil
1744
1744
}
1745
1745
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
+
1746
1761
// ProcessImportSection processes the `import` section in stack manifests
1747
1762
// The `import` section can contain:
1748
1763
// 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")
1751
1766
func ProcessImportSection (stackMap map [string ]any , filePath string ) ([]schema.StackImport , error ) {
1752
1767
stackImports , ok := stackMap [cfg .ImportSectionName ]
1753
1768
@@ -1773,15 +1788,7 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
1773
1788
var importObj schema.StackImport
1774
1789
err := mapstructure .Decode (imp , & importObj )
1775
1790
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 )
1785
1792
result = append (result , importObj )
1786
1793
continue
1787
1794
}
@@ -1795,13 +1802,7 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
1795
1802
return nil , fmt .Errorf ("invalid empty import in the file '%s'" , filePath )
1796
1803
}
1797
1804
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 )
1805
1806
result = append (result , schema.StackImport {Path : s })
1806
1807
}
1807
1808
0 commit comments