Skip to content

Commit 377de51

Browse files
qmuntaldagood
andauthored
Disable GOTOOLCHAIN (#1612)
* disable GOTOOLCHAIN * Apply suggestions from code review Co-authored-by: Davis Goodin <[email protected]> * add missing changes --------- Co-authored-by: Davis Goodin <[email protected]>
1 parent 1f9425c commit 377de51

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: qmuntal <[email protected]>
3+
Date: Thu, 8 Jun 2023 14:17:13 +0200
4+
Subject: [PATCH] Disable GOTOOLCHAIN support
5+
6+
The GOTOOLCHAIN feature can potentially make a Go invocation switch
7+
to a non-Microsoft toolchain. To avoid it, change the GOTOOLCHAIN
8+
default value from "auto" to "local" and instruct the Go toolchain
9+
to panic if the user manually modifies the GOTOOLCHAIN variable.
10+
---
11+
go.env | 6 ++++--
12+
src/cmd/go/internal/cfg/cfg.go | 18 ++++++++++++++++++
13+
src/cmd/go/script_test.go | 1 +
14+
3 files changed, 23 insertions(+), 2 deletions(-)
15+
16+
diff --git a/go.env b/go.env
17+
index 6ff2b921d464bc..36c3bdfc9b6087 100644
18+
--- a/go.env
19+
+++ b/go.env
20+
@@ -7,6 +7,8 @@
21+
GOPROXY=https://proxy.golang.org,direct
22+
GOSUMDB=sum.golang.org
23+
24+
-# Automatically download newer toolchains as directed by go.mod files.
25+
+# Use the locally installed Go toolchain, never downloading a different one.
26+
+# Upstream uses `GOTOOLCHAIN=auto` instead, but `auto` can download and switch
27+
+# to a Go toolchain not built by Microsoft, and we want to avoid that.
28+
# See https://go.dev/doc/toolchain for details.
29+
-GOTOOLCHAIN=auto
30+
+GOTOOLCHAIN=local
31+
diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go
32+
index 3b9f27e91d517e..3084f681499c2c 100644
33+
--- a/src/cmd/go/internal/cfg/cfg.go
34+
+++ b/src/cmd/go/internal/cfg/cfg.go
35+
@@ -401,6 +401,24 @@ func Getenv(key string) string {
36+
}
37+
val := os.Getenv(key)
38+
if val != "" {
39+
+ if key == "GOTOOLCHAIN" && val != "local" {
40+
+ // Don't allow GOTOOLCHAIN to be set to anything but "local".
41+
+ // That could cause the go command to use a different toolchain
42+
+ // than the Microsoft build of Go without warning. This can be
43+
+ // difficult to diagnose and may silently cause the user to
44+
+ // unintentionally build a program that violates Microsoft's
45+
+ // internal policies for Go.
46+
+ //
47+
+ // We allow bypassing this safety feature. We need to while running
48+
+ // the TestScript test from the cmd/go package, else many tests will fail.
49+
+ // It's also possible for existing workflows to intentionally depend on this behavior.
50+
+ if v := os.Getenv("MS_GOTOOLCHAIN_ALLOW_NON_LOCAL"); v != "1" {
51+
+ println("GOTOOLCHAIN is set to \"" + val + "\" but only \"local\" is allowed.")
52+
+ println("To allow this, set MS_GOTOOLCHAIN_ALLOW_NON_LOCAL=1 in your environment.")
53+
+ print("Take into account that that could cause the go command to use a different toolchain than the Microsoft build of Go.")
54+
+ os.Exit(1)
55+
+ }
56+
+ }
57+
return val
58+
}
59+
envCache.once.Do(initEnvCache)
60+
diff --git a/src/cmd/go/script_test.go b/src/cmd/go/script_test.go
61+
index 0576ea8add72af..1345ea8bb8e530 100644
62+
--- a/src/cmd/go/script_test.go
63+
+++ b/src/cmd/go/script_test.go
64+
@@ -253,6 +253,7 @@ func scriptEnv(srv *vcstest.Server, srvCertFile string) ([]string, error) {
65+
"CMDGO_TEST_RUN_MAIN=true",
66+
"HGRCPATH=",
67+
"GOTOOLCHAIN=auto",
68+
+ "MS_GOTOOLCHAIN_ALLOW_NON_LOCAL=1", // allow non-local toolchains, some tests expect GOTOOLCHAIN to be honored
69+
"newline=\n",
70+
}
71+

patches/0005-Update-default-go.env.patch

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)