Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARG is evaluated twice (once under +base, once under +target) #1448

Closed
alexcb opened this issue Nov 26, 2021 · 0 comments · Fixed by #1450
Closed

ARG is evaluated twice (once under +base, once under +target) #1448

alexcb opened this issue Nov 26, 2021 · 0 comments · Fixed by #1450
Assignees
Labels
type:bug Something isn't working

Comments

@alexcb
Copy link
Contributor

alexcb commented Nov 26, 2021

foo:
    FROM alpine
    ARG myvar
    WORKDIR /test
    RUN echo "myvar is $myvar\n; dir contents is $(ls -la)"

run:
    FROM alpine
    RUN echo bar > data
    BUILD +foo --myvar="$(cat data)"

under 0.5.24, it worked:

$ earthly-v0.5.24 --buildkit-volume-name=five-two-four +run
           buildkitd | Starting buildkit daemon as a docker container (earthly-buildkitd)...
           buildkitd | ...Done
              alpine | --> Load metadata linux/amd64
                +run | --> FROM alpine
                +run | [██████████] resolve docker.io/library/alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300 ... 100%
                +run | [██████████] sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3 ... 100%
                +run | [██████████] extracting sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3 ... 100%
                +run | --> RUN echo bar > data
                +run | --> ARG myvar = RUN $(cat data)
                +foo | myvar=bar
                +foo | --> WORKDIR /test
                +foo | myvar=bar
                +foo | --> RUN echo "myvar is $myvar\n; dir contents is $(ls -la)"
                +foo | myvar is bar\n; dir contents is total 8
                +foo | drwxr-xr-x    2 root     root          4096 Nov 26 19:04 .
                +foo | drwxr-xr-x    1 root     root          4096 Nov 26 19:04 ..

However under 0.6.0, it now fails:

 1. Init 🚀
————————————————————————————————————————————————————————————————————————————————

           buildkitd | Found buildkit daemon as docker container (earthly-buildkitd)
           buildkitd | Updated image available. Restarting buildkit daemon...
           buildkitd | ...Done


 2. Build 🔧
————————————————————————————————————————————————————————————————————————————————

Error: build target: build main: bkClient.Build: failed to solve: parse build args: parse build arg myvar=$(cat data): the first command has to be FROM, FROM DOCKERFILE, LOCALLY, ARG, BUILD or IMPORT

If however we introduce a FROM ... under the base, like:

FROM alpine

foo:
    FROM alpine
    RUN echo foo-data /data
    ARG myvar
    RUN echo "myvar is $myvar\n"

run:
    FROM alpine
    RUN echo run-data > /data
    BUILD +foo --myvar="$(cat /data)"

this works, and produces:

 1. Init 🚀
————————————————————————————————————————————————————————————————————————————————

           buildkitd | Found buildkit daemon as docker container (earthly-buildkitd)


 2. Build 🔧
————————————————————————————————————————————————————————————————————————————————

              alpine | --> Load metadata linux/amd64
               +base | --> FROM alpine
               +base | [██████████] 100% resolve docker.io/library/alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
               +base | [██████████] 100% sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3
               +base | [██████████] 100% extracting sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3
               +base | --> RUN echo base-data /data
               +base | base-data /data
                +run | --> ARG myvar = RUN $(cat /data)
                +run | cat: can't open '/data': No such file or directory
                +run | --> RUN echo run-data > /data
                +run | --> ARG myvar = RUN $(cat /data)
                +foo | --> RUN echo foo-data /data
                +foo | foo-data /data
                +foo | myvar=run-data
                +foo | --> RUN echo "myvar is $myvar\n"
                +foo | myvar is run-data\n
              output | --> exporting outputs

It's interesting to note: +run | cat: can't open '/data': No such file or directory indicates it's being evaluated under +base; however, when we make it to +foo | --> RUN echo "myvar is $myvar\n", it correctly prints out +foo | myvar is run-data\n. It looks like this is being (incorrectly) evaluated twice: once under +base and another time under +run.

@alexcb alexcb changed the title ARG ARG is evaluated twice (once under +base, once under +target) Nov 26, 2021
@alexcb alexcb self-assigned this Nov 26, 2021
@alexcb alexcb added the type:bug Something isn't working label Nov 26, 2021
alexcb added a commit that referenced this issue Nov 27, 2021
This fixes #1448

Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
alexcb added a commit that referenced this issue Nov 29, 2021
* Prevent parallel builds when arg is dynamic

This fixes #1448

Signed-off-by: Alex Couture-Beil <alex@earthly.dev>

* expand isSafeAsyncBuildArg checking for new-style format

Additionally expands to check for build args such as:

    BUILD +foo key="$(echo value)"

Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant