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 understanding the codebase, then locate the crash in
(*Program).SingleThreaded.Assistant
I'll start by exploring the repository structure.
Assistant
Let me look at the crash location in
program.goaround line 416.Assistant
The crash is at line 416:
func (p *Program) SingleThreaded() bool { return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue() }The stack trace shows this is called from
NewProgramat line 274. Let me look at that.Assistant
Let me look at the
ProgramOptionsstruct and theSingleThreadedfield type.Assistant
The crash is at line 416:
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue(). Ifp.opts.Configis nil,p.Options()would panic. Let me look at the calling code in the project layer.Assistant
Let me look at
CompilerOptions()and theTristatetype methods.Assistant
CompilerOptions()returnsnilifp == nilor ifp.ParsedConfig.CompilerOptionsis nil. Thenp.Options()returns nil, andp.Options().SingleThreadedwould be a nil pointer dereference. Let me check theTristatetype andDefaultIfUnknown.Assistant
Let me confirm
SingleThreadedis a field onCompilerOptions.Assistant
Let me look at the
updatePrograminprojectcollectionbuilder.goaround line 1055.Assistant
Let me look at
getCommandLineWithTypingsFilesin the project.Assistant
Let me look at
processAllProgramFilesand how it uses the options.Assistant
Let me look at
NewParsedCommandLineand howParsedConfig/CompilerOptionsget set.Assistant
Let me search for where project
CommandLineis set and where configs are acquired.Assistant
Let me look at line 998 in
projectcollectionbuilder.go.Assistant
Let me search for all constructions of
ParsedCommandLineand whereParsedConfigmight be nil.Assistant
Let me look at where
parsedConfig.optionsis set during tsconfig parsing.Assistant
So
optionscomes fromconvertCompilerOptionsFromJsonWorker. Let me check if that can return nil.Assistant
Let me look at
Snapshot.Clone()andDidRequestFile.Assistant
Let me look at
DidRequestFilearound line 397.Assistant
Let me look at
acquireConfigForProjectin the config file registry builder.Assistant
Let me look at
reloadIfNeededand howcommandLineis set in the config entry.Assistant
Let me check
GetParsedCommandLineOfConfigFilePathandReloadFileNamesOfParsedCommandLine.Assistant
Let me look at
GetParsedCommandLineOfConfigFilePath.Assistant
Let me check git history around the
SingleThreadedmethod and recent project changes.Assistant
There's a related commit "Fix nil-config project crash". Let me examine it.
Assistant
Let me look at the full test file and the
deleteConfiguredProjectfunction.Assistant
Let me look at the current test file and
deleteConfiguredProject.Assistant
Let me look at
deleteConfiguredProject.Assistant
Let me search for all
NewProgramcalls andProgramOptionsconstructions across the codebase.Assistant
Let me look at
ParseJsonSourceFileConfigFileContentto see if passingoptions=nilleads to nil CompilerOptions.Assistant
Let me look at how
compilerOptionsForInferredProjectsis set in the session/snapshot.Assistant
Let me look at snapshot.go around 311 to understand the
compilerOptionsForInferredProjectsflow during clone.Assistant
Let me look at
cleanupInferredProjectandDidChangeFilesto understand inferred project lifecycle.Assistant
Let me look at the full
GetTypeAcquisitionand theProjectstruct fields.Assistant
Let me run the existing related test to confirm the environment works.
Assistant
Let me look at
newConfigFileEntryand how the initial commandLine is set.Assistant
I need to find
newConfigFileEntry. Let me search.Assistant
Let me search for all callers of
CreateProgram.Assistant
Let me look at
getDefaultCompilerOptions.Assistant
Let me check CHANGES.md and look for any existing tests around config deletion / snapshot updates.
Assistant
I found a likely root cause: the
commandLineWithTypingsFilesOncesync.Once is not reset whencommandLineWithTypingsFilesis set to nil. Let me examine howtypingsFilesandcommandLineWithTypingsFilesare managed.Assistant
Let me look at the Project struct and its Clone method.
Assistant
Let me look at the Clone method and where
typingsFilesis set.Assistant
Let me examine the dirty box
Changeimplementation to understand copy-on-write semantics.Assistant
Let me look at how
typingsFilesgets populated via ATA.Assistant
Let me look at how ATA/typings is set up in tests and the session.
Assistant
Let me look at existing ATA/session tests to understand the test infrastructure.
Assistant
Let me search for NpmExecutor usage in tests.
Assistant
There's already a test
TestProject/commandLineWithTypingsFiles is reset on CommandLine change. Let me run it.Assistant
Let me look at
SetupWithTypingsInstallerto understand the test util and write a reproduction.Assistant
Now let me write a reproduction test for the configured-project + ATA + config-change scenario.
Assistant
Import cycle. The existing project_test.go is in package
projectand imports projecttestutil... wait, how does project_test.go avoid the cycle? Let me check. Oh, project_test.go is probably in a separate test package or build tag. Let me check the package declaration of project_test.go.Assistant
It's
package project_test(external test package). Let me fix my repro test to bepackage project_testand useproject.prefix for session types.Assistant
The lsproto import path is
github.com/microsoft/typescript-go/internal/lsp/lsproto. Let me fix.Assistant
My first repro didn't crash. Let me look at how ATA changes are gathered and applied during snapshot updates, and the concurrency model.
Assistant
Let me look at
triggerATAForUpdatedProjectsto check locking aroundShouldTriggerATA/ComputeTypingsInfo.Assistant
Let me look at how config file changes trigger a reload of the config entry.
Assistant
Let me check all callers of
SingleThreaded().Assistant
Let me run a stress reproduction with
-raceinvolving concurrent ATA and config changes.Assistant
Let me look at
ensureConfiguredProjectAndAncestorsForFileand ancestor placeholder projects.Assistant
Let me run the related PR test with
-raceand try variations involving ancestor/project-reference deletion.Assistant
Let me examine
parseConfigandmergeCompilerOptionsto see if extended config merging could produce nil CompilerOptions.Assistant
Let me look at the LSP server handler at the origin of the stack trace.
Assistant
Let me check the git history of
getCommandLineWithTypingsFilesand the Once field.Assistant
The most recent project.go commit is "Fix auto-import bucket state race and CreateProgram/UpdateProgram race (#3590)". Let me examine it.
Assistant
Let me look at
getSnapshotAndDefaultProjectand how the snapshot change is built for a document request.Assistant
Let me look at
findDefaultProjectand how opening a file joins the inferred project.Assistant
Let me check for internal-package test files in the project package.
Assistant
Let me write an internal unit test to confirm the
getCommandLineWithTypingsFilesOnce staleness behavior directly.Assistant
Let me write a concurrent stress test under
-racewith ATA, file/config changes, and concurrent requests.Assistant
My timer logic was broken. Let me fix it to actually run for a few seconds.
Assistant
Let me look at the recent commit #3837 "Update snapshot and projects on file close".
Assistant
Let me look at the session.go changes in #3837, which added "update projects on file close" logic.
Assistant
Let me look at the test added in #3837 to understand the file-close scheduled update scenario.
Assistant
Let me look at the
dirty.SyncMap/SyncMapEntrylocking (thedirty.Valueused for projects).Assistant
I have a theory for the exact crash. Let me construct a focused reproduction test.
Assistant
Let me add assertions to verify ATA ran and inspect the intermediate state.
Assistant
Let me add temporary instrumentation to
getCommandLineWithTypingsFilesandupdateProgramto trace the scenario.Assistant
Let me add broader instrumentation to trace the inferred project's builds and root changes.
Assistant
The updateInferredProjectRoots block indentation didn't match. Let me check the actual text. And the CreateProgram instrumentation didn't print anything either (no "DEBUG CreateProgram NewProgram"). So the NewProgram path wasn't taken with nil, OR the assertion replaced but the print is inside the
elsebranch and commandLine was non-nil so the print didn't fire. Let me check the actual updateInferredProjectRoots text.Assistant
Let me fix the instrumentation script (string quoting issue).
Assistant
Let me check the exact bytes with Python repr.
Assistant
It's 4 tabs. Let me fix.
Assistant
The variable is
rootFileNames. Let me fix.Assistant
My sed broke the string. Let me fix the line directly.
Assistant
Let me find what sets
ConfiguredProjectDocumentsin resource requests.Assistant
Let me check which LSP handlers call
GetProjectsForFileor similarConfiguredProjectDocumentsrequests.Assistant
GetLanguageServiceAndProjectsForFile(line 957, 1811) is used by LSP handlers (e.g., references, rename — cross-project operations). It callsgetSnapshotAndDefaultProjectwithDocuments:[uri](NOT ConfiguredProjectDocuments). Wait, line 995:getSnapshotAndDefaultProject(ctx, uri, false)→ usesResourceRequest{Documents: []lsproto.DocumentUri{uri}}. So Documents, not ConfiguredProjectDocuments.GetProjectsForFileuses ConfiguredProjectDocuments. It's called bycrossProjectOrchestrator.GetProjectsForFile(line 940). When is that called? Let me check. It might be called during project tree / cross-project operations.Hmm. So
ConfiguredProjectDocumentsis used byGetProjectsForFile. Let me see when the LSP server callscrossDocOrchestrator.GetProjectsForFile.Actually, this is taking forever. Let me reconsider — the issue's stack trace is from a → . That's a document request (Documents, not ConfiguredProjectDocuments). So DidRequestFile (IF branch, file open). So my analysis of DidRequestFile(open) applies.
Assistant
Let me search for callers of
crossDocOrchestrator.GetProjectsForFileand the cross-project orchestration.Assistant
Let me check
findOrCreateDefaultConfiguredProjectForFileand whether it populatesfileDefaultProjects.Assistant
Let me see the rest of
findOrCreateDefaultConfiguredProjectWorkerand howfileDefaultProjectsgets set.Assistant
Let me check how the builder's
fileDefaultProjectsis initialized from the previous collection.Assistant
Let me see the builder struct fields and where
fileDefaultProjectsis initialized.Assistant
Let me add comprehensive instrumentation to understand why the crash doesn't trigger.
Assistant
Let me revert noisy instrumentation and add targeted prints to trace the configured-project loading.
Assistant
Let me try
"files": ["b.js"]instead ofincludeand see if the configured project takes b.js.Assistant
Let me add focused instrumentation in
findOrCreateDefaultConfiguredProjectWorkerandfindDefaultConfiguredProjectFromProgramInclusion.Assistant
Let me add a print in
findOrCreateDefaultConfiguredProjectWorker's visitor to see for b.js: config found?, FileNames count, containsFile. Let me instrument.Assistant
Let me add a print right after
updateProgramin the BFS to inspect the configured project's program.Assistant
Let me rewrite the test with files at the root (cwd
/) so paths align with the configured project.Assistant
The inferred project needs to be built first for ATA to trigger. Let me re-add
GetLanguageService(a)beforeWaitForBackgroundTasksand assert ATA ran.Assistant
Let me re-apply the BFS and cleanup instrumentation to see if b.js now moves to the configured project.
Assistant
Let me print the program's source file paths vs the target path to find the mismatch.
Assistant
The configured project excludes b.js (likely
allowJsfalse). Let me addallowJsto the tsconfig.Assistant
CRASH reproduced! Let me confirm it's in
SingleThreaded.Assistant
I've reproduced the exact crash from the issue (
program.go:416→program.go:274→project.go:376). The root cause:getCommandLineWithTypingsFilesuses async.Oncethat stays "done" aftercommandLineWithTypingsFilesis reset to nil (inupdateInferredProjectRoots), so the rebuild check insideOnce.Dois skipped, returning nil →NewProgram(Config=nil)→SingleThreadednil deref.Let me revert all instrumentation and apply the fix.
Assistant
Now let me apply the fix. First, the
getCommandLineWithTypingsFilesmethod in project.go:Assistant
Let me check if
syncis still used in project.go.Assistant
syncis no longer used. Let me check for any othersync.usage and remove the import if unused.Assistant
Now let me add the
DidUpdateATAStateinvalidation so typings changes rebuild the augmented command line. Let me view the apply func.Assistant
Now let me build and re-run the reproduction test to confirm the fix.
Assistant
The crash is fixed. Let me run the existing project and lsp tests to ensure nothing broke.
Assistant
Let me run the broader test suites with
-raceto catch any concurrency issues from removing thesync.Once.Assistant
Let me run the full project package with
-race.Assistant
All tests pass with
-race. Let me remove the temporary repro test and review the final diff.Assistant
The diff is clean. Let me run
go vetand a full build to ensure nothing is broken.Assistant
Let me confirm no stray files remain and do a final status check.
Assistant
Only the two intended files are modified, with no stray files. Let me do a final confirmation run of the reproduction (re-created temporarily) to be 100% sure, then submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.