Skip to content

Commit 8c21b84

Browse files
committed
cmd/cue/cmd: undo panic when multiple commands are run in one process
This should resolve the panics encountered by cmd/cue/cmd Go API users who were running multiple commands in a single Go process, which is a fairly reasonable use case which worked before. This mostly reverts https://cuelang.org/cl/1198496, but it adds a note for future reference to the relevant code and updates the new test. I briefly considered alternative routes such as only doing the mkRunE setup work on the first command run. However, it's not clear to me that such a strategy would lead to better behavior for those users. For example, if they rely on the collection of CUE stats, it would be very odd if only the first command run were to produce them. Fixes #3458. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Iadb274de8c3d233796ee6feb33e40ffc03bc0f08 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202646 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202871
1 parent 00295ad commit 8c21b84

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

cmd/cue/cmd/root.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,16 @@ type Stats struct {
8585
}
8686
}
8787

88-
var hasRunCommand bool
89-
9088
func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error {
9189
return func(cmd *cobra.Command, args []string) error {
92-
// The init work below should only happen once per cmd/cue invocation;
93-
// if it happens twice, we'll misbehave by writing stats twice
94-
// or miscalculating pprof and stats numbers.
95-
if hasRunCommand {
96-
panic("cmd/cue/cmd.mkRunE init ran twice")
97-
}
98-
hasRunCommand = true
99-
10090
c.Command = cmd
10191

92+
// Note that the setup code below should only run once per cmd/cue invocation.
93+
// This is because part of it modifies the global state like cueexperiment,
94+
// but also because running this twice may result in broken CUE stats or Go profiles.
95+
// However, users of the exposed Go API may be creating and running many commands,
96+
// so we can't panic or fail if this setup work happens twice.
97+
10298
statsEnc, err := statsEncoder(c)
10399
if err != nil {
104100
return err

cmd/cue/cmd/root_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ func TestCommand(t *testing.T) {
5656
qt.Assert(t, qt.Equals(buf.String(), "{\n \"foo\": 123\n}\n"))
5757

5858
// Verify that we can use the API exposed by the embedded cobra command.
59-
// TODO(mvdan): panics due to https://cuelang.org/issue/3458; fix it.
60-
qt.Assert(t, qt.PanicMatches(func() {
61-
c, err = cmd.New([]string{"fmt", "nosuchfile.cue"})
62-
err = c.Execute()
63-
qt.Assert(t, qt.IsNotNil(err))
64-
}, "cmd/cue/cmd.mkRunE init ran twice"))
59+
c, err = cmd.New([]string{"fmt", "nosuchfile.cue"})
60+
err = c.Execute()
61+
qt.Assert(t, qt.IsNotNil(err))
6562
}

0 commit comments

Comments
 (0)