Skip to content

Bake segfault when setting variable defaulting to null from env #1362

@skycaptain

Description

@skycaptain

When setting a variable which defaults to null, I'm getting the following segfault.

docker-bake.hcl:

variable "FOO" { default = null }
target "default" { }

Error:

$ FOO="bar" docker buildx bake --print
[+] Building 0.0s (0/0)                                                                                                                                                                                                                           
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x845567]

goroutine 1 [running]:
github.com/docker/buildx/bake/hclparser.(*parser).resolveValue(0xc000531720, {0xc000908980, 0x3})
        github.com/docker/buildx/bake/hclparser/hclparser.go:244 +0xe27
github.com/docker/buildx/bake/hclparser.Parse({0x221aec8, 0xc000895698}, {0x1ff3f90?, 0xc000759d70?, 0x1ff0a50?}, {0x1d408e0?, 0xc000759da0})
        github.com/docker/buildx/bake/hclparser/hclparser.go:358 +0xbcd
github.com/docker/buildx/bake.ParseFiles({0xc0004e05a0, 0x1, 0x19?}, 0x18?)
        github.com/docker/buildx/bake/bake.go:238 +0xa45
github.com/docker/buildx/bake.ReadTargets({0x13?, 0x0?}, {0xc0004e05a0?, 0xc000056004?, 0x59?}, {0xc000837a20, 0x1, 0xc00079f801?}, {0x0, 0x0, ...}, ...)
        github.com/docker/buildx/bake/bake.go:88 +0x52
github.com/docker/buildx/commands.runBake({0x222a098, 0xc00017a2a0}, {0xc0003c2520, 0x0, 0x1}, {{0x3145700, 0x0, 0x0}, {0x0, 0x0, ...}, ...})
        github.com/docker/buildx/commands/bake.go:106 +0xb2c
github.com/docker/buildx/commands.bakeCmd.func1(0xc000733900?, {0xc0003c2520, 0x0, 0x1})
        github.com/docker/buildx/commands/bake.go:182 +0x205
github.com/spf13/cobra.(*Command).execute(0xc000733900, {0xc000409400, 0x1, 0x1})
        github.com/spf13/cobra@v1.5.0/command.go:872 +0x694
github.com/spf13/cobra.(*Command).ExecuteC(0xc000366500)
        github.com/spf13/cobra@v1.5.0/command.go:990 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/cobra@v1.5.0/command.go:918
github.com/docker/cli/cli-plugins/plugin.RunPlugin(0x1bf2840?, 0xc000733400, {{0x1ed207c, 0x5}, {0x1edbec5, 0xb}, {0x21f811c, 0x6}, {0x0, 0x0}, ...})
        github.com/docker/cli@v20.10.17+incompatible/cli-plugins/plugin/plugin.go:51 +0x130
main.runPlugin(0x1f12b08?)
        github.com/docker/buildx/cmd/buildx/main.go:45 +0x105
main.main()
        github.com/docker/buildx/cmd/buildx/main.go:62 +0xf9

docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.9.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.11.2
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
  scan: Docker Scan (Docker Inc.)
    Version:  v0.17.0
    Path:     /usr/libexec/docker/cli-plugins/docker-scan

My usecase: I need to pass an ARG to my Dockerfile, which my be undefined, an empty string or a string. With plain Docker this is supported by just leaving the ARG empty, e.g. in my Dockerfile ARG FOO. If FOO is not defined via --build-args, it is not defined as environment variable either. To support a similar behaviour with buildx, this is the docker-bake.hcl I'm came up so far:

function "objectcompact" {
  // Takes an object and returns a new object with elements removed whose
  // value equals null
  params = [obj]
  result = {for k, v in obj : k => v if v != null}
}

variable "FOO" { default = null }

target "default" {
    args = objectcompact({
      FOO = FOO
    })
}

Dockerfile:

FROM debian
ARG FOO
RUN export -p

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions