 This provides a testbed for debugging code without Doom (or your private config)
 standing in the way, and without sacrificing access to installed packages."
   (interactive)
-  (let ((buf (get-buffer-create "*doom:vanilla-sandbox*")))
+  (let* ((buffer-name "*doom:vanilla-sandbox*")
+         (exists (get-buffer buffer-name))
+         (buf (get-buffer-create buffer-name)))
     (with-current-buffer buf
       (emacs-lisp-mode)
       (local-set-key (kbd "C-c C-c") #'doom--run-vanilla-sandbox)

      (local-set-key (kbd "C-c C-k") #'kill-this-buffer)
      (setq header-line-format "C-c C-c to run the session / C-c C-d to run it with vanilla Doom loaded / C-c C-k to abort it")
      (setq-local default-directory doom-emacs-dir)
      (doom-template-insert "VANILLA_SANDBOX")
      (goto-char (point-max)))
    (pop-to-buffer buf)))


(defun doom--run-vanilla-sandbox (&optional load-doom-p)
  (interactive)
  (let ((contents (buffer-string))
        (file (make-temp-file "doom-sandbox-")))
    (with-temp-file file
      (insert
       (prin1-to-string
        `(cond (,load-doom-p
                (setq doom-private-dir "/tmp/does/not/exist"
                      doom-modules ,doom-modules)
                (load ,user-init-file))
               ((setq package--init-file-ensured t
                      package-user-dir ,package-user-dir
                      package-archives ',package-archives
                      user-emacs-directory ,doom-emacs-dir)
                (package-initialize))))
       "\n(unwind-protect (progn\n" contents "\n)\n"
       (format "(delete-file %S))" file)))
    (let ((args (list "-Q" "-l" file)))
      (require 'restart-emacs)
      (condition-case e
          (cond ((display-graphic-p)
                 (if (memq system-type '(windows-nt ms-dos))
                     (restart-emacs--start-gui-on-windows args)
                   (restart-emacs--start-gui-using-sh args)))
                ((memq system-type '(windows-nt ms-dos))
                 (user-error "Cannot start another Emacs from Windows shell."))
                ((suspend-emacs
                  (format "%s %s -nw; fg"
                          (shell-quote-argument (restart-emacs--get-emacs-binary))
                          (string-join (mapcar #'shell-quote-argument args) " ")))))
        (error
         (delete-file file)
         (signal (car e) (cdr e)))))))

(defun doom--run-vanilla-doom-sandbox ()
  (interactive)
