Post

Homebrew の Cask 管理下のものを AppCleaner で消してしまったので対応する

Mac でアプリ管理に Homebrew Cask を使っていると、AppCleaner などでアプリを手動削除した際に、Brew 側がアプリの存在を認識できずエラーになることがあります。

  1. AppCleaner で削除した場合のエラー例
1
2
Error: Problems with multiple casks:
appflowy: It seems the App source '/Applications/AppFlowy.app' is not there.

これは AppCleaner でアプリ本体を削除してしまったため、Brew が管理対象として認識できなくなったことによるものです。

私の環境だと他にも同様に Calibre でエラーが出ます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
==> Upgrading calibre
==> Downloading https://download.calibre-ebook.com/8.8.0/calibre-8.8.0.dmg
Already downloaded: /Users/username/Library/Caches/Homebrew/downloads/3b3b0bb754a4fc4b354e8a2340def6541bfcfb9f0415b766278fabcdab52b0c9--calibre-8.8.0.dmg
==> Purging files for version 8.8.0 of Cask calibre
Error: Problems with multiple casks:
appflowy: It seems the App source '/Applications/AppFlowy.app' is not there.
background-music: Failure while executing; `/usr/bin/sudo -E -- /bin/launchctl kickstart -kp system/com.apple.audio.coreaudiod` exited with 150. Here's the output:
Could not kickstart service "com.apple.audio.coreaudiod": 150: Operation not permitted while System Integrity Protection is engaged


calibre: It seems the App source '/Applications/calibre.app' is not there.
❯ man brew
❯ man brew
❯ brew uninstall --cask calibre
==> Uninstalling Cask calibre
==> Purging files for version 7.26.0 of Cask calibre
==> Autoremoving 1 unneeded formula:
openjdk@11
Uninstalling /opt/homebrew/Cellar/openjdk@11/11.0.28... (667 files, 296.1MB)

brewから消せばよいです。多分。

  1. Brew 側でアンインストールする

AppCleaner で消してしまったアプリは、Brew からも削除する必要があります。

1
2
3
❯ brew uninstall --cask appflowy
==> Uninstalling Cask appflowy
==> Purging files for version 0.1.0 of Cask appflowy

Calibre も同様です。

1
2
3
4
5
6
❯ brew uninstall --cask calibre
==> Uninstalling Cask calibre
==> Purging files for version 7.26.0 of Cask calibre
==> Autoremoving 1 unneeded formula:
openjdk@11
Uninstalling /opt/homebrew/Cellar/openjdk@11/11.0.28... (667 files, 296.1MB)

こうすることで、Brew の管理下からも安全に削除できます。

Brew で管理しているアプリと、DMG やバイナリを手動でインストールしたアプリを混在させると、今回のような「実態のない Cask」が発生しやすくなるのかも。 brewで管理しているものと、dmgでインストール、バイナリ落としたものをそれぞれうまく管理する方法を考えねばこういうことがずっと起こり続けますね。辛い。 なんかいい方法を探します。

たくさんある場合

実態のないBrew Caskの対象を発見するスクリプトを作っておきます。

  • DRY_RUN=1 で実行すると、削除予定のアプリを確認。
  • DRY_RUN=0 にすると実際に削除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
: ${DRY_RUN:=1}

for c in $(brew list --cask); do
  APP_DIR=$(brew info --cask "$c" \
    | grep -Eo '/Applications/[^"]+\.app' \
    | sort -u)
  if [ -n "$APP_DIR" ] && [ ! -d "$APP_DIR" ]; then
    if [ "$DRY_RUN" -eq 1 ]; then
      echo "[DRY-RUN] Would remove: $c ($APP_DIR missing)"
    else
      echo "Removing $c..."
      brew uninstall --cask "$c"
    fi
  fi
done

元々for を brew list --cask | while read c; do で実装したら Error: Invalid usage: Cannot specify --cask when using --json=v1! で怒られました。調べると --json=v2 でしか使えないようです。私のbrewが古いのかもしれないです。

1
2
--json Print a JSON representation. Currently the default value for version is v1 for formula. For formula and cask use
        v2. See the docs for examples of using the JSON output: <https://docs.brew.sh/Querying-Brew>

Error: Invalid usage: Cannot specify –cask when using –json=v1! - Google 検索

ということで修正版を動かしてみます

1
2
3
4
5
6
7
❯ bash brew-remove.sh
[DRY-RUN] Would remove: beyond-compare (/Applications/Beyond Compare.app missing)
Warning: Cask crunch was renamed to crunch-app.
[DRY-RUN] Would remove: wine@staging (/Applications/Wine Staging.app missing)
Warning: Cask wireshark was renamed to wireshark-app.
[DRY-RUN] Would remove: wireshark (/Applications/Wireshark.app missing)
[DRY-RUN] Would remove: wireshark-app (/Applications/Wireshark.app missing)

dry-run はOKそうです。

実際に消してみます

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DRY_RUN=0 bash brew-remove.sh
Removing beyond-compare...
==> Uninstalling Cask beyond-compare
==> Purging files for version 5.0.6.30713 of Cask beyond-compare
Warning: Cask crunch was renamed to crunch-app.
Removing wine@staging...
==> Uninstalling Cask wine@staging
==> Purging files for version 10.6 of Cask wine@staging
Warning: Cask wireshark was renamed to wireshark-app.
Removing wireshark...
==> Uninstalling Cask wireshark-app
Password: 
==> Uninstalling packages with sudo; the password may be necessary:
==> Purging files for version 4.0.4 of Cask wireshark-app
Removing wireshark-app...
Error: Cask 'wireshark-app' is not installed.

消えました。winreshark-app はよくわからないのでまた調べてみますが、一旦はOKそうです。

1
2
3
4
5
6
7
8
9
10
❯ brew list --cask
1password-cli		crunch			monitorcontrol		unity-hub		windsurf
alfred			crunch-app		ngrok			upscayl			wireshark
background-music	figma			powershell		vagrant			xquartz
brave-browser		gstreamer-runtime	responsively		warp
❯ brew list --cask | grep wireshark
wireshark
❯ brew list --cask | grep wireshark
❯ brew uninstall --cask wireshark
Error: Cask 'wireshark-app' is not installed.

という感じです。一旦このスタイルで定期的に見直す運用にしようかなと思います。

This post is licensed under CC BY 4.0 by the author.