diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go index 11bea7b1723..dea05c9129c 100644 --- a/cmd/cue/cmd/root.go +++ b/cmd/cue/cmd/root.go @@ -85,20 +85,16 @@ type Stats struct { } } -var hasRunCommand bool - func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { - // The init work below should only happen once per cmd/cue invocation; - // if it happens twice, we'll misbehave by writing stats twice - // or miscalculating pprof and stats numbers. - if hasRunCommand { - panic("cmd/cue/cmd.mkRunE init ran twice") - } - hasRunCommand = true - c.Command = cmd + // Note that the setup code below should only run once per cmd/cue invocation. + // This is because part of it modifies the global state like cueexperiment, + // but also because running this twice may result in broken CUE stats or Go profiles. + // However, users of the exposed Go API may be creating and running many commands, + // so we can't panic or fail if this setup work happens twice. + statsEnc, err := statsEncoder(c) if err != nil { return err diff --git a/cmd/cue/cmd/root_test.go b/cmd/cue/cmd/root_test.go index 648e55a82ce..30737858911 100644 --- a/cmd/cue/cmd/root_test.go +++ b/cmd/cue/cmd/root_test.go @@ -56,10 +56,7 @@ func TestCommand(t *testing.T) { qt.Assert(t, qt.Equals(buf.String(), "{\n \"foo\": 123\n}\n")) // Verify that we can use the API exposed by the embedded cobra command. - // TODO(mvdan): panics due to https://cuelang.org/issue/3458; fix it. - qt.Assert(t, qt.PanicMatches(func() { - c, err = cmd.New([]string{"fmt", "nosuchfile.cue"}) - err = c.Execute() - qt.Assert(t, qt.IsNotNil(err)) - }, "cmd/cue/cmd.mkRunE init ran twice")) + c, err = cmd.New([]string{"fmt", "nosuchfile.cue"}) + err = c.Execute() + qt.Assert(t, qt.IsNotNil(err)) }