Skip to content

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

Closed

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

Activity

  1. changed the title [-]Format Dockerfile RUN for loop[/-] [+]Dockerfile/Containerfile formatting issues with for loops and RUN <<EOF[/+] on Jul 10, 2024
  2. changed the title [-]Dockerfile/Containerfile formatting issues with for loops and RUN <<EOF[/-] [+]Dockerfile/Containerfile formatting issues with for loops/semicolons and `RUN <<EOF`[/+] on Jul 10, 2024
  3. jtkiesel commented on Apr 6, 2025

    @jtkiesel

    For anyone else experiencing this issue, it can be worked around by disabling the spaceRedirects option, which is enabled by default in prettier-plugin-sh. An example .prettierrc.yaml:

    plugins:
      - prettier-plugin-sh
    
    spaceRedirects: false

    If you wish to keep spaceRedirects enabled normally (in actual Shell script files, for example), but really just want to work around this issue in Containerfiles/Dockerfiles, you can use something like the following:

    plugins:
      - prettier-plugin-sh
    
    overrides:
      - files:
          - Containerfile
          - Dockerfile
        options:
          spaceRedirects: false
  4. reopened this on Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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