auto-virtualenvwrapper 和 auto-virtualenv 的坑

这两个 package 是用来在编写 Python 项目的时候,动态地切换 virtualenv,可以做到打开项目 A 的代码时使用项目 A 的 virtualenv 而打开项目 B 的代码时使用 B 的。

然而今天装上后老是报告说 Can’t find project root,翻了下代码,里面有段代码是这么写的

(defun auto-virtualenv--project-root-vc ()
  "Return vc root if file is in version control"
  (when (or
         (vc-find-root (or (buffer-file-name) "") ".git")
         (vc-find-root (or (buffer-file-name) "") ".hg")))

而根据 Emacs Manual: Conditions 里的说明,when 的语法是

(when condition then-forms...)

上面的代码只给了 contidion 但是没有给 then-forms……所以 auto-virtualenv--project-root-vc 这个方法总是返回 nil

直接去掉 when 语句就好了,改成

(defun auto-virtualenv--project-root-vc ()
  "Return vc root if file is in version control"
  (or
   (vc-find-root (or (buffer-file-name) "") ".git")
   (vc-find-root (or (buffer-file-name) "") ".hg")))

就能够正常工作。

给两个项目都提了 PR,不过不知道什么时候能合掉更新到 MELPA 上。

2 个赞