doomemacs icon indicating copy to clipboard operation
doomemacs copied to clipboard

Treemacs Incorrectly Displays $HOME as Root on First Project Open in Doom Emacs

Open xxh160 opened this issue 1 year ago • 3 comments

I confirm that...

  • [X] I have searched the issue tracker, documentation, FAQ, Discourse, and Google, in case this issue has already been reported/resolved.

  • [X] I have read "How to Debug Issues", and will use it to provide as much information about this issue as possible.

  • [X] The issue can be reproduced on the latest available commit of Doom.

  • [X] The issue can be reproduced on a stable release of Emacs, such as 27, 28, or 29. (Unstable versions end in .50, .60, or .9x)

Expected behavior

After opening Doom Emacs from a closed state, the first project opened with spc p p (which has a .projectile in the root directory) should correctly display its project root path.

Current behavior

When I open Doom Emacs from a closed state and open the first project with spc p p, the treemacs view does not display correctly. The root directory shown is my $HOME.

After some investigation, I think the problem may lie in the +workspaces-switch-to-project-h function:

diff --git a/modules/ui/workspaces/autoload/workspaces.el b/modules/ui/workspaces/autoload/workspaces.el
index 840ca1d5..04ef2240 100644
--- a/modules/ui/workspaces/autoload/workspaces.el
+++ b/modules/ui/workspaces/autoload/workspaces.el
@@ -545,7 +545,11 @@ This be hooked to `projectile-after-switch-project-hook'."
       (unwind-protect
           (if (and (not (null +workspaces-on-switch-project-behavior))
                    (or (eq +workspaces-on-switch-project-behavior t)
-                       (equal (safe-persp-name (get-current-persp)) persp-nil-name)
+                       ;; HACK Perspective main
+                       (let ((current-persp-name (safe-persp-name (get-current-persp))))
+                         (or (equal current-persp-name persp-nil-name)
+                             (equal current-persp-name "main")))
+                       ;; (equal (safe-persp-name (get-current-persp)) persp-nil-name)
                        (+workspace-buffer-list)))
               (let* ((persp
                       (let ((project-name (doom-project-name +workspaces--project-dir)))

When Doom Emacs starts, the default workspace is Perspective main. At this point, the if check in +workspaces-switch-to-project-h will be false, and the function behavior is to call +workspace-rename to rename the current workspace, but it only changes the name of the perspective, not the project name and path.

As shown in the diff, after I added the check for "main", the behavior became to create a new workspace whether or not Doom Emacs has just been opened, which temporarily solves my problem.

Steps to reproduce

  1. Open Doom Emacs from a closed state.
  2. Use spc p p to open a pre-configured project. As seen in the image(treemacs-edit-workspaces), the project path is correctly configured. image image
  3. Use spc o p to open the treemacs sidebar.
  4. Notice that the root directory in treemacs is $HOME. image

System Information

https://pastebin.com/a98uiJcL

xxh160 avatar Jan 07 '24 21:01 xxh160

I have the same issue and your patch resolves it for me as well. If you need any help testing fixes let me know.

chris-johnson-7r avatar Feb 23 '24 18:02 chris-johnson-7r

Argh this is driving me crazy! I'm now running into exactly this issue consistently but I didn't have this behaviour before. @hlissner is this introduced by something doom patched for treemacs? The workaround-patch mentioned above works, but I do get a noisy error message with a bunch of nils on exit when using it. Would really like a fix for this.

rubin55 avatar Jun 04 '24 22:06 rubin55

When this happens to me, I use this workaround: rm ~/.config/emacs/.local/cache/treemacs-persist

panchoh avatar Jun 18 '24 07:06 panchoh

I can't reproduce this, but I think I know where you're coming from. Treemacs, by default, remembers all the projects you open it from, and this memory of projects persists across Emacs sessions (saved in $EMACSDIR/.local/cache/treemacs-persist), so if you've opened treemacs in the past from $HOME, $HOME will be in the treemacs buffer (among other projects, but it'll be the first if it's the first project you opened -- see the screenshot).

You can either remove projects from treemacs with M-x treemacs-remove-project-from-workspace (and add them with M-x treemacs-add-project-to-workspace). OR, and I think this is what you are all asking for, use the treemacs-display-current-project-exclusively command (has no default keybind in Doom). This opens treemacs with only the current project visible.

So try it out: M-x treemacs-add-and-display-current-project-exclusively, and see if you're seeing what you're expecting.


By the way, the patch above fixes the issue indirectly because Doom configures Treemacs to have different project listings per workspace. By creating a new workspace, you're just resetting Treemacs.

hlissner avatar Jul 01 '24 22:07 hlissner

As of f9dfb7e I've changed +treemacs/toggle (on SPC o p) to use treemacs-add-and-display-current-project-exclusively.

hlissner avatar Jul 01 '24 22:07 hlissner

The new behavior fixes the issue for me, yay! so thanks for that, @hlissner.

Also, with your explanation it is clear to me now that, in my case at least, this was caused by me running emacs as a daemon (as setup by Home Manager), since the emacs process will be opened on $HOME. I just checked, and starting with no treemacs-persist, launching the daemon automatically yields:

$ cat ~/.config/emacs/.local/cache/treemacs-persist
* No Perspective
** pancho
 - path :: ~

panchoh avatar Jul 03 '24 03:07 panchoh