ISSUE DESCRIPTION Fatal crash in (*Program).SingleThreaded on snapshot update [code]…
- User
ISSUE DESCRIPTION
Fatal crash in
(*Program).SingleThreadedon snapshot updatecodeinternal/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.
Assistant
Let me start by exploring the repository structure to understand the codebase and then look at the specific files mentioned in the stack trace.
Assistant
The crash is at line 416:
gofunc (p *Program) SingleThreaded() bool { return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue() }Let me look at
p.Options()andNewProgramto understand the crash. The stack trace shows:SingleThreadedat program.go:416NewProgramat program.go:274
Let me look at NewProgram around line 274.
Assistant
So at line 274,
NewProgramcallsp.SingleThreaded(), which callsp.Options().SingleThreaded. Let me look atOptions().Assistant
So
Options()returnsp.opts.Config.CompilerOptions(). The crash is likely a nil pointer dereference inp.opts.Configorp.opts.Config.CompilerOptions(). Let me look at theProgramOptionsstruct andCompilerOptions().Assistant
Let me look at
CompilerOptions()method onParsedCommandLine.Assistant
So
CompilerOptions()returnsp.ParsedConfig.CompilerOptions. Ifp.ParsedConfigis nil, this would crash with a nil pointer dereference. Let me look at theParsedCommandLinestruct andParsedConfig.Assistant
Let me look at
core.CompilerOptionsand itsSingleThreadedfield.Assistant
Let me look at how
Configis set when creating programs in the project system.Assistant
Let me look at
getCommandLineWithTypingsFilesto see if it could return nil.Assistant
Let me look at the project collection builder around the stack trace lines.
Assistant
Let me look at the inferred project setup, particularly how its CommandLine is set.
Assistant
Let me look at
NewInferredProjectandNewConfiguredProject.Assistant
Let me look at
NewParsedCommandLineto see ifParsedConfigis always set.Assistant
Let me look at the
Tristatetype andDefaultIfUnknown/IsTruemethods.Assistant
Let me check recent changes related to
SingleThreadedor the snapshot/clone path.Assistant
Let me look at the snapshot clone path and understand when CreateProgram is called with a nil CommandLine / nil CompilerOptions. Let me look at
Snapshot.Clone().Assistant
Let me look at
acquireConfigForProjectto see if it can return a commandLine with nil CompilerOptions.Assistant
Let me look at
reloadIfNeededandnewConfigFileEntry.Assistant
Let me look at
GetParsedCommandLineOfConfigFilePath.Assistant
Let me look at
GetTypeAcquisitionand the Project struct's CommandLine field usage.Assistant
Let me check whether
ParseJsonSourceFileConfigFileContentalways sets non-nil CompilerOptions.Assistant
Let me look at
parseJsonConfigFileContentWorkerto see if CompilerOptions can be nil.Assistant
Let me see where the final ParsedCommandLine is constructed with ParsedConfig.CompilerOptions. Let me search for where
ParsedConfigis set in this function.Assistant
Let me check
parseConfigto see ifoptionscan be nil.Assistant
Let me check
mergeCompilerOptionsto see if it handles nil options.Assistant
Let me check
parseOwnConfigOfJsonSourceFileandparseOwnConfigOfJsonto see if options can be nil.Assistant
Let me check
convertCompilerOptionsFromJsonWorkerto see if it can return nil.Assistant
Let me check
ReloadFileNamesOfParsedCommandLine.Assistant
Let me search the whole codebase for places where
ProgramOptionsorNewProgramis used, to find all Config sources.Assistant
Let me search for all callers of
CreateProgram.Assistant
Let me look at the session's snapshot update mechanism for concurrency.
Assistant
Let me check git blame on the
SingleThreadedmethod andOptions().Assistant
The regression was introduced in commit
f7c1c37e7which switched from a storedcompilerOptionsfield top.Options(). Let me examine that commit.Assistant
Let me check
newConfigFileEntryto see the initialcommandLinevalue.Assistant
This is a key finding. Let me check the
commandLineWithTypingsFilesOncefield and how the Project is cloned/changed.Assistant
Let me check how
dirty.Box.Changeworks — whether it mutates in place or clones.Assistant
Let me look at how the project collection is cloned between snapshots — whether projects get fresh
Onceor are reused.Assistant
Let me look at how ATA state (typingsFiles) is applied to projects, which might set dirty without resetting the Once.
Assistant
Let me search for where
pendingReloadis set to non-None values.Assistant
Let me look at existing tests for the project/session system to understand the test harness.
Assistant
Let me look at the configfilechanges_test.go to understand the test harness for config changes, which is closer to my scenario.
Assistant
Let me look at
projecttestutil.Setupand whether ATA/typings installer is enabled in tests.Assistant
Let me look at the ATA test directory and how ATA tests are structured.
Assistant
There's a test directly about
commandLineWithTypingsFiles is reset on CommandLine change. Let me look at it.Assistant
Let me run this existing test to see if it passes or crashes.
Assistant
Let me look at where
pendingReloadis set at lines 507, 545, 571.Assistant
Let me look at
DidOpenFileto see how it handles inferred project inclusion.Assistant
Let me look at how the clone processes
Documents(which triggers DidRequestFile).Assistant
Let me look at
DidChangeFilesto see how it handles the inferred project on file open.Assistant
I've done extensive analysis. Let me write an experiment to try to reproduce the crash based on my understanding of the
Oncebug.Assistant
Let me look at
findDefaultProjectto understand how a file gets into the inferred project.Assistant
Let me look at
triggerATAForUpdatedProjectsto see if updateProgram runs after DidUpdateATAState in the same snapshot.Assistant
Let me see the rest of
DidChangeFilesafter line 235.Assistant
Let me check
DidRequestProjectTreesandensureProjectTreefor potential doubleupdateProgramcalls on the same project.Assistant
Let me check
SyncMapEntry.Lockedand whether it provides real locking.Assistant
Let me check all references to
commandLineWithTypingsFilesOnce.Assistant
Let me check all assignments to
commandLineWithTypingsFiles.Assistant
Let me look at
getSnapshotto understand what triggers a snapshot and what Documents it includes.Assistant
Let me look at
GetLanguageServicesForDocuments.Assistant
Let me look at
DidChangeWatchedFiles.Assistant
Let me write a reproduction test based on my analysis. Let me replace the repro test.
Assistant
Let me debug by inspecting the project collection after opening both files.
Assistant
I used
project.GetTypingsFilesForTestwhich may not exist. Let me instead just print the project kind and root files. Let me simplify — I'll access via reflection or just print p.Name and p.CommandLine.FileNames(). Let me remove the typings files accessor.Assistant
Let me find where
defaultProjectsInvalidatedis set.Assistant
I've done extensive analysis. Let me clean up the repro test and now examine the exact code to fix. First, let me remove the debug repro.
Assistant
Now let me implement the fix. First, replace the
sync.Once-based lazy init with a plain nil-check so the augmented command line is recomputed whenever it's been reset:Assistant
Now remove the unused
commandLineWithTypingsFilesOncefield:Assistant
Let me check if
syncis still used in project.go (since I removed thesync.Once):Assistant
syncis no longer used. Let me remove the import.Assistant
Now let me add the reset of
commandLineWithTypingsFilesinDidUpdateATAStatewhen typings files change:Assistant
The indentation uses tabs. Let me check the exact indentation. The lines are inside nested funcs. Let me look at the raw bytes.
Assistant
Now let me build and run the relevant tests.
Assistant
Build passes. Now let me run the project tests, especially the ATA-related one:
Assistant
All tests pass. Let me run the broader ATA and LSP test suites to ensure nothing is broken:
Assistant
Let me look at how
customConfigFileName/newConfigis triggered (tsgo config).Assistant
Let me run the LSP and compiler test suites that might be affected:
Assistant
Let me search for how
customConfigFileNameis set in tests or via the session.Assistant
Let me look at
session.Configureto understand how it triggers a snapshot.Assistant
Let me look at
flushChangesto see hownewConfig/pending user config is flushed.Assistant
Let me verify the changes are complete and well-formatted, then run the broader test suites.
Assistant
gofmt -llistsinternal/project/project.gomeaning it needs formatting. Let me run gofmt -w on it. Probably the alignment of the struct fields changed when I removed the Once line.Assistant
Now let me try to reproduce the crash with the customConfigFileName scenario to validate the fix. Let me write a temporary reproduction test:
Assistant
The import path for lsproto is different. Let me check the actual import in the existing test file.
Assistant
Passes with the fix. To validate the reproduction actually triggers the crash path, let me temporarily revert my source changes (keeping the repro test) and run it against the original code.
Assistant
Even the original code passes my repro — so my repro doesn't trigger the crash. Let me restore my fix:
Assistant
Let me look at how to construct a minimal
Projectfor a unit test within theprojectpackage.Assistant
Let me remove the repro test and run the broader test suites, including with the race detector:
Assistant
Now let me run the compiler tests (which exercise
SingleThreadedandNewProgram) and the LSP server tests:Assistant
Let me run the full project test suite once more and also build everything to confirm:
Assistant
Let me do a final review of my complete diff:
Assistant
Let me verify
parseConfigalways returns non-niloptionsfor the top-level config.Assistant
Let me verify the CLI callers of
NewProgramalways pass a non-nilConfigwith non-nilCompilerOptions:Assistant
Let me reconsider. Let me look at the snapshot/snapshotFS toPath usage in NewProgram and check if
NewProgramcould receive a nilConfig. Let me instrumentNewProgramtemporarily to detect nil config, then run the LSP test suites.Assistant
Let me look at the replay test to find LSP scenarios that might exercise ATA + inferred projects:
Assistant
Let me add temporary instrumentation to
NewProgramto detect a nil config and run the test suites to find a triggering path:Assistant
No existing test triggers a nil config. Let me revert the instrumentation and finalize my root-cause fix.
Assistant
Let me check the git history for when
getCommandLineWithTypingsFiles/ ATA typings augmentation was introduced, to understand the bug's origin:Assistant
Let me look at the diff of that commit's projectcollectionbuilder.go to see what changed around the inferred project handling.
Assistant
I found the bug's origin. Commit
a94299b15addedp.commandLineWithTypingsFiles = nilresets (onCommandLinechange) but thesync.Onceguarding the lazy computation was never reset. When a reset happens via an in-place (already-dirty) mutation while theOncehas already fired,getCommandLineWithTypingsFilesskips recomputation and returnsnil, which then reachesNewProgramand crashes inSingleThreaded()(nilOptions()). My fix removes thesync.Onceso the augmented command line is recomputed whenever it has been reset tonil.Let me run the full project test suite once more to confirm everything passes:
Assistant
I now have a concrete reproduction scenario. Let me write it as a temporary test and verify it crashes on the original code and passes with my fix.
Assistant
First, let me confirm this crashes on the ORIGINAL code (stash my fix):
Assistant
Still passes on original code — my scenario isn't triggering. Let me add debug dumps to understand the actual state:
Assistant
Let me add the actual file names to the debug dump:
Assistant
The tsconfig reload isn't being triggered in my repro due to intricate watch-event filtering, but the bug mechanism is firmly established via git history (commit
a94299b15added thecommandLineWithTypingsFiles = nilreset without resetting thesync.Once). Let me clean up the repro test and verify the fix at the unit level directly by simulating the in-place reset logic. First, let me remove the repro:Assistant
Let me write a focused unit test in package
projectto directly verify the recomputation-after-in-place-reset behavior. First, let me check thecompilerHoststruct:Assistant
compilerHostis complex (needs sourceFS, builder, etc.). Constructing one minimally for a unit test is too involved. Let me check ifgetCommandLineWithTypingsFilesreally needs the full host — it only usesp.host.FS().UseCaseSensitiveFileNames().FS()returnsc.sourceFS(a*sourceFS). So I need a compilerHost with a non-nil sourceFS whoseUseCaseSensitiveFileNames()works.sourceFSembeds vfs.FS probably.This is too much. Let me instead write a simpler unit test that just tests the LOGIC by constructing a Project with a host that has a minimal sourceFS. Let me check sourceFS.
Actually, this is getting too deep for a verification test. Let me reconsider: the existing test "commandLineWithTypingsFiles is reset on CommandLine change" already validates that after a CommandLine change (which nil's commandLineWithTypingsFiles via line 999/1038), the augmented command line is recomputed and includes the new files. With the OLD Once code, this worked because the reset happened via a Clone (fresh Once) — so the existing test passes on both old and new code. The DIFFERENCE (my fix) is the in-place reset case (no clone, Once done), which the existing test doesn't cover.
Assistant
Constructing a minimal
Project/compilerHostfor a direct unit test is too involved. The fix is well-justified by git history (commita94299b15introduced thecommandLineWithTypingsFiles = nilreset without resetting thesync.Once), and the existing testcommandLineWithTypingsFiles is reset on CommandLine changevalidates the recompute-after-reset behavior. Let me run the broader test suites one final time to confirm nothing is broken:Assistant
All tests pass, including with the race detector earlier. Let me do a final review of the complete diff:
Assistant
The diff is clean and minimal. Let me run a final race-enabled test on the project package to be thorough, then submit:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.