null-ls.nvim icon indicating copy to clipboard operation
null-ls.nvim copied to clipboard

Issue with null-ls and `dprint` when formatting non-existent files

Open leonasdev opened this issue 1 year ago • 1 comments

FAQ

  • [X] I have checked the FAQ and it didn't resolve my problem.

Issues

  • [X] I have checked existing issues and there are no issues with the same problem.

Neovim Version

NVIM v0.9.0

Dev Version?

  • [X] I am using a stable Neovim release version, or if I am using a dev version of Neovim I have confirmed that my issue is reproducible on a stable version.

Operating System

Ubuntu 22.04

Minimal Config

-- this template is borrowed from nvim-lspconfig
local on_windows = vim.loop.os_uname().version:match("Windows")

local function join_paths(...)
    local path_sep = on_windows and "\\" or "/"
    local result = table.concat({ ... }, path_sep)
    return result
end

vim.g.loaded_remote_plugins = ""
vim.cmd([[set runtimepath=$VIMRUNTIME]])

local temp_dir = vim.loop.os_getenv("TEMP") or "/tmp"

vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))

local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")

local null_ls_config = function()
    local null_ls = require("null-ls")
    -- add only what you need to reproduce your issue
    null_ls.setup({
        sources = {
	    null_ls.builtins.formatting.dprint,
	},
        debug = true,
    })
end

local function load_plugins()
    -- only add other plugins if they are necessary to reproduce the issue
    require("packer").startup({
        {
            "wbthomason/packer.nvim",
            {
                "jose-elias-alvarez/null-ls.nvim",
                requires = { "nvim-lua/plenary.nvim" },
                config = null_ls_config,
            },
        },
        config = {
            package_root = package_root,
            compile_path = compile_path,
        },
    })
end

if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
    load_plugins()
    require("packer").sync()
else
    load_plugins()
    require("packer").sync()
end

Steps to Reproduce

  1. Install dprint using cargo or any package manager
  2. dprint init to initial dprint.json file (make sure you have selected dprint-plugin-typescript)
  3. Open a new file using: nvim --clean -u minimal_init.lua test.ts
  4. Add the following text: function test () { }, *do not save file*
  5. :lua vim.lsp.buf.format( { bufnr = 0 } ) to request textDocument/formatting
  6. Content is not formatted correctly
  7. :w to save file
  8. :lua vim.lsp.buf.format( { bufnr = 0 } ) again
  9. Content is formatted correctly

Reproducibility Check

  • [X] I confirm that my minimal config is based on the minimal_init.lua template and that my issue is reproducible by running nvim --clean -u minimal_init.lua and following the steps above.

Expected Behavior

  • When sending a textDocument/formatting request to null-ls with a not exist file (e.g. nvim test.ts) that has some content, the content should be formatted properly by dprint.

Actual Behavior

  • When sending a textDocument/formatting request to null-ls with a not exist file (e.g. nvim test.ts) that has some content, the content is not formatted by dprint.

  • Other formatters (e.g. stylua, prettier) do not have this issue.

Debug Log

[TRACE Thu 13 Apr 2023 10:17:47 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:102: received LSP request for method shutdown
[TRACE Thu 13 Apr 2023 10:17:47 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:127: received LSP notification for method exit
[TRACE Thu 13 Apr 2023 10:17:49 PM CST] ...te/pack/packer/start/null-ls.nvim/lua/null-ls/client.lua:97: starting null-ls client
[TRACE Thu 13 Apr 2023 10:17:49 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:102: received LSP request for method initialize
[DEBUG Thu 13 Apr 2023 10:17:49 PM CST] ...te/pack/packer/start/null-ls.nvim/lua/null-ls/client.lua:152: unable to notify client for method textDocument/didOpen (client not active): {
  textDocument = {
    uri = "file:///root/dprint-test/test.ts"
  }
}
[TRACE Thu 13 Apr 2023 10:17:49 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:127: received LSP notification for method initialized
[TRACE Thu 13 Apr 2023 10:17:49 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:127: received LSP notification for method textDocument/didOpen
[TRACE Thu 13 Apr 2023 10:17:49 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG Thu 13 Apr 2023 10:17:49 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE Thu 13 Apr 2023 10:18:05 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:127: received LSP notification for method textDocument/didChange
[TRACE Thu 13 Apr 2023 10:18:05 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS
[DEBUG Thu 13 Apr 2023 10:18:05 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE Thu 13 Apr 2023 10:18:14 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:102: received LSP request for method textDocument/formatting
[TRACE Thu 13 Apr 2023 10:18:14 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_FORMATTING
[DEBUG Thu 13 Apr 2023 10:18:14 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:320: spawning command "dprint" at /root/dprint-test with args { "fmt", "--stdin", "/root/dprint-test/test.ts" }
[TRACE Thu 13 Apr 2023 10:18:14 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:204: error output: Error canonicalizing path /root/dprint-test/test.ts: No such file or directory (os error 2)

[TRACE Thu 13 Apr 2023 10:18:14 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:205: output: nil
[TRACE Thu 13 Apr 2023 10:18:14 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:209: ignoring stderr due to generator options
[TRACE Thu 13 Apr 2023 10:18:20 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:127: received LSP notification for method textDocument/didSave
[TRACE Thu 13 Apr 2023 10:18:20 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_SAVE
[DEBUG Thu 13 Apr 2023 10:18:20 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:102: received LSP request for method textDocument/formatting
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_FORMATTING
[DEBUG Thu 13 Apr 2023 10:18:21 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:320: spawning command "dprint" at /root/dprint-test with args { "fmt", "--stdin", "/root/dprint-test/test.ts" }
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:204: error output: nil
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:205: output: function test() {}

[DEBUG Thu 13 Apr 2023 10:18:21 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/formatting.lua:89: received edits from generators
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/formatting.lua:90: {
  newText = "test() {",
  range = {
    ["end"] = {
      character = 25,
      line = 0
    },
    start = {
      character = 9,
      line = 0
    }
  },
  rangeLength = 16
}
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:127: received LSP notification for method textDocument/didChange
[TRACE Thu 13 Apr 2023 10:18:21 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS
[DEBUG Thu 13 Apr 2023 10:18:21 PM CST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:24: no generators available

Help

Yes, but I don't know how to start. I would need guidance

Implementation Help

  • It is suspected that this may be due to the fact that the $FILENAME passed to dprint's --stdin flag by null-ls does not yet exist, causing dprint to do nothing.

Requirements

  • [X] I have read and followed the instructions above and understand that my issue will be closed if I did not provide the required information.

leonasdev avatar Apr 13 '23 14:04 leonasdev

Unfortunately the log makes it pretty clear that this is an issue with dprint. Unless they add some way to support stdin without specifically passing in a filepath (which seems unlikely, since that would make it difficult to find config files) there's not much we can do.

jose-elias-alvarez avatar Apr 21 '23 23:04 jose-elias-alvarez