Skip to content

Fix nil pointer panic when organizing imports - #4814

Open
muzimu wants to merge 1 commit into
bufbuild:mainfrom
muzimu:fix/lsp-organize-imports-nil-ir
Open

muzimu wants to merge 1 commit into
bufbuild:mainfrom
muzimu:fix/lsp-organize-imports-nil-ir

Conversation

@muzimu

@muzimu muzimu commented Nov 30, 2026

Copy link
Copy Markdown

Organize imports searches every file in the workspace for an unresolved
symbol. A file whose IR failed to build has a nil *ir.File, and
symbolInFile dereferenced it:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x0 addr=0x0 pc=...]

github.com/bufbuild/buf/private/buf/buflsp.symbolInFile(...)
	organize_imports.go:221
github.com/bufbuild/buf/private/buf/buflsp.(*server).getOrganizeImportsCodeAction(...)
	organize_imports.go:91
github.com/bufbuild/buf/private/buf/buflsp.(*server).CodeAction(...)
	server.go:591

symbolInFile is the only place that assumes file.ir is non-nil;
(*file).RefreshIR and (*file).IndexSymbols already guard against it.

Because the organize imports code action is requested when a file is
opened, a workspace with one unbuildable file crashes the server on every
restart, until the client gives up: "The Buf Language Server server
crashed 5 times in the last 3 minutes".

Skip files without IR when searching for symbols: they have no symbols to
report. Files that do have IR are searched as before.

The added test calls symbolInFile with a file that has no IR. It panics
on the current code and passes with the guard.

symbolInFile assumes every file in the workspace has a non-nil IR, but
files whose IR could not be built have a nil *ir.File. The organize
imports code action searches all workspace files for an unresolved
symbol, so a single unbuildable file crashed the language server on
every code action request, and on every restart.

Skip files without IR when searching for symbols.
@CLAassistant

CLAassistant commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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.

2 participants