Skip to content

x/tools/go/packages: Load might return duplicated errors, when export data requested #74302

Open
@mateusz834

Description

@mateusz834

Reproducer:

package main

import (
	"log"

	"golang.org/x/tools/go/packages"
)

func main() {
	pkgs, err := packages.Load(&packages.Config{
		Mode: packages.LoadSyntax,
	}, "module/pkg")
	if err != nil {
		log.Fatal(err)
	}
	packages.PrintErrors(pkgs)
}

module/pkg package contains a file, that has a syntax error.

package pkg

func a() {
	var a
}
$ go run .
-: # module/pkg
pkg/invalid.go:4:7: syntax error: unexpected newline, expected type
/home/mateusz/tmp/module/pkg/invalid.go:4:7: expected type, found newline
/home/mateusz/tmp/module/pkg/invalid.go:4:6: declared and not used: a

This happens in two cases, when NeedDeps is not specified or NeedExportFile is set. In such cases the -export=true is passed to go list. That in turn causes it to return an error, which ends up being stored in Package.Errors with Kind == ListError. go/packages additionally parses the files, that go list returned, and it append them to Package.Errors, thus we end up with duplicated errors.

CC @adonovan @findleyr @matloob

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions