Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/zen-go/cmd_toolexec_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func toolexecCompileCommand(stdout io.Writer, stderr io.Writer, tool string, too
fmt.Fprintf(stderr, "zen-go: compiling package %s\n", pkgPath)
}

if err := checkZenToolFileIncluded(pkgPath, toolArgs); err != nil {
if err := checkZenToolFileIncluded(pkgPath, toolArgs, stderr); err != nil {
return err
}

Expand Down Expand Up @@ -196,7 +196,7 @@ func updateImportcfgInArgs(stderr io.Writer, args []string, importcfgPath string
// without zen.tool.go. This happens when users run e.g. `go build main.go`
// instead of `go build .`, which causes zen.tool.go to be excluded from the
// build and results in cryptic compiler errors.
func checkZenToolFileIncluded(pkgPath string, toolArgs []string) error {
func checkZenToolFileIncluded(pkgPath string, toolArgs []string, stderr io.Writer) error {
Comment thread
tomaisthorpe marked this conversation as resolved.
if pkgPath != "main" {
return nil
}
Expand Down Expand Up @@ -227,6 +227,7 @@ func checkZenToolFileIncluded(pkgPath string, toolArgs []string) error {
return errors.New("zen-go: zen.tool.go exists but was not included in the build, use 'go build -toolexec=\"zen-go toolexec\" .' instead of specifying individual files")
}

fmt.Fprintf(stderr, "zen-go: warning: zen.tool.go not found in %s. zen.tool.go must be in the same directory as your main package. Run 'zen-go init' from that directory to set up instrumentation.\n", sourceDir)
return nil
}

Expand Down
17 changes: 11 additions & 6 deletions cmd/zen-go/cmd_toolexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"bytes"
"context"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -418,7 +420,7 @@ func TestCheckZenToolFileIncluded(t *testing.T) {
mainGoPath := filepath.Join(tmpDir, "main.go")
toolArgs := []string{"-p", "main", "-o", "/tmp/out.a", mainGoPath}

err = checkZenToolFileIncluded("main", toolArgs)
err = checkZenToolFileIncluded("main", toolArgs, io.Discard)

require.Error(t, err)
assert.Contains(t, err.Error(), "zen.tool.go exists but was not included in the build")
Expand All @@ -432,20 +434,23 @@ func TestCheckZenToolFileIncluded(t *testing.T) {

toolArgs := []string{"-p", "main", "-o", "/tmp/out.a", mainGoPath, zenToolPath}

err := checkZenToolFileIncluded("main", toolArgs)
err := checkZenToolFileIncluded("main", toolArgs, io.Discard)

assert.NoError(t, err)
})

t.Run("no error when zen.tool.go does not exist", func(t *testing.T) {
t.Run("warns when zen.tool.go does not exist", func(t *testing.T) {
tmpDir := t.TempDir()
mainGoPath := filepath.Join(tmpDir, "main.go")

toolArgs := []string{"-p", "main", "-o", "/tmp/out.a", mainGoPath}

err := checkZenToolFileIncluded("main", toolArgs)
var stderr strings.Builder
err := checkZenToolFileIncluded("main", toolArgs, &stderr)

assert.NoError(t, err)
assert.Contains(t, stderr.String(), "zen.tool.go not found in")
assert.Contains(t, stderr.String(), "must be in the same directory as your main package")
})

t.Run("no error for non-main packages", func(t *testing.T) {
Expand All @@ -460,15 +465,15 @@ func TestCheckZenToolFileIncluded(t *testing.T) {

toolArgs := []string{"-p", "github.com/example/pkg", "-o", "/tmp/out.a", goFilePath}

err = checkZenToolFileIncluded("github.com/example/pkg", toolArgs)
err = checkZenToolFileIncluded("github.com/example/pkg", toolArgs, io.Discard)

assert.NoError(t, err)
})

t.Run("no error when no go files in args", func(t *testing.T) {
toolArgs := []string{"-p", "main", "-o", "/tmp/out.a"}

err := checkZenToolFileIncluded("main", toolArgs)
err := checkZenToolFileIncluded("main", toolArgs, io.Discard)

assert.NoError(t, err)
})
Expand Down
Loading