Skip to content

fix: better errors reporting form autoincludes - #5985

Merged
denis256 merged 26 commits into
mainfrom
5980-auto-include-parse-errors
May 4, 2026
Merged

fix: better errors reporting form autoincludes#5985
denis256 merged 26 commits into
mainfrom
5980-auto-include-parse-errors

Conversation

@denis256

@denis256 denis256 commented Apr 27, 2026

Copy link
Copy Markdown
Member

Description

  • improved handling of errors in autoincludes
  • reject dependency blocks with zero or 2+ labels
  • improved fuzzy tests

Fixes #5980.

RFC: #5663

TODOs

Read the Gruntwork contribution guidelines.

  • I authored this code entirely myself
  • I am submitting code based on open source software (e.g. MIT, MPL-2.0, Apache)
  • I am adding or upgrading a dependency or adapted code and confirm it has a compatible open source license
  • Update the docs.
  • Run the relevant tests successfully, including pre-commit checks.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Added / Removed / Updated [X].

Migration Guide

Summary by CodeRabbit

  • New Features

    • Typed errors for stack dependency expansion and malformed autoinclude dependencies.
    • Autoinclude resolution preserves original file bytes for accurate expression slicing.
  • Bug Fixes

    • Malformed dependency blocks now produce diagnostics/errors instead of being skipped.
    • Stack parsing/discovery surfaces parse/read failures; parser-incompatible autoinclude usage fails loudly.
  • Tests

    • Expanded unit/integration tests and fixtures covering autoinclude parsing, dependency validation, and generation outcomes.

@vercel

vercel Bot commented Apr 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
terragrunt-docs Ready Ready Preview, Comment Apr 30, 2026 8:14pm

Request Review

@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds typed errors for malformed autoinclude dependencies and stack-dependency expansion, enforces strict dependency block validation, threads original source bytes through autoinclude parsing/resolution, changes stack-dir discovery error semantics, and updates generation, tests, and fixtures to fail fast on parser-incompatible autoinclude cases.

Changes

Cohort / File(s) Summary
Typed errors
internal/discovery/errors.go, internal/hclparse/errors.go
Add StackDependencyExpansionError and MalformedDependencyError types with Error()/Unwrap() (and constructor) to wrap failures and preserve contextual fields (DepPath, Name, FilePath, Reason).
Autoinclude parsing & validation
internal/hclparse/autoinclude.go, internal/hclparse/autoinclude_test.go, internal/hclparse/errors.go
Require exactly one label on dependency blocks; validate/evaluate config_path as a non-null string; replace silent skipping with error accumulation (MalformedDependencyError) and return joined errors; add SourceBytes []byte to AutoIncludeResolved; tests updated to assert loud-fail behavior.
Parse pipeline & source bytes
internal/hclparse/parse.go, internal/hclparse/generate.go, internal/hclparse/stack.go, internal/hclparse/stack_test.go
Record per-AutoIncludeHCL source bytes during parse/include merging and attach them to resolved autoinclude results; tighten ParseStackFileFromPath/UnitPathsFromStackDir behavior and update related tests.
Dependency expansion & discovery
internal/discovery/helpers.go, internal/discovery/stack_dependency_expansion_error_test.go
Only attempt stack-dir expansion when depPath is a directory (stat-guard); wrap expansion failures via NewStackDependencyExpansionError(depPath, err) instead of silently skipping; add tests for wrapper/unwrap behavior.
Stack generation & autoinclude detection
pkg/config/stack.go, pkg/config/stack_autoinclude_internal_test.go
On autoinclude parse failure detect native autoinclude blocks in merged bodies and return a wrapped parse error; generate autoinclude files using resolved.SourceBytes; add detection helpers and tests.
Integration tests & fixtures
test/integration_stack_dependencies_test.go, test/fixtures/stacks/...
Add/rename fixtures and integration tests covering parser-compatible and -incompatible autoinclude flows; assert hard-fail for incompatible cases and success for compatible includes; many new fixture files added.
Docs/comments & minor refactors
internal/hclparse/generate.go, internal/hclparse/stack.go
Documentation/comment clarifications and small in-function simplifications (inlined struct appends), no API signature changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant CLI as CLI
  participant Parser as AutoInclude Parser
  participant FS as Filesystem
  participant Resolver as AutoInclude Resolver
  participant Discovery as Dependency Expander
  participant Generator as Autoinclude Generator

  CLI->>Parser: Parse stack file (include merging)
  Parser->>FS: Read included file bytes
  FS-->>Parser: Return bytes
  Parser-->>Resolver: Return AutoIncludeHCL + SourceBytes
  Resolver->>Resolver: Validate dependency blocks (labels, config_path)
  alt malformed blocks
    Resolver-->>CLI: Return MalformedDependencyError(s)
  else valid
    Resolver->>Discovery: Expand dependency config_path -> unit paths (os.Stat gate)
    alt expansion error
      Discovery-->>CLI: Return StackDependencyExpansionError (wraps inner)
    else success
      Discovery-->>Generator: Provide unit paths
      Generator->>FS: Write autoinclude file using SourceBytes for slicing
      Generator-->>CLI: Success
    end
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

Suggested reviewers

  • yhakbar
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.03% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: better errors reporting form autoincludes' directly relates to the main changes: improved error handling and validation in autoinclude functionality.
Description check ✅ Passed The description covers the main changes (error handling in autoincludes, reject invalid dependency blocks, improved tests) and references the linked issue #5980. However, it lacks specific details about the changes and the release notes section is incomplete.
Linked Issues check ✅ Passed The PR addresses issue #5980 by implementing stricter error handling for malformed dependency blocks and improved error reporting in autoincludes. Code changes include new error types, validation logic, and comprehensive tests that enforce proper dependency block structure.
Out of Scope Changes check ✅ Passed All changes are directly related to improving autoinclude error handling: new error types, validation logic, refactored parsing, test fixtures for various autoinclude scenarios, and integration tests. No unrelated changes detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 5980-auto-include-parse-errors

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@denis256
denis256 marked this pull request as ready for review April 28, 2026 20:44

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
internal/hclparse/autoinclude.go (1)

245-247: Consider returning discovered paths alongside errors.

The current implementation returns nil when any dependency block is malformed. For better error recovery, consider returning successfully extracted paths along with the accumulated errors, allowing callers to decide how to handle partial results.

However, the all-or-nothing approach is also valid if downstream DAG processing requires complete dependency information to function correctly.

♻️ Optional: Return partial results with errors
-	if len(errs) > 0 {
-		return nil, errors.Join(errs...)
+	if len(errs) > 0 {
+		return paths, errors.Join(errs...)
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/hclparse/autoinclude.go` around lines 245 - 247, The current block
returns nil on any accumulated errors (len(errs) > 0); change it to return the
successfully discovered paths alongside the joined error so callers can handle
partial results. Specifically, when len(errs) > 0 return the variable holding
the collected paths (e.g. paths or discoveredPaths) and errors.Join(errs...)
instead of nil, and ensure the function signature and callers (the functions
that call this autoinclude routine) accept and handle a non-nil slice plus
error.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@internal/hclparse/autoinclude.go`:
- Around line 245-247: The current block returns nil on any accumulated errors
(len(errs) > 0); change it to return the successfully discovered paths alongside
the joined error so callers can handle partial results. Specifically, when
len(errs) > 0 return the variable holding the collected paths (e.g. paths or
discoveredPaths) and errors.Join(errs...) instead of nil, and ensure the
function signature and callers (the functions that call this autoinclude
routine) accept and handle a non-nil slice plus error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 716f072c-656f-42c7-852d-6f4dd89f9e17

📥 Commits

Reviewing files that changed from the base of the PR and between ae8ed13 and 15e9793.

📒 Files selected for processing (6)
  • internal/hclparse/autoinclude.go
  • internal/hclparse/errors.go
  • internal/hclparse/generate.go
  • internal/hclparse/parse.go
  • internal/hclparse/stack.go
  • test/integration_stack_dependencies_test.go
✅ Files skipped from review due to trivial changes (2)
  • internal/hclparse/errors.go
  • internal/hclparse/generate.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/hclparse/stack.go

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
internal/hclparse/autoinclude_test.go (1)

421-438: Consider consolidating the error type check.

The test writes both a .json suffixed file and a regular file but only reads from the regular path. The assertion at line 437 using errors.As twice with || is correct but slightly verbose.

♻️ Minor simplification
-	assert.True(t, errors.As(err, &fpe) || errors.As(err, &ube), "expected FileParseError or UnexpectedBodyTypeError, got %T: %v", err, err)
+	isExpectedErr := errors.As(err, &fpe) || errors.As(err, &ube)
+	assert.True(t, isExpectedErr, "expected FileParseError or UnexpectedBodyTypeError, got %T: %v", err, err)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/hclparse/autoinclude_test.go` around lines 421 - 438, In
TestAutoIncludeDependencyPaths_UnexpectedBodyTypeOnJSON, replace the two
separate errors.As checks with a single consolidated check that unwraps the
error chain and type-switches to detect either hclparse.FileParseError or
hclparse.UnexpectedBodyTypeError; locate the assertion using errors.As(err,
&fpe) || errors.As(err, &ube) and replace it with a loop that walks err via
errors.Unwrap (or errors.As into an interface and type-switches) and fails the
test unless a FileParseError or UnexpectedBodyTypeError is found, keeping
references to the test function name and the err variable for clarity.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/hclparse/stack_test.go`:
- Line 406: Update the misleading comment in internal/hclparse/stack_test.go
near the test that asserts symlink resolution: change the line that currently
reads “not the symlink target” to clearly state that the path should resolve to
the real directory (the symlink target) and reject the symlink path (e.g., “Path
should resolve to the real directory (the symlink target), not the symlink
path.”) so it matches the assertions in the test surrounding the symlink
handling code in this test function.

---

Nitpick comments:
In `@internal/hclparse/autoinclude_test.go`:
- Around line 421-438: In
TestAutoIncludeDependencyPaths_UnexpectedBodyTypeOnJSON, replace the two
separate errors.As checks with a single consolidated check that unwraps the
error chain and type-switches to detect either hclparse.FileParseError or
hclparse.UnexpectedBodyTypeError; locate the assertion using errors.As(err,
&fpe) || errors.As(err, &ube) and replace it with a loop that walks err via
errors.Unwrap (or errors.As into an interface and type-switches) and fails the
test unless a FileParseError or UnexpectedBodyTypeError is found, keeping
references to the test function name and the err variable for clarity.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 15cfec02-6eb4-4e29-af74-a89b234ee936

📥 Commits

Reviewing files that changed from the base of the PR and between 15e9793 and 64a58f9.

📒 Files selected for processing (6)
  • internal/discovery/helpers.go
  • internal/hclparse/autoinclude_test.go
  • internal/hclparse/parse.go
  • internal/hclparse/stack_test.go
  • pkg/config/stack.go
  • test/integration_stack_dependencies_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/hclparse/parse.go

Comment thread internal/hclparse/stack_test.go Outdated
filePath := filepath.Join(tmpDir, "another-name.hcl")
require.NoError(t, os.WriteFile(filePath, []byte(`# regular file, not a directory`), 0644))

result, err := hclparse.ParseStackFileFromPath(vfs.NewOSFS(), filePath)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: can't we use an in-memory fs here?

assert.Equal(t, "app", result.Units[0].Name)
}

// Uses OSFS because MemMapFS does not faithfully reproduce os.Symlink semantics that util.ResolvePath relies on.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting. Is this a bug in our vfs implementation?

@denis256
denis256 merged commit ef69123 into main May 4, 2026
52 checks passed
@denis256
denis256 deleted the 5980-auto-include-parse-errors branch May 4, 2026 12:27
Rahul-Kumar-prog pushed a commit to Rahul-Kumar-prog/terragrunt that referenced this pull request May 10, 2026
* bug: HCL parsing errors handling

* bug: handling of auto include parsing errors

* chore: auto include fixes

* chore: simplified parsing errors

* chore: stack parsing fixes

* chore: internal tests

* chore: statck parsing fixes

* chore: stack dependencies autoinclude

* chore: internal test fix

* chore: internal test fix

* chore: improved discovery errors

* chore: fuzz test

* chore: fuzzy tests improvements

* chore: auto include fix

* chore: stacks simplification

* chore: simplified auto include errors

* chore: PR comments

* chore: failing tests fixes

* chore: discovery helpers

* chore: symlinks handling

* chore: parser cleanup
@coderabbitai coderabbitai Bot mentioned this pull request May 14, 2026
8 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Jun 2, 2026
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error trying to make the autoinclude experiment work

2 participants