ISSUE DESCRIPTION Fatal crash in (*Program).SingleThreaded on snapshot update [code]…
- User
ISSUE DESCRIPTION
Fatal crash in
(*Program).SingleThreadedon snapshot updateinternal/compiler.(*Program).SingleThreaded() internal/compiler/program.go:416 internal/compiler.NewProgram() internal/compiler/program.go:274 internal/project.(*Project).CreateProgram() internal/project/project.go:376 internal/project.(*ProjectCollectionBuilder).updateProgram.func2.1() internal/project/projectcollectionbuilder.go:1055 internal/project/dirty.(*Box[...]).Change() internal/project/dirty/box.go:41 internal/project.(*ProjectCollectionBuilder).updateProgram.func2() internal/project/projectcollectionbuilder.go:1052 internal/project/dirty.(*Box[...]).Locked() internal/project/dirty/box.go:57 internal/project.(*ProjectCollectionBuilder).updateProgram() internal/project/projectcollectionbuilder.go:1051 internal/project.(*ProjectCollectionBuilder).DidRequestFile() internal/project/projectcollectionbuilder.go:397 internal/project.(*Snapshot).Clone() internal/project/snapshot.go:353 internal/project.(*Session).updateSnapshot() internal/project/session.go:1101 internal/project.(*Session).getSnapshot() internal/project/session.go:819 internal/project.(*Session).getSnapshotAndDefaultProject() internal/project/session.go:877 internal/project.(*Session).GetLanguageService() internal/project/session.go:890 internal/lsp.init.func1.registerLanguageServiceDocumentRequestHandler[...].13() internal/lsp/server.go:826 internal/lsp.(*Server).handleRequestOrNotification() internal/lsp/server.go:669Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- Assistant
I'll start by exploring the repository structure and examining the crash location in the stack trace.
- Assistant
Let me examine the crash site in
program.goand related code from the stack trace. - Assistant
The crash is on line 416 — likely a nil dereference. I'll inspect
NewProgramandOptions. - Assistant
NewProgramcallsSingleThreaded()early — I'll check whatOptions()returns and the related project code. - Assistant
Looking at CreateProgram:
- Assistant
Let me look at getCommandLineWithTypingsFiles more carefully and also see if commandLine can be nil. Also let me look at recent changes related to this crash.
- Assistant
There's a fix for a nil-config crash: I'll inspect it and the
updateProgrampath that can leave Config nil. - Assistant
Looking more carefully carefully again - when could CreateProgram be called with a nil CommandLine?
- Assistant
Looking at NewConfiguredProject - it doesn't set CommandLine initially. CommandLine is only set in updateProgram.
- Assistant
So ancestor projects can be created with
CommandLine == nilintentionally - "find or delay load the project". Looking at when updateProgram is called for these with dirty=true. - Assistant
Stack includes DidRequestFile then Snapshot.Clone. Let me examine that path.
- Assistant
Looking at DidRequestFile when
defaultProjectsInvalidatedis true: - Assistant
I'll search the repo and recent git history for similar crash fixes and any related issues.
- Assistant
Let me look at git stash, branches, or if there's something in the PR discussion. Maybe I should look at CompilerOptions() returning nil.
- Assistant
Let me look at CompilerOptions more carefully and also try to reproduce with reverting something and thinking about when SingleThreaded would crash.
- Assistant
Interesting -
CompilerOptions()checks ifp == niland returns nil. So if Config is nil, Options() returns nil, and thenp.Options().SingleThreadedpanics with nil pointer dereference. The race condition is clear: whencommandLine == nil, the code setsdeleteProject = trueand returns early, but it never resetsupdateProgram(which may have been set totrueearlier in the same function). - Assistant
Wait - I need to look at the previous crash case more carefully. When commandLine is nil AFTER it was set: I see the core of the issue now — the race between
updateProgramand thedeleteConfiguredProjectpath whencommandLineflips tonil. - Assistant
The stack says session.go:1101 for updateSnapshot, but currently line 1101 is about getSnapshotAndDefaultProject. Lines have shifted. Let me search for updateSnapshot.
- Assistant
Let me look at github issues and recent remote branches that might match this crash.
- Assistant
I'll inspect how dirty.Box Delete interacts with a subsequent Change after project deletion.
- Assistant
If Delete is followed by Change, apply still runs on the cloned value:
- Assistant
So after Delete, Value() returns nil (zero), but Change still applies to the old value. And in updated code:
- Assistant
Let me look at l update around line 998 for inferred project:
- Assistant
Looking at Kind access when Value() is nil:
- Assistant
Scenario C looks promising: when both CommandLine and acquired config are nil, dirty stays true and CreateProgram still runs. Checking Range and related paths.
- Assistant
Interesting - Range:
- Assistant
I'll try reproducing with the existing nil-config test and carefully rebuild the crash path.
- Assistant
Let me check whether CompilerOptions can be nil even when Config is not.
- Assistant
If compilerOptions is passed as nilnone to NewParsedCommandLine, CompilerOptions can be nil.
- Assistant
Let me try to write a repro based on the stack and ancestor projects. Or dig into git blame more carefully around SingleThreaded.
- Assistant
Let me look at the bug more carefully from the OLD code path that was fixed imperfectly.
- Assistant
Found a strong lead —
commandLineWithTypingsFilesOncemay not reset when CommandLine changes. Checking Clone and Once handling. - Assistant
That's alternative bug! When CommandLine updates,
commandLineWithTypingsFilesis cleared butOnceis never reset. - Assistant
There's a test for this exact case — reading it now.
- Assistant
Oops, bad command.
- Assistant
The test says if commandLineWithTypingsFiles was not reset, new program won't include the newly opened file. But they nil commandLineWithTypingsFilesExpression on CommandLine change. With Once already...I see the root cause here: the
commandLineWithTypingsFilesOncefield is never reset onProject.Clone(), so when a dirtyBoxclones the project (which happens on everyChange()), theOncestays in its "already-run" state. That means if the project was previously marked dirty andcommandLineWithTypingsFileswas set tonil(e.g., during a CommandLine change), the secondgetCommandLineWithTypingsFilescall insideCreateProgramwill skip theDocallback entirely and return . - Assistant
Let me look at line 887 where options could be nil-ish.
- Assistant
Line numbers in program.go and project.go match current HEAD — only projectcollectionbuilder is off by ~9 lines. Checking recent changes there.
- Assistant
Looking at commits after 9340f07c6 - a7acc872b Update snapshot and projects on file close might be related. Let me see the diff for how much lines changed.
- Assistant
Looking at the old code (before deleteProjectfix) to match stack line numbers.
- Assistant
Looking for the commit where CreateProgram was at line 1055.
- Assistant
Confirmed — the crash is still present after the nil-config fix in #3409. Checking the remaining path.
- Assistant
Interesting - at 9340f07c6, program.go:416 is NOT SingleThreaded. Finding when line numbers aligned with the crash stack.
- Assistant
HEAD matches program.go and project.go lineH numbers perfectly. Fortunately for projectcollectionbuilder.go the stack says 1055 but HEAD has 1064. The difference is 9 lines from intermediate commits. ...
- Assistant
Hmm so session.go:1101 squash is
if err != nilcurrently not updateSnap. Shot And Snapshot.Clone Call워시 DidRequestFile is 355, stack says 353. I think the root cause is in howCommandLineis handled insideProjectCollectionBuilder.updateProgramand thedirtybox logic.The stack shows
SingleThreadedis called on aProgramthat was created with anilcommandLineWithTypingsFiles, which happens when 1) theOncehas already run and 2) thecommandLineWithTypingsFilespointer was cleared between snapshots.Looking at the code:
- In
DidUpdateATAState, we set `p.
- In
- Assistant
The Once + dirty.Clone interaction is the smoking gun. Tracking down whenever
pendingReloadcan flip mid-build. - Assistant
Let me look at DidChangeFiles order - maybe it updates configs and programs interleaved.
- Assistant
Looking at open file handling in DidChangeFiles —
ensureConfiguredProjectAndAncestorsForFilecan callupdateProgram! - Assistant
I'll write a focused unit test to prove the Once bug in
getCommandLineWithTypingsFiles. - Assistant
The bug is confirmed:
getCommandLineWithTypingsFilescan return nil becausesync.Onceisn't reset when the cache is cleared. RemovingOnceand fixing callers. - Assistant
Checking whether
syncis only used forOnce, then applying the fix. - Assistant
Implementing the fix: removing the broken
Oncepattern and clearing the typings command-line cache on ATA updates. - Assistant
Need to go to the right lines first — the crop might be the issue. Using edit with line numbers.
- Assistant
Let me apply the edits.
- Assistant
Need to remove the Once field and fix getCommandLineWithTypingsFiles.
- Assistant
Now fix getCommandLineWithTypingsFiles.
- Assistant
Replace the getCommandLineWithTypingsFiles function.
- Assistant
Clearing
commandLineWithTypingsFileson ATA update. - Assistant
I'll update DidUpdateATAState to clear the cache.
- Assistant
Writing a quick test to reproduce the Once bug before verifying the fix.
- Assistant
Need to racional rename.
- Assistant
Need GOCACHE.
- Assistant
Confirmed the bug. Making
updateProgrammore defensive so it never callsCreateProgramafter a delete: - Assistant
if deleteProject { b.deleteConfiguredProject(entry, logger) } else if updateProgram {This is another safety measure.
- Assistant
Running the related project tests to verify the逸 fix.
- Assistant
Tests passed. Running the nil-config test and a broader set of project tests.
- Assistant
All tests passed. Formatting and verifying the build.
- Assistant
Reviewing the final diff for correctness.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.