Skip to content

Commit d5527cf

Browse files
committed
internal/core/runtime: consistently follow CUE_DEBUG env var flags
The previous behavior followed the env var CUE_DEBUG if using the cmd/cue tool, but explicitly ignored the same env var if using the Go API via cuecontext.New. We have been moving towards consistent behavior where users of cuecontext.New see the same behavior in regards to following defaults and environment variables; for example, https://cuelang.org/cl/1202685 taught cuecontext.New to obey the env var CUE_EXPERIMENT=evalv3. Do the same for CUE_DEBUG, which now follows the environment variable by default for both cmd/cue and cuecontext.New. Go API users can use the cuecontext.CUE_DEBUG option to override any flags set by the environment, or they can change the environment via os.Setenv. This fixes CUE_DEBUG=sortfields so that it is obeyed by the Go API just as it already was for cmd/cue. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I18f5aadf1efce859f14d12c81ccbf3f17dd6949b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205202 Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 4258e9a commit d5527cf

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

cmd/cue/cmd/root.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"cuelang.org/go/internal"
3636
"cuelang.org/go/internal/core/adt"
3737
cueruntime "cuelang.org/go/internal/core/runtime"
38-
"cuelang.org/go/internal/cuedebug"
3938
"cuelang.org/go/internal/cueexperiment"
4039
"cuelang.org/go/internal/encoding"
4140
"cuelang.org/go/internal/filetypes"
@@ -105,9 +104,6 @@ func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error {
105104
if err := cueexperiment.Init(); err != nil {
106105
return err
107106
}
108-
if err := cuedebug.Init(); err != nil {
109-
return err
110-
}
111107
var opts []cuecontext.Option
112108
if wasmInterp != nil {
113109
opts = append(opts, cuecontext.Interpreter(wasmInterp))
@@ -138,10 +134,6 @@ func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error {
138134
const dev = internal.DevVersion
139135
(*cueruntime.Runtime)(c.ctx).SetVersion(internal.EvaluatorVersion(dev))
140136
}
141-
// Note that, while some debugging flags like "strict" or "sharing"
142-
// are only used by evalv3, some others like "sortfields" are used by all versions.
143-
(*cueruntime.Runtime)(c.ctx).SetDebugOptions(&cuedebug.Flags)
144-
145137
err = f(c, args)
146138

147139
// TODO(mvdan): support -memprofilerate like `go help testflag`.

cmd/cue/cmd/testdata/script/sortfields.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ exec cue export input.cue
2626
cmp stdout export.stdout
2727

2828
# Also ensure that it's wired up for the Go API.
29-
# TODO: it is not, currently.
3029
stdin input.cue
3130
exec cuectx_export
32-
cmp stdout export-unsorted.stdout
31+
cmp stdout export.stdout
3332

3433
-- input.cue --
3534
c: true

cue/cuecontext/cuecontext.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,15 @@ type Option struct {
3131
apply func(r *runtime.Runtime)
3232
}
3333

34-
// defaultFlags defines the debug flags that are set by default.
35-
var defaultFlags cuedebug.Config
36-
37-
func init() {
38-
if err := envflag.Parse(&defaultFlags, ""); err != nil {
39-
panic(err)
40-
}
41-
}
42-
43-
// New creates a new Context.
34+
// New creates a new [*cue.Context].
35+
//
36+
// The environment variables CUE_EXPERIMENT and CUE_DEBUG are followed to configure
37+
// the evaluator, just like the cue tool documents via [cue help environment].
38+
// You can override these settings via options like [EvaluatorVersion] and [CUE_DEBUG].
39+
//
40+
// [cue help environment]: https://cuelang.org/docs/reference/command/cue-help-environment/
4441
func New(options ...Option) *cue.Context {
4542
r := runtime.New()
46-
// Ensure default behavior if the flags are not set explicitly.
47-
r.SetDebugOptions(&defaultFlags)
4843
for _, o := range options {
4944
o.apply(r)
5045
}

internal/core/runtime/runtime.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func (r *Runtime) BuildData(b *build.Instance) (x interface{}, ok bool) {
5757
return x, ok
5858
}
5959

60-
// New is short for [NewWithSettings] while obeying `CUE_EXPERIMENT=evalv3`
61-
// for the evaluator version and using zero [cuedebug] flags.
60+
// New creates a new Runtime obeying the CUE_EXPERIMENT and CUE_DEBUG flags set
61+
// via environment variables.
6262
func New() *Runtime {
6363
r := &Runtime{}
6464
r.Init()
@@ -69,9 +69,11 @@ func New() *Runtime {
6969
// debug flags. The builtins registered with RegisterBuiltin are available for
7070
// evaluation.
7171
func NewWithSettings(v internal.EvaluatorVersion, flags cuedebug.Config) *Runtime {
72-
r := &Runtime{flags: flags}
73-
r.Init()
72+
r := New()
73+
// Override the evaluator version and debug flags derived from env vars
74+
// with the explicit arguments given to us here.
7475
r.version = v
76+
r.SetDebugOptions(&flags)
7577
return r
7678
}
7779

@@ -119,4 +121,10 @@ func (r *Runtime) Init() {
119121
r.version = internal.DefaultVersion
120122
}
121123
r.topoSort = cueexperiment.Flags.TopoSort
124+
125+
// By default we follow the environment's CUE_DEBUG settings,
126+
// which can be overriden via [Runtime.SetDebugOptions],
127+
// such as with the API option [cuelang.org/go/cue/cuecontext.CUE_DEBUG].
128+
cuedebug.Init()
129+
r.SetDebugOptions(&cuedebug.Flags)
122130
}

0 commit comments

Comments
 (0)