   (concat (file-name-as-directory (ar/bazel-workspace-path))
           ".bazelrules"))
 
-(defun ar/bazel--init-fpath ()
-  (let ((fpath (concat (temporary-file-directory)
-                       "bazel-init.el")))
+(defun ar/bazel--write-rules-cache-async-path ()
+  (let ((calling-dpath default-directory)
+        (fpath (concat (temporary-file-directory)
+                       "ar-bazel--write-rules-cache-async-path.el")))
     (with-current-buffer (find-file-noselect fpath)
+      (setq default-directory calling-dpath)
       (delete-region (point-min) (point-max))
       (insert "(setq load-path '(\n")
       (mapc (lambda (path)

(defun ar/bazel-cache-build-rules ()
  (interactive)
  (async-shell-command (concat (expand-file-name invocation-name invocation-directory)
                               " --batch -Q"
                               " -l " (ar/bazel--init-fpath)
                               (format " --execute '(ar/bazel--write-rules-cache (ar/bazel-workspace-build-rules t) \"%s\")'"
                                       (ar/bazel--rules-cache-fpath)))
                       "*bazel cache*"))

(defun ar/bazel-insert-rule ()

      (insert "(require 'subr-x)\n")
      (insert "(require 'projectile)\n")
      (insert "(require 'ar-bazel)")
      (insert (format "(setq default-directory \"%s\")\n" calling-dpath))
      (insert (format "(ar/bazel--write-rules-cache (ar/bazel-workspace-build-rules t) \"%s\")\n"
                      (ar/bazel--rules-cache-fpath)))
      (write-file fpath nil))
    fpath))

