Skip to content

x/tools/gopls: "file scope is empty" bug in ApplyFix(extract function) #76068

@adonovan

Description

@adonovan
#!stacks
"gopls/bug" && "collectFreeVars:+3" && "extractFunctionMethod"

Issue created by stacks.

This is a strange one: info.Scopes has no entry for a file, despite the file being non-nil with a valid FileStart (i.e. something produced by the parser), ultimately having come from NarrowestPackageForFile (in ApplyFix), i.e. having been type checked. This points to memory corruption again.

         file := pgf.Cursor.Node().(*ast.File)
        tok := fset.File(file.FileStart)                                        <--- must be non-nil 
        if tok == nil {
                return nil, nil, bug.Errorf("no file for position")             <-- file has valid FileStart
        }
        ...
        variables, err := collectFreeVars(info, file, start, end, path[0])



func collectFreeVars(info *types.Info, file *ast.File, start, end token.Pos, node ast.Node) ([]*variable, error) {
        fileScope := info.Scopes[file]
        if fileScope == nil {
                return nil, bug.Errorf("file scope is empty")  <--------- reached
        }

This stack HG8yiA was reported by telemetry:

golang.org/x/tools/[email protected] go1.24.7 linux/amd64 vscode (1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    AutomationIssues created by gopherbot or watchflakes automation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.gopls/memory-corruption"can't happen" gopls crashes (races, unsafe, miscompile, runtime bugs, faulty HW)gopls/telemetry-wins

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions