Skip to content

Commit 6743d40

Browse files
committed
go/packages: rewrite Go 1.11's no such directory err to look like 1.13's
Then the rest of go/packages can process the go list output as usual. Fixes golang/go#33491 Change-Id: I1bd46154ce75d67dc3e18454a067bcc084526c5d Reviewed-on: https://go-review.googlesource.com/c/tools/+/189160 Run-TryBot: Michael Matloob <matloob@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent be5259f commit 6743d40

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

go/packages/golist.go

+13
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,20 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
841841
return bytes.NewBufferString(output), nil
842842
}
843843

844+
// Backwards compatibility for Go 1.11 because 1.12 and 1.13 put the directory in the ImportPath.
845+
// If the package doesn't exist, put the absolute path of the directory into the error message,
846+
// as Go 1.13 list does.
847+
const noSuchDirectory = "no such directory"
848+
if len(stderr.String()) > 0 && strings.Contains(stderr.String(), noSuchDirectory) {
849+
errstr := stderr.String()
850+
abspath := strings.TrimSpace(errstr[strings.Index(errstr, noSuchDirectory)+len(noSuchDirectory):])
851+
output := fmt.Sprintf(`{"ImportPath": %q,"Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
852+
abspath, strings.Trim(stderr.String(), "\n"))
853+
return bytes.NewBufferString(output), nil
854+
}
855+
844856
// Workaround for #29280: go list -e has incorrect behavior when an ad-hoc package doesn't exist.
857+
// Note that the error message we look for in this case is different that the one looked for above.
845858
if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "no such file or directory") {
846859
output := fmt.Sprintf(`{"ImportPath": "command-line-arguments","Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
847860
strings.Trim(stderr.String(), "\n"))

0 commit comments

Comments
 (0)