Repository: un-ts/prettier (ts)

Issue:
Errors with `dockerfile` parser
Hi.
I updated to `0.17.1` and trying to use new `dockerfile` parser to format dockerfile.
But it dies with error:

```
panic: runtime error: index out of range
RuntimeError: unreachable
    at main.runtime.runtimePanicAt (wasm://wasm/main-004aad7a:wasm-function[211]:0x10867)
    at main.runtime.lookupPanic (wasm://wasm/main-004aad7a:wasm-function[18]:0x1dff)
    at main.github.com/reteps/dockerfmt/lib.formatRun (wasm://wasm/main-004aad7a:wasm-function[1788]:0xc012b)
    at main.github.com/reteps/dockerfmt/lib.FormatNode (wasm://wasm/main-004aad7a:wasm-function[1780]:0xbe2c2)
    at main.(*github.com/reteps/dockerfmt/lib.ParseState).processNode (wasm://wasm/main-004aad7a:wasm-function[1778]:0xbde86)
    at main.(*github.com/reteps/dockerfmt/lib.ParseState).processNode (wasm://wasm/main-004aad7a:wasm-function[1778]:0xbdf31)
    at main.formatBytes.command_export (wasm://wasm/main-004aad7a:wasm-function[1797]:0xc6475)
    at formatDockerfileContents (file:///d:/projects/softvisio-node/cli/node_modules/@reteps/dockerfmt/dist/format.js:20:27)
    at async printAstToDoc (file:///d:/projects/softvisio-node/cli/node_modules/prettier/index.mjs:20124:16)
    at async coreFormat (file:///d:/projects/softvisio-node/cli/node_modules/prettier/index.mjs:20538:14)
```
`Dockerfile`:
```
RUN \
    # cleanup
    ls -la
```

Seems, that such comments syntax is not supported.
Bash script in markdown file gets partially deleted
Updating to prettier-plugin-sh 0.17.0 in our project results in prettier deleting lines from our README.md file. 

This can be reproduced by adding a simple README.md like this:
~~~text
```bash
curl --location --request PUT 'https://url.com/bla?q=\'hi\'' \
--header 'Content-Type: application/json'
``` 
~~~

Executing prettier on this file leads to prettier deleting the first line of the bash-snipped:
~~~text
```bash
--header 'Content-Type: application/json'
```
~~~

Pull request:
fix(sh): fallback to `sh` printer when `dockerfmt` throws
close #441

