Skip to content

feat: template ranges + two-way template/source navigation#1624

Open
megha-narayanan wants to merge 4 commits into
aws:feat/cdk-lspfrom
megha-narayanan:feat/explorer-range-mapping
Open

feat: template ranges + two-way template/source navigation#1624
megha-narayanan wants to merge 4 commits into
aws:feat/cdk-lspfrom
megha-narayanan:feat/explorer-range-mapping

Conversation

@megha-narayanan

@megha-narayanan megha-narayanan commented Jun 12, 2026

Copy link
Copy Markdown
  • Computes character ranges for CloudFormation resource blocks in synthesized templates and uses them for navigation in both directions:
  • CodeLens "go to" now selects the whole resource block (was a zero-width cursor).
  • New go-to-definition from a synthesized template back to the construct's source.

A note on the JSON parser dependency Computing character ranges needs a position-aware JSON parser, since JSON.parse discards offsets. The natural choice is jsonc-parser (what the VS Code JSON language service uses). We could not use it here: its UMD entry loads internal modules through a parameter-shadowed require("./impl/...") that esbuild (used by node-backpack to bundle the CLI) cannot statically trace, so the bundled cdk/cdk-assets binaries fail with Cannot find module './impl/format'. Known, still-open: microsoft/node-jsonc-parser#57, evanw/esbuild#1619. Its ESM build bundles fine, but the esbuild mainFields workaround isn't exposed by node-backpack, and importing the ESM build directly breaks our CommonJS tests; marking it external isn't appropriate for a self-contained CLI. So we use json-source-map, a single-file CommonJS module that bundles cleanly.

Checklist

  • This change contains a major version upgrade for a dependency and I confirm all breaking changes are addressed
    • Release notes for the new version:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Adds jsonc-parser@3.2.0 as a runtime dep, in preparation for computing
character ranges of template resource/property blocks (PR-A). Dedupes to
the jsonc-parser already resolved in the lockfile; no new version added.
…te ranges

Compute character-accurate ranges of CloudFormation resource blocks in
synthesized templates and use them for navigation in both directions.

cloud-assembly-api:
- resolveResourceRange(text, logicalId): the character range of a resource's
  value block, via jsonc-parser. A position-aware parse is used instead of a
  line scan because real templates contain literal braces and escaped quotes
  inside string values (for example Fn::Sub placeholders) that defeat naive
  brace matching.
- resolveLogicalIdAtOffset(text, offset): the inverse, mapping a position in a
  template back to the enclosing resource's logical id.

cdk-explorer LSP:
- resourceTarget now returns the real resource block range (previously a
  zero-width cursor at the logical-id key), so the CodeLens "go to" selects the
  whole block.
- onDefinition: go-to-definition from a synthesized template back to the
  construct's source, keyed by (templateFile, logicalId) since logical ids are
  only unique within a template.
- offset and position conversions extracted to lib/lsp/positions.ts.

The jsonc-parser runtime dependency is added in the preceding commit.
@github-actions github-actions Bot added the p2 label Jun 12, 2026
@aws-cdk-automation aws-cdk-automation requested a review from a team June 12, 2026 20:06
@megha-narayanan megha-narayanan marked this pull request as ready for review June 12, 2026 20:35
cloud-assembly-api now depends on jsonc-parser, so the bundled CLIs that
include it (aws-cdk, cdk-assets, integ-runner) must attribute it. Regenerated
via node-backpack so the build's self-mutation check passes.
…for template ranges

jsonc-parser cannot be bundled into the CLI. Its UMD entry loads its internal
modules through a parameter-shadowed require("./impl/..."), which esbuild (via
node-backpack) cannot statically trace, so the packaged cdk and cdk-assets
binaries fail their build-time sanity check with "Cannot find module
'./impl/format'". This is a known, still-open limitation
(microsoft/node-jsonc-parser#57, evanw/esbuild#1619), and node-backpack does not
expose the esbuild mainFields workaround.

Switch resolveResourceRange and resolveLogicalIdAtOffset to json-source-map, a
single-file CommonJS module that bundles cleanly and gives the same byte offsets.
The range contract and the JSON.parse(slice) oracle tests are unchanged.
json-source-map is a strict parser, so the one test that exercised lenient input
now asserts undefined.

Regenerates THIRD_PARTY_LICENSES for aws-cdk, cdk-assets, and integ-runner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant