treefmt-nix icon indicating copy to clipboard operation
treefmt-nix copied to clipboard

Formatter does not work in a git worktree

Open kuruczgy opened this issue 9 months ago • 6 comments

Describe the bug

Running nix fmt (set up as recommended in the README) fails with

Error: failed to load config: failed to determine tree root: failed to find tree-root based on tree-root-file: could not find [.git/config] in /path/to/your/worktree

To Reproduce

Steps to reproduce the behavior:

  1. Create a git worktree using git worktree add
  2. Try to run nix fmt in the worktree.

Expected behavior

Formats as normal.

System information

treefmt-nix version: a05be418a1af1198ca0f63facb13c985db4cb3c5 nixpkgs version: 56b033fe4f9da755b1872466f24b32df7cfc229e

nix-shell -p nix-info --run "nix-info -m":

 - system: `"aarch64-linux"`
 - host os: `Linux 6.16.0-rc3, NixOS, 25.11 (Xantusia), 25.11.20250615.cbfb263`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Lix, like Nix) 2.91.1
System type: aarch64-linux
Additional system types: 
Features: gc, signed-caches
System configuration file: /etc/nix/nix.conf
User configuration files: /home/user/.config/nix/nix.conf:/etc/xdg/nix/nix.conf:/home/user/.nix-profile/etc/xdg/nix/nix.conf:/nix/profile/etc/xdg/nix/nix.conf:/home/user/.local/state/nix/profile/etc/xdg/nix/nix.conf:/etc/profiles/per-user/user/etc/xdg/nix/nix.conf:/nix/var/nix/profiles/default/etc/xdg/nix/nix.conf:/run/current-system/sw/etc/xdg/nix/nix.conf
Store directory: /nix/store
State directory: /nix/var/nix
Data directory: /nix/store/g9clkwljsncp6fadzc105ay78b7l6mg2-lix-2.91.1/share`
 - nixpkgs: `/nix/store/nki040zkkasz5mnd7c6jv0gn3sipmc30-source`

kuruczgy avatar Jun 25 '25 20:06 kuruczgy

I suspect the issue is that we unconditionally set --tree-root-file to .git/config by default here: https://github.com/numtide/treefmt-nix/blob/a05be418a1af1198ca0f63facb13c985db4cb3c5/module-options.nix#L205

I think if we just omitted this option by default treefmt would be smart enough to figure out the root on its own.

kuruczgy avatar Jun 25 '25 20:06 kuruczgy

I suspect the issue is that we unconditionally set --tree-root-file to .git/config by default here

@kuruczgy that used to be the case, but we recently changed this to flake.nix for flakes. See https://github.com/numtide/treefmt-nix/commit/6a846d387e641a534c388de44c6cf804c3380feb. However, you say you're using https://github.com/numtide/treefmt-nix/commit/a05be418a1af1198ca0f63facb13c985db4cb3c5, which does include that commit.

Are you able to share a repro?

jfly avatar Jun 28 '25 21:06 jfly

@jfly I am using default.nix as an entry point, so that explains why the commit you linked does not affect my project.

Here is my project, specifically my treefmt-nix instantiation: https://github.com/kuruczgy/x1e-nixos-config/blob/c37010a5569bbf51e07631d7094ae3d036dd50a6/flake.nix#L106

kuruczgy avatar Jun 28 '25 21:06 kuruczgy

Got it! Then I'd suggest that you set projectRootFile. Something like this:

diff --git a/flake.nix b/flake.nix
index d6c9d1c..9b82024 100644
--- a/flake.nix
+++ b/flake.nix
@@ -113,6 +113,8 @@
             );
           in
           (treefmt-nix.evalModule pkgs {
+            projectRootFile = "flake.nix";
+
             programs.nixfmt.enable = true;
             settings.on-unmatched = "info";
             programs.mdformat = {

jfly avatar Jun 28 '25 22:06 jfly

Thanks, that seems like a pragmatic workaround for my project.

Do you believe this issue should stay open? This workaround will not work for projects without a flake.nix.

kuruczgy avatar Jun 28 '25 22:06 kuruczgy

Good question. My (not very strong) opinion is that we provide a good experience for folks using flakes, and for folks not using flakes, they may be choosing a bit more of an adventure.

If you're correct that core treefmt would handle this correctly, I suppose there's an argument for getting rid of this logic and just letting treefmt figure it out.

jfly avatar Jun 29 '25 20:06 jfly