this repo has no description
0
fork

Configure Feed

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

cmd/cue: rip out cue.Instance and cue.Merge from 'cue cmd'

As can be seen by the cmd_many.txtar test just added,
we still support the scenario where 'cue cmd' is given multiple packages
by unifying all of their values together, rather than using cue.Merge.

Other than cue.Merge having been deprecated with DO NOT USE warnings
since early 2021, it seemed to have had somewhat odd semantics.
Structs in CUE are open by default, so unifying {} with {b: "b"}
should result in {b: "b"} without any issue, as we do now.

The merge semantics, which aren't documented and deprecated, disagree.
However, the passing test mimicking what a few users have reported using
still passes just as it did before, and the new semantics are now
well understood - if not documented as such yet - so this seems fine.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ia3195643612f629ac61a36f8469945ee345e0719
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193718
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>

+20 -76
+12 -63
cmd/cue/cmd/common.go
··· 39 39 "cuelang.org/go/internal/core/adt" 40 40 "cuelang.org/go/internal/encoding" 41 41 "cuelang.org/go/internal/filetypes" 42 - "cuelang.org/go/internal/value" 43 42 ) 44 43 45 44 var requestedVersion = os.Getenv("CUE_SYNTAX_OVERRIDE") ··· 765 764 return insts 766 765 } 767 766 768 - func buildToolInstances(binst []*build.Instance) ([]*cue.Instance, error) { 769 - instances := cue.Build(binst) 770 - for _, inst := range instances { 771 - if inst.Err != nil { 772 - return nil, inst.Err 773 - } 774 - } 775 - 776 - // TODO check errors after the fact in case of ignore. 777 - for _, inst := range instances { 778 - if err := inst.Value().Validate(); err != nil { 779 - return nil, err 780 - } 781 - } 782 - return instances, nil 783 - } 784 - 785 - func buildTools(cmd *Command, args []string) (*cue.Instance, error) { 767 + func buildTools(cmd *Command, args []string) (cue.Value, error) { 786 768 cfg, err := defaultConfig() 787 769 if err != nil { 788 - return nil, err 770 + return cue.Value{}, err 789 771 } 790 772 loadCfg := *cfg.loadCfg 791 773 loadCfg.Tools = true 792 774 f := cmd.cmd.Flags() 793 775 setTags(&loadCfg, f, cmd.cmd.Parent().Flags()) 794 776 795 - binst := loadFromArgs(args, &loadCfg) 796 - if len(binst) == 0 { 797 - return nil, nil 798 - } 799 - included := map[string]bool{} 800 - 801 - ti := binst[0].Context().NewInstance(binst[0].Root, nil) 802 - for _, inst := range binst { 803 - k := 0 804 - for _, f := range inst.Files { 805 - if strings.HasSuffix(f.Filename, "_tool.cue") { 806 - if !included[f.Filename] { 807 - _ = ti.AddSyntax(f) 808 - included[f.Filename] = true 809 - } 810 - continue 811 - } 812 - inst.Files[k] = f 813 - k++ 814 - } 815 - inst.Files = inst.Files[:k] 816 - } 777 + builds := loadFromArgs(args, &loadCfg) 817 778 818 - insts, err := buildToolInstances(binst) 779 + values, err := cmd.ctx.BuildInstances(builds) 819 780 if err != nil { 820 - return nil, err 781 + return cue.Value{}, err 821 782 } 822 - 823 - inst := insts[0] 824 - if len(insts) > 1 { 825 - inst = cue.Merge(insts...) 783 + // 'cue cmd' with multiple package arguments, such as './...', 784 + // currently runs a single command on the unification of all given packages. 785 + // See https://cuelang.org/issue/1325. 786 + v := values[0] 787 + for _, v2 := range values[1:] { 788 + v = v.Unify(v2) 826 789 } 827 - 828 - r := value.ConvertToRuntime(inst.Value().Context()) 829 - for _, b := range binst { 830 - for _, i := range b.Imports { 831 - if _, err := r.Build(i); err != nil { 832 - return nil, err 833 - } 834 - } 835 - } 836 - 837 - // Set path equal to the package from which it is loading. 838 - ti.ImportPath = binst[0].ImportPath 839 - 840 - inst = inst.Build(ti) 841 - return inst, inst.Err 790 + return v, nil 842 791 } 843 792 844 793 func shortFile(root string, f *build.File) string {
+5 -9
cmd/cue/cmd/custom.go
··· 43 43 const commandSection = "command" 44 44 45 45 func lookupString(obj cue.Value, key, def string) string { 46 - str, err := obj.Lookup(key).String() 46 + str, err := obj.LookupPath(cue.MakePath(cue.Str(key))).String() 47 47 if err == nil { 48 48 def = str 49 49 } ··· 62 62 // addCustomCommands iterates over all commands defined under field typ 63 63 // and adds them as cobra subcommands to cmd. 64 64 // The func is only used in `cue help cmd`, which doesn't show errors. 65 - func addCustomCommands(c *Command, cmd *cobra.Command, typ string, tools *cue.Instance) { 66 - commands := tools.Lookup(typ) 65 + func addCustomCommands(c *Command, cmd *cobra.Command, typ string, tools cue.Value) { 66 + commands := tools.LookupPath(cue.MakePath(cue.Str(typ))) 67 67 if !commands.Exists() { 68 68 return 69 69 } ··· 80 80 } 81 81 82 82 // customCommand creates a cobra.Command out of a CUE command definition. 83 - func customCommand(c *Command, typ, name string, tools *cue.Instance) (*cobra.Command, error) { 84 - if tools == nil { 85 - return nil, errors.New("no commands defined") 86 - } 87 - 83 + func customCommand(c *Command, typ, name string, tools cue.Value) (*cobra.Command, error) { 88 84 // TODO: validate allowing incomplete. 89 85 o := tools.Lookup(typ, name) 90 86 if !o.Exists() { ··· 149 145 return sub, nil 150 146 } 151 147 152 - func doTasks(cmd *Command, typ, command string, root *cue.Instance) error { 148 + func doTasks(cmd *Command, typ, command string, root cue.Value) error { 153 149 cfg := &flow.Config{ 154 150 Root: cue.MakePath(cue.Str(commandSection), cue.Str(command)), 155 151 InferTasks: true,
+3 -4
cmd/cue/cmd/testdata/script/cmd_many.txtar
··· 20 20 exec cue export ./... 21 21 cmp stdout stdout.export-post.golden 22 22 23 - # Attempt to ls in this state, which fails, as each of the CUE packages 24 - # hold an items list with different lengths. 25 - ! exec cue cmd ls ./... 26 - stderr 'itemsList: incompatible list lengths \(0 and 1\)' 23 + # Verify that cue cmd ls ./... still works the same. 24 + exec cue cmd ls ./... 25 + cmp stdout stdout.ls-pre.golden 27 26 28 27 -- cue.mod/module.cue -- 29 28 module: "mod.com"