Skip to content

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

@jameshearttech

Description

@jameshearttech

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"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions