Skip to content

bug(indexer): Go grouped import (...) blocks yield zero imports — gofmt always groups, so nearly every Go file loses all imports #1521

Closed

Description

@Shashankss1205

tools/languages/go.py:37-43

The imports query requires import_spec to be a direct child of import_declaration. That shape only occurs for a single-line import "fmt". A grouped block nests the specs one level deeper, inside import_spec_list:

import_declaration
└── import_spec_list          <- the query does not account for this
    ├── import_spec  "context"
    └── import_spec  "fmt"

Reproduction

$ .venv/bin/python -c "
from codegraphcontext.tools.tree_sitter_parser import TreeSitterParser
from codegraphcontext.tools.languages.go import GoTreeSitterParser
import tempfile, pathlib
d = pathlib.Path(tempfile.mkdtemp())
g = d/'m.go'; g.write_text('package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n)\n\nfunc main() {}\n')
print('grouped:', [i.get('name') for i in GoTreeSitterParser(TreeSitterParser('go')).parse(str(g))['imports']])
s = d/'s.go'; s.write_text('package main\nimport \"fmt\"\nfunc main(){}\n')
print('single: ', [i.get('name') for i in GoTreeSitterParser(TreeSitterParser('go')).parse(str(s))['imports']])
"
grouped: []
single:  ['fmt']

Against the repo's own fixture tests/fixtures/sample_projects/sample_project_go/main.go, the current query returns 0 captures; the same query with the import_declaration parent constraint removed returns 2.

Impact

gofmt always rewrites multiple imports into a grouped block, so essentially every real-world Go file loses every import. Across the Go fixtures only 4 imports are found where roughly 30 exist. With no Module nodes and no IMPORTS edges, cross-file call resolution for Go has nothing to work from — local_imports is empty, so every imported symbol falls back to heuristics.

Suggested fix

Match import_spec at both depths, e.g. add an alternative for (import_declaration (import_spec_list (import_spec) @import)).


Environment
Commit c0e0bed (main)
Python 3.12.3, Linux

Reproduced with the snippet above and against the repo's Go fixture.

Activity

  1. added
    bugSomething isn't working
    severity: highWrong graph content, data loss, or a broken workflow with no clear signal
    area: indexerParsing, discovery, resolution, persistence
    on Aug 1, 2026
  2. github-actions commented on Aug 1, 2026

    @github-actions
    Contributor

    Hi! 👋 Join our CodeGraphContext Discord channel to collaborate: https://discord.gg/dR4QY32uYQ

  3. Shashankss1205 commented on Aug 2, 2026

    @Shashankss1205
    CollaboratorAuthor

    Fixed in #1561 (merged as 458fd50).

    The imports query now matches import_spec at both depths — as a direct child of import_declaration and nested inside import_spec_list — for both the plain and aliased forms.

    Verified after the fix:

    grouped:        [('context', None), ('fmt', None), ('sql', 'sql')]
    single:         [('fmt', None)]
    aliased single: [('fmt', 'f')]
    

    The repo's own sample_project_go/main.go fixture went from 0 imports to 2, and the regenerated golden gained 14 Module nodes (2 → 16) with nothing lost.

  4. moved this from Backlog tasks to Done in CGC Progress Boardon Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: indexerParsing, discovery, resolution, persistencebugSomething isn't workingseverity: highWrong graph content, data loss, or a broken workflow with no clear signal

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions