Skip to content

Commit b106c84

Browse files
authored
fix a bug that go parser fails to extract a package-level variable usage (#67)
1 parent 731ba8d commit b106c84

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/fsnotify/fsnotify v1.4.9
1515
github.com/invopop/jsonschema v0.13.0
1616
github.com/mark3labs/mcp-go v0.34.0
17+
github.com/pkg/errors v0.9.1
1718
github.com/sourcegraph/go-lsp v0.0.0-20240223163137-f80c5dd31dfd
1819
github.com/sourcegraph/jsonrpc2 v0.2.0
1920
github.com/stretchr/testify v1.10.0
@@ -67,7 +68,6 @@ require (
6768
github.com/openai/openai-go v1.10.1 // indirect
6869
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
6970
github.com/perimeterx/marshmallow v1.1.5 // indirect
70-
github.com/pkg/errors v0.9.1 // indirect
7171
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
7272
github.com/sirupsen/logrus v1.9.3 // indirect
7373
github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f // indirect

lang/golang/parser/file.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,26 @@ func (p *GoParser) parseSelector(ctx *fileContext, expr *ast.SelectorExpr, infos
295295
return false
296296
}
297297
return false
298+
} else if obj, ok := use.(*types.Var); ok {
299+
// collect global var
300+
addPkgVarDep := func() {
301+
pkg := obj.Pkg()
302+
if pkg == nil {
303+
return
304+
}
305+
path := pkg.Path()
306+
mod, err := ctx.GetMod(path)
307+
if err != nil {
308+
return
309+
}
310+
id := NewIdentity(mod, path, obj.Name())
311+
dep := NewDependency(id, ctx.FileLine(ident))
312+
infos.globalVars = InsertDependency(infos.globalVars, dep)
313+
}
314+
// check if it is a global var
315+
if isPkgScope(obj.Parent()) {
316+
addPkgVarDep()
317+
}
298318
}
299319
}
300320
} else if sel, ok := expr.X.(*ast.SelectorExpr); ok {

0 commit comments

Comments
 (0)