cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.1k stars 291 forks source link

cmd/cue: trim output differs with defaulted extendable list and k8s constraints #1294

Open robjtede opened 3 years ago

robjtede commented 3 years ago

What version of CUE are you using (cue version)?

$ cue version
cue version 0.4.0 darwin/amd64

What did you do?

I was searching for a way to merge lists that exist on the same key-path and came up with this ineffective attempt (still havent found one). Happened to run a trim on it.

// ./test.cue

import apps_v1 "k8s.io/api/apps/v1"

deployment: [string]: apps_v1.#Deployment // works if line removed
deployment: [string]: spec: template: spec: containers: [...{
    env: [...] | *[
        {name: "version", value: "value"},
        {name: "-", value:       "-"}, // works if line removed
        ...,
    ]
}]

deployment: ancile: spec: template: spec: containers: [{
    env: [
        {name: "version", value: "value"},
    ]
}]
cue trim test.cue

What did you expect to see?

No error.

What did you see instead?

$ cue trim test.cue
  {
      deployment: {
          ancile: {
              metadata?: {}
              spec: {
                  replicas?: null | int32
                  selector?: null | {}
                  template: {
                      metadata?: {}
                      spec: {
                          volumes?: []
                          initContainers?: []
                          containers: [
                              {
                                  ... // 5 identical elements
                                  ports?: []
                                  envFrom?: []
                                  env: [
                                      {
-                                         name: "version"
+                                         name: string
                                          value: "value"
                                          valueFrom?: null | {}
                                      }
                                  ]
                                  resources?: {}
                                  volumeMounts?: []
                                  ... // 12 identical elements
                              }
                          ]
                          ephemeralContainers?: []
                          restartPolicy?: string
                          ... // 30 identical elements
                      }
                  }
                  strategy?: {}
                  minReadySeconds?: int32
                  ... // 3 identical elements
              }
              kind?: string
              status?: {}
              ... // 1 identical elements
          }
      }
  }
Aborting trim, output differs after trimming. This is a bug! Use -i to force trim.

Forcing the trim results in this. Note the missing env var name.

// ./test.cue

import apps_v1 "k8s.io/api/apps/v1"

deployment: [string]: apps_v1.#Deployment // works if line removed
deployment: [string]: spec: template: spec: containers: [...{
    env: [...] | *[
        {name: "version", value: "value"},
        {name: "-", value:       "-"}, // works if line removed
        ...,
    ]
}]

deployment: ancile: spec: template: spec: containers: [{
    env: [
        {value: "value"},
    ]
}]

Trim works if one or the other of the marked lines are removed.

mpvl commented 2 years ago

The issue here is that trim picks the default value, even though that option will be deleted during unification.

myitcv commented 1 year ago

Dropping the now meaningless v0.4.x milestone. We have relabelled this from trim to roadmap/trim to indicate that a fix for this issue (and indeed all trim issues) requires a more substantial piece of work.