]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: add line numbers to godebug errors
authorMichael Matloob <matloob@golang.org>
Mon, 3 Jun 2024 22:42:12 +0000 (18:42 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 6 Jun 2024 18:36:30 +0000 (18:36 +0000)
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>

src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_errors_pos.txt [new file with mode: 0644]
src/cmd/go/testdata/script/work.txt
src/cmd/go/testdata/script/work_errors_pos.txt [new file with mode: 0644]

index 4b196825364ffb8286c61224bdcaf8342d79ed19..d1e9feab1305b356d0b1d227a53437001375da49 100644 (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
                                }
                        }
diff --git a/src/cmd/go/testdata/script/mod_errors_pos.txt b/src/cmd/go/testdata/script/mod_errors_pos.txt
new file mode 100644 (file)
index 0000000..48f92d7
--- /dev/null
@@ -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
\ No newline at end of file
index 69391efc862fd7dbb5791c2ab88a0c0321545f38..28df1a008c17ee28e3ca48155d97c7a6c75dbe67 100644 (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
diff --git a/src/cmd/go/testdata/script/work_errors_pos.txt b/src/cmd/go/testdata/script/work_errors_pos.txt
new file mode 100644 (file)
index 0000000..2d05703
--- /dev/null
@@ -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