                ;; REVIEW We do these modification checks manually because
                ;;        Straight's checks seem to miss stale elc files. Need
                ;;        more tests to confirm this.
-               (when (or (gethash package straight--cached-package-modifications)
-                         (file-newer-than-file-p (straight--repos-dir local-repo)
-                                                 (straight--build-dir package))
-                         (cl-loop for file
-                                  in (doom-files-in (straight--build-dir package)
-                                                    :match "\\.el$"
-                                                    :full t)
-                                  for elc-file = (byte-compile-dest-file file)
-                                  if (and (file-exists-p elc-file)
-                                          (file-newer-than-file-p file elc-file))
-                                  return t))
+               (when (or (ignore-errors
+                           (gethash package straight--packages-to-rebuild))
+                         (gethash package straight--cached-package-modifications)
+                         (not (file-directory-p (straight--build-dir package))))
                  (print! (info "Rebuilding %s") package)
-                 ;; REVIEW `straight-rebuild-package' alone wasn't enough. Why?
-                 (delete-directory (straight--build-dir package) 'recursive)
-                 (straight-rebuild-package package)
+                 (straight-rebuild-package package 'recursive)
+                 (when (cl-loop for file
+                                in (doom-files-in (straight--build-dir package)
+                                                  :match "\\.el$"
+                                                  :full t)
+                                for elc-file = (byte-compile-dest-file file)
+                                if (and (file-exists-p elc-file)
+                                        (file-newer-than-file-p file elc-file))
+                                return t)
+                   (straight--byte-compile-package recipe))
                  (cl-incf n)))))))
      (if (= n 0)
          (ignore (print! (success "No packages need rebuilding")))
+       (doom--finalize-straight)
        (print! (success "Rebuilt %d package(s)" n))
        t))))
 

                    (condition-case e
                        (let* ((default-directory (straight--repos-dir local-repo))
                               (n (string-to-number
                                   (shell-command-to-string "git rev-list --right-only --count HEAD..@'{u}'")))
                               (pretime
                                (string-to-number
                                 (shell-command-to-string "git log -1 --format=%at HEAD")))
                               (time
                                (string-to-number
                                 (shell-command-to-string "git log -1 --format=%at FETCH_HEAD"))))
                          (with-current-buffer (straight--process-get-buffer)
                            (with-silent-modifications

     (doom-reload-core-autoloads)
     (when (progn ,@body)
       (doom-reload-package-autoloads 'force-p))
     t))


