this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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í <mvdan@mvdan.cc>
Change-Id: Iadb274de8c3d233796ee6feb33e40ffc03bc0f08
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202646
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+9 -16
+6 -10
cmd/cue/cmd/root.go
··· 85 85 } 86 86 } 87 87 88 - var hasRunCommand bool 89 - 90 88 func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error { 91 89 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 - 100 90 c.Command = cmd 91 + 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. 101 97 102 98 statsEnc, err := statsEncoder(c) 103 99 if err != nil {
+3 -6
cmd/cue/cmd/root_test.go
··· 56 56 qt.Assert(t, qt.Equals(buf.String(), "{\n \"foo\": 123\n}\n")) 57 57 58 58 // 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)) 65 62 }