From 4b4aae96e4cf7723695574b2bf32d9ff3b51edc7 Mon Sep 17 00:00:00 2001 From: idodod Date: Tue, 28 Nov 2023 13:44:37 -0500 Subject: [PATCH] Display a warning when ~ is used in a `COPY` destination (#3540) 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. --- CHANGELOG.md | 3 +++ earthfile2llb/interpreter.go | 7 +++++++ tests/Earthfile | 12 ++++++++++- tests/copy-tilde.earth | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/copy-tilde.earth diff --git a/CHANGELOG.md b/CHANGELOG.md index 2324119b..8e129158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/earthfile2llb/interpreter.go b/earthfile2llb/interpreter.go index 9263d7ce..a0bcf186 100644 --- a/earthfile2llb/interpreter.go +++ b/earthfile2llb/interpreter.go @@ -6,6 +6,7 @@ import ( "net" "os" "path" + "slices" "strconv" "strings" @@ -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. diff --git a/tests/Earthfile b/tests/Earthfile index d75f77d1..6e8226ab 100644 --- a/tests/Earthfile +++ b/tests/Earthfile @@ -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 @@ -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 && \ @@ -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 diff --git a/tests/copy-tilde.earth b/tests/copy-tilde.earth new file mode 100644 index 00000000..ad3f68b0 --- /dev/null +++ b/tests/copy-tilde.earth @@ -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.