diff options
author | Rebecca Stambler <rstambler@golang.org> | 2019-10-15 17:11:27 -0400 |
---|---|---|
committer | Rebecca Stambler <rstambler@golang.org> | 2019-10-16 22:16:03 +0000 |
commit | 6f5e27347a0cc033990d87f7a27f1fab67bad829 (patch) | |
tree | af5f012efe81d7247eb33b5e41d6270fe5b9a5e1 /go/packages/packages_test.go | |
parent | f0068bd333b234c38cb0ed6b2946665a40ad171b (diff) | |
download | golang-x-tools-6f5e27347a0cc033990d87f7a27f1fab67bad829.tar.gz |
go/packages: handle invalid files in overlays
This change handles a specific case where `go list` returns an error on
a file that lacks a package declaration. If only one package is returned
as a response, we should add that file to that package, since it may
have valid content in an overlay.
This change uses the internal/span package to correctly parse the error
message from `go list`.
Change-Id: I5909c4fd765746df1003685aa915b7c5f9cdcee5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/201220
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Diffstat (limited to 'go/packages/packages_test.go')
-rw-r--r-- | go/packages/packages_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 2a73156a3..e3c70080b 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -1079,6 +1079,51 @@ func testNewPackagesInOverlay(t *testing.T, exporter packagestest.Exporter) { } } +func TestContainsInOverlays(t *testing.T) { packagestest.TestAll(t, testcontainsInOverlays) } +func testcontainsInOverlays(t *testing.T, exporter packagestest.Exporter) { + // This test checks a specific case where a file is empty on disk. + // In this case, `go list` will return the package golang.org/fake/c + // with only c.go as a GoFile, with an error message for c2.go. + // Since there is only one possible package for c2.go to be a part of, + // go/packages will parse the filename out of error and add it to GoFiles and CompiledGoFiles. + exported := packagestest.Export(t, exporter, []packagestest.Module{{ + Name: "golang.org/fake", + Files: map[string]interface{}{ + "c/c.go": `package c; const C = "c"`, + "c/c2.go": ``, + }}}) + defer exported.Cleanup() + + dir := filepath.Dir(exported.File("golang.org/fake", "c/c.go")) + + for _, test := range []struct { + overlay map[string][]byte + want string + }{ + { + overlay: map[string][]byte{filepath.Join(dir, "c2.go"): []byte(`nonsense`)}, + want: "golang.org/fake/c", + }, + } { + exported.Config.Overlay = test.overlay + exported.Config.Mode = packages.LoadImports + exported.Config.Logf = t.Logf + for filename := range exported.Config.Overlay { + initial, err := packages.Load(exported.Config, "file="+filename) + if err != nil { + t.Fatal(err) + } + if len(initial) == 0 { + t.Fatalf("no packages for %s", filename) + } + pkg := initial[0] + if pkg.PkgPath != test.want { + t.Errorf("got %s, want %s", pkg.PkgPath, test.want) + } + } + } +} + func TestAdHocPackagesBadImport(t *testing.T) { // TODO: Enable this test when github.com/golang/go/issues/33374 is resolved. t.Skip() @@ -2201,10 +2246,14 @@ func testAdHocContains(t *testing.T, exporter packagestest.Exporter) { }() exported.Config.Mode = packages.NeedImports | packages.NeedFiles + exported.Config.Logf = t.Logf pkgs, err := packages.Load(exported.Config, "file="+filename) if err != nil { t.Fatal(err) } + if len(pkgs) == 0 { + t.Fatalf("no packages for %s", filename) + } if len(pkgs) != 1 && pkgs[0].PkgPath != "command-line-arguments" { t.Fatalf("packages.Load: want [command-line-arguments], got %v", pkgs) } |