From 0b8f89c79c9cc79f8e3b1cb8b230322a1d902399 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 21 Feb 2023 15:55:06 -0800 Subject: [PATCH] cmd/tsconnect: find the build dir independently of -trimpath trimmed builds don't have absolute path information in executable metadata, which leads the runtime.Caller approach failing mysteriously in yarn with complaints about relative package paths. So, instead of using embedded package metadata to find paths, expect that we're being invoked within the tailscale repo, and locate the tsconnect directory that way. Signed-off-by: David Anderson --- cmd/tsconnect/common.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/tsconnect/common.go b/cmd/tsconnect/common.go index 8b9387f87..8bf843879 100644 --- a/cmd/tsconnect/common.go +++ b/cmd/tsconnect/common.go @@ -28,10 +28,13 @@ const ( func commonSetup(dev bool) (*esbuild.BuildOptions, error) { // Change cwd to to where this file lives -- that's where all inputs for // esbuild and other build steps live. - if _, filename, _, ok := runtime.Caller(0); ok { - if err := os.Chdir(path.Dir(filename)); err != nil { - return nil, fmt.Errorf("Cannot change cwd: %w", err) - } + root, err := findRepoRoot() + if err != nil { + return nil, err + } + tsConnectDir := filepath.Join(root, "cmd", "tsconnect") + if err := os.Chdir(tsConnectDir); err != nil { + return nil, fmt.Errorf("Cannot change cwd: %w", err) } if err := installJSDeps(); err != nil { return nil, fmt.Errorf("Cannot install JS deps: %w", err) @@ -67,6 +70,22 @@ func commonSetup(dev bool) (*esbuild.BuildOptions, error) { }, nil } +func findRepoRoot() (string, error) { + cwd, err := os.Getwd() + if err != nil { + return "", err + } + for { + if _, err := os.Stat(path.Join(cwd, "go.mod")); err == nil { + return cwd, nil + } + if cwd == "/" { + return "", fmt.Errorf("Cannot find repo root") + } + cwd = path.Dir(cwd) + } +} + func commonPkgSetup(dev bool) (*esbuild.BuildOptions, error) { buildOptions, err := commonSetup(dev) if err != nil {