Skip to content

Commit 522243a

Browse files
Bryan C. Millsgopherbot
authored andcommitted
go/ssa/interp: avoid hard-coding GOOS and GOARCH
GOOS and GOARCH are not necessarily present in the environment at all, so overriding them in interp doesn't have a clear purpose, especially given that CL 495255 no longer overrides them in the build context. Unfortunately, the fake (reduced) GOROOT/src used for the test also hard-coded the runtime constants for GOOS and GOARCH. Since in general the testdata directory is not writable (it may be in the module cache), we need to copy the fake GOROOT into a testdata directory in order to modify it. This change also deletes an unused function that I noticed while investigating. Fixes golang/go#60226 (maybe). Change-Id: I409292af76d411efb8a3b4ca6d8d929b53325610 Reviewed-on: https://go-review.googlesource.com/c/tools/+/495258 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Bryan Mills <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent a059382 commit 522243a

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

go/ssa/interp/external.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,6 @@ func ext۰os۰Getenv(fr *frame, args []value) value {
312312
switch name {
313313
case "GOSSAINTERP":
314314
return "1"
315-
case "GOARCH":
316-
return "amd64"
317-
case "GOOS":
318-
return "linux"
319315
}
320316
return os.Getenv(name)
321317
}

go/ssa/interp/interp.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,6 @@ func doRecover(caller *frame) value {
635635
return iface{}
636636
}
637637

638-
// setGlobal sets the value of a system-initialized global variable.
639-
func setGlobal(i *interpreter, pkg *ssa.Package, name string, v value) {
640-
if g, ok := i.globals[pkg.Var(name)]; ok {
641-
*g = v
642-
return
643-
}
644-
panic("no global variable: " + pkg.Pkg.Path() + "." + name)
645-
}
646-
647638
// Interpret interprets the Go program whose main package is mainpkg.
648639
// mode specifies various interpreter options. filename and args are
649640
// the initial values of os.Args for the target program. sizes is the

go/ssa/interp/interp_test.go

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,14 @@ func init() {
138138
testdataTests = append(testdataTests, "typeassert.go")
139139
testdataTests = append(testdataTests, "zeros.go")
140140
}
141+
142+
// GOROOT/test used to assume that GOOS and GOARCH were explicitly set in the
143+
// environment, so do that here for TestGorootTest.
144+
os.Setenv("GOOS", runtime.GOOS)
145+
os.Setenv("GOARCH", runtime.GOARCH)
141146
}
142147

143-
func run(t *testing.T, input string) {
148+
func run(t *testing.T, input string, goroot string) {
144149
// The recover2 test case is broken on Go 1.14+. See golang/go#34089.
145150
// TODO(matloob): Fix this.
146151
if filepath.Base(input) == "recover2.go" {
@@ -151,8 +156,8 @@ func run(t *testing.T, input string) {
151156

152157
start := time.Now()
153158

154-
ctx := build.Default // copy
155-
ctx.GOROOT = "testdata" // fake goroot
159+
ctx := build.Default // copy
160+
ctx.GOROOT = goroot
156161
ctx.GOOS = runtime.GOOS
157162
ctx.GOARCH = runtime.GOARCH
158163
if filepath.Base(input) == "width32.go" && unsafe.Sizeof(int(0)) > 4 {
@@ -222,24 +227,72 @@ func run(t *testing.T, input string) {
222227
}
223228
}
224229

230+
// makeGoroot copies testdata/src into the "src" directory of a temporary
231+
// location to mimic GOROOT/src, and adds a file "runtime/consts.go" containing
232+
// declarations for GOOS and GOARCH that match the GOOS and GOARCH of this test.
233+
//
234+
// It returns the directory that should be used for GOROOT.
235+
func makeGoroot(t *testing.T) string {
236+
goroot := t.TempDir()
237+
src := filepath.Join(goroot, "src")
238+
239+
err := filepath.Walk("testdata/src", func(path string, info os.FileInfo, err error) error {
240+
if err != nil {
241+
return err
242+
}
243+
244+
rel, err := filepath.Rel("testdata/src", path)
245+
if err != nil {
246+
return err
247+
}
248+
targ := filepath.Join(src, rel)
249+
250+
if info.IsDir() {
251+
return os.Mkdir(targ, info.Mode().Perm()|0700)
252+
}
253+
254+
b, err := os.ReadFile(path)
255+
if err != nil {
256+
return err
257+
}
258+
return os.WriteFile(targ, b, info.Mode().Perm())
259+
})
260+
if err != nil {
261+
t.Fatal(err)
262+
}
263+
264+
constsGo := fmt.Sprintf(`package runtime
265+
const GOOS = %q
266+
const GOARCH = %q
267+
`, runtime.GOOS, runtime.GOARCH)
268+
err = os.WriteFile(filepath.Join(src, "runtime/consts.go"), []byte(constsGo), 0644)
269+
if err != nil {
270+
t.Fatal(err)
271+
}
272+
273+
return goroot
274+
}
275+
225276
// TestTestdataFiles runs the interpreter on testdata/*.go.
226277
func TestTestdataFiles(t *testing.T) {
278+
goroot := makeGoroot(t)
227279
cwd, err := os.Getwd()
228280
if err != nil {
229281
log.Fatal(err)
230282
}
231283
for _, input := range testdataTests {
232284
t.Run(input, func(t *testing.T) {
233-
run(t, filepath.Join(cwd, "testdata", input))
285+
run(t, filepath.Join(cwd, "testdata", input), goroot)
234286
})
235287
}
236288
}
237289

238290
// TestGorootTest runs the interpreter on $GOROOT/test/*.go.
239291
func TestGorootTest(t *testing.T) {
292+
goroot := makeGoroot(t)
240293
for _, input := range gorootTestTests {
241294
t.Run(input, func(t *testing.T) {
242-
run(t, filepath.Join(build.Default.GOROOT, "test", input))
295+
run(t, filepath.Join(build.Default.GOROOT, "test", input), goroot)
243296
})
244297
}
245298
}
@@ -251,6 +304,7 @@ func TestTypeparamTest(t *testing.T) {
251304
if !typeparams.Enabled {
252305
return
253306
}
307+
goroot := makeGoroot(t)
254308

255309
// Skip known failures for the given reason.
256310
// TODO(taking): Address these.
@@ -295,7 +349,7 @@ func TestTypeparamTest(t *testing.T) {
295349
t.Skipf("skipping: %s", reason)
296350
}
297351

298-
run(t, input)
352+
run(t, input, goroot)
299353
})
300354
}
301355
}

go/ssa/interp/testdata/src/runtime/runtime.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,4 @@ type Error interface {
1616
RuntimeError()
1717
}
1818

19-
const GOOS = "linux"
20-
const GOARCH = "amd64"
21-
2219
func GC()

0 commit comments

Comments
 (0)