]> git.eshelyaron.com Git - emacs.git/commitdiff
Prepend 'rubocop' with 'bundle exec' when appropriate
authorDmitry Gutov <dgutov@yandex.ru>
Tue, 25 Dec 2018 16:23:01 +0000 (18:23 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Tue, 25 Dec 2018 16:23:01 +0000 (18:23 +0200)
etc/NEWS
lisp/progmodes/ruby-mode.el

index 34151d8caad07275ae77a7b4d6a265ef8ff7cc6f..e427e84780de87044605bf16c7afbd6a2208fbb4 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -511,6 +511,8 @@ This enables more efficient backends.  See the docstring of
 *** The Rubocop Flymake diagnostic function will only run Lint cops if
 it can't find the config file.
 
+*** Rubocop is called with 'bundle exec' if Gemfile mentions it.
+
 ** Package
 
 *** New function 'package-get-version' lets packages query their own version.
index 351dac2f8527e9f110a005851040a122348056ba..d0ae9b4644f850b7a89c37470e02b0e79801f074 100644 (file)
@@ -2323,6 +2323,7 @@ If there is no Rubocop config file, Rubocop will be passed a flag
   (let ((command (list "rubocop" "--stdin" buffer-file-name "--format" "emacs"
                        "--cache" "false" ; Work around a bug in old version.
                        "--display-cop-names"))
+        (default-directory default-directory)
         config-dir)
     (when buffer-file-name
       (setq config-dir (locate-dominating-file buffer-file-name
@@ -2331,7 +2332,12 @@ If there is no Rubocop config file, Rubocop will be passed a flag
           (setq command (append command '("--lint")))
         (setq command (append command (list "--config"
                                             (expand-file-name ruby-rubocop-config
-                                                              config-dir)))))
+                                                              config-dir))))
+        (when (ruby-flymake-rubocop--use-bundler-p config-dir)
+          (setq command (append '("bundle" "exec") command))
+          ;; In case of a project with multiple nested subprojects,
+          ;; each one with a Gemfile.
+          (setq default-directory config-dir)))
 
       (ruby-flymake--helper
        "rubocop-flymake"
@@ -2369,6 +2375,13 @@ If there is no Rubocop config file, Rubocop will be passed a flag
           into diags
           finally (funcall report-fn diags)))))))
 
+(defun ruby-flymake-rubocop--use-bundler-p (dir)
+  (let ((file (expand-file-name "Gemfile" dir)))
+    (and (file-exists-p file)
+         (with-temp-buffer
+           (insert-file-contents file)
+           (re-search-forward "^ *gem ['\"]rubocop['\"]" nil t)))))
+
 (defun ruby-flymake-auto (report-fn &rest args)
   (apply
    (if (and ruby-flymake-use-rubocop-if-available