mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.98k stars 332 forks source link

shfmt にてヒアドキュメントの変換に失敗する / Conversion of heredocument fails in shfmt #973

Closed qq542vev closed 1 year ago

qq542vev commented 1 year ago

当方は英語を全く読み書き出来ない日本語話者なので、DeepL翻訳による英語への翻訳文も掲載します。そのため拙い文や読みにくいとところがあるかもしれませんが、ご了承ください。

As we are Japanese speakers who cannot read or write English at all, we will also include a translation into English by DeepL translation. Please understand that there may be parts of the text that are poorly written or difficult to read.

こんにちは。初めまして。普段より shfmt を利用しております。とても便利なツールです。開発者の皆様ありがとうございます。

Hello. It is nice to meet you. I am a regular user of shfmt. It is a very useful tool. Thank you very much to the developers.

この度バグか仕様か分からないのですが、気になる点を発見したので、恐縮ではございますが、この場を借りてご報告致します。(もし既に同様の内容の報告がございましたら申し訳ございません。)

I am not sure if it is a bug or a specification, but I have found a point of concern. (We apologize if you have already received a similar report.)

次のような内容のシェルスクリプトファイル、a.sh があるとします。

Suppose you have a shell script file, a.sh, with the following contents

#!/usr/bin/bssh

n=$(
        cat <<-'EOF'
        1
        2
        3
        EOF
)

echo "${n}"

この a.sh を shfmt で最適化・最小化の変換を行うと、bash でファイルを実行できず、エラーが返されます。

If I do an optimize/minimize conversion of this a.sh with shfmt, the file cannot be executed by bash and an error is returned.

$ shfmt -s -mn -ln bash a.sh 
n=$(cat \
<<-'EOF')
        1
        2
        3
        EOF
echo "$n"
$ shfmt -s -mn -ln bash a.sh | bash
bash: 行 3: 警告: ヒアドキュメントの 3 行目でファイル終了 (EOF) に達しました (`EOF' が必要)
bash: 行 2: 警告: ヒアドキュメントの 2 行目でファイル終了 (EOF) に達しました (`EOF' が必要)
bash: 行 3: 1: コマンドが見つかりません
bash: 行 4: 2: コマンドが見つかりません
bash: 行 5: 3: コマンドが見つかりません
bash: 行 6: EOF: コマンドが見つかりません

この変換はバグでしょうか。もしよろしければ、これについて改善していただければ、当方としても幸いでございます。

Is this conversion a bug? If so, we would be happy if you could improve this for us.

当方のコンピューターの環境は以下になります。

Our computer environment is as follows

$ shfmt --version
v3.6.0
$ bash --version
GNU bash, バージョン 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 またはそれ以降 <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

何卒よろしくお願い致します。

Thank you in advance for your cooperation.

mvdan commented 1 year ago

Thanks for filing this bug! I think the translation worked pretty well :) This is a known bug; see https://github.com/mvdan/sh/issues/923. Closing as a duplicate.