Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when prettier is configed to use crlf eol, unexpected empty line is appended to the file after conform formatting #360

Closed
1 task done
li6in9muyou opened this issue Apr 2, 2024 · 1 comment
Labels
bug Something isn't working question Further information is requested

Comments

@li6in9muyou
Copy link
Contributor

Neovim version (nvim -v)

nvim 0.9.5

Operating system/version

Windows 11

Add the debug logs

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

Log file

before my fix

22:50:06[DEBUG] Running formatters on D:\projects\conform-prettier-crlf-reproduce-bug\index.html: { "prettier" }
22:50:06[INFO] Run prettier on D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:06[TRACE] Input lines: { "<body>", "  <div></div>", "</body>" }
22:50:06[DEBUG] Run command: { "prettier.cmd", "--stdin-filepath", "D:\\projects\\conform-prettier-crlf-reproduce-bug\\index.html" }
22:50:06[DEBUG] Run CWD: D:/projects/conform-prettier-crlf-reproduce-bug
22:50:06[DEBUG] prettier exited with code 0
22:50:06[TRACE] Output lines: { "<body>\r", "  <div></div>\r", "</body>\r" }
22:50:06[TRACE] prettier stderr: { "" }
22:50:06[TRACE] Applying formatting to D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:06[TRACE] Comparing lines { "<body>", "  <div></div>", "</body>" } and { "<body>\r", "  <div></div>\r", "</body>\r" }
22:50:06[TRACE] Diff indices { { 1, 3, 1, 3 } }
22:50:06[TRACE] Applying text edits: { {
    newText = "\r\n  <div></div>\r\n</body>\r",
    range = {
      ["end"] = {
        character = 7,
        line = 2
      },
      start = {
        character = 6,
        line = 0
      }
    }
  } }
22:50:06[TRACE] Done formatting D:\projects\conform-prettier-crlf-reproduce-bug\index.html

after my fix

22:50:03[DEBUG] Running formatters on D:\projects\conform-prettier-crlf-reproduce-bug\index.html: { "prettier" }
22:50:03[INFO] Run prettier on D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:03[TRACE] Input lines: { "<body>", "  <div></div>", "</body>" }
22:50:03[DEBUG] Run command: { "prettier.cmd", "--stdin-filepath", "D:\\projects\\conform-prettier-crlf-reproduce-bug\\index.html" }
22:50:03[DEBUG] Run CWD: D:/projects/conform-prettier-crlf-reproduce-bug
22:50:04[DEBUG] prettier exited with code 0
22:50:04[TRACE] Output lines: { "<body>", "  <div></div>", "</body>" }
22:50:04[TRACE] prettier stderr: { "" }
22:50:04[TRACE] Applying formatting to D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:04[TRACE] Comparing lines { "<body>", "  <div></div>", "</body>" } and { "<body>", "  <div></div>", "</body>" }
22:50:04[TRACE] Diff indices {}
22:50:04[TRACE] Applying text edits: {}
22:50:04[TRACE] Done formatting D:\projects\conform-prettier-crlf-reproduce-bug\index.html

Describe the bug

when format a html file with the prettier formatter, an empty line is unexpected appended to the file after running conform formatting.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. create a .prettierrc.json with content {"endOfLine": "crlf"}
  2. create a html fie with content <body>\r\n<div></div>\r\n</body>
  3. trigger a conform format with :w

Expected Behavior

Nothing should be changed by prettier however actual behaviour is that an empty line is append to the html file.

Minimal example file

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.TRACE,
      notify_on_error = false,
      format_on_save = function(bufnr)
        -- Disable "format_on_save lsp_fallback" for languages that don't
        -- have a well standardized coding style. You can add additional
        -- languages here or re-enable it for the disabled ones.
        local disable_filetypes = { c = true, cpp = true }
        return {
          timeout_ms = 500,
          lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
        }
      end,
      formatters_by_ft = {
        lua = { 'stylua' },
        html = { 'prettier' },
        -- Conform can also run multiple formatters sequentially
        -- python = { "isort", "black" },
        --
        -- You can use a sub-list to tell conform to run *until* a formatter
        -- is found.
        javascript = { 'prettier' },
        typescript = { 'prettier' },
      },
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

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

Additional context

When fixing #274 in commit 9a785eb in file lua/conform/runner.lua, \r\n eol is handled in one branch of the if block so that no elements output end with \r. My fix ensures that this behaviour is consistent in both branches.

@stevearc
Copy link
Owner

Is this fixed by your PR?

@stevearc stevearc added the question Further information is requested label Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants