Skip to content

Commit b1630e0

Browse files
Merge pull request #414 from AikidoSec/warn-no-zen-tool
Add warning when zen.tool.go does not exist in main package
2 parents 853770c + 93e0991 commit b1630e0

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

cmd/zen-go/cmd_toolexec_compile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func toolexecCompileCommand(stdout io.Writer, stderr io.Writer, tool string, too
3333
fmt.Fprintf(stderr, "zen-go: compiling package %s\n", pkgPath)
3434
}
3535

36-
if err := checkZenToolFileIncluded(pkgPath, toolArgs); err != nil {
36+
if err := checkZenToolFileIncluded(pkgPath, toolArgs, stderr); err != nil {
3737
return err
3838
}
3939

@@ -196,7 +196,7 @@ func updateImportcfgInArgs(stderr io.Writer, args []string, importcfgPath string
196196
// without zen.tool.go. This happens when users run e.g. `go build main.go`
197197
// instead of `go build .`, which causes zen.tool.go to be excluded from the
198198
// build and results in cryptic compiler errors.
199-
func checkZenToolFileIncluded(pkgPath string, toolArgs []string) error {
199+
func checkZenToolFileIncluded(pkgPath string, toolArgs []string, stderr io.Writer) error {
200200
if pkgPath != "main" {
201201
return nil
202202
}
@@ -227,6 +227,7 @@ func checkZenToolFileIncluded(pkgPath string, toolArgs []string) error {
227227
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")
228228
}
229229

230+
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)
230231
return nil
231232
}
232233

cmd/zen-go/cmd_toolexec_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"bytes"
55
"context"
6+
"io"
67
"os"
78
"os/exec"
89
"path/filepath"
910
"runtime"
11+
"strings"
1012
"testing"
1113

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

421-
err = checkZenToolFileIncluded("main", toolArgs)
423+
err = checkZenToolFileIncluded("main", toolArgs, io.Discard)
422424

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

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

435-
err := checkZenToolFileIncluded("main", toolArgs)
437+
err := checkZenToolFileIncluded("main", toolArgs, io.Discard)
436438

437439
assert.NoError(t, err)
438440
})
439441

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

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

446-
err := checkZenToolFileIncluded("main", toolArgs)
448+
var stderr strings.Builder
449+
err := checkZenToolFileIncluded("main", toolArgs, &stderr)
447450

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

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

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

463-
err = checkZenToolFileIncluded("github.com/example/pkg", toolArgs)
468+
err = checkZenToolFileIncluded("github.com/example/pkg", toolArgs, io.Discard)
464469

465470
assert.NoError(t, err)
466471
})
467472

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

471-
err := checkZenToolFileIncluded("main", toolArgs)
476+
err := checkZenToolFileIncluded("main", toolArgs, io.Discard)
472477

473478
assert.NoError(t, err)
474479
})

0 commit comments

Comments
 (0)