Skip to content

feat: refresh diagnostics and CodeLens when cdk.out changes - #1630

Merged
megha-narayanan merged 3 commits into
aws:feat/cdk-lspfrom
megha-narayanan:feat/explorer-cdkout-watcher
Jun 16, 2026
Merged

feat: refresh diagnostics and CodeLens when cdk.out changes#1630
megha-narayanan merged 3 commits into
aws:feat/cdk-lspfrom
megha-narayanan:feat/explorer-cdkout-watcher

Conversation

@megha-narayanan

Copy link
Copy Markdown
Contributor

The LSP currently reads cdk.out once at startup and never refreshes. After this PR, any
rewrite of cdk.out refreshes the editor's diagnostics and CodeLenses
automatically.

  • New lib/core/assembly-watcher.ts: a chokidar-backed watcher with a debounced
    200ms onChange, filtered to manifest.json, tree.json,
    validation-report.json. RWLock marker files (synth.lock,
    read.<pid>.<n>.lock) are excluded. Throws from onChange route through
    onError rather than leaking from the timer. Lives in lib/core/ so the web
    explorer can reuse it.
  • refreshFromAssembly is now the single fan-out for new assembly data:
    rebuild cachedIndex, publish empty diagnostics for URIs that no longer have
    violations (clearing resolved squiggles), republish current diagnostics, and
    send workspace/codeLens/refresh (gated on workspace.codeLens.refreshSupport).
  • When the validation report fails to load, last-good diagnostics are preserved,
    matching the existing contract for 'error' and 'not-found' reads.
  • Watcher started in onInitialized after the initial refresh, closed in
    onShutdown.
    Fixes #

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

…nges

The LSP read cdk.out once at startup and never refreshed. This PR makes
the editor live: any time cdk.out is rewritten (an external `cdk synth`,
`cdk watch`, or a future in-process synth), diagnostics and CodeLenses
update automatically.

* New `lib/core/assembly-watcher.ts`: a chokidar-backed watcher that fires
  a debounced (200ms) onChange when `manifest.json`, `tree.json`, or
  `validation-report.json` changes. RWLock marker files (`synth.lock`,
  `read.<pid>.<n>.lock`) are filtered out. Throws from onChange are
  forwarded to onError rather than leaking from the timer. The module is
  LSP-agnostic, ready for the future web server consumer.

* `refreshFromAssembly` is now the single 'new data -> all surfaces
  update' path: it rebuilds `cachedIndex`, publishes empty arrays for
  URIs that no longer have violations (so resolved squiggles disappear
  from the editor), republishes current diagnostics, and sends
  `workspace/codeLens/refresh` (gated on the client's
  `workspace.codeLens.refreshSupport` capability).

* When the validation report fails to load, last-good diagnostics are
  preserved instead of being wiped. This matches the existing contract
  for `'error'` and `'not-found'` reads.

* Watcher lifecycle: started in `onInitialized` after the initial
  refresh, closed in `onShutdown`.

Manual synth and auto-synth-on-save are deferred to follow-up PRs; this
PR delivers freshness for any external producer.

`chokidar@^4` is now a direct cdk-explorer dep (added via projen).
It was already in the bundled graph through toolkit-lib, so this is a
promotion, not a net-new bundled dependency.
…ionsError

* Export `MANIFEST_FILE` from `cloud-assembly-api` (was private). Add
  `TREE_FILE` as a local export of `assembly-watcher.ts`. aws-cdk-lib's
  TreeMetadata hard-codes `tree.json` rather than importing a shared
  constant, so `TREE_FILE` is a consumer-side label only.

* Reader, watcher, fixtures, and tests now use the constants instead of
  repeating filename literals. Non-signal-file paths in the watcher test
  stay literal so they continue to exercise the rejection path.

* Drop `AssemblyData.violationsError` and the if-guard around the
  publish/clear loop. The producer writes `validation-report.json`
  synchronously, so a corrupt file is unreachable through normal synth
  flow. A failed load surfaces via the existing `warnings` array; the
  rare partial-write race resolves on the next watcher tick.
@megha-narayanan
megha-narayanan marked this pull request as ready for review June 15, 2026 19:29
@rix0rrr rix0rrr self-assigned this Jun 16, 2026
…er-cdkout-watcher

# Conflicts:
#	packages/@aws-cdk/cdk-explorer/test/lsp/server.test.ts
@megha-narayanan
megha-narayanan merged commit 5afd770 into aws:feat/cdk-lsp Jun 16, 2026
7 of 8 checks passed
@megha-narayanan
megha-narayanan deleted the feat/explorer-cdkout-watcher branch June 16, 2026 18:30
@megha-narayanan megha-narayanan mentioned this pull request Jun 16, 2026
1 task
megha-narayanan added a commit to megha-narayanan/aws-cdk-cli that referenced this pull request Jun 22, 2026
Integrates the aws#1630 cdk.out watcher with the async assembly read path. readAssembly and buildConstructTreeAsync stay async; the watcher onChange refreshes via the async path with its rejection routed to console.error. aws#1630's watcher tests now await the async refresh (triggerWatcher settles the fire-and-forget refresh), and the sync readAssembly test doubles were made async to match the Promise-only seam.
megha-narayanan added a commit that referenced this pull request Jun 30, 2026
Adds manual synth and auto-synth-on-save to the CDK LSP. The cdk.out
watcher (landed in #1630) handles all diagnostics/lens refreshes --
these commands just add new ways to produce a fresh assembly.

Features:
↻ Synth now CodeLens at the top of any CDK source file (only when
auto-synth is off)
▶ Enable auto-synth / ⏹ Disable auto-synth toggle
Auto-synth starts disabled
When auto-synth is on, saving any non-ignored file in the project
triggers a synth
All feedback/output goes to the Output panel, no popups

Design decisions (the important ones):

Concurrent synths are supressed: if a save fires during a slow synth,
that save is skipped. The next save picks it up.
cdk.json is read once at startup. Changing it requires an LSP restart.
The toggle/synth lenses only appear on files that already have L1
resource lenses. Files with no CDK resources see nothing.
Toggle state resets to disabled on LSP restart (not persisted).

NOTE: One thing not in this PR (but must land before prod) is a
workspace trust gate. We should verify with the user that they trust
this workspace before running any synth. I may need to set up some way
to preserve this between sessions.

Open questions / things I need some feedback on:
- Is "auto-synth" the right name? Alternatives: "synth on save", "live
synth"?
- Where should the toggle live? Line 0 of a CDK file works for LSP-only,
but it's only visible when you're in a file with constructs. Status bar
item would be better UX but needs a client extension. Is there a better
alternative?
- Should synth failures (app compile errors) be more visible than the
Output panel? A diagnostic on the first line of the failing file, for
example?
- If the app has context lookups and no cached cdk.context.json, synth
fails with an auth/context error. Should the error message detect this
and suggest running cdk synth in terminal first?

<img width="809" height="161" alt="Screenshot 2026-06-16 at 3 41 01 PM"
src="https://github.com/user-attachments/assets/b542927c-0206-462f-8464-2c43f19bbe0b"
/>
<img width="771" height="166" alt="Screenshot 2026-06-16 at 3 41 10 PM"
src="https://github.com/user-attachments/assets/52695e76-2e1d-4fc8-ada7-d1ce338512cf"
/>

### 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
fossamagna pushed a commit to fossamagna/aws-cdk-cli that referenced this pull request Jul 17, 2026
Merges the`feat/cdk-lsp' branch into `main`. The change is additive and
introduces no behavior change to existing CLI commands.

- New `@aws-cdk/cdk-explorer` package containing the Language Server
under `lib/lsp` (server, diagnostics, CodeLens, template locator,
position mapping).
- Extends `@aws-cdk/cloud-assembly-api` with two parsing modules
consumed by the server: `construct-tree.ts` (builds the construct tree
from a cloud assembly) and `template-ranges.ts` (resolves a logical ID
or property to its byte range in the template).

Capabilities (folds in aws#1559, aws#1593, aws#1592, aws#1617, aws#1624, aws#1630, aws#1631,
aws#1662, aws#1634, aws#1674):

- Diagnostics: surfaces synth errors and policy-validation violations in
the editor, mapped back to the source.
- Surfaces CFN resources and adds CodeLens navigation from a construct
to its template resource.
- Navigation between construct source and the synthesized template in
both directions.
- Live refresh: diagnostics and CodeLens update when `cdk.out` changes.
- Reads are constrained to the project directory, and template reads run
off the LSP event loop.

This PR is the server and parsing foundation. It does not add a shipped
CLI command or the web explorer.

### 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

---------

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com>
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.

2 participants