this repo has no description
0
fork

Configure Feed

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

cmd/cue: add CUE_EXPERIMENT=cmdreferencepkg

When enabled, `cue cmd` tasks must be defined by referencing a task
from one of the tool packages, and trying to define a task directly
by only defining `kind` or `$id` will not work.

The experiment is disabled by default for v0.13 as it is very late
in the release cycle and too many users would be broken
with too little notice. We plan to flip the default for v0.14.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I246acf526910b1486ab9c41b96abe56dd1980eab
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214987
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+67 -20
+29 -6
cmd/cue/cmd/custom.go
··· 32 32 "cuelang.org/go/cue/errors" 33 33 "cuelang.org/go/cue/token" 34 34 "cuelang.org/go/internal/core/adt" 35 + "cuelang.org/go/internal/cueexperiment" 35 36 itask "cuelang.org/go/internal/task" 36 37 "cuelang.org/go/internal/value" 37 38 _ "cuelang.org/go/pkg/tool/cli" // Register tasks ··· 253 254 if v.Kind() != cue.StructKind { 254 255 return false 255 256 } 256 - if v.Lookup("$id").Exists() { 257 - return true 257 + 258 + id := v.LookupPath(cue.MakePath(cue.Str("$id"))) 259 + 260 + cueexperiment.Init() 261 + if !cueexperiment.Flags.CmdReferencePkg { 262 + // In the old mode, $id or kind being present is enough. 263 + if id.Exists() { 264 + return true 265 + } 266 + // Is it an existing legacy kind. 267 + str, err := v.Lookup("kind").String() 268 + _, ok := legacyKinds[str] 269 + return err == nil && ok 258 270 } 259 - // Is it an existing legacy kind. 260 - str, err := v.Lookup("kind").String() 261 - _, ok := legacyKinds[str] 262 - return err == nil && ok 271 + 272 + // In the new mode, $id must exist and be a reference to the hidden _id field from a tool package. 273 + // TODO: surely we can check this via id.BuildInstance().ImportPath, but it's not obvious how to do so. 274 + // Or perhaps add a method on cue.Value to get the package info directly, like the import path. 275 + if !id.Exists() { 276 + return false 277 + } 278 + fromToolsPackage := false 279 + id.Walk(func(v cue.Value) bool { 280 + if strings.HasPrefix(v.Pos().Filename(), "tool/") { 281 + fromToolsPackage = true 282 + } 283 + return true 284 + }, nil) 285 + return fromToolsPackage 263 286 } 264 287 265 288 var legacyKinds = map[string]string{
+12 -6
cmd/cue/cmd/help.go
··· 309 309 toposort (default true) 310 310 Enable topological sorting of struct fields. 311 311 Provide feedback via https://cuelang.org/issue/3558 312 + cmdreferencepkg 313 + Require referencing imported tool packages to declare "cue cmd" tasks. 312 314 313 315 CUE_DEBUG 314 316 Comma-separated list of debug flags to enable or disable, such as: ··· 778 780 outputs. Outputs are typically filled out by the task 779 781 implementation as the task completes. 780 782 783 + Tasks are found as regular fields underneath the top-level "command" field, 784 + excluding hidden fields and definitions. Note that regular fields 785 + may be produced by comprehensions or from the result of other tasks, 786 + which can result in discovering more tasks. 787 + To avoid this, set the CUE_EXPERIMENT=cmdreferencepkg experiment flag. 788 + 781 789 Inputs of tasks my refer to outputs of other tasks. The cue tool 782 790 does a static analysis of the configuration and only starts tasks 783 791 that are fully specified. Upon completion of each task, cue ··· 908 916 909 917 // A Task defines a step in the execution of a command. 910 918 Task: { 911 - $type: "tool.Task" // legacy field 'kind' still supported for now. 912 - 913 - // $id indicates the operation to run. It must be of the form 914 - // packagePath.Operation. 919 + // $id indicates the operation to run. Do not use this field directly; 920 + // instead unify with a task imported from one of the tool packages. 915 921 $id: =~#"\."# 916 922 917 - // $after can be used to specify a task is run after another one, when 918 - // it does not otherwise refer to an output of that task. 923 + // $after can be used to specify a task is run after another one, 924 + // when it does not otherwise refer to an output of that task. 919 925 $after?: Task | [...Task] 920 926 } 921 927 `,
+14
cmd/cue/cmd/testdata/script/cmd_referencepkg.txtar
··· 33 33 exec cue cmd useCliPrintCopy 34 34 stdout -count=1 '^hello$' 35 35 36 + # Enabling the cmdreferencepkg experiment, tasks only work when referencing imported tool packages. 37 + env CUE_EXPERIMENT=cmdreferencepkg 38 + 39 + exec cue cmd inputKindOutside 40 + stdout -count=1 '^hello$' 41 + ! stdout '^command from input$' 42 + 43 + exec cue cmd inputDollarIdOutside 44 + stdout -count=1 '^hello$' 45 + ! stdout '^command from input$' 46 + 47 + ! exec cue cmd useCliPrintCopy 48 + stderr 'no tasks found' 49 + 36 50 -- input_kind.json -- 37 51 {"cmd": {"kind": "print", "text": "command from input"}, "data": "hello"} 38 52 -- input_dollar_id.json --
+4
internal/cueexperiment/exp.go
··· 23 23 // TODO(v0.14): deprecate this flag to forbid disabling this feature. 24 24 TopoSort bool `envflag:"default:true"` 25 25 26 + // CmdReferencePkg requires referencing an imported tool package to declare tasks. 27 + // Otherwise, declaring tasks by setting "$id" or "kind" string fields is allowed. 28 + CmdReferencePkg bool 29 + 26 30 // The flags below describe completed experiments; they can still be set 27 31 // as long as the value aligns with the final behavior once the experiment finished. 28 32 // Breaking users who set such a flag seems unnecessary,
+4 -4
pkg/tool/pkg.go
··· 69 69 // 70 70 // // A Task defines a step in the execution of a command. 71 71 // Task: { 72 - // // $id indicates the operation to run. It must be of the form 73 - // // packagePath.Operation. 72 + // // $id indicates the operation to run. Do not use this field directly; 73 + // // instead unify with a task imported from one of the tool packages. 74 74 // $id: =~#"\."# 75 75 // 76 - // // $after can be used to specify a task is run after another one, when 77 - // // it does not otherwise refer to an output of that task. 76 + // // $after can be used to specify a task is run after another one, 77 + // // when it does not otherwise refer to an output of that task. 78 78 // $after?: Task | [...Task] 79 79 // } 80 80 //
+4 -4
pkg/tool/tool.cue
··· 64 64 65 65 // A Task defines a step in the execution of a command. 66 66 Task: { 67 - // $id indicates the operation to run. It must be of the form 68 - // packagePath.Operation. 67 + // $id indicates the operation to run. Do not use this field directly; 68 + // instead unify with a task imported from one of the tool packages. 69 69 $id: =~#"\."# 70 70 71 - // $after can be used to specify a task is run after another one, when 72 - // it does not otherwise refer to an output of that task. 71 + // $after can be used to specify a task is run after another one, 72 + // when it does not otherwise refer to an output of that task. 73 73 $after?: Task | [...Task] 74 74 } 75 75