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

Dockerfile/Containerfile formatting issues with for loops/semicolons and RUN <<EOF #376

Open
jameshearttech opened this issue Jul 10, 2024 · 0 comments

Comments

@jameshearttech
Copy link

jameshearttech commented Jul 10, 2024

Recently started using prettier-plugin-sh. Seems to work okay with shell scripts, but I have been struggling with formatting Dockerfile/Containerfile.

Here is the prettier loaded options from the CLI debug.

{
    "plugins":["prettier-plugin-sh"],
    "arrowParens":"avoid",
    "printWidth":100,
    "singleQuote":true,
    "trailingComma":"none",
    "bracketSpacing":true,
    "tabWidth":4,
    "semi":true
}

Here is an example Dockerfile/Containerfile not formatted.

FROM debian:12.6-slim
RUN set -eux; for x in {1..3}; do echo 'foo'; echo 'bar'; echo "$x"; done

Here is what it looks like after formatting.

FROM debian:12.6-slim
RUN set -eux
for x in {1..3}; do
    echo 'foo'
    echo 'bar'
    echo "$x"
done

Here is the output from podman build. Obviously, FOR is not a Dockerfile instruction.

STEP 1/7: FROM debian:12.6-slim
STEP 2/7: RUN set -eux
--> 9816982104ff
STEP 3/7: for x in {1..3}; do
time="2024-07-10T08:01:59-10:00" level=error msg="+(UNHANDLED LOGLEVEL) &imagebuilder.Step{Env:[]string{\"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"}, Command:\"for\", Args:[]string{\"\"}, Flags:[]string{}, Attrs:map[string]bool(nil), Message:\"FOR \", Heredocs:[]parser.Heredoc(nil), Original:\"for x in {1..3}; do\"}"
Error: building at STEP "FOR ": Build error: Unknown instruction: "FOR" &imagebuilder.Step{Env:[]string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, Command:"for", Args:[]string{""}, Flags:[]string{}, Attrs:map[string]bool(nil), Message:"FOR ", Heredocs:[]parser.Heredoc(nil), Original:"for x in {1..3}; do"}

I would rather write RUN <<EOF, but that is also broken by the formatting.

FROM debian:12.6-slim
RUN <<EOF
    set -eux
    for x in {1..3}
    do
        echo 'foo'
        echo 'bar'
        echo "$x"
    done
EOF

Here is what it looks like after formatting.

FROM debian:12.6-slim
RUN << EOF
    set -eux
    for x in {1..3}
    do
        echo 'foo'
        echo 'bar'
        echo "$x"
    done
EOF

The problem here is the space that is added between << and EOF.

STEP 1/10: FROM debian:12.6-slim
STEP 2/10: RUN << EOF
--> 687a1076c4e0
STEP 3/10: set -eux
time="2024-07-10T08:20:19-10:00" level=error msg="+(UNHANDLED LOGLEVEL) &imagebuilder.Step{Env:[]string{\"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"}, Command:\"set\", Args:[]string{\"\"}, Flags:[]string{}, Attrs:map[string]bool(nil), Message:\"SET \", Heredocs:[]parser.Heredoc(nil), Original:\"set -eux\"}"
Error: building at STEP "SET ": Build error: Unknown instruction: "SET" &imagebuilder.Step{Env:[]string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, Command:"set", Args:[]string{""}, Flags:[]string{}, Attrs:map[string]bool(nil), Message:"SET ", Heredocs:[]parser.Heredoc(nil), Original:"set -eux"}
@jameshearttech jameshearttech changed the title Format Dockerfile RUN for loop Dockerfile/Containerfile formatting issues with for loops and RUN <<EOF Jul 10, 2024
@jameshearttech jameshearttech changed the title Dockerfile/Containerfile formatting issues with for loops and RUN <<EOF Dockerfile/Containerfile formatting issues with for loops/semicolons and RUN <<EOF Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant