Skip to content

Support cancellation in checker #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged

Support cancellation in checker #856

merged 2 commits into from
May 9, 2025

Conversation

ahejlsberg
Copy link
Member

With this PR the CheckSourceFile and GetDiagnostics methods on Checker and GetSyntacticDiagnostics, GetBindDiagnostics, and GetSemanticDiagnostics on Program support cancellation through a context.Context which is passed as the first parameter to those methods.

When an operation is cancelled, any subsequent call to CheckSourceFile, GetDiagnostics, GetDiagnosticsWithoutCheck, or GetGlobalDiagnostics on that checker will panic regardless of which context the checker is invoked on. (Following cancellation, a checker contains only a partial list of diagnostics, and without more extensive work to track which source elements were checked and which weren't, that checker can no longer deliver a consistent set of diagnostics.)

@ahejlsberg ahejlsberg requested review from Copilot, jakebailey and andrewbranch and removed request for Copilot and jakebailey May 9, 2025 16:44
Copy link
Contributor

@Copilot 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

This PR adds support for context‐based cancellation across key diagnostic and checking methods, updating several APIs to accept a context parameter. Key changes include propagating a context.Context in methods such as GetSyntacticDiagnostics, GetSemanticDiagnostics, CheckSourceFile and GetDiagnostics, along with adding helper methods in the Checker to handle cancellation and update existing tests accordingly.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/testutil/harnessutil/harnessutil.go Updated diagnostic function calls to pass context.Background() where a context was previously omitted.
internal/ls/diagnostics.go Modified diagnostic methods to use a context for obtaining diagnostics.
internal/execute/tsc.go Updated tsc execution logic to pass context for diagnostic retrieval.
internal/compiler/program.go Added context parameters to several methods (CheckSourceFiles, GetSyntacticDiagnostics, etc.) and updated the helper to pass cancellation checks.
internal/checker/utilities.go Added cancellation helper methods and integrated them in Checker functions.
internal/checker/checker.go Updated Checker’s diagnostic and checking methods to accept a context and handle cancellation via a new cancellation flag.
internal/ast/ast.go Added helper methods to retrieve statement lists to support cancellation flow.
cmd/tsgo/main.go Updated main execution to pass a context in calls to diagnostic methods.
Comments suppressed due to low confidence (1)

internal/checker/checker.go:13155

  • [nitpick] The current design calls c.checkNotCanceled() and panics if cancellation was previously detected; consider a more graceful approach (e.g. returning nil or an error) to allow callers to handle cancellation without a panic.
func (c *Checker) GetDiagnostics(ctx context.Context, sourceFile *ast.SourceFile) []*ast.Diagnostic {

Comment on lines 345 to 350
if ctx != nil && ctx.Err() != nil {
return nil
}
if len(sourceFile.CommentDirectives) == 0 {
return diags
}
Copy link
Preview

Copilot AI May 9, 2025

Choose a reason for hiding this comment

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

Consider checking for cancellation earlier in getDiagnosticsHelper to avoid processing diagnostics when the context is already cancelled, which may save unnecessary computation.

Suggested change
if ctx != nil && ctx.Err() != nil {
return nil
}
if len(sourceFile.CommentDirectives) == 0 {
return diags
}

Copilot uses AI. Check for mistakes.

Comment on lines +12 to +13
syntaxDiagnostics := program.GetSyntacticDiagnostics(context.Background(), file)
semanticDiagnostics := program.GetSemanticDiagnostics(context.Background(), file)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
syntaxDiagnostics := program.GetSyntacticDiagnostics(context.Background(), file)
semanticDiagnostics := program.GetSemanticDiagnostics(context.Background(), file)
syntaxDiagnostics := program.GetSyntacticDiagnostics(context.TODO(), file)
semanticDiagnostics := program.GetSemanticDiagnostics(context.TODO(), file)

I think this is one place we're going to want to plumb it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to leave that to a separate PR though.

Copy link
Member

Choose a reason for hiding this comment

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

To be clear my suggestion wasn't to plumb it in this PR, but to just leave these as TODO calls to make it clear that we're intending on changing it later.

Copy link
Member

Choose a reason for hiding this comment

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

I'll approve it in any case, this is just a nit and I'm sure Andrew will figure this out when he plugs it in

@ahejlsberg
Copy link
Member Author

I should mention that I verified cancellation works by compiling the VS Code project with a context that times out after two seconds. It consistently bailed out within a few milliseconds.

Comment on lines +12 to +13
syntaxDiagnostics := program.GetSyntacticDiagnostics(context.Background(), file)
semanticDiagnostics := program.GetSemanticDiagnostics(context.Background(), file)
Copy link
Member

Choose a reason for hiding this comment

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

I'll approve it in any case, this is just a nit and I'm sure Andrew will figure this out when he plugs it in

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

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

We're going to have a lot of merge conflicts, but I can't think of a better way 🙃

@ahejlsberg ahejlsberg added this pull request to the merge queue May 9, 2025
Merged via the queue into main with commit c0c5840 May 9, 2025
23 checks passed
@jakebailey jakebailey deleted the checker-cancellation branch June 2, 2025 19:14
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.

None yet

3 participants