Skip to content

Commit

Permalink
cmd/tsconnect: find the build dir independently of -trimpath
Browse files Browse the repository at this point in the history
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 <danderson@tailscale.com>
  • Loading branch information
danderson committed Feb 22, 2023
1 parent f9b7468 commit 0b8f89c
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cmd/tsconnect/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0b8f89c

Please sign in to comment.