Skip to content

bake: hcl index function doesn't work as expected #2383

@Usual-Coder

Description

@Usual-Coder

Contributing guidelines

I've found a bug and checked that ...

  • ... the documentation does not mention anything about my problem
  • ... there are no open or closed issues that are related to my problem

Description

When given the expected parameters, a collection as first parameter and a value as second, index function should return the corresponding index (number), if the value is present in the collection?! Instead it raises an error (...)Call to function "index" failed: key for tuple must be number.(...) (presumably because of collection.go of go-cty)

index is in fact behaving exactly as the element function. What's the purpose of an index function expecting a number (aka the searched index itself) = why search for something you already know?!!!

Expected behaviour

Knowing the differences between element and index functions (per hcl docs and/or based on comments in collection.go and the corresponding functions mapped from go-cty):

element(list, index)

  • element retrieves a single element from a list (prefer the built-in index syntax list[index]).
  • The index is zero-based. This function produces an error if used with an empty list.

index(list, value)

  • index should find the element index for a given value in a list.
  • The returned index is zero-based.
  • This function should produce an error if the given value is not present in the list.

With bug.hcl (aka dummy config ... just for demonstration / nothing specifically related to annotations = same error no matter where you use index function):

target "bug" {
  annotations = [
    "foo=${element(["a", "b", "c"], 1)}",  // Work as expected/documented = returns "b"
    "bar=${index(["a", "b", "c"], "b")}",  // Raise an error !!! Should return 1
    // "baz=${index(["a", "b", "c"], 1)}", // NOK = same behavior as "element" function = if you already know the index ... what's the purpose of this function ?!!!
  ]
}

Expected:

$ docker buildx bake --file path/to/bug.hcl --print bug
[+] Building 0.0s (1/1) FINISHED
 => [internal] load local bake definitions                                   0.0s
 => => reading path/to/bug.hcl 479B / 479B                                   0.0s
{
  "group": {
    "default": {
      "targets": [
        "bug"
      ]
    }
  },
  "target": {
    "bug": {
      "annotations": [
        "foo=b",
        "bar=1"
      ],
      "context": ".",
      "dockerfile": "Dockerfile"
    }
  }
}

Actual behaviour

Raise an error with the same/previous bug.hcl:

$ docker buildx bake --file path/to/bug.hcl --print bug
[+] Building 0.0s (1/1) FINISHED
 => [internal] load local bake definitions                                   0.0s
 => => reading path/to/bug.hcl 479B / 479B                                   0.0s
path/to/bug.hcl:4
--------------------
   2 |       annotations = [
   3 |         "foo=${element(["a", "b", "c"], 1)}",  // Work as expected/documented = returns "b"
   4 | >>>     "bar=${index(["a", "b", "c"], "b")}",  // Raise an error !!! Should return 1
   5 |         // "baz=${index(["a", "b", "c"], 1)}", // NOK = same behavior as "element" function = if you already know the index ... what's the purpose of this function ?!!!
   6 |       ]
--------------------
ERROR: path/to/bug.hcl:4,12-18: Error in function call; Call to function "index" failed: key for tuple must be number., and 1 other diagnostic(s)

Buildx version

github.com/docker/buildx v0.13.1 7884339

Docker info

Client: Docker Engine - Community
 Version:    26.0.0
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.13.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.4.1
    Path:     /usr/local/lib/docker/cli-plugins/docker-compose
  scan: Docker Scan (Docker Inc.)
    Version:  v0.23.0
    Path:     /usr/libexec/docker/cli-plugins/docker-scan

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 2
 Server Version: 26.0.0
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.5.0-26-generic
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: ...
 Total Memory: ...
 Name: ...
 ID: ...
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Builders list

NAME/NODE     DRIVER/ENDPOINT   STATUS    BUILDKIT   PLATFORMS
default*      docker                                 
 \_ default    \_ default       running   v0.13.1    linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386

Configuration

target "bug" {
  annotations = [
    "foo=${element(["a", "b", "c"], 1)}",  // Work as expected/documented = returns "b"
    "bar=${index(["a", "b", "c"], "b")}",  // Raise an error !!! Should return 1
    // "baz=${index(["a", "b", "c"], 1)}", // NOK = same behavior as "element" function = if you already know the index ... what's the purpose of this function ?!!!
  ]
}

Build logs

No response

Additional info

First things first: bake feature is great 😍 Thanks for that 🙇

Since index is such a fundamental function, not having found an issue about it (and seen others not bothering/using it as/instead of element), I doubted myself for a while before filling this bug (not being fluent in Go, I stopped when digging deeper in go-cty sources became needed).

I don't know if it's related to how you're mapping go-cty in your hcl parser or the library itself ... but we should expect index or at least a function (no matter how it's named ... it should have a different behavior than the one expected from element ... the features subset being limited, may I say duplicates should not be expected?!) returning an index (as a number) ... not already known (the expected result shouldn't be provided as one of its parameters 🤐🤷)

Hope it can be fixed easily.

Thanks for everything.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions