buildkite / cli

A command line interface for Buildkite.
MIT License
165 stars 44 forks source link

panic following Development section in README #325

Closed mmlb closed 1 month ago

mmlb commented 1 month ago

I'm trying out the cli locally and can't get past a nil deref panic early in factory.go. I've followed the README and see the following:

[11:14:12]-[~/t/b/b/cli]-[manny@c3mv18nix]
git status
On branch 3.x
Your branch is up to date with 'origin/3.x'.

nothing to commit, working tree clean
[11:14:14]-[~/t/b/b/cli]-[manny@c3mv18nix]
git rev-parse HEAD
f178e56e753ef45bf80f9bdf09e49b3546e4fb07
[11:14:16]-[~/t/b/b/cli]-[manny@c3mv18nix]
echo $BUILDKITE_GRAPHQL_TOKEN | head -c 7
bkua_f7āŽ                                                                                                                                                                              [11:14:19]-[~/t/b/b/cli]-[manny@c3mv18nix]
go generate
Generating GraphQL code
[11:14:21]-[~/t/b/b/cli]-[manny@c3mv18nix]
go run cmd/bk/main.go --help
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x903056]

goroutine 1 [running]:
github.com/buildkite/go-buildkite/v3/buildkite.NewClient(0xc00024a690)
    /home/manny/.cache/go/pkg/mod/github.com/buildkite/go-buildkite/v3@v3.11.0/buildkite/buildkite.go:105 +0x536
github.com/buildkite/cli/v3/pkg/cmd/factory.New({0xb46d94, 0x3})
    /home/manny/t/bk/buildkite/cli/pkg/cmd/factory/factory.go:34 +0x3db
main.mainRun()
    /home/manny/t/bk/buildkite/cli/cmd/bk/main.go:20 +0x25
main.main()
    /home/manny/t/bk/buildkite/cli/cmd/bk/main.go:14 +0x13
exit status 2

Adding in a little bit of debug code:

diff --git a/pkg/cmd/factory/factory.go b/pkg/cmd/factory/factory.go
index c22ce0f..1b2c9dc 100644
--- a/pkg/cmd/factory/factory.go
+++ b/pkg/cmd/factory/factory.go
@@ -23,7 +23,10 @@ type Factory struct {
 func New(version string) *Factory {
    repo, _ := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true, EnableDotGitCommonDir: true})
    conf := config.New(nil, repo)
-   tk, _ := buildkite.NewTokenConfig(conf.APIToken(), false)
+   tk, err := buildkite.NewTokenConfig(conf.APIToken(), false)
+   if err != nil {
+       panic(err)
+   }

    return &Factory{
        Config:        conf,

I get the following output:

go run cmd/bk/main.go --help
panic: Invalid token, empty string supplied

goroutine 1 [running]:
github.com/buildkite/cli/v3/pkg/cmd/factory.New({0xb46d94, 0x3})
    /home/manny/t/bk/buildkite/cli/pkg/cmd/factory/factory.go:28 +0x4d3
main.mainRun()
    /home/manny/t/bk/buildkite/cli/cmd/bk/main.go:20 +0x25
main.main()
    /home/manny/t/bk/buildkite/cli/cmd/bk/main.go:14 +0x13
exit status 2

looks like the env var is not being picked up.

jradtilbrook commented 1 month ago

Oh nice find @mmlb šŸ‘

I think this is both an error in the docs as well as a bug in the code.

That env var is actually only meant for the generate step. The bk CLI is configured through a file with bk configure. But it still shouldn't be causing a panic like this anyway. We'll take a look into it