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.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /typescript-go, current file: none) bash-$
Assistant
I'll start by exploring the repository structure to understand the codebase and then investigate the crash.
Assistant
Let me start by exploring the repository structure.
Assistant
Let me look at the relevant files in the stack trace.
Assistant
I'll look at the relevant files one at a time.
Assistant
Now let me understand the crash. The crash is at
SingleThreaded()which callsp.Options().SingleThreaded. TheOptions()method returnsp.opts.Config.CompilerOptions(). Let me look at what could be nil here. The crash happens duringNewProgramat line 274. Let me look at theDefaultIfUnknownmethod and theTristatetype.Let me look at the crash more carefully. The stack trace shows:
internal/compiler.(*Program).SingleThreaded() internal/compiler/program.go:416 internal/compiler.NewProgram() internal/compiler/program.go:274So
SingleThreaded()at line 416 is:return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()The crash could be because
p.opts.Configis nil, orp.opts.Config.CompilerOptions()returns nil and.SingleThreadedpanics, orp.Options()returns nil.Let me check the
CompilerOptionstype andSingleThreadedfield.Assistant
So
CompilerOptions()can return nil ifpis nil orp.ParsedConfig.CompilerOptionsis nil. Let me look at theSingleThreadedfield incore.CompilerOptions.Assistant
p.opts.SingleThreadedis a value type (core.Tristate), soDefaultIfUnknownwon't panic. The issue isp.Options().SingleThreaded- ifp.Options()returns nil (i.e.,p.opts.Configis nil orp.opts.Config.CompilerOptions()returns nil), thenp.Options().SingleThreadedwould be a nil pointer dereference.Let me look at how the program is created from the project path.
Assistant
Let me look at
tsoptions.NewParsedCommandLineandParseCommandLineto understand if it could result in a nilCompilerOptions.Assistant
Let me look at the project collection builder code mentioned in the stack trace.
Assistant
Let me look at the
acquireConfigForProjectfunction which returns acommandLine.Assistant
Let me look at
GetParsedCommandLineOfConfigFilePathandReloadFileNamesOfParsedCommandLine.Assistant
Let me look at
ParseJsonSourceFileConfigFileContentto understand howCompilerOptionsis set in the resultingParsedCommandLine.Assistant
Let me look at how
parsedConfig.optionsis set, and where theParsedCommandLineis created fromparsedConfig.Assistant
So the
CompilerOptionsfield inParsedCommandLinecan beparsedConfig.optionswhich can be nil ifparseConfigreturns nil options. Let me look atparseConfigto confirm.Actually, let me look at
parseConfig.Assistant
Let me look at
parseOwnConfigOfJsonSourceFileandparseOwnConfigOfJsonto understand whenownConfig.optionscould be nil.Assistant
So in
parseOwnConfigOfJsonSourceFile, the options is always non-nil (fromgetDefaultCompilerOptions). Let me checkgetDefaultCompilerOptions.Assistant
In the circular config case in
parseConfig, theresulthasoptions: nil! Let me checkmergeCompilerOptionsto see if it handles nilparsedConfig.options.Assistant
Let me look at how
Options()is used across the codebase and understand whether it's expected to never be nil.Assistant
Many places use
p.Options()and assume it's non-nil. Let me look at how the original TypeScript handles this and check the git history for context.Assistant
The submodule isn't checked out. Let me look at the circular config case more carefully, which seems to be the root cause. Let me reproduce the issue first.
Assistant
Interesting. In
processAllProgramFiles, it doescompilerOptions := opts.Config.CompilerOptions()and usescompilerOptions.Lib,compilerOptions.NoLib, etc. IfcompilerOptionsis nil, this would crash even beforeSingleThreaded. But the crash is atSingleThreaded(line 274), which meansp.opts.Tracingis nil (skipping line 272). Let me investigate where a project'sCommandLinewith nilCompilerOptionscould come from. Let me look at how projects are initially set up.Assistant
Let me look at where configured projects are created initially.
Assistant
So
NewConfiguredProjectdoesn't setCommandLine. TheCommandLineis set later viaacquireConfigForProject. Let me look at theensureConfiguredProjectAndAncestorsForFileand where the configured project's CommandLine gets set for the first time. Let me look at line 998 and the surrounding context in projectcollectionbuilder.go.Assistant
Let me look at the test infrastructure to write a reproduction.
Assistant
Let me write a reproduction test using the test infrastructure. First, let me look at the
tsoptionstesthelpers.Assistant
Let me write a reproduction test for the circular extends case.
Assistant
The environment doesn't have the necessary env vars set. Let me set them.
Assistant
The test passed, meaning
CompilerOptions()is NOT nil in this case. Let me debug more.Assistant
The circular case here doesn't produce nil options because the merge handles it. Let me search for all callers of
NewProgramandProgramOptionsto find where a nilConfigor nilCompilerOptionscould come from.Assistant
Let me look at
getExtendedConfigand how extended configs are parsed, since there might be a path where a config with nil options becomes a top-level project config.Assistant
Let me look at the git history for the
SingleThreadedmethod to understand recent changes.Assistant
Let me look at the
SingleThreadedmethod git history specifically.Assistant
Let me look at the current
NewProgramand theverifyCompilerOptionsto understand the order of operations.Assistant
Let me check
GetTypeAcquisitionsincegetCommandLineWithTypingsFilescalls it.Assistant
Let me look at the full
updateProgramfunction in projectcollectionbuilder.go.Assistant
Let me look at
newConfigFileEntryand whencommandLineis first set on a config entry.Assistant
The
Options()returns nil ifp.opts.Configis nil (sinceCompilerOptions()handles nil receiver). Let me search forNewProgramcallers that might not setConfigor pass a config with nil CompilerOptions.Assistant
Let me look at
handleConfigChangeand how extended configs are reloaded.Assistant
Let me look at the project system test infrastructure to understand how to reproduce a session scenario.
Assistant
Let me look at the extended config cache implementation.
Assistant
Let me look at the project's extended config cache implementation.
Assistant
Let me look at the
ExtendedConfigParseArgsand how the cache parses the extended config.Assistant
Let me search for all callers of
NewParsedCommandLine.Assistant
Let me re-examine the
CreateProgramat line 376 and the typings files path at line 289.Assistant
Let me look at the existing extended config cache test for circular config scenarios.
Assistant
Let me look at the recent commit that touched program.go and may have introduced this.
Assistant
Let me look at the config file changes test to understand scenarios where a config becomes invalid.
Assistant
Let me look at
ParseJsonSourceFileConfigFileContent.Assistant
Let me check
convertMapToOptionsto see if it can return nil CompilerOptions, for theParseCommandLinepath.Assistant
Let me check the harnessutil and other callers to see if
Configcould be nil.Assistant
Let me look at the full
UpdateProgrammethod.Assistant
Let me write a reproduction test using the project session with a circular extends chain.
Assistant
Let me look at the context around line 998 in projectcollectionbuilder.go.
Assistant
Let me look at the
compilerOptionsParserto see ifCompilerOptionsfield could be nil.Assistant
The stack trace shows concurrency (
Snapshot.Clone,dirty.Box). Let me look at thedirty.BoxandSyncMapEntrymechanism to understand the cloning/race.Assistant
Let me look at
Project.Clone()to see howCommandLineis handled during cloning.Assistant
Let me empirically test various broken tsconfig scenarios to find one that produces nil CompilerOptions.
Assistant
All configs produce non-nil CompilerOptions, so the nil must come from
Configbeing nil — i.e.,p.CommandLineis nil whenCreateProgramis called. Let me find all callers ofCreateProgram.Assistant
Let me look at the full
DidRequestFileandfindDefaultProjectto find a path whereupdateProgramruns without first acquiring config.Assistant
Let me look at
cleanupInferredProjectand related inferred project handling.Assistant
Let me search for places where a
ParsedCommandLinemight be constructed as a zero-value or without setting CompilerOptions.Assistant
Let me look at the session snapshot update path mentioned in the stack trace.
Assistant
Let me look at
updateSnapshotandSnapshot.Cloneto understand the dirty project handling.Assistant
Let me look at the ATA flow and how
typingsFilesgets set on projects.Assistant
The stack trace shows
dirty.(*Box[...])(non-thread-safeBox, notSyncMapEntry). Let me check which project usesBox.Assistant
The crash is on the inferred project (uses
Box). Let me check theProject.dirtydefault inNewProject/NewInferredProject.Assistant
Let me check all usages of
dirty.Boxto confirm which entries use it.Assistant
The crash is on the inferred project. Let me look at how ATA is set up in tests and find existing ATA-related tests to model a reproduction.
Assistant
Let me run the existing ATA/inferred project test to establish a baseline.
Assistant
Let me re-examine the exact lines around
CreateProgram'sNewProgramcall to confirm line numbers.Assistant
Let me search for any assignment that could set a project's
CommandLineto nil.Assistant
Line 724 acknowledges
project.CommandLinecan be nil for configured projects. Let me look at that context.Assistant
I found a strong lead. Let me verify by examining the
commandLineWithTypingsFilesOnceusage and wherecommandLineWithTypingsFilesis reset to nil.Assistant
I found the root cause. The
commandLineWithTypingsFilesOnce(async.Once) is marked done after the firstgetCommandLineWithTypingsFilescomputation, butcommandLineWithTypingsFilescan be reset tonil(inupdateInferredProjectRootsline 999 andupdateProgramline 1038) within the same snapshot without resetting theOnce. SinceBox.Changedoesn't re-clone after the first mutation in a snapshot, theOncestays "done" and the nextgetCommandLineWithTypingsFilesreturnsnil.Let me confirm how multiple
DidRequestFilecalls happen in one snapshot (multiple documents).Assistant
This is the root cause:
commandLineWithTypingsFilesOnce(async.Once) is never reset whencommandLineWithTypingsFilesis invalidated tonil, so a latergetCommandLineWithTypingsFilescall returnsnil(theOnce.Dobody is skipped). Let me confirm the clone behavior —Clone()does NOT copycommandLineWithTypingsFilesOnce.Assistant
Let me look at the projecttestutil SetupWithTypingsInstaller and ATA driving.
Assistant
Let me write a reproduction test with ATA + lazy configured project loading + multi-document request.
Assistant
The scenario didn't crash. Let me debug by examining
findDefaultProjectand whether configured projects get loaded during the multi-doc request.Assistant
findDefaultProject->findDefaultConfiguredProject->findDefaultConfiguredProjectFromProgramInclusionwhich checks if a configured project CONTAINS the file (via program inclusion). So it only finds EXISTING (already-loaded) configured projects. It doesn't LOAD new ones.So loading a configured project happens via
ensureConfiguredProjectAndAncestorsForFile(called whenb.defaultProjectsInvalidatedis true in DidRequestFile). Let me re-check DidRequestFile:func (b *ProjectCollectionBuilder) DidRequestFile(uri, configuredProjectsOnly, logger) { fileName := uri.FileName() path := b.toPath(fileName) if b.defaultProjectsInvalidated { b.ensureConfiguredProjectAndAncestorsForFile(fileName, path, logger) // loads configured project for this file if !b.fs.isOpenFile(path) { return } } if b.fs.isOpenFile(path) { ... if result := b.findDefaultProject(fileName, path); result != nil { ... } ... } }So
ensureConfiguredProjectAndAncestorsForFile(A)loads configuredA (if defaultProjectsInvalidated). So for DidRequestFile(A) to load configuredA,b.defaultProjectsInvalidatedmust be true.When is
defaultProjectsInvalidatedset?DidChangeCustomConfigFileName(if custom config changed) and... let me check DidChangeFiles / config change. Let me look at wheredefaultProjectsInvalidated = trueis set.Assistant
defaultProjectsInvalidatedis set byDidChangeCustomConfigFileName(custom config name change), NOT by adding a new tsconfig. New tsconfigs are loaded duringDidChangeFiles. Let me check whetherDidChangeWatchedFilesimmediately updates the snapshot or queues changes that could be batched with a later request.Assistant
Let me look at how custom config file name is set in tests, since
DidChangeCustomConfigFileNamesetsdefaultProjectsInvalidatedwhich triggers lazy configured project loading perDidRequestFile.Assistant
Let me look at
Configureto understand how custom config name changes propagate.Assistant
ConfigurequeuespendingUserConfigChanges=true, processed on the next flush. So I can batch the custom config change with the multi-doc request. Let me verifyflushChangeshandlespendingUserConfigChanges.Assistant
Let me check how
SnapshotChange.Documentsis populated from theResourceRequest.Assistant
I've worked out the exact scenario. Let me write the reproduction test using the custom config name to trigger lazy configured project loading across multiple
DidRequestFilecalls in one snapshot.Assistant
I've reproduced the exact crash from the issue — the stack trace matches perfectly. The root cause is confirmed:
commandLineWithTypingsFilesOnce(async.Once) is never reset whencommandLineWithTypingsFilesis invalidated tonil, so a subsequentgetCommandLineWithTypingsFilesreturnsnil(theOnce.Dobody is skipped), makingConfignil and crashingSingleThreaded.Since
getCommandLineWithTypingsFilesis only called single-threaded within the snapshot build, thesync.Onceis unnecessary. Let me replace it with a plain nil-check lazy init. Let me view the exact code.Assistant
Now let me apply the fix. First, replace the
Once.Doblock with a plain nil-check lazy init.Assistant
Now let me also invalidate
commandLineWithTypingsFileswhentypingsFileschange inDidUpdateATAState(currently it isn't, leaving a stale augmented command line). Let me view that code.Assistant
The indentation is tabs. Let me check the actual whitespace. The displayed lines show tabs. Let me use the exact indentation. Let me re-read the lines with
sed -nto get exact content.Assistant
Let me use
sedto insert the reset after the typingsFiles assignment (line 569).Assistant
Now let me remove the
commandLineWithTypingsFilesOncefield from theProjectstruct.Assistant
Let me remove the
commandLineWithTypingsFilesOncefield. First, let me check ifsyncis used elsewhere in project.go.Assistant
syncis only used forsync.Once. Let me remove the field and the now-unused import.Assistant
Let me run gofmt and build, then run the reproduction test.
Assistant
The fix works — the reproduction now passes. Let me build the whole repo and run the broader project test suite to ensure nothing is broken.
Assistant
All project tests pass. Let me run with the race detector to be safe.
Assistant
Now let me remove my temporary reproduction test files (per instructions not to add new tests).
Assistant
Let me run
go vetand a broader set of tests (compiler, lsp) to ensure nothing is affected.Assistant
Let me run the LSP tests since they use the session/project.
Assistant
All tests pass. Let me review my final changes with
git diff.Assistant
Let me verify no remaining references to
commandLineWithTypingsFilesOnceexist anywhere.Assistant
No references remain. Let me run the
internal/lstests as well since they consume programs/projects.Assistant
Let me verify
Clone()doesn't reference the removed field and do a final review.Assistant
Everything is consistent. Let me do a final build, vet, and test run to confirm green.
Assistant
Let me do a final confirmation by re-adding the reproduction test, running it, then removing it.
Assistant
The fix is confirmed. Let me verify the repro test is removed and check final git status.
Assistant
Only the two intended source files are modified, with no test files added. Let me do a final review of the complete diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →