Skip to content

bug: treefmt seems to remove whole file in nix files #793

@DrTeagle

Description

@DrTeagle

Neovim version (nvim -v)

NVIM v0.12.0-nightly+cf48741 Build type: Release LuaJIT 2.1.1741730670 Run "nvim -V1 -v" for more info

Operating system/version

nixos

Read debugging tips

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

treefmt root directory not found.

However, it seems that nixd formatter runs for God knows what reason, even though it is the exact same command.

Describe the bug

I made this beast, I am more asking if anybody else is able to reproduce it. whenever using treefmt on nix files+ nixd as lsp server. treefmt or something freaks the fuck out, and deletes the whole file.

What is the severity of this bug?

tolerable (can work around it)

Steps To Reproduce

  1. use treefmt as your everything formatter
  2. have this after/lsp/nixd.lua:
---@type vim.lsp.ClientConfig
return {
  root_markers = { "flake.nix", ".git" },
  cmd = {
    "nixd",
  },
  filetypes = { "nix" },
  settings = {
    nixd = {
      inlay_hints = true,
      formatting = {
        command = { "treefmt" },
      },
      nixpkgs = {
        expr = "(builtins.getFlake (builtins.toString ./.)).inputs.nixpkgs {}",
      },
      options = {
        nixOptions = {
          expr = '(builtins.getFlake "/home/cdockter/Documents/myNixOS").nixosConfigurations.nixos.options',
        },
        home_manager = {
          expr = '(builtins.getFlake "/home/cdockter/Documents/myNixOS").homeConfigurations."cdockter".options',
        },
        flake_parts = {
          expr = "(builtins.getFlake (builtins.toString ./.)).debug.options",
        },
        perSystem = {
          expr = "(builtins.getFlake (builtins.toString ./.)).currentSystem.options",
        },
      },
    },
  },
}

as well as this conform option set:

  formatters_by_ft = {
    markdown = { "injected" },
    ["*"] = { "treefmt" },
  },
  1. write seemingly to any nix file
  2. file is now blank

Expected Behavior

I would like treefmt(the module I introduced, so I will have to fix this sometime if it is not a massive skill issue) to not delete my entire nix file.
I am using treefmt-nix so that might be the issue, I will try and use treefmt proper and see if it still occurs.

Minimal example file

No response

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        -- add your config here
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

as far as I know conform calls the command; yet when I call the command, treefmt works w/o a hitch. it is possible that treefmt made it no longer possible to do it one file at a time. In which case I would remove the formatter, as I assume that running treefmt on your entire repo(even though it is very fast) can be a little annoying to have all the files change on a single write.


edit: I tested it and it seems to be a treefmt-nix issue. which makes no sense to me. I have exposed the treefmt config to the shell properly.
the error that I do get is this:
RPC[Error] code_name = UnknownErrorCode, message = "formatting treefmt command exited with 256"

I think it might have to do with how I am passing in formatting to treefmt, I will get back to you "tommorow" (even though it is already 01:26) when I sleep a tad bit more

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions