Skip to content

Commit 731d161

Browse files
nikola-jokicgopherbot
authored andcommitted
x/playground: remove -mod=mod and execute go mod tidy before command
Previously, `-mod=mod` was included to pull down dependencies. This change executes go mod tidy before building packages, that will allow for go work usage, and address golang/go#40728 Fixes golang/go#54741 Change-Id: I66affc8d2a2e72d958415ea1c052dd3dcf38841e Reviewed-on: https://go-review.googlesource.com/c/playground/+/458895 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Commit-Queue: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent baeddcd commit 731d161

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

mod.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
"os/exec"
12+
"strings"
13+
)
14+
15+
func modTidy(ctx context.Context, dir, goPath string) (output string, execErr error) {
16+
cmd := exec.Command("go", "mod", "tidy")
17+
cmd.Dir = dir
18+
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOPATH="+goPath)
19+
cmd.Env = append(cmd.Env,
20+
"GO111MODULE=on",
21+
"GOPROXY="+playgroundGoproxy(),
22+
)
23+
out, err := cmd.CombinedOutput()
24+
if err == nil {
25+
return "", nil
26+
}
27+
if _, ok := err.(*exec.ExitError); !ok {
28+
return "", fmt.Errorf("error vetting go source: %v", err)
29+
}
30+
31+
// Rewrite compiler errors to refer to progName
32+
// instead of '/tmp/sandbox1234/main.go'.
33+
errs := strings.Replace(string(out), dir, "", -1)
34+
35+
return errs, nil
36+
}

sandbox.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ func sandboxBuild(ctx context.Context, tmpDir string, in []byte, vet bool) (br *
460460
// Create a GOPATH just for modules to be downloaded
461461
// into GOPATH/pkg/mod.
462462
cmd.Args = append(cmd.Args, "-modcacherw")
463-
cmd.Args = append(cmd.Args, "-mod=mod")
464463
br.goPath, err = os.MkdirTemp("", "gopath")
465464
if err != nil {
466465
log.Printf("error creating temp directory: %v", err)
@@ -472,6 +471,12 @@ func sandboxBuild(ctx context.Context, tmpDir string, in []byte, vet bool) (br *
472471
out := &bytes.Buffer{}
473472
cmd.Stderr, cmd.Stdout = out, out
474473

474+
// Run "go mod tidy" before executing a command.
475+
_, err = modTidy(ctx, tmpDir, br.goPath)
476+
if err != nil {
477+
return nil, fmt.Errorf("error running go mod tidy: %v", err)
478+
}
479+
475480
if err := cmd.Start(); err != nil {
476481
return nil, fmt.Errorf("error starting go build: %v", err)
477482
}

tests.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,4 +633,40 @@ func print() {
633633
`, errors: `./foo.go:6:2: syntax error: unexpected =, expecting }
634634
`,
635635
},
636+
{
637+
name: "workspace",
638+
prog: `
639+
package main
640+
641+
import "internal/bar"
642+
643+
func main() {
644+
bar.Print()
645+
}
646+
-- go.work --
647+
go 1.18
648+
649+
use (
650+
.
651+
./projects/bar
652+
)
653+
-- go.mod --
654+
module internal/foo
655+
656+
go 1.18
657+
658+
require internal/bar v0.0.0
659+
-- projects/bar/go.mod --
660+
module internal/bar
661+
662+
go 1.18
663+
-- projects/bar/upper.go --
664+
package bar
665+
666+
import "fmt"
667+
668+
func Print() {
669+
fmt.Println("bar")
670+
}
671+
`, want: "bar\n"},
636672
}

vet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func vetCheckInDir(ctx context.Context, dir, goPath string) (output string, exec
6262
mGoVetLatency.M(float64(time.Since(start))/float64(time.Millisecond)))
6363
}()
6464

65-
cmd := exec.Command("go", "vet", "--tags=faketime", "--mod=mod")
65+
cmd := exec.Command("go", "vet", "--tags=faketime")
6666
cmd.Dir = dir
6767
// Linux go binary is not built with CGO_ENABLED=0.
6868
// Prevent vet to compile packages in cgo mode.

0 commit comments

Comments
 (0)