Browse Source

tool/gocross: break circular dependency on tailcfg (#15829)

Instead of using the version package (which depends on
tailcfg.CurrentCapabilityVersion) to get the git commit hash, do it
directly using debug.BuildInfo. This way, when changing struct fields in
tailcfg, we can successfully `go generate` it without compiler errors.

Updates #9634
Updates https://github.com/tailscale/corp/issues/26717

Signed-off-by: Andrew Lytvynov <[email protected]>
Andrew Lytvynov 10 months ago
parent
commit
a9b3e09a1f
2 changed files with 37 additions and 7 deletions
  1. 18 7
      tool/gocross/gocross.go
  2. 19 0
      tool/gocross/gocross_test.go

+ 18 - 7
tool/gocross/gocross.go

@@ -15,9 +15,9 @@ import (
 	"fmt"
 	"os"
 	"path/filepath"
+	"runtime/debug"
 
 	"tailscale.com/atomicfile"
-	"tailscale.com/version"
 )
 
 func main() {
@@ -28,8 +28,19 @@ func main() {
 		// any time.
 		switch os.Args[1] {
 		case "gocross-version":
-			fmt.Println(version.GetMeta().GitCommit)
-			os.Exit(0)
+			bi, ok := debug.ReadBuildInfo()
+			if !ok {
+				fmt.Fprintln(os.Stderr, "failed getting build info")
+				os.Exit(1)
+			}
+			for _, s := range bi.Settings {
+				if s.Key == "vcs.revision" {
+					fmt.Println(s.Value)
+					os.Exit(0)
+				}
+			}
+			fmt.Fprintln(os.Stderr, "did not find vcs.revision in build info")
+			os.Exit(1)
 		case "is-gocross":
 			// This subcommand exits with an error code when called on a
 			// regular go binary, so it can be used to detect when `go` is
@@ -85,9 +96,9 @@ func main() {
 		path := filepath.Join(toolchain, "bin") + string(os.PathListSeparator) + os.Getenv("PATH")
 		env.Set("PATH", path)
 
-		debug("Input: %s\n", formatArgv(os.Args))
-		debug("Command: %s\n", formatArgv(newArgv))
-		debug("Set the following flags/envvars:\n%s\n", env.Diff())
+		debugf("Input: %s\n", formatArgv(os.Args))
+		debugf("Command: %s\n", formatArgv(newArgv))
+		debugf("Set the following flags/envvars:\n%s\n", env.Diff())
 
 		args = newArgv
 		if err := env.Apply(); err != nil {
@@ -103,7 +114,7 @@ func main() {
 //go:embed gocross-wrapper.sh
 var wrapperScript []byte
 
-func debug(format string, args ...any) {
+func debugf(format string, args ...any) {
 	debug := os.Getenv("GOCROSS_DEBUG")
 	var (
 		out *os.File

+ 19 - 0
tool/gocross/gocross_test.go

@@ -0,0 +1,19 @@
+// Copyright (c) Tailscale Inc & AUTHORS
+// SPDX-License-Identifier: BSD-3-Clause
+
+package main
+
+import (
+	"testing"
+
+	"tailscale.com/tstest/deptest"
+)
+
+func TestDeps(t *testing.T) {
+	deptest.DepChecker{
+		BadDeps: map[string]string{
+			"tailscale.com/tailcfg": "circular dependency via go generate",
+			"tailscale.com/version": "circular dependency via go generate",
+		},
+	}.Check(t)
+}