GCBallesteros / jupytext.nvim

Jupyter notebooks on neovim powered by Jupytext
66 stars 14 forks source link

Fails on documents with "\n" (backslash and then n, NOT a literal newline) in them #15

Closed Samasaur1 closed 1 month ago

Samasaur1 commented 5 months ago

I'm working with a ipynb file that has the literal characters \ and then n in it (as part of a LaTeX \neq macro). When I try to open this file in Neovim, I get the error Error executing lua callback: ...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:97: 'replacement string' item contains newlines, and then the file is empty.

I rerean neovim with nvim -V10nvim.log linear.ipynb and searched through the log, and I found this:

Executing BufReadCmd Autocommands for "*.ipynb"
autocommand <Lua 363: /nix/store/wh32ljllfk8a3fk7kr58hl5n7pshmd24-vim-pack-dir/pack/myNeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:175>

Executing command: "'/run/current-system/sw/bin/bash' '-c' 'jupytext '/Users/sam/linear.ipynb' --to=auto:hydrogen --output='/Users/sam/linear.py' '"

Error detected while processing BufReadCmd Autocommands for "*.ipynb":
Error executing lua callback: ...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:97: 'replacement string' item contains newlines
stack traceback:
    [C]: in function 'nvim_buf_set_lines'
    ...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:97: in function 'read_from_ipynb'
    ...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:176: in function <...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:175>

I'm pretty sure that this is because nvim_buf_set_lines is being passed a table, where each element is supposed to be a line, and thus not have newlines in them. I managed to fix the plugin, and I'll open a PR in a minute

GCBallesteros commented 4 months ago

I finally had a bit of time to look into this but I couldn't reproduce with the current main branch. Could you send over a raw minimal ipynb file that shows the problem and your config?

Samasaur1 commented 4 months ago

Okay, sorry, I had some trouble tracking down the specific file that was causing issues. Here's the error message I was getting:

Error detected while processing BufReadd Autocommands for "*. ipynb":
Error executing lua callback: ...NeovimPackages/start/jupytext-nvim/Lua/jupytext/init.Lua:97: 'replacement string' item contains newlines
stack traceback:
    [C]: in function 'nvim buf set lines'
    ...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:97: in function 'read_from_ipynb'
    ...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:176: in function <...NeovimPackages/start/jupytext-nvim/lua/jupytext/init.lua:175>

The actual issue is not \n in the source code, as I thought, but is actually null bytes. This does mean that this PR is wrong, because it shouldn't put newlines in the generated file, but I'm pushing a new commit that correctly substitutes.

An example file (zipped) is attached: example.ipynb.zip

GCBallesteros commented 1 month ago

Hi @Samasaur1 I have finally decided against merging this PR. I think that having null bytes within "normal" code is an uncommon enough occurence that a fix here is not required. The null byte is quite special in and on itself and if somebody put it there in the first place for whatever reason they probably don't want to have jupytext.nvim messing with it.