解決 rb-inotify 的 Permission Denied 錯誤

在開發 Ruby on Rails 網站時,使用 rails g controller 指令新增 controller 時,出現一個錯誤:

/home/**/.rbenv/versions/2.6.10/lib/ruby/gems/2.6.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:200:in `initialize': Permission denied @ dir_initialize - /home/**/**/srv/pg (Errno::EACCES)

原因是 rb-inotify 掃描專案根目錄下的 srv/pg 資料夾時遇到存取權限問題。該資料夾是用 PostgreSQL docker container 建立的,只有 root 才有讀取權限。

我想知道能不能設定 rb-inotify ,將 srv/pg 加入例外清單,但發現無法,且作者似乎也不打算處理 [1][2]。最後找到解法,在 config/sprint.rb 原本的內容上方加入以下程式碼,改寫原本的 watch 方法:

INOTIFY_BLACKLIST = [
  ".git",
  "node_modules",
  "tmp",
  "log",
  "srv"
].map { |dir| Pathname(Spring.watcher.root).glob(dir) }.flatten.map(&:to_s)

require "rb-inotify"
module INotifyNotifierIgnorer
  def watch(path, *flags, &callback)
    return if INOTIFY_BLACKLIST.include?(path)

    # warn path # to see all the paths listened to
    super(path, *flags, &callback)
  end
end
INotify::Notifier.prepend INotifyNotifierIgnorer

Comments