Repository navigation
Closed
Description
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"; doneHere 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
EOFHere 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"}Reacted by Arnaud Héritier, Konstantin Vyatkin, Jordan Kiesel and Peter Stenger
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Format Dockerfile RUN for loop[/-][+]Dockerfile/Containerfile formatting issues with for loops and RUN <<EOF[/+][-]Dockerfile/Containerfile formatting issues with for loops and RUN <<EOF[/-][+]Dockerfile/Containerfile formatting issues with for loops/semicolons and `RUN <<EOF`[/+]jtkiesel commentedon Apr 6, 2025
For anyone else experiencing this issue, it can be worked around by disabling the
spaceRedirectsoption, which is enabled by default in prettier-plugin-sh. An example.prettierrc.yaml:If you wish to keep
spaceRedirectsenabled 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: