rake タスク実行時の「zsh: no matches found」エラーの解決

macOS ではデフォルトのシェルとして zsh が採用されているため、この問題は頻繁に発生します。

zsh での例:

% bin/rake plugin:spec[discourse-solved]
zsh: no matches found: plugin:spec[discourse-solved]
なぜこのようなことが起こるのか?

これは、角括弧が glob パターンとして解釈されるためです。bash では glob がパターンに一致しなくてもコマンドは実行され、問題なく動作します。一方、zsh ではエラーを発生させ、rake を実行しません。

例を挙げると、bash では:

○ → echo bin/rake plugin:spec[discourse-solved]
bin/rake plugin:spec[discourse-solved]

○ → touch plugin:specd

○ → echo bin/rake plugin:spec[discourse-solved]
bin/rake plugin:specd

failglob オプションを有効にすることで、bash でも同様の zsh の動作を再現できます:

○ → echo bin/rake plugin:spec[discourse-solved]
bin/rake plugin:spec[discourse-solved]

○ → shopt -s failglob

○ → echo bin/rake plugin:spec[discourse-solved]
bash: no match: plugin:spec[discourse-solved]

(正しい方向へ導いてくれた @j.jaffeux@pmusaraj、そして bash の例を提供してくれた @supermathie に感謝します :heart_eyes:

期待通りに動作させるには、rake の引数全体を引用符で囲む必要があります。

% bin/rake "plugin:spec[discourse-solved]"
「いいね!」 22

Ouch. That sounds like it’s going to cause a bunch of pain to people who are not likely to have any idea how to figure out the problem. Hopefully they’ll find this topic.

「いいね!」 3

It’s mostly fine - scripts can just specify bash to avoid any pain.

「いいね!」 2

True enough. I guess that’s why all of my bash scripts start with #!/usr/bin/env bash even though I don’t expect them ever to run on a Mac :wink:

「いいね!」 3

Or, you could add an alias to your ZSH config. :slight_smile:
That works on Linux, so it will probably work on macOS as well.

alias rake='noglob rake'
「いいね!」 7