Skip to content

Commit

Permalink
Don't expand RUN heredocs ourselves, let the shell do it
Browse files Browse the repository at this point in the history
When handling RUN instructions that use heredoc syntax, don't bother
interpolating environment variables and argument values, and let the
command that's running handle it.

Signed-off-by: Nalin Dahyabhai <[email protected]>
  • Loading branch information
nalind committed Apr 11, 2024
1 parent 392c64a commit 120b10c
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 5 deletions.
7 changes: 7 additions & 0 deletions tests/conformance/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3102,6 +3102,13 @@ var internalTestCases = []testCase{
contextDir: "multistage/copyback",
dockerUseBuildKit: true,
},

{
name: "heredoc-quoting",
dockerfile: "Dockerfile.heredoc-quoting",
dockerUseBuildKit: true,
fsSkip: []string{"(dir):etc:(dir):hostname"}, // buildkit does not create a phantom /etc/hostname
},
}

func TestCommit(t *testing.T) {
Expand Down
215 changes: 215 additions & 0 deletions tests/conformance/testdata/Dockerfile.heredoc-quoting
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
FROM busybox
ARG argA=argvA
ENV varA=valueA

# An argument, an environment variable, and one set in the heredoc
RUN <<EOF
varB=valueB
touch /run-argA=$argA.unquoted1.txt
touch /run-varA=$varA.unquoted1.txt
touch /run-varB=$varB.unquoted1.txt
EOF

# An argument, an environment variable, and one set in the heredoc
RUN <<EOF
varB=valueB
touch /run-argA="$argA".unquoted2.txt
touch /run-varA="$varA".unquoted2.txt
touch /run-varB="$varB".unquoted2.txt
EOF

# An argument, an environment variable overridden in the heredoc, and one set in the heredoc
RUN <<EOF
varA=valueA2
varB=valueB
touch /run-argA="$argA".unquoted3.txt
touch /run-varA="$varA".unquoted3.txt
touch /run-varB="$varB".unquoted3.txt
EOF

# An overridden argument, an environment variable overridden in the heredoc, and one set in the heredoc
RUN <<EOF
argA=argvA2
varA=valueA2
varB=valueB
touch /run-argA="$argA".unquoted4.txt
touch /run-varA="$varA".unquoted4.txt
touch /run-varB="$varB".unquoted4.txt
EOF

# An argument, an environment variable, and one set in the heredoc
RUN <<"EOF"
varB=valueB
touch /run-argA=$argA.quoted1.txt
touch /run-varA=$varA.quoted1.txt
touch /run-varB=$varB.quoted1.txt
EOF

# An argument, an environment variable, and one set in the heredoc
RUN <<"EOF"
varB=valueB
touch /run-argA="$argA".quoted2.txt
touch /run-varA="$varA".quoted2.txt
touch /run-varB="$varB".quoted2.txt
EOF

# An argument, an environment variable overridden in the heredoc, and one set in the heredoc
RUN <<"EOF"
varA=valueA2
varB=valueB
touch /run-argA="$argA".quoted3.txt
touch /run-varA="$varA".quoted3.txt
touch /run-varB="$varB".quoted3.txt
EOF

# An overridden argument, an environment variable overridden in the heredoc, and one set in the heredoc
RUN <<"EOF"
argA=argvA2
varA=valueA2
varB=valueB
touch /run-argA="$argA".quoted4.txt
touch /run-varA="$varA".quoted4.txt
touch /run-varB="$varB".quoted4.txt
EOF

# An argument, an environment variable, and one set in the heredoc
COPY <<EOF /copy-unquoted1.txt
varB=valueB
touch /argA=$argA
touch /varA=$varA
touch /varB=$varB
EOF

# An argument, an environment variable, and one set in the heredoc
COPY <<EOF /copy-unquoted2.txt
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable overridden in the heredoc, and one set in the heredoc
COPY <<EOF /copy-unquoted3.txt
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An overridden argument, an environment variable overridden in the heredoc, and one set in the heredoc
COPY <<EOF /copy-unquoted4.txt
argA=argvA2
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable, and one set in the heredoc
COPY <<"EOF" /copy-quoted1.txt
varB=valueB
argA=$argA
varA=$varA
varB=$varB
EOF

# An argument, an environment variable, and one set in the heredoc
COPY <<"EOF" /copy-quoted2.txt
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable overridden in the heredoc, and one set in the heredoc
COPY <<"EOF" /copy-quoted3.txt
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An overridden argument, an environment variable overridden in the heredoc, and one set in the heredoc
COPY <<"EOF" /copy-quoted4.txt
argA=argvA2
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable, and one set in the heredoc
ADD <<EOF /add-unquoted1.txt
varB=valueB
touch /argA=$argA
touch /varA=$varA
touch /varB=$varB
EOF

# An argument, an environment variable, and one set in the heredoc
ADD <<EOF /add-unquoted2.txt
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable overridden in the heredoc, and one set in the heredoc
ADD <<EOF /add-unquoted3.txt
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An overridden argument, an environment variable overridden in the heredoc, and one set in the heredoc
ADD <<EOF /add-unquoted4.txt
argA=argvA2
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable, and one set in the heredoc
ADD <<"EOF" /add-quoted1.txt
varB=valueB
argA=$argA
varA=$varA
varB=$varB
EOF

# An argument, an environment variable, and one set in the heredoc
ADD <<"EOF" /add-quoted2.txt
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An argument, an environment variable overridden in the heredoc, and one set in the heredoc
ADD <<"EOF" /add-quoted3.txt
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

# An overridden argument, an environment variable overridden in the heredoc, and one set in the heredoc
ADD <<"EOF" /add-quoted4.txt
argA=argvA2
varA=valueA2
varB=valueB
argA="$argA"
varA="$varA"
varB="$varB"
EOF

RUN touch -r /etc/passwd /*.txt
11 changes: 6 additions & 5 deletions vendor/github.com/openshift/imagebuilder/dispatchers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 120b10c

Please sign in to comment.