 This should be run whenever your `doom!' block, or a module autoload file, is
 modified."
   (interactive)
-  (let ((default-directory doom-emacs-dir)
-        (targets
-         (file-expand-wildcards
-          (expand-file-name "autoload/*.el" doom-core-dir))))
-    (dolist (path (doom-module-load-path))
-      (let ((auto-dir  (expand-file-name "autoload" path))
-            (auto-file (expand-file-name "autoload.el" path)))
+  (let* ((default-directory doom-emacs-dir)
+         (doom-modules (doom-modules))
+         (targets
+          (file-expand-wildcards
+           (expand-file-name "autoload/*.el" doom-core-dir)))
+         (enabled-targets (copy-sequence targets))
+         case-fold-search)
+    (dolist (path (doom-module-load-path t))
+      (let* ((auto-dir  (expand-file-name "autoload" path))
+             (auto-file (expand-file-name "autoload.el" path))
+             (module    (doom-module-from-path auto-file))
+             (module-p  (or (doom-module-p (car module) (cdr module))
+                            (file-equal-p path doom-private-dir))))
         (when (file-exists-p auto-file)
-          (push auto-file targets))
-        (when (file-directory-p auto-dir)
-          (dolist (file (doom-files-in auto-dir :match "\\.el$" :full t))
-            (push file targets)))))
+          (push auto-file targets)
+          (if module-p (push auto-file enabled-targets)))
+        (dolist (file (doom-files-in auto-dir :match "\\.el$" :full t))
+          (push file targets)
+          (if module-p (push file enabled-targets)))))
     (if (and (not force-p)
              (not doom-emacs-changed-p)
              (file-exists-p doom-autoload-file)

it exists."
  (cl-check-type file string)
  (when (file-exists-p file)
    (delete-file file)
    (ignore-errors (delete-file (byte-compile-dest-file file)))
    (message "Deleted old %s" (file-name-nondirectory file))))

             (not (cl-loop for file in targets
                           if (file-newer-than-file-p file doom-autoload-file)
                           return t)))
        (progn (print! (green "Doom core autoloads is up-to-date"))
               (doom-initialize-autoloads doom-autoload-file)
               nil)
      (doom-delete-autoloads-file doom-autoload-file)
      (message "Generating new autoloads.el")
      (make-directory (file-name-directory doom-autoload-file) t)
      (with-temp-file doom-autoload-file
        (doom--generate-header 'doom//reload-doom-autoloads)
        (save-excursion
          (doom--generate-autoloads (reverse enabled-targets)))
          ;; Replace autoload paths (only for module autoloads) with absolute
          ;; paths for faster resolution during load and simpler `load-path'
        (save-excursion
          (doom--expand-autoloads)
          (print! (green "✓ Expanded module autoload paths")))
        ;; Generates stub definitions for functions/macros defined in disabled
        ;; modules, so that you will never get a void-function when you use
        ;; them.
        (save-excursion
          (doom--generate-autodefs (reverse targets) enabled-targets))
        ;; Remove byte-compile inhibiting file variables so we can byte-compile
        ;; the file, and autoload comments.
        (doom--cleanup-autoloads)
        (print! (green "✓ Clean up autoloads")))
      ;; Byte compile it to give the file a chance to reveal errors.
      (doom--byte-compile-file doom-autoload-file)
      (when (and noninteractive (not (daemonp)))
        (doom--server-load doom-autoload-file))
      t)))


;;
;; Package autoloads
;;

(defun doom--generate-package-autoloads ()
  (dolist (spec (doom-get-package-alist))
    (if-let* ((pkg  (car spec))
              (desc (cdr spec)))
        (unless (memq pkg doom-autoload-excluded-packages)
          (let ((file (concat (package--autoloads-file-name desc) ".el")))
            (when (file-exists-p file)
              (insert "(let ((load-file-name " (prin1-to-string (abbreviate-file-name file)) "))\n")
              (insert-file-contents file)
              (while (re-search-forward "^\\(?:;;\\(.*\n\\)\\|\n\\|(provide '[^\n]+\\)" nil t)
                (unless (nth 8 (syntax-ppss))
                  (replace-match "" t t)))
              (unless (bolp) (insert "\n"))
              (insert ")\n"))))
      (message "Couldn't find package desc for %s" (car spec)))))

(defun doom--generate-var-cache ()
  (doom-initialize-packages)
  (prin1 `(setq load-path ',load-path
                auto-mode-alist ',auto-mode-alist
                Info-directory-list ',Info-directory-list
                doom-disabled-packages ',doom-disabled-packages
                package-activated-list ',package-activated-list)
         (current-buffer)))

(defun doom--cleanup-package-autoloads ()
  (while (re-search-forward "^\\s-*\\((\\(?:add-to-list\\|\\(?:when\\|if\\) (boundp\\)\\s-+'\\(?:load-path\\|auto-mode-alist\\)\\)" nil t)
    (goto-char (match-beginning 1))
    (kill-sexp)))

(defun doom//reload-package-autoloads (&optional force-p)
  "Compiles `doom-package-autoload-file' from the autoloads files of all
