cmd/go/internal/modload: add line numbers to godebug errors

In addition, add a line number to the go.work error for multiple use statements
for the same directory. Also clean up the error prefix for go.work
errors now containing line numbers.

Fixes #67623

Change-Id: Ia7edcc50f7d7ec907b4a9eb4fe270c75d04c1fa5
Reviewed-on: https://go-review.googlesource.com/c/go/+/590135
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
Auto-Submit: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Michael Matloob 2024-06-03 18:42:12 -04:00 committed by Gopher Robot
parent 2be4b3913a
commit 4b0dd55608
4 changed files with 64 additions and 7 deletions

View file

@ -695,7 +695,7 @@ func loadWorkFile(path string) (workFile *modfile.WorkFile, modRoots []string, e
}
if seen[modRoot] {
return nil, nil, fmt.Errorf("path %s appears multiple times in workspace", modRoot)
return nil, nil, fmt.Errorf("error loading go.work:\n%s:%d: path %s appears multiple times in workspace", base.ShortPath(path), d.Syntax.Start.Line, modRoot)
}
seen[modRoot] = true
modRoots = append(modRoots, modRoot)
@ -703,7 +703,7 @@ func loadWorkFile(path string) (workFile *modfile.WorkFile, modRoots []string, e
for _, g := range wf.Godebug {
if err := CheckGodebug("godebug", g.Key, g.Value); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("error loading go.work:\n%s:%d: %w", base.ShortPath(path), g.Syntax.Start.Line, err)
}
}
@ -715,12 +715,12 @@ func ReadWorkFile(path string) (*modfile.WorkFile, error) {
path = base.ShortPath(path) // use short path in any errors
workData, err := fsys.ReadFile(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("reading go.work: %w", err)
}
f, err := modfile.ParseWork(path, workData, nil)
if err != nil {
return nil, err
return nil, fmt.Errorf("errors parsing go.work:\n%w", err)
}
if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 && cfg.CmdName != "work edit" {
base.Fatal(&gover.TooNewError{What: base.ShortPath(path), GoVersion: f.Go.Version})
@ -841,7 +841,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
var err error
workFile, modRoots, err = loadWorkFile(workFilePath)
if err != nil {
return nil, fmt.Errorf("reading go.work: %w", err)
return nil, err
}
for _, modRoot := range modRoots {
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
@ -951,7 +951,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
ok := true
for _, g := range f.Godebug {
if err := CheckGodebug("godebug", g.Key, g.Value); err != nil {
errs = append(errs, fmt.Errorf("%s: %v", base.ShortPath(filepath.Dir(gomod)), err))
errs = append(errs, fmt.Errorf("error loading go.mod:\n%s:%d: %v", base.ShortPath(gomod), g.Syntax.Start.Line, err))
ok = false
}
}

View file

@ -0,0 +1,11 @@
# Test case for #67623 in go.mod files: make sure the error for
# an unknown godebug is printed on a line starting with the file
# and line number, so it can be easily parsed by tools.
! go list
stderr '^go.mod:3: unknown godebug "foo"$'
-- go.mod --
module example.com/bar
godebug foo=1

View file

@ -41,7 +41,7 @@ env GOWORK=
cp go.work go.work.backup
cp go.work.dup go.work
! go run example.com/b
stderr 'reading go.work: path .* appears multiple times in workspace'
stderr 'go.work:6: path .* appears multiple times in workspace'
cp go.work.backup go.work
cp go.work.d go.work

View file

@ -0,0 +1,46 @@
# Test case for #67623 in go.work files: make sure the errors are
# printed on lines starting with file and line number so they
# can be easily parsed by tools.
cp go.work.repeated.txt go.work
! go list
stderr '^go.work:4: path .* appears multiple times in workspace$'
cp go.work.badgodebug.txt go.work
! go list
stderr '^go.work:3: unknown godebug "foo"$'
cp go.work.unparsable.txt go.work
! go list
stderr '^go.work:5: unknown directive: notadirective'
cp go.work.firstlineerr.txt go.work
! go list
stderr '^go.work:1: unknown godebug "bar"$'
cp go.work.firsterrlisted.txt go.work
! go list
stderr '^go.work:1: unknown godebug "baz"$'
-- foo/go.mod --
module example.com/foo
-- go.work.repeated.txt --
use foo
use foo
-- go.work.badgodebug.txt --
godebug foo=1
-- go.work.unparsable.txt --
notadirective
-- go.work.firstlineerr.txt --
godebug bar=1
-- go.work.firsterrlisted.txt --
godebug baz=1
godebug baz=1