fix(lib): doom/add-directory-as-project

If the target directory wasn't in a project, this command would throw a
type error (see #8032).

This also adds more checks and informative error handling to the
command.

Fix: #8032
This commit is contained in:
Henrik Lissner 2024-08-31 00:45:43 -04:00
parent 538ddf5e66
commit 22fc36dba7
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -79,16 +79,21 @@ file will be created within it so that it will always be treated as one. This
command will throw an error if a parent of DIR is a valid project (which would command will throw an error if a parent of DIR is a valid project (which would
mask DIR)." mask DIR)."
(interactive "D") (interactive "D")
(when-let ((proj-dir (doom-project-root dir)))
(if (file-equal-p proj-dir dir)
(user-error "ERROR: Directory is already a project: %s" proj-dir)
(user-error "ERROR: Directory is already inside another project: %s" proj-dir)))
(let ((short-dir (abbreviate-file-name dir))) (let ((short-dir (abbreviate-file-name dir)))
(unless (file-equal-p (doom-project-root dir) dir) (when (projectile-ignored-project-p dir)
(with-temp-file (doom-path dir ".project"))) (user-error "ERROR: Directory is in projectile's ignore list: %s" short-dir))
(let ((proj-dir (doom-project-root dir))) (dolist (proj projectile-known-projects)
(unless (file-equal-p proj-dir dir) (when (file-in-directory-p proj dir)
(user-error "Can't add %S as a project, because %S is already a project" (user-error "ERROR: Directory contains a known project: %s" short-dir))
short-dir (abbreviate-file-name proj-dir))) (when (file-equal-p proj dir)
(message "%S was not a project; adding .project file to it" (user-error "ERROR: Directory is already a known project: %s" short-dir)))
short-dir (abbreviate-file-name proj-dir)) (with-temp-file (doom-path dir ".project"))
(projectile-add-known-project dir)))) (message "Added directory as a project: %s" short-dir)
(projectile-add-known-project dir)))
;; ;;