Skip to content

Fix CodeLens positioned inside preceding code blocks#15893

Open
adamint wants to merge 3 commits intomicrosoft:mainfrom
adamint:fix/codelens-wrong-line-15618
Open

Fix CodeLens positioned inside preceding code blocks#15893
adamint wants to merge 3 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.

while (i >= 0) {
const ch = text[i];
if (ch === ';' || ch === '{') {
if (ch === ';' || ch === '{' || ch === '}') {
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.

This still breaks existing fluent chains where the Add* call comes after a callback block in the same statement. For example, playground/TestShop/TestShop.AppHost/AppHost.cs has .WithPgAdmin(resource => { ... }).AddDatabase("catalogdb"), and playground/waitfor/WaitForSandbox.AppHost/AppHost.cs has .RunAsContainer(c => { ... }).AddDatabase("db"). In both cases the new } stop condition makes _findStatementStartLine stop at the callback closing brace, so statementStartLine becomes the .AddDatabase(...) line instead of the top of the fluent chain.

That means the CodeLens is still misplaced for real AppHost code after merge, and it regresses the statementStartLine contract from "first line of the full statement chain" to "line after the last callback block". The fix likely needs to distinguish a } that closes a previous statement from one that belongs to the current fluent call (for example by tracking nesting / scanning for the previous real statement boundary), and this needs a focused regression test for these callback-chain shapes. The same issue is duplicated in jsTsAppHostParser.ts.

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

3 participants