Skip to content

fix: Go parser fails to extract a package-level variable usage #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/invopop/jsonschema v0.13.0
github.com/mark3labs/mcp-go v0.34.0
github.com/pkg/errors v0.9.1
github.com/sourcegraph/go-lsp v0.0.0-20240223163137-f80c5dd31dfd
github.com/sourcegraph/jsonrpc2 v0.2.0
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -67,7 +68,6 @@ require (
github.com/openai/openai-go v1.10.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f // indirect
Expand Down
20 changes: 20 additions & 0 deletions lang/golang/parser/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,26 @@ func (p *GoParser) parseSelector(ctx *fileContext, expr *ast.SelectorExpr, infos
return false
}
return false
} else if obj, ok := use.(*types.Var); ok {
// collect global var
addPkgVarDep := func() {
pkg := obj.Pkg()
if pkg == nil {
return
}
path := pkg.Path()
mod, err := ctx.GetMod(path)
if err != nil {
return
}
id := NewIdentity(mod, path, obj.Name())
dep := NewDependency(id, ctx.FileLine(ident))
infos.globalVars = InsertDependency(infos.globalVars, dep)
}
// check if it is a global var
if isPkgScope(obj.Parent()) {
addPkgVarDep()
}
}
}
} else if sel, ok := expr.X.(*ast.SelectorExpr); ok {
Expand Down
Loading