From 921fa361784613160e65caa51dec8181120008e8 Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Wed, 15 May 2024 00:28:09 +0100 Subject: [PATCH 1/2] Handle non-zero exit status when formatting using shfmt Non-zero exist statuses from `shfmt` weren't checked for or handled. As a result the (empty) output from `shfmt` was returned as the formatted document. An exception is now thrown when a non-zero exit status is encountered. Fixes #1162 --- server/src/shfmt/__tests__/index.test.ts | 10 +++++++++- server/src/shfmt/index.ts | 10 +++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/src/shfmt/__tests__/index.test.ts b/server/src/shfmt/__tests__/index.test.ts index 3bc3e3c3a..d29f98d0c 100644 --- a/server/src/shfmt/__tests__/index.test.ts +++ b/server/src/shfmt/__tests__/index.test.ts @@ -38,7 +38,7 @@ describe('formatter', () => { expect(new Formatter({ executablePath: 'foo' }).canFormat).toBe(true) }) - it('should set canFormat to false when formatting fails', async () => { + it('should set canFormat to false when the executable cannot be found', async () => { const [result, formatter] = await getFormattingResult({ document: textToDoc(''), executablePath: 'foo', @@ -54,6 +54,14 @@ describe('formatter', () => { ) }) + it('should throw when formatting fails', async () => { + expect(async () => { + await getFormattingResult({ document: FIXTURE_DOCUMENT.PARSE_PROBLEMS }) + }).rejects.toThrow( + 'Shfmt: exited with status 1: :10:1: > must be followed by a word', + ) + }) + it('should format when shfmt is present', async () => { const [result] = await getFormattingResult({ document: FIXTURE_DOCUMENT.SHFMT }) expect(result).toMatchInlineSnapshot(` diff --git a/server/src/shfmt/index.ts b/server/src/shfmt/index.ts index d10f5033a..e039a04df 100644 --- a/server/src/shfmt/index.ts +++ b/server/src/shfmt/index.ts @@ -91,14 +91,10 @@ export class Formatter { proc.stdin.end(documentText) }) - // NOTE: do we care about exit code? 0 means "ok", 1 possibly means "errors", - // but the presence of parseable errors in the output is also sufficient to - // distinguish. let exit try { exit = await proc } catch (e) { - // TODO: we could do this up front? if ((e as any).code === 'ENOENT') { // shfmt path wasn't found, don't try to format any more: logger.warn( @@ -107,7 +103,11 @@ export class Formatter { this._canFormat = false return '' } - throw new Error(`Shfmt: failed with code ${exit}: ${e}\nout:\n${out}\nerr:\n${err}`) + throw new Error(`Shfmt: child process error: ${e}`) + } + + if (exit != 0) { + throw new Error(`Shfmt: exited with status ${exit}: ${err}`) } return out From 522a5d61fa0ce21872f61a79d275dd4a5bcce6a6 Mon Sep 17 00:00:00 2001 From: Kenneth Skovhus Date: Wed, 15 May 2024 08:20:56 +0200 Subject: [PATCH 2/2] Bump server version --- server/CHANGELOG.md | 4 ++++ server/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/server/CHANGELOG.md b/server/CHANGELOG.md index 8e0388a80..79e788a2f 100644 --- a/server/CHANGELOG.md +++ b/server/CHANGELOG.md @@ -1,5 +1,9 @@ # Bash Language Server +## 5.3.2 + +- Handle non-zero exit status when formatting using shfmt https://github.com/bash-lsp/bash-language-server/pull/1163 + ## 5.3.1 - Clear diagnostics when closing document https://github.com/bash-lsp/bash-language-server/pull/1135 diff --git a/server/package.json b/server/package.json index 6e6f5ea3d..589838ff8 100644 --- a/server/package.json +++ b/server/package.json @@ -3,7 +3,7 @@ "description": "A language server for Bash", "author": "Mads Hartmann", "license": "MIT", - "version": "5.3.1", + "version": "5.3.2", "main": "./out/server.js", "typings": "./out/server.d.ts", "bin": {