5

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?

10
  • 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? Commented 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? Commented 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. Commented 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. Commented 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!) Commented Sep 28, 2021 at 21:20

3 Answers 3

4

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 :)

4

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.

4
  • 2
    This 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. Commented Jun 7, 2021 at 10:43
  • 1
    @RichieHH Also see the questions I asked under the question. Commented 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". Commented Nov 7, 2024 at 18:58
  • @RichieHH See the update Commented Nov 11, 2024 at 1:12
0

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)) 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.