sh icon indicating copy to clipboard operation
sh copied to clipboard

shfmt incorrectly reports "unclosed here-document" for tab-indented closing delimiter with <<-

Open jooftian opened this issue 1 year ago • 1 comments

shfmt is incorrectly reporting an "unclosed here-document" error for a bash script that uses the <<- syntax with a tab-indented closing delimiter. This syntax should be valid according to bash heredoc documentation.

Script content (test.sh):

#!/bin/bash

main() {
	bash <<-EXEC
		uname -a
	EXEC
}

main

Note: The closing EXEC and the content are indented with tabs, not spaces.

Error message:

PS C:\...> shfmt -ln bash -l -w "C:\...\test.sh"
C:\...\test.sh:4:7: unclosed here-document 'EXEC'

Environment:

  • shfmt version: v3.8.0
  • Go version: go1.23.0 windows/amd64
  • OS: Windows 11 Home Single Language 23H2 22631.3880
  • PowerShell: 5.1.22621.3880
  • VSCode: 1.92.1 (user setup)

Additional notes:

  • The script works correctly when executed.
  • The error occurs both when using shfmt directly and through the VSCode extension.
  • This issue also affects the VSCode extension shell-format, which uses shfmt as a dependency. This suggests the problem has a wider impact on the ecosystem of shell scripting tools.

Expected behavior: shfmt should recognize the tab-indented closing delimiter and content as valid when using the <<- syntax and not report an "unclosed here-document" error.

Steps to reproduce:

  1. Create a file named test.sh with the content provided above.
  2. Run shfmt -ln bash -l -w "path\to\test.sh"
  3. Observe the "unclosed here-document" error.

Additional readings:

Please let me know if you need any additional information or if there's a workaround for this issue. Thank you for your attention to this matter.

jooftian avatar Aug 14 '24 18:08 jooftian

Even when trying with the following code, the error persist

#!/bin/bash

bash <<-EXEC
	uname -a
EXEC

jooftian avatar Aug 14 '24 18:08 jooftian

Thanks for reporting. You say you're on Windows so this appears to be a bug with CRLF line endings, which I can reproduce.

mvdan avatar Sep 29 '24 09:09 mvdan