I have a situation where (project-current) gets the project root "wrong", i.e. not what I want it to be - specifically the project is part of a larger project that project.el doesn't know about. Is there any way to override that, using perhaps a dir-local variable or some other method?
- Two questions: do you mean that the project root would be a subdirectory of a large Git (mono)repo? Or that you want the project to be larger than the current Git repo?Dmitry– Dmitry2021-09-27 11:43:04 +00:00Commented Sep 27, 2021 at 11:43
- And if it's the former: would you want to "parent" project to still be able to include subproject's files as its own? And have project-find-file and project-find-regexp always search across subprojects?Dmitry– Dmitry2021-09-27 11:44:16 +00:00Commented Sep 27, 2021 at 11:44
- What I meant by "specifically the project is part of a larger project" is that project.el thinks the project is rooted at /a/b/c/d (for whatever reason), but I want to override that so the project root is /a/b, which conceptually is my project root. So in my case there are no subprojects, just one big project to search.GaryO– GaryO2021-09-28 12:13:04 +00:00Commented Sep 28, 2021 at 12:13
- I'm asking about particular reasons the project root is detected where it is detected. To be able to choose the optimal technical implementation.Dmitry– Dmitry2021-09-28 15:40:56 +00:00Commented Sep 28, 2021 at 15:40
- To be honest, at the time I wasn't super inclined to dig into the implementation to know why it was wrong. If I remember, my case was a Typescript/Vue app in a larger git monorepo, which might work fine now -- I guess I was hoping for a general "escape hatch" rather than a smarter root autodetection (though that's always great too!)GaryO– GaryO2021-09-28 21:20:08 +00:00Commented Sep 28, 2021 at 21:20
3 Answers
I ran into this, too, and published the project-find-functions hook I use on https://michael.stapelberg.ch/posts/2021-04-02-emacs-project-override/:
;; Returns the parent directory containing a .project.el file, if any, ;; to override the standard project.el detection logic when needed. (defun zkj-project-override (dir) (let ((override (locate-dominating-file dir ".project.el"))) (if override (cons 'vc override) nil))) (use-package project ;; Cannot use :hook because 'project-find-functions does not end in -hook ;; Cannot use :init (must use :config) because otherwise ;; project-find-functions is not yet initialized. :config (add-hook 'project-find-functions #'zkj-project-override)) Hope this helps :)
See the user option project-vc-extra-root-markers, it allows to denote certain files (using wildcard entries) as project root markers, in addition to VCS root directories.
So it can be used to make a project more "fine-grained", but not the reverse (currently anyway).
It's available since Emacs 29.1 - though I'd recommend upgrading to the latest version of project.el for some minor fixes.
- 2This is a crazy omission from a base project tool. it has taken me ages to find this. I think I'll need to move back to projectile.RichieHH– RichieHH2021-06-07 10:43:14 +00:00Commented Jun 7, 2021 at 10:43
- 1@RichieHH Also see the questions I asked under the question.Dmitry– Dmitry2021-09-27 11:54:25 +00:00Commented Sep 27, 2021 at 11:54
- And back in 2024 running into the same issue. I want a subdirectory of a larger project to be a "project".RichieHH– RichieHH2024-11-07 18:58:51 +00:00Commented Nov 7, 2024 at 18:58
-
If you want to invalidate the project root and detect again (e.g. project-vc-extra-root-markers just changed or a custom root marker file added):
(defun my/project/invalidate () (interactive) (vc-file-setprop default-directory 'project-vc nil) (message "Project root invalidated for dir %s" default-directory))