Skip to content

Commit 3f84ba6

Browse files
committed
cmd/cue: do not initialize cmd.Command twice in cue cmd
This allows `cue cmd --cpuprofile` to work, avoids other issues such as initializing cueexperiment and cuedebug twice unnecessarily, and also adds an assertion so that future double-init bugs like this one don't go unnoticed for a while. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I97dd014cb2997f20122dd761822a32e483d9e951 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198496 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Matthew Sackman <[email protected]>
1 parent 17b3e52 commit 3f84ba6

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

cmd/cue/cmd/custom.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ func customCommand(c *Command, typ, name string, tools *cue.Instance) (*cobra.Co
136136
Use: usage,
137137
Short: lookupString(o, "$short", short),
138138
Long: lookupString(o, "$long", long),
139-
RunE: mkRunE(c, func(cmd *Command, args []string) error {
139+
// Note that we don't use mkRunE here, as the parent func is already wrapped by
140+
// another mkRunE call, and all Command initialization has already happened.
141+
RunE: func(cmd *cobra.Command, args []string) error {
140142
// TODO:
141143
// - parse flags and env vars
142144
// - constrain current config with config section
143145

144-
return doTasks(cmd, name, tools)
145-
}),
146+
return doTasks(c, name, tools)
147+
},
146148
}
147149

148150
// TODO: implement var/flag handling.

cmd/cue/cmd/root.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,18 @@ type Stats struct {
8585
}
8686
}
8787

88+
var hasRunCommand bool
89+
8890
func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error {
8991
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+
90100
c.Command = cmd
91101

92102
statsEnc, err := statsEncoder(c)

cmd/cue/cmd/testdata/script/pprof.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ exists cpu.out
1515
exists mem.out
1616

1717
# The flags should still work with 'cue cmd', which runs a command within a command.
18-
# TODO: this is currently broken, as we try to start pprof twice.
1918
rm cpu.out
20-
! exec cue cmd --cpuprofile=cpu.out hello
21-
stderr 'cpu profiling already in use'
19+
exec cue cmd --cpuprofile=cpu.out hello
2220
go tool pprof -top cpu.out
2321
stdout 'Type: cpu'
2422

0 commit comments

Comments
 (0)