close #445
<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Add fallback to `sh` printer in `dockerPrinter` when `dockerfmt` throws, with new test case for verification.
> 
>   - **Behavior**:
>     - In `dockerPrinter.print()` in `index.ts`, add fallback to `sh` printer when `dockerfmt` throws an error.
>     - Handles unexpected errors from `dockerfmt` by using `sh` printer with the same options.
>   - **Testing**:
>     - Add test case for `445.Dockerfile` in `fixtures.spec.ts.snap` to verify fallback behavior.
>     - New fixture `445.Dockerfile` added for testing in `test/fixtures`.
>   - **Misc**:
>     - Update `ShPrintOptions` in `index.ts` to include additional printer options.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=un-ts%2Fprettier&utm_source=github&utm_medium=referral)<sup> for 1ea670090e9f693e01fe57e40814cbfe89bb2308. You can [customize](https://app.ellipsis.dev/un-ts/settings/summaries) this summary. It will automatically update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **Bug Fixes**
  - Improved error handling for Dockerfile formatting to ensure formatting continues even if the Dockerfile formatter encounters issues, by automatically falling back to an alternative formatter.

- **Tests**
  - Added a new Dockerfile test case to verify formatting and fallback behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Patch:
diff --git a/.changeset/rotten-dancers-bow.md b/.changeset/rotten-dancers-bow.md
new file mode 100644
index 0000000..7dcc754
--- /dev/null
+++ b/.changeset/rotten-dancers-bow.md
@@ -0,0 +1,5 @@
+---
+"prettier-plugin-sh": patch
+---
+
+fix: fallback to `sh` printer when `dockerfmt` throws
diff --git a/packages/sh/src/index.ts b/packages/sh/src/index.ts
index 8cedf98..888db6b 100644
--- a/packages/sh/src/index.ts
+++ b/packages/sh/src/index.ts
@@ -113,18 +113,62 @@ const dockerPrinter: Printer<string> = {
   async print(
     path,
     {
+      filepath,
+
+      // parser options
+      keepComments = true,
+      variant,
+      stopAt,
+      recoverErrors,
+
+      // printer options
       useTabs,
       tabWidth,
-      indent = useTabs ? 0 : tabWidth,
-      spaceRedirects = false,
-    }: DockerfilePrintOptions,
+      indent = useTabs ? 0 : (tabWidth ?? 2),
+      binaryNextLine = true,
+      switchCaseIndent = true,
+      spaceRedirects,
+      // eslint-disable-next-line sonarjs/deprecation
+      keepPadding,
+      minify,
+      singleLine,
+      functionNextLine,
+    }: ShPrintOptions,
   ) {
     const formatDockerfileContents = await getFormatDockerfileContents()
-    return formatDockerfileContents(path.node, {
-      indent,
-      spaceRedirects,
-      trailingNewline: true,
-    })
+    try {
+      return await formatDockerfileContents(path.node, {
+        indent,
+        spaceRedirects: spaceRedirects ?? false,
+        trailingNewline: true,
+      })
+    } catch {
+      /*
+       * `dockerfmt` is buggy now and could throw unexpectedly, so we fallback to
+       * the `sh` printer automatically in this case.
+       *
+       * @see {https://github.com/reteps/dockerfmt/issues/21}
+       * @see {https://github.com/reteps/dockerfmt/issues/25}
+       */
+      return processor(path.node, {
+        print: true,
+        filepath,
+        keepComments,
+        variant,
+        stopAt,
+        recoverErrors,
+        useTabs,
+        tabWidth,
+        indent,
+        binaryNextLine,
+        switchCaseIndent,
+        spaceRedirects: spaceRedirects ?? true,
+        keepPadding,
+        minify,
+        singleLine,
+        functionNextLine,
+      })
+    }
   },
 }
 


Test patch:
diff --git a/packages/sh/test/__snapshots__/fixtures.spec.ts.snap b/packages/sh/test/__snapshots__/fixtures.spec.ts.snap
index af9b887..ef3d292 100644
--- a/packages/sh/test/__snapshots__/fixtures.spec.ts.snap
+++ b/packages/sh/test/__snapshots__/fixtures.spec.ts.snap
@@ -890,6 +890,17 @@ exports[`parser and printer > should format 441.Dockerfile fixture correctly wit
 "
 `;
 
+exports[`parser and printer > should format 445.Dockerfile fixture correctly 1`] = `
+"FROM nginx
+WORKDIR /app
+ARG PROJECT_DIR=/
+ARG NGINX_CONF=nginx.conf
+COPY $NGINX_CONF /etc/nginx/conf.d/nginx.conf
+COPY $PROJECT_DIR /app
+CMD mkdir --parents /var/log/nginx && nginx -g "daemon off;"
+"
+`;
+
 exports[`parser and printer > should format 445.sh fixture correctly 1`] = `"reached EOF without closing quote '"`;
 
 exports[`parser and printer > should format Dockerfile fixture correctly 1`] = `
diff --git a/packages/sh/test/fixtures/445.Dockerfile b/packages/sh/test/fixtures/445.Dockerfile
new file mode 100644
index 0000000..d61d876
--- /dev/null
+++ b/packages/sh/test/fixtures/445.Dockerfile
@@ -0,0 +1,7 @@
+FROM nginx
+WORKDIR /app
+ARG PROJECT_DIR=/
+ARG NGINX_CONF=nginx.conf
+COPY $NGINX_CONF /etc/nginx/conf.d/nginx.conf
+COPY $PROJECT_DIR /app
+CMD mkdir --parents /var/log/nginx && nginx -g "daemon off;"