Skip to content

Commit 1d5531f

Browse files
committed
chore: panic updates
1 parent 7b593b8 commit 1d5531f

4 files changed

Lines changed: 25 additions & 14 deletions

File tree

internal/hclparse/autoinclude.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ func BuildAutoIncludeEvalContext(unitRefs, stackRefs []ComponentRef) *hcl.EvalCo
182182
// Returns (nil, nil) if the file does not exist or has no dependencies.
183183
func AutoIncludeDependencyPaths(fs vfs.FS, unitDir string) ([]string, error) {
184184
if fs == nil {
185-
panic(fmt.Sprintf("hclparse.AutoIncludeDependencyPaths: fs is nil; a vfs.FS is required to read the generated %s file and extract dependency paths for DAG construction (unitDir=%q)", AutoIncludeFile, unitDir))
185+
panic(fmt.Sprintf("hclparse.AutoIncludeDependencyPaths: fs is nil (unitDir=%q)", unitDir))
186186
}
187187

188188
if unitDir == "" {
189-
panic(fmt.Sprintf("hclparse.AutoIncludeDependencyPaths: unitDir is empty (got %q); unitDir must be the generated unit directory containing %s", unitDir, AutoIncludeFile))
189+
panic("hclparse.AutoIncludeDependencyPaths: unitDir is empty")
190190
}
191191

192192
unitDir = util.ResolvePath(unitDir)

internal/hclparse/generate.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ const (
3737
// source text for expressions via byte ranges.
3838
func GenerateAutoIncludeFile(fs vfs.FS, resolved *AutoIncludeResolved, targetDir string, srcBytes []byte, evalCtx *hcl.EvalContext) error {
3939
if fs == nil {
40-
panic(fmt.Sprintf("hclparse.GenerateAutoIncludeFile: fs is nil; a vfs.FS is required to create targetDir and write the generated autoinclude file (targetDir=%q)", targetDir))
40+
panic(fmt.Sprintf("hclparse.GenerateAutoIncludeFile: fs is nil (targetDir=%q, sourceFile=%q)", targetDir, resolvedSourceFile(resolved)))
4141
}
4242

4343
if targetDir == "" {
44-
panic(fmt.Sprintf("hclparse.GenerateAutoIncludeFile: targetDir is empty (got %q); targetDir is the generated unit directory where %s will be written", targetDir, AutoIncludeFile))
44+
panic(fmt.Sprintf("hclparse.GenerateAutoIncludeFile: targetDir is empty (sourceFile=%q)", resolvedSourceFile(resolved)))
4545
}
4646

4747
if resolved == nil {
@@ -252,3 +252,14 @@ func writeNonDependencyContent(outBody *hclwrite.Body, body *hclsyntax.Body, src
252252
func quotedStringTokens(value string) hclwrite.Tokens {
253253
return hclwrite.TokensForValue(cty.StringVal(value))
254254
}
255+
256+
// resolvedSourceFile extracts the originating HCL filename from a resolved
257+
// autoinclude, used to enrich panic messages with file context. Returns ""
258+
// when resolved or its body is nil.
259+
func resolvedSourceFile(resolved *AutoIncludeResolved) string {
260+
if resolved == nil || resolved.RawBody == nil {
261+
return ""
262+
}
263+
264+
return resolved.RawBody.MissingItemRange().Filename
265+
}

internal/hclparse/parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ func ParseStackFile(fs vfs.FS, input *ParseStackFileInput) (*ParseResult, error)
6868
filename = input.Filename
6969
}
7070

71-
panic(fmt.Sprintf("hclparse.ParseStackFile: fs is nil; a vfs.FS is required to read included stack files (e.g. pass vfs.NewOSFS() for disk or vfs.NewMemMapFS() for tests) (filename=%q)", filename))
71+
panic(fmt.Sprintf("hclparse.ParseStackFile: fs is nil (filename=%q)", filename))
7272
}
7373

7474
if input == nil {
75-
panic("hclparse.ParseStackFile: input is nil; caller must provide a *ParseStackFileInput with Src and StackDir set")
75+
panic("hclparse.ParseStackFile: input is nil")
7676
}
7777

7878
if input.StackDir == "" {
79-
panic(fmt.Sprintf("hclparse.ParseStackFile: input.StackDir is empty (got %q); StackDir is required to resolve relative include paths and compute generated unit directories (filename=%q)", input.StackDir, input.Filename))
79+
panic(fmt.Sprintf("hclparse.ParseStackFile: input.StackDir is empty (filename=%q)", input.Filename))
8080
}
8181

8282
file, diags := hclsyntax.ParseConfig(input.Src, input.Filename, hcl.Pos{Line: 1, Column: 1})

internal/hclparse/stack.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ func ExtractStackRefs(stacks []*StackBlockHCL) []ComponentRef {
157157
// and performs a two-pass parse. Returns nil, nil if the file does not exist.
158158
func ParseStackFileFromPath(fs vfs.FS, stackDir string) (*ParseResult, error) {
159159
if fs == nil {
160-
panic(fmt.Sprintf("hclparse.ParseStackFileFromPath: fs is nil; a vfs.FS is required to read terragrunt.stack.hcl from disk (pass vfs.NewOSFS() or vfs.NewMemMapFS()) (stackDir=%q)", stackDir))
160+
panic(fmt.Sprintf("hclparse.ParseStackFileFromPath: fs is nil (stackDir=%q)", stackDir))
161161
}
162162

163163
if stackDir == "" {
164-
panic(fmt.Sprintf("hclparse.ParseStackFileFromPath: stackDir is empty (got %q); stackDir is required to locate terragrunt.stack.hcl and resolve include paths", stackDir))
164+
panic("hclparse.ParseStackFileFromPath: stackDir is empty")
165165
}
166166

167167
stackDir = util.ResolvePath(stackDir)
@@ -188,11 +188,11 @@ func ParseStackFileFromPath(fs vfs.FS, stackDir string) (*ParseResult, error) {
188188
// Returns nil if the file does not exist or cannot be parsed.
189189
func UnitPathsFromStackDir(fs vfs.FS, stackDir string) []string {
190190
if fs == nil {
191-
panic(fmt.Sprintf("hclparse.UnitPathsFromStackDir: fs is nil; a vfs.FS is required to read the stack file and enumerate its generated unit directories (stackDir=%q)", stackDir))
191+
panic(fmt.Sprintf("hclparse.UnitPathsFromStackDir: fs is nil (stackDir=%q)", stackDir))
192192
}
193193

194194
if stackDir == "" {
195-
panic(fmt.Sprintf("hclparse.UnitPathsFromStackDir: stackDir is empty (got %q); stackDir is required to locate terragrunt.stack.hcl and compute generated unit paths", stackDir))
195+
panic("hclparse.UnitPathsFromStackDir: stackDir is empty")
196196
}
197197

198198
stackDir = util.ResolvePath(stackDir)
@@ -229,15 +229,15 @@ const maxDiscoverDepth = 1000
229229
// stack's units will be generated (.terragrunt-stack/stack_path/).
230230
func DiscoverStackChildUnits(fs vfs.FS, stackSourceDir, stackGenDir string) []ComponentRef {
231231
if fs == nil {
232-
panic(fmt.Sprintf("hclparse.DiscoverStackChildUnits: fs is nil; a vfs.FS is required to read the nested stack's terragrunt.stack.hcl (stackSourceDir=%q, stackGenDir=%q)", stackSourceDir, stackGenDir))
232+
panic(fmt.Sprintf("hclparse.DiscoverStackChildUnits: fs is nil (stackSourceDir=%q, stackGenDir=%q)", stackSourceDir, stackGenDir))
233233
}
234234

235235
if stackSourceDir == "" {
236-
panic(fmt.Sprintf("hclparse.DiscoverStackChildUnits: stackSourceDir is empty (got %q); this is the directory containing the nested stack's source terragrunt.stack.hcl (stackGenDir=%q)", stackSourceDir, stackGenDir))
236+
panic(fmt.Sprintf("hclparse.DiscoverStackChildUnits: stackSourceDir is empty (stackGenDir=%q)", stackGenDir))
237237
}
238238

239239
if stackGenDir == "" {
240-
panic(fmt.Sprintf("hclparse.DiscoverStackChildUnits: stackGenDir is empty (got %q); this is the target directory where the nested stack's units will be generated (stackSourceDir=%q)", stackGenDir, stackSourceDir))
240+
panic(fmt.Sprintf("hclparse.DiscoverStackChildUnits: stackGenDir is empty (stackSourceDir=%q)", stackSourceDir))
241241
}
242242

243243
return discoverStackChildUnitsWithDepth(fs, stackSourceDir, stackGenDir, 0)

0 commit comments

Comments
 (0)