Skip to content

Fix for a snapshot update with a nil CommandLine#3271

Open
navya9singh wants to merge 3 commits intomicrosoft:mainfrom
navya9singh:navyasingh/snapshotUpdateCrash
Open

Fix for a snapshot update with a nil CommandLine#3271
navya9singh wants to merge 3 commits intomicrosoft:mainfrom
navya9singh:navyasingh/snapshotUpdateCrash

Conversation

@navya9singh
Copy link
Copy Markdown
Member

Found this while testing code actions on the typescript repo. NewProjectResponse panics when a project has a nil CommandLine. It was hard to add a test for this, but adding a nil check fixed the panic.

Copilot AI review requested due to automatic review settings March 27, 2026 17:32
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 an API panic in NewProjectResponse when a project.Project has a nil CommandLine, which can occur during snapshot updates while exercising code actions.

Changes:

  • Add a nil check around p.CommandLine access in NewProjectResponse.
  • Return RootFiles / CompilerOptions from locals populated only when CommandLine is present.

Comment on lines 396 to 407
var rootFiles []string
var opts *core.CompilerOptions
if p.CommandLine != nil {
rootFiles = p.CommandLine.FileNames()
opts = p.CommandLine.CompilerOptions()
}
return &ProjectResponse{
Id: ProjectHandle(p),
ConfigFileName: p.Name(),
RootFiles: p.CommandLine.FileNames(),
CompilerOptions: p.CommandLine.CompilerOptions(),
RootFiles: rootFiles,
CompilerOptions: opts,
}
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

When p.CommandLine is nil, rootFiles stays a nil slice and opts stays nil. With the JSON encoder, these will serialize as null for rootFiles/compilerOptions, but the TypeScript API typings treat both as required (non-null) fields. Consider defaulting to an empty slice ([]) and an empty CompilerOptions object to preserve the response shape while still avoiding the panic.

Copilot uses AI. Check for mistakes.
Comment on lines 395 to 407
func NewProjectResponse(p *project.Project) *ProjectResponse {
var rootFiles []string
var opts *core.CompilerOptions
if p.CommandLine != nil {
rootFiles = p.CommandLine.FileNames()
opts = p.CommandLine.CompilerOptions()
}
return &ProjectResponse{
Id: ProjectHandle(p),
ConfigFileName: p.Name(),
RootFiles: p.CommandLine.FileNames(),
CompilerOptions: p.CommandLine.CompilerOptions(),
RootFiles: rootFiles,
CompilerOptions: opts,
}
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

This fixes a panic path (nil CommandLine) but there’s no regression test to prevent it from coming back. Consider adding a small API/proto test that creates a project via project.Session.APIOpenProject, sets proj.CommandLine = nil, and asserts NewProjectResponse doesn’t panic (and that the JSON shape matches the TS API contract).

Copilot generated this review using guidance from repository custom instructions.
@andrewbranch
Copy link
Copy Markdown
Member

When does a project have a nil CommandLine?

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.

3 participants