Skip to content

Commit c03e5c2

Browse files
edigaryevadonovan
authored andcommitted
go/packages: do not nullify Fset when NeedSyntax is set
Currently, Fset is initialized when either NeedTypes or NeedSyntax are set in newLoader(). However, later in refine() it is nullified using a different condition that doesn't take NeedSyntax into account. Use the inverse condition to that of in newLoader() when deciding on whether to nullify Fset or not. Resolves golang/go#48226. Change-Id: Ic7c05dfe3337d5cf14aa185350a8e766e224c898 GitHub-Last-Rev: a906871 GitHub-Pull-Request: #506 Reviewed-on: https://go-review.googlesource.com/c/tools/+/601239 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 6a6fd99 commit c03e5c2

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

go/packages/packages.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
//
4747
// Unfortunately there are a number of open bugs related to
4848
// interactions among the LoadMode bits:
49-
// - https://github.com/golang/go/issues/48226
5049
// - https://github.com/golang/go/issues/56633
5150
// - https://github.com/golang/go/issues/56677
5251
// - https://github.com/golang/go/issues/58726
@@ -76,7 +75,7 @@ const (
7675
// NeedTypes adds Types, Fset, and IllTyped.
7776
NeedTypes
7877

79-
// NeedSyntax adds Syntax.
78+
// NeedSyntax adds Syntax and Fset.
8079
NeedSyntax
8180

8281
// NeedTypesInfo adds TypesInfo.
@@ -961,12 +960,14 @@ func (ld *loader) refine(response *DriverResponse) ([]*Package, error) {
961960
}
962961
if ld.requestedMode&NeedTypes == 0 {
963962
ld.pkgs[i].Types = nil
964-
ld.pkgs[i].Fset = nil
965963
ld.pkgs[i].IllTyped = false
966964
}
967965
if ld.requestedMode&NeedSyntax == 0 {
968966
ld.pkgs[i].Syntax = nil
969967
}
968+
if ld.requestedMode&NeedTypes == 0 && ld.requestedMode&NeedSyntax == 0 {
969+
ld.pkgs[i].Fset = nil
970+
}
970971
if ld.requestedMode&NeedTypesInfo == 0 {
971972
ld.pkgs[i].TypesInfo = nil
972973
}

go/packages/packages_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,42 @@ func testTypecheckCgo(t *testing.T, exporter packagestest.Exporter) {
26452645
}
26462646
}
26472647

2648+
// TestIssue48226 ensures that when NeedSyntax is provided we do not nullify the
2649+
// Fset, which is needed to resolve the syntax tree element positions to files.
2650+
func TestIssue48226(t *testing.T) { testAllOrModulesParallel(t, testIssue48226) }
2651+
func testIssue48226(t *testing.T, exporter packagestest.Exporter) {
2652+
exported := packagestest.Export(t, exporter, []packagestest.Module{
2653+
{
2654+
Name: "golang.org/fake/syntax",
2655+
Files: map[string]interface{}{
2656+
"syntax.go": `package test`,
2657+
},
2658+
},
2659+
})
2660+
defer exported.Cleanup()
2661+
2662+
exported.Config.Mode = packages.NeedFiles | packages.NeedSyntax
2663+
2664+
initial, err := packages.Load(exported.Config, "golang.org/fake/syntax")
2665+
if err != nil {
2666+
t.Fatal(err)
2667+
}
2668+
if len(initial) != 1 {
2669+
t.Fatalf("exepected 1 package, got %d", len(initial))
2670+
}
2671+
pkg := initial[0]
2672+
2673+
if len(pkg.Errors) != 0 {
2674+
t.Fatalf("package has errors: %v", pkg.Errors)
2675+
}
2676+
2677+
fname := pkg.Fset.File(pkg.Syntax[0].Pos()).Name()
2678+
if filepath.Base(fname) != "syntax.go" {
2679+
t.Errorf("expected the package declaration position "+
2680+
"to resolve to \"syntax.go\", got %q instead", fname)
2681+
}
2682+
}
2683+
26482684
func TestModule(t *testing.T) {
26492685
testAllOrModulesParallel(t, testModule)
26502686
}

0 commit comments

Comments
 (0)