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

bug: isort adds additional newline to end of file #365

Closed
1 task done
tanj opened this issue Apr 4, 2024 · 4 comments
Closed
1 task done

bug: isort adds additional newline to end of file #365

tanj opened this issue Apr 4, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@tanj
Copy link

tanj commented Apr 4, 2024

Neovim version (nvim -v)

NVIM v0.10.0-dev-2752+g35239e977

Operating system/version

Windows 11 Pro

Add the debug logs

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

Log file

15:35:45[DEBUG] Running formatters on C:\src\test.py: { "isort" }
15:35:45[INFO] Run isort on C:\src\test.py
15:35:45[DEBUG] Run command: { "isort", "--stdout", "--filename", "C:\\\\src\\test.py", "-" }
15:35:45[DEBUG] Run CWD: C:/src/
15:35:45[DEBUG] isort exited with code 0

Describe the bug

When saving a python file with isort as one of the formatters the file will end up with an extra newline (\n) at the end of the file.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. nvim --clean test.py
  2. insert print("hello")
  3. :wq
  4. nvim -u repro.lua test.py
  5. :w

Expected Behavior

No extra newline added to file

Minimal example file

example input file xxd

00000000: 7072 696e 7428 2268 656c 6c6f 2229 0a    print("hello").

after isort run by conform xxd

00000000: 7072 696e 7428 2268 656c 6c6f 2229 0a0a  print("hello")..

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({
                format_on_save = function(bufnr)
                    return {
                        timeout_ms = 1500,
                    }
                end,
                log_level = vim.log.levels.DEBUG,
                -- add your config here
                formatters_by_ft = {
                    python = { "isort" },
                },
            })
        end,
    },
    -- add any other plugins here
}
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

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

Additional context

No response

@tanj tanj added the bug Something isn't working label Apr 4, 2024
@msyavuz
Copy link

msyavuz commented Apr 19, 2024

I couldn't reproduce this in Linux. It may very well be the default behavior of isort on Windows. Either way conform calls isort with some default arguments nothing more. Here

@tanj
Copy link
Author

tanj commented Apr 19, 2024

If I run isort directly on the file (not through stdin/stdout) then no extra line at the end occurs. If, however, I pipe the input through and back out via stdin/stdout then the line endings of the file get changed from \n to \r\n. I wonder if this may be a bug in neovim where if the replacement text has \r\n line endings in a \n buffer it ends up with the extra \n at the end of the file.

@msyavuz
Copy link

msyavuz commented Apr 19, 2024

It can also be an intended effect with the fixeol setting. You can try turning it off.

@tanj
Copy link
Author

tanj commented Apr 19, 2024

I added a simple dos2unix formatter to my formatter pipeline and it fixes this. I created a pull request if it is wanted. #383

Format pipline:
python = { 'isort', 'dos2unix' },

this fixes the issue for me.

@tanj tanj closed this as completed Apr 19, 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
Projects
None yet
Development

No branches or pull requests

2 participants