Skip to content

feat: rename, references, and go-to-definition for dependency block labels - #143

Draft
diofeher wants to merge 8 commits into
mainfrom
feat/rename-dependency-blocks
Draft

feat: rename, references, and go-to-definition for dependency block labels#143
diofeher wants to merge 8 commits into
mainfrom
feat/rename-dependency-blocks

Conversation

@diofeher

@diofeher diofeher commented May 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Builds on top of #141. Adds the same three LSP features to terragrunt-ls for dependency block labels:

  • textDocument/prepareRename + textDocument/rename
  • textDocument/references
  • textDocument/definition extended to dependency.X.outputs.Y traversal references
Kind Definition site Reference site
Dependency label dependency "vpc" {} dependency.vpc.outputs.X

Stacked on top of #141 (locals). The base branch will switch to main once #141 merges.

What changed

  • Re-introduces RenameContextDependency and the dependency case in traversalDefinitionTarget that were factored out of feat: rename, references, and go-to-definition for locals #141 to keep that PR scoped to locals.
  • Extends blockLabelTarget so cursor on a dependency "X" block label resolves to a renameable target.
  • definitionOccurrences now also collects the dependency block label as a definition site so renames update both the label declaration and every dependency.X.outputs.Y reference.
  • Adds tests for cursor-on-label, cursor-on-traversal-reference, and the cross-file find/rename cases.

Existing pre-PR behavior (cursor on dependency "X" label or config_path attr jumping to the dependent unit's terragrunt.hcl) is unchanged and continues to work via ast.GetNodeDependencyLabel.

Test plan

  • go build ./...
  • go test ./... — 163 passing
  • golangci-lint run ./... — 0 issues
  • Manual test in VS Code: F2 rename on a dependency "vpc" {} label updates the label and every dependency.vpc.outputs.* reference in the same file.
  • Manual test: "Find All References" on dependency "vpc" highlights the label declaration plus every dependency.vpc.outputs.* reference.
  • Manual test: "Go to Definition" on dependency.vpc.outputs.id jumps to the dependent unit's terragrunt.hcl.

diofeher added 5 commits May 5, 2026 06:37
…abels

Add LSP textDocument/rename and textDocument/prepareRename for three kinds
of identifiers in Terragrunt configs:

- Local variables (`locals { name = ... }` and `local.name` references)
- Dependency block labels (`dependency "name" {}` and `dependency.name.outputs.X`)
- Include block labels (`include "name" {}` and `include.name.X`)

Rename is module-aware: edits propagate across all `.hcl` files in the
same folder, honoring unsaved editor buffers via `s.Configs`. Stack and
values files are excluded.

Implementation is AST-based via `hclsyntax.VisitAll` over `ScopeTraversalExpr`
and `IndexedAST.Locals`/`Body.Blocks` for definitions. Prepare-rename returns
a range covering only the bare identifier (placeholder excludes `local.`
prefix and label quotes). New names are validated against the HCL identifier
grammar.
…include labels

Reuse the AST walker and target-resolution introduced for rename to power
find-all-references on the same three identifier kinds. Honors the LSP
`includeDeclaration` flag.

The `rename.Occurrence` type gains an `IsDefinition` flag so references can
filter out the declaration site when the client requests references only.
…rsal references

`textDocument/definition` previously only fired when the cursor was on an
`include "X" {}` label or a `dependency "X" { config_path = ... }` attribute.
Extend it to also fire on traversal references:

- `local.X` jumps to the `X = ...` declaration in the locals block (same
  file or any sibling .hcl file in the module folder).
- `include.X.Y` jumps to the file referenced by `include "X" { path = ... }`
  (existing path-resolution behavior, now reachable from references too).
- `dependency.X.outputs.Y` jumps to the dependent unit's `terragrunt.hcl`
  (existing path-resolution behavior, now reachable from references too).

Implementation reuses `ast.MinReferenceTraversalLen` and the rename package's
`FindAllOccurrences` for the local-declaration lookup. The Unit-only file-type
guard is replaced with `canRename` so go-to-definition also works on auxiliary
HCL files (e.g., `common.hcl`).
Remove rename, references, and go-to-definition support for include block
labels and `include.X.Y` traversal references. Keeping this PR focused on
locals and dependency labels.

Include support has additional complexity worth landing in a dedicated PR:
- The Terragrunt parser emits "Unsupported attribute" / "Unknown variable"
  diagnostics for `include.X` traversals at LS time, which need a separate
  diagnostic-filter change in `internal/tg/parse.go`.
- `find_in_parent_folders` failures cascade into multiple diagnostic kinds,
  expanding the filter scope.
- Include label rename also requires updating the `path` attribute that
  references the parent file, which is out of scope for the symmetric
  identifier-rename pattern this PR ships.
Remove rename, references, and go-to-definition support for
`dependency "X" {}` block labels and `dependency.X.outputs.Y`
traversal references. Keeping this PR focused on locals.

Existing dependency support (cursor on `dependency` block label or on
`config_path` attribute jumping to the dependent unit) is unaffected
and continues to work via `ast.GetNodeDependencyLabel`.
@diofeher
diofeher requested a review from yhakbar as a code owner May 5, 2026 10:55
@coderabbitai

coderabbitai Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d6d9221e-b412-4f92-9d70-9678998f62d3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/rename-dependency-blocks

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.

@diofeher
diofeher force-pushed the feat/rename-dependency-blocks branch from 0ffcf6e to 82e055b Compare May 5, 2026 11:13
diofeher added 2 commits May 5, 2026 12:15
- traversalDefinitionTarget now only handles `local`; comment updated.
- Switch test code from context.Background() to t.Context() so the
  context is automatically canceled when the test ends.
@diofeher
diofeher force-pushed the feat/rename-dependency-blocks branch from 82e055b to a064794 Compare May 5, 2026 15:17
@diofeher
diofeher marked this pull request as draft May 5, 2026 19:16
Base automatically changed from feat/rename-locals-dependencies-includes to main May 5, 2026 21:13
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.

1 participant