cue: support producing CUE_STATS_FILE via the Go API
Is your feature request related to a problem? Please describe.
CUE_STATS_FILE can be set be end users of the CLI to get helpful info about how their code runs in the evaluator. Users of the SDK cannot make use of this (accurately) because some of the stats API requires access to internal packages
https://github.com/cue-lang/cue/blob/901604a413b7a00bdd25d00049c46bfc9ebd8385/cmd/cue/cmd/root.go#L118
Describe the solution you'd like
CUE_STATS_FILE to work via the SDK
Describe alternatives you've considered
It doesn't seem possible to access stats directly because much of it requires access to the adt package
Additional context
hof user asked why this doesn't work with hof
I've brought up the same to Marcel in the past, this would indeed be nice to have. We just have to be careful to clearly document that the structure of the stats is unstable and may change with future CUE versions. We could make the struct fully opaque to hide the fields from the user entirely, or use a map[string]any, although that also makes using the stats directly in Go rather annoying. I'd be fine with exposing a plain struct with exported fields as long as the documentation is clear.
Also to add a bit of clarification: this needs some design to decide if the stats should be collected...
- globally; probably not, as that would not mesh well with concurrent or long lived programs
- per
cue.Contextfrom its creation; for example, via added methods on thecue.Contexttype - per operation, like one call to
Unify; however, it's unclear how that could be done in a backwards-compatible way
Currently the cmd/cue tool produces a global stats file from start to finish, which is OK because most of the work it does relates to a single CUE context. And at a high level, its commands like vet or export can be thought of as one operation. However, the Go API is way more flexible.
For the sake of tracking this work better, let's use https://github.com/cue-lang/cue/issues/2856 going forward. I've added all relevant info there now.