Skip to content

Commit

Permalink
Display a warning when ~ is used in a COPY destination (#3540)
Browse files Browse the repository at this point in the history
Got some feedback about how `~` is expected to expand to a home
directory.
However this is not something that Earthly currently supports (see #1789
for more info).
This PR will display a warning if a `~` is used to make it clear to the
user.
  • Loading branch information
idodod committed Nov 28, 2023
1 parent 3bcb139 commit 4b4aae9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to [Earthly](https://github.com/earthly/earthly) will be doc

## Unreleased

### Added
- A warning when a `COPY` destination includes a tilde (~). Related to [#1789](https://github.com/earthly/earthly/issues/1789).

## v0.7.22 - 2023-11-27

### Added
Expand Down
7 changes: 7 additions & 0 deletions earthfile2llb/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"path"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -969,6 +970,12 @@ func (i *Interpreter) handleCopy(ctx context.Context, cmd spec.Command) error {
if !allClassical && !allArtifacts {
return i.errorf(cmd.SourceLocation, "combining artifacts and build context arguments in a single COPY command is not allowed: %v", srcs)
}

if slices.ContainsFunc(strings.Split(dest, "/"), func(s string) bool {
return s == "~" || strings.HasPrefix(s, "~")
}) {
i.console.Warnf(`destination path %q contains a "~" which does not expand to a home directory`, dest)
}
if allArtifacts {
if dest == "" || dest == "." || len(srcs) > 1 {
dest += string("/") // TODO needs to be the containers platform, not the earthly hosts platform. For now, this is always Linux.
Expand Down
12 changes: 11 additions & 1 deletion tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ga-no-qemu-group1:
BUILD +privileged-test
BUILD +copy-test
BUILD +copy-test-verbose-output
BUILD +copy-tilde-test
BUILD +copy-keep-own-test
BUILD +git-clone-test
BUILD +builtin-args-invalid-default-test
Expand Down Expand Up @@ -227,7 +228,7 @@ copy-test:
echo "1" > in/sub/1/file && \
echo "2" > in/sub/2/file && \
echo "sub" > in/sub/file
DO +RUN_EARTHLY --earthfile=copy.earth
DO +RUN_EARTHLY --earthfile=copy.earth --output_does_not_contain="which does not expand to a home directory"

copy-test-verbose-output:
RUN mkdir -p subdir/a.txt && \
Expand Down Expand Up @@ -264,6 +265,15 @@ copy-keep-own-test:
DO +RUN_EARTHLY --target=+test-known-user --earthfile=copy-keep-own.earth
DO +RUN_EARTHLY --target=+test-unknown-user --earthfile=copy-keep-own.earth

copy-tilde-test:
RUN touch in
DO +RUN_EARTHLY --target=+copy-tilde-destination --earthfile=copy-tilde.earth --output_contains='destination path \"~/.\" contains a \"~\" which does not expand to a home directory'
DO +RUN_EARTHLY --target=+copy-tilde-in-destination --earthfile=copy-tilde.earth --output_contains='destination path \"/some/dir/~/.\" contains a \"~\" which does not expand to a home directory'
DO +RUN_EARTHLY --target=+copy-tilde-in-destination-prefix --earthfile=copy-tilde.earth --output_contains='destination path \"~some/dir.\" contains a \"~\" which does not expand to a home directory'
DO +RUN_EARTHLY --target=+copy-tilde-arg-in-destination --earthfile=copy-tilde.earth --output_contains='destination path \"/some/dir/~/.\" contains a \"~\" which does not expand to a home directory'
DO +RUN_EARTHLY --target=+copy-tilde-artifact --earthfile=copy-tilde.earth --output_contains='destination path \"/some/dir/~/.\" contains a \"~\" which does not expand to a home directory'
DO +RUN_EARTHLY --target=+copy-tilde-in-destination-not-prefix --earthfile=copy-tilde.earth --output_does_not_contain="which does not expand to a home directory"

cache-test:
# Test that a file can be passed between runs through the mounted cache.
DO +RUN_EARTHLY --earthfile=cache1.earth --target=+test-pass-file --use_tmpfs=false
Expand Down
39 changes: 39 additions & 0 deletions tests/copy-tilde.earth
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
VERSION 0.7

FROM alpine:3.18
WORKDIR /test

artifact:
COPY --dir in in
SAVE ARTIFACT in

copy-tilde-destination:
RUN echo destination is tilde
COPY in ~/.

copy-tilde-in-destination:
RUN echo destination contains tilde
RUN mkdir -p /some/dir
COPY in /some/dir/~/.

copy-tilde-in-destination-prefix:
RUN echo destination contains tilde in prefix
RUN mkdir -p ~some/dir
COPY in ~some/dir.

copy-tilde-arg-in-destination:
RUN echo destination arg contains tilde
RUN mkdir -p /some/dir
ARG dest=/some/dir/~/.
COPY in $dest

copy-tilde-artifact:
RUN echo destination contains tilde for artifact
RUN mkdir -p /some/dir
ARG dest=/some/dir/~/.
COPY +artifact/in /some/dir/~/.

copy-tilde-in-destination-not-prefix:
RUN echo destination contains a tilde but that should not trigger a warning
RUN mkdir -p some/di~r
COPY in some/di~r.

0 comments on commit 4b4aae9

Please sign in to comment.