Skip to content

Commit ed623fe

Browse files
committed
chore: parser cleanup
1 parent 0e16b31 commit ed623fe

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

internal/discovery/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ func stackDependencyPaths(fs vfs.FS, depPaths []string, c component.Component) (
291291
expanded := make([]string, 0, len(depPaths))
292292

293293
for _, depPath := range depPaths {
294+
// Stat upfront so a non-directory dep path (e.g. another-name.hcl) is preserved instead of being passed to the parser, which would reject it as ENOTDIR. The duplication of work is intentional.
294295
info, statErr := fs.Stat(depPath)
295296
// Real I/O errors (permission denied, etc.) must surface so a malformed DAG isn't silently produced; only ENOENT is treated as "keep the raw path".
296297
if statErr != nil && !errors.Is(statErr, iofs.ErrNotExist) {
297298
return nil, NewStackDependencyExpansionError(depPath, statErr)
298299
}
299300

300-
// Dependency paths can also point at non-default-named config files (e.g. another-name.hcl), which the stack-file parser would otherwise reject as "not a directory".
301301
if statErr != nil || !info.IsDir() {
302302
expanded = append(expanded, depPath)
303303
continue

internal/hclparse/parse_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,46 @@ unit "vpc" {
878878
assert.Contains(t, err.Error(), "must not define nested includes")
879879
}
880880

881+
// Locks the per-file SourceBytes invariant: when a unit's autoinclude block lives in an included stack file, GenerateAutoIncludeFile must use the included file's bytes (not the root's) to slice expression text.
882+
func TestParseStackFile_AutoIncludeInsideIncludedFile(t *testing.T) {
883+
t.Parallel()
884+
885+
fs := vfs.NewMemMapFS()
886+
887+
rootSrc := `
888+
include "shared" {
889+
path = "shared.hcl"
890+
}
891+
`
892+
includedSrc := `
893+
unit "vpc" {
894+
source = "../catalog/units/vpc"
895+
path = "vpc"
896+
897+
autoinclude {
898+
dependency "db" {
899+
config_path = "../db"
900+
}
901+
}
902+
}
903+
`
904+
905+
require.NoError(t, fs.MkdirAll(testStackDir, 0755))
906+
require.NoError(t, vfs.WriteFile(fs, filepath.Join(testStackDir, "shared.hcl"), []byte(includedSrc), 0644))
907+
908+
result, err := hclparse.ParseStackFile(fs, &hclparse.ParseStackFileInput{Src: []byte(rootSrc), Filename: filepath.Join(testStackDir, "terragrunt.stack.hcl"), StackDir: testStackDir})
909+
require.NoError(t, err)
910+
require.Len(t, result.AutoIncludes, 1)
911+
912+
resolved, ok := result.AutoIncludes[hclparse.AutoIncludeKey("unit", "vpc")]
913+
require.True(t, ok, "expected resolved autoinclude for unit 'vpc'")
914+
require.NotNil(t, resolved)
915+
916+
// SourceBytes must point at the included file's bytes, not the root's, so the generator can slice expression byte ranges correctly.
917+
assert.Equal(t, []byte(includedSrc), resolved.SourceBytes, "SourceBytes must equal the included file's bytes")
918+
assert.NotEqual(t, []byte(rootSrc), resolved.SourceBytes, "SourceBytes must not be the root file's bytes")
919+
}
920+
881921
// Benchmarks
882922

883923
func BenchmarkParseStackFile_Simple(b *testing.B) {

internal/hclparse/stack_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func TestParseStackFileFromPath_StackDirIsFileReturnsError(t *testing.T) {
356356

357357
var readErr hclparse.FileReadError
358358
require.ErrorAs(t, err, &readErr)
359-
// ParseStackFileFromPath calls util.ResolvePath, which on macOS resolves /tmp -> /private/tmp; resolve our side too before comparing.
359+
// On macOS, t.TempDir() returns paths under /var/folders/... where /var is a symlink to /private/var; util.ResolvePath follows it, so resolve our side too before comparing.
360360
resolvedFilePath, evalErr := filepath.EvalSymlinks(filePath)
361361
require.NoError(t, evalErr)
362362
assert.Equal(t, filepath.Join(resolvedFilePath, "terragrunt.stack.hcl"), readErr.FilePath)

0 commit comments

Comments
 (0)