Skip to content

Fix CodeLens positioned inside preceding code blocks#15893

Merged
adamint merged 5 commits intomicrosoft:mainfrom
adamint:fix/codelens-wrong-line-15618
Apr 9, 2026
Merged

Fix CodeLens positioned inside preceding code blocks#15893
adamint merged 5 commits intomicrosoft:mainfrom
adamint:fix/codelens-wrong-line-15618

Conversation

@adamint
Copy link
Copy Markdown
Member

@adamint adamint commented Apr 4, 2026

Description

Fix CodeLens being positioned inside preceding code blocks instead of on the resource declaration line.

Root cause: _findStatementStartLine() in both the C# and JS/TS AppHost parsers walked backwards from a resource match looking for ; or { as statement delimiters, but ignored }. When a code block like if (...) { ... } preceded a resource call, the algorithm walked past the } into the block and stopped at its {, placing the CodeLens inside the preceding block.

Fix: Add } as a statement delimiter in the backward walk so that closing braces correctly bound the start of the current statement.

Also fixes: tsconfig.json updated to include mocha types (resolves "Cannot find name 'test'" errors in VS Code) and skipLibCheck: true (resolves duplicate MCP type definition conflicts between @types/vscode and the proposed API file).

Validation: 26 new tests (13 C#, 13 JS/TS) covering: preceding if blocks, nested braces, blocks with semicolons, comments between blocks and resources (line comments, block comments, mixed comment styles), multi-line fluent chains, try/catch blocks, single-line blocks, empty blocks, for loops, multiple resources interspersed with blocks, and comments before fluent chains after blocks. All existing tests continue to pass.

Fixes #15618

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

The _findStatementStartLine() method walked backwards from a resource
match looking for ';' or '{' but ignored '}'. When a code block like
if (...) { ... } preceded a resource call, the algorithm walked past
the '}' into the block and stopped at its '{', placing the CodeLens
inside the preceding block instead of on the resource line.

Fix: add '}' as a statement delimiter in the backward walk so that
closing braces correctly bound the start of the current statement.

Also fix tsconfig.json to include mocha types for test globals and
skipLibCheck to avoid MCP duplicate type definition conflicts.
Copilot AI review requested due to automatic review settings April 4, 2026 04:27
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 15893

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 15893"

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect CodeLens placement in the VS Code extension by improving how the AppHost parsers compute the start line of a resource statement when preceded by code blocks.

Changes:

  • Update C# and JS/TS AppHost parsers to treat } as a statement delimiter when walking backwards to find statementStartLine.
  • Add extensive unit tests for statementStartLine behavior around preceding blocks (C# + JS/TS).
  • Update extension/tsconfig.json (mocha types, skipLibCheck) to address editor/test typing issues.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
extension/tsconfig.json Adds mocha/node types and enables skipLibCheck for TS build/editor experience.
extension/src/test/parsers.test.ts Adds coverage for statementStartLine with preceding blocks and complex brace scenarios.
extension/src/editor/parsers/jsTsAppHostParser.ts Includes } as a delimiter in statement-start detection for JS/TS AppHost parsing.
extension/src/editor/parsers/csharpAppHostParser.ts Includes } as a delimiter in statement-start detection for C# AppHost parsing.

Copy link
Copy Markdown
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

3 issues found: missing test coverage for callback-in-chain regression (1), skipLibCheck: true suppressing all .d.ts type-checking (1), minor edge case in } line-skip logic (1).

…e } else {

- Fix _findStatementStartLine to track brace nesting: when a '}' is
  encountered during the backward walk, match it with '{' and check
  if preceded by '=>'. Lambda bodies are skipped; other blocks are
  treated as statement boundaries. Fixes regression where callback
  lambdas in fluent chains (e.g. .WithPgAdmin(r => { ... }).AddDatabase())
  caused CodeLens to stop at the callback's '}' instead of reaching
  the top of the chain.
- Change } line-skip to only skip lines matching '} [// comment]',
  not lines like '} else {' or '} catch (e) {'.
- Revert tsconfig.json: remove skipLibCheck and types array (compilation
  succeeds without them; the mocha/MCP type issues are pre-existing
  and should be addressed separately).
- Add tests for callback lambda chains, RunAsContainer callbacks,
  and } else { between blocks and resource calls.
@adamint adamint requested review from JamesNK and sebastienros April 8, 2026 17:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

Re-running the failed jobs in the CI workflow for this pull request because 2 jobs were identified as retry-safe transient failures in the CI run attempt.
GitHub was asked to rerun all failed jobs for that attempt, and the rerun is being tracked in the rerun attempt.
The job links below point to the failed attempt jobs that matched the retry-safe transient failure rules.

Copy link
Copy Markdown
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

There is some duplication. It would be good if parsing can be reused where possible. e.g. C#, TS and Java probably share some parsing logic because they're all C syntax languages.

Move _findStatementStartLine, _findMatchingOpenBrace, and
_isPrecededByArrow from both C# and JS/TS parsers into a shared
parserUtils.ts module. Both parsers now import from parserUtils
instead of duplicating the identical implementations.

Addresses review feedback from JamesNK about code duplication.
@adamint adamint enabled auto-merge (squash) April 9, 2026 04:35
@adamint adamint merged commit bc0c339 into microsoft:main Apr 9, 2026
524 of 527 checks passed
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.

VS Code: Code Lens shown in weird place

4 participants