benlubas / molten-nvim

A neovim plugin for interactively running code with the jupyter kernel. Fork of magma-nvim with improvements in image rendering, performance, and more
GNU General Public License v3.0
551 stars 30 forks source link

[Bug] Issues running multiple kernels (Python and R) in the same Quarto file #145

Closed andy941 closed 7 months ago

andy941 commented 8 months ago

make sure to do the following: read the README check existing issues (the config problem tag is helpful) try with the latest version of molten and image.nvim, latest releases and then main/master branches run :UpdateRemotePlugins

Done all that

`:checkhealth molten` molten: require("molten.health").check() molten-nvim ~ - OK NeoVim >=0.9 - OK Python >=3.10 - OK Python module pynvim found - OK Python module jupyter-client found - WARNING Optional python module cairosvg not found - ADVICE: - pip install cairosvg - WARNING Optional python module pnglatex not found - ADVICE: - pip install pnglatex - OK Python module plotly found - WARNING Optional python module kaleido not found - ADVICE: - pip install kaleido - OK Python module pyperclip found - OK Python module nbformat found
`:checkhealth provider` (the python parts) ============================================================================== molten: require("molten.health").check() molten-nvim ~ - OK NeoVim >=0.9 - OK Python >=3.10 - OK Python module pynvim found - OK Python module jupyter-client found - WARNING Optional python module cairosvg not found - ADVICE: - pip install cairosvg - WARNING Optional python module pnglatex not found - ADVICE: - pip install pnglatex - OK Python module plotly found - WARNING Optional python module kaleido not found - ADVICE: - pip install kaleido - OK Python module pyperclip found - OK Python module nbformat found

Description

A clear and concise description of what the bug is. When I open a quarto file that has python and R cells, I can start the respective kernels fine and run the first one (whichever language cell comes first) but when I try to run a cell in the other kernel I get an error like this:

Error executing vim.schedule lua callback: vim/_editor.lua:0: nvim_exec2()..function MoltenEvaluateRange[1]..remote#define#request, line 2: Vim(let):Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenEvaluateRange' on channel 6 (python3-rplugin-host):
error caught in request handler '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenEvaluateRange [['ir', 17, 21, 1, 0]]':
Traceback (most recent call last):
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/utils.py", line 11, in inner
    func(self, *args, **kwargs)
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/__init__.py", line 534, in evaulate_range
    self._do_evaluate(kernel.strip(), span)
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/__init__.py", line 330, in _do_evaluate
    kernel.run_code(code, span)
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/moltenbuffer.py", line 117, in run_code
    self.update_interface()
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/moltenbuffer.py", line 330, in update_interface
    self._show_selected(self.selected_cell)
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/moltenbuffer.py", line 404, in _show_selected
    self.outputs[span].show_floating_win(span.end)
  File "/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten/outputbuffer.py", line 252, in show_floating_win
    self.display_buf[0] = lines[0]
    ~~~~~~~~~~~~~~~~^^^
  File "/usr/lib/python3.11/site-packages/pynvim/api/buffer.py", line 104, in __setitem__
    return self.request('nvim_buf_set_lines', i, i + 1, True, lines)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pynvim/api/common.py", line 80, in request
    return self._session.request(name, self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pynvim/api/nvim.py", line 199, in request
    res = self._session.request(name, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pynvim/msgpack_rpc/session.py", line 137, in request
    raise self.error_wrapper(err)
pynvim.api.common.NvimError: Vim:error caught in async handler '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenUpdateInterface [[]]'
stack traceback:
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    ...andrea/.local/share/nvim/lazy/molten-nvim/lua/prompt.lua:82: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

After that anytime I try to run a cell (any language) it gets stuck in a on hold indefinitely. Sometimes the error is slightly different (but similar) depending on which language I try to run first. I can run one python cell and one R cell if I start the kernels first and then run both. Then if I run a python cell again it gives me that error or similar, I thought it was worth mentioning.

Reproduction Steps

You can use this file (probably GitHub will mess up the quarto markup):

---
format: html
title: "TRIAL"
author: "Andrea Finocchio"
#bibliography: cite.bib
citeproc: true
useMath: true
standalone: true
html:
  embed-resources: true
  self-contained-math: true
  code-fold: true
engine: knitr
---

```{r}
# Define the cars vector with 5 values
cars <- c(1, 3, 6, 4, 9)

# Graph the cars vector with all defaults
plot(cars)

For a demonstration of a line plot on a polar axis, see @fig-polar.

# | label: fig-polar
# | fig-cap: "A line plot on a polar axis"

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(1, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()

I also sometimes get hit with a barrage of errors that look like this when I run my first cell (infinite loop):

Error detected while processing BufUnload Autocommands for "*"..function MoltenOnBufferUnload[1]..remote#define#request:
line 2:
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5
Error invoking '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenOnBufferUnload' on channel 5:
Invalid channel: 5

maybe this is related to the other error?

_Optionally you can include a minimal config to reproduce the issue. This will help me figure things out much more quickly. You can find a sample minimal config [here](https://github.com/benlubas/molten-nvim/blob/main/docs/minimal.lua). If you include one, please also include the output of `pip freeze` from the python3 host program that you specify in the config._

The minimal config does not have the quarto commands so I'll paste my config as is at the moment:

``` lua
-- lazy.lua
    {
        "3rd/image.nvim",
        lazy = true,
        ft = { "markdown", "quarto" },
        config = function()
            require("config.image")
        end,
    },
    {
        "benlubas/molten-nvim",
        lazy = true,
        build = ":UpdateRemotePlugins",
        ft = { "markdown", "quarto" },
        config = function()
            require("config.molten")
        end,
    },
    {
        "jmbuhr/otter.nvim",
        lazy = true,
        dev = false,
        opts = {
            buffers = { set_filetype = true, write_to_disk = true },
        },
    },
    {
        "quarto-dev/quarto-nvim",
        dev = false,
        dependencies = {
            "jmbuhr/otter.nvim",
            "3rd/image.nvim",
        },
        ft = { "markdown", "quarto" },
        config = function()
            require("config.quarto")
        end,
    },
-- molten
vim.g.molten_image_provider = "image.nvim"
vim.g.molten_auto_open_output = true
vim.g.molten_virt_text_output = true
vim.g.molten_wrap_output = true
vim.g.molten_virt_lines_off_by_1 = true

local utils = require("utils")
utils.map("n", "<leader>mi",   "<cmd>MoltenInit<cr>", opts)

-- image
require("image").setup({
  backend = "kitty",
  integrations = {
    markdown = {
      enabled = true,
      clear_in_insert_mode = true,
      download_remote_images = true,
      only_render_image_at_cursor = true,
      filetypes = { "markdown", "vimwiki", "quarto" }, -- markdown extensions (ie. quarto) can go here
    },
    neorg = {
      enabled = true,
      clear_in_insert_mode = false,
      download_remote_images = true,
      only_render_image_at_cursor = false,
      filetypes = { "norg" },
    },
  },
  max_width = 1000,
  max_height = 38,
  max_width_window_percentage = math.huge,
  max_height_window_percentage = math.huge,
  window_overlap_clear_enabled = true, -- toggles images when windows are overlapped
  window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
  editor_only_render_when_focused = false, -- auto show/hide images when the editor gains/looses focus
  tmux_show_only_in_active_window = false, -- auto show/hide images in the correct Tmux window (needs visual-activity off)
  hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp" }, -- render image files as images when opened
})

-- quarto
require("quarto").setup({
    debug = false,
    closePreviewOnExit = true,
    lspFeatures = {
        enabled = true,
        languages = { "r", "python", "julia", "sh", "zsh", "bash", "c", "cpp" },
        chunks = "all", -- 'curly' or 'all'
        diagnostics = {
            enabled = true,
            triggers = { "BufWritePost" },
        },
        completion = {
            enabled = true,
        },
    },
    codeRunner = {
        enabled = true,
        default_method = "molten", -- 'molten' or 'slime'
        --ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
        -- Takes precedence over `default_method`
        never_run = { "yaml" }, -- filetypes which are never sent to a code runner
    },
    keymap = {
        hover = "K",
        definition = "gD",
        rename = "<leader>rn",
        references = "gr",
    },
})

local runner = require("quarto.runner")

vim.keymap.set("n", "<C-s>", runner.run_cell, { desc = "run cell", silent = true })
-- vim.keymap.set("n", "<>ra", runner.run_above, { desc = "run cell and above", silent = true })
vim.keymap.set("n", "<localleader>a", runner.run_all, { desc = "run all cells", silent = true })
vim.keymap.set("n", "<localleader>l", runner.run_line, { desc = "run line", silent = true })
vim.keymap.set("v", "<C-s>", runner.run_range, { desc = "run visual range", silent = true })
vim.keymap.set("n", "<localleader>A", function()
    runner.run_all(true)
end, { desc = "run all cells of all languages", silent = true })
vim.keymap.set("n", "<leader>rs", "<cmd>QuartoActivate<cr>", opts)
vim.keymap.set("n", "<leader>vv", "<cmd>QuartoPreview<cr>", opts)
vim.keymap.set("n", "<leader>cB", "<cmd>Telescope bibtex format=markdown<cr>", opts)
`pip freeze` aiohttp==3.9.1 aiosignal==1.3.1 annotated-types==0.6.0 anyio==4.2.0 anytree==2.12.1 apparmor==3.1.6 appdirs==1.4.4 application-utility==1.3.2 apsw==3.43.1.0 arandr==0.1.11 argcomplete==2.0.0 argon2-cffi==23.1.0 argon2-cffi-bindings==21.2.0 asciidoc==10.2.0 asn1crypto==1.5.1 asttokens==2.4.1 async-lru==2.0.4 async-timeout==4.0.3 attrs==23.1.0 autocommand==2.2.2 Automat==22.10.0 Babel==2.12.1 backcall==0.2.0 bauh==0.10.7 bcrypt==4.1.2 Beaker==1.12.0 beautifulsoup4==4.12.2 bleach==6.1.0 blosc==1.11.1 Bottleneck==1.3.7 breezy==3.3.4 Brotli==1.1.0 brotlicffi==1.1.0.0 btrfsutil==6.6.3 build==1.0.3 CacheControl==0.13.1 cachetools==5.3.2 cairocffi==1.6.1 certifi==2023.11.17 cffi==1.16.0 chardet==5.2.0 charset-normalizer==3.3.2 click==8.1.7 colorama==0.4.6 comm==0.2.1 commonmark==0.9.1 configobj==5.0.8 constantly==15.1.0 contextlib2==21.6.0 contourpy==1.2.0 coverage==6.5.0 cryptography==41.0.7 css-parser==1.0.9 cssselect==1.2.0 cycler==0.12.1 Cython==3.0.8 dbus-python==1.3.2 decorator==5.1.1 defusedxml==0.7.1 deluge==2.1.1 deprecation==2.1.0 distlib==0.3.8 distro==1.9.0 dnspython==2.4.2 docopt==0.6.2 docutils==0.20.1 dulwich==0.21.7 duplicity==2.1.4 editables==0.5 entrypoints==0.4 et-xmlfile==1.1.0 exceptiongroup==1.2.0 executing==2.0.0 fastbencode==0.2 fasteners==0.19 fastjsonschema==2.19.1 faust-cchardet==2.1.19 feedparser==6.0.11 filelock==3.13.1 flit_core==3.9.0 fonttools==4.47.0 frozenlist==1.4.1 fsspec==2023.9.2 future==0.18.3 GeoIP==1.3.2 google-api-core==2.15.0 google-api-python-client==2.111.0 google-auth==2.25.2 google-auth-httplib2==0.1.1 googleapis-common-protos==1.62.0 greenlet==3.0.1 gufw==24.4.0 hatch-jupyter-builder==0.8.3 hatchling==1.21.0 html2text==2020.1.16 html5-parser==0.4.11 html5lib==1.1 httplib2==0.22.0 hyperlink==21.0.0 hypothesis==6.92.2 i3-py==0.6.5 idna==3.5 ifaddr==0.2.0 importlib-metadata==5.0.0 incremental==22.10.0 inflate64==0.3.1 inflect==7.0.0 iniconfig==2.0.0 installer==0.7.0 ipykernel==6.28.0 ipython==8.20.0 Jade-Application-Kit==3.5.7 jaraco.context==4.3.0 jaraco.functools==3.9.0 jaraco.text==3.12.0 jarowinkler==2.0.1 jedi==0.19.1 jeepney==0.8.0 Jinja2==3.1.2 json5==0.9.14 jsonschema==4.20.0 jsonschema-specifications==2023.12.1 jupyter-events==0.9.0 jupyter_client==8.6.0 jupyter_core==5.7.1 jupyter_packaging==0.12.3 jupyter_server==2.12.2 jupyterlab==4.0.10 jupyterlab_pygments==0.3.0 jupyterlab_server==2.25.2 jupytext==1.16.1 kiwisolver==1.4.5 lark==1.1.8 lensfun==0.3.4 Levenshtein==0.23.0 LibAppArmor==3.1.6 libtorrent==2.0.9 lit==16.0.6.dev0 llvmlite==0.41.0 lockfile==0.12.2 louis==3.28.0 lxml==4.9.2 Mako==1.3.0 mallard-ducktype==1.0.2 manjaro-sdk==0.1.1 Markdown==3.5.1 markdown-it-py==3.0.0 MarkupSafe==2.1.3 matplotlib==3.8.2 matplotlib-inline==0.1.6 mdit-py-plugins==0.4.0 mdurl==0.1.2 mechanize==0.4.8 mercurial==6.6.1 merge3==0.0.14 meson==1.3.1 mistune==2.0.5 monotonic==1.6 more-itertools==10.1.0 msgpack==1.0.5 multidict==6.0.4 multivolumefile==0.2.3 nbclient==0.9.0 nbconvert==7.14.0 nbformat==5.9.2 nest-asyncio==1.5.7 netifaces==0.11.0 netsnmp-python==1.0a1 nftables==0.1 notebook==7.0.6 notebook_shim==0.2.3 npyscreen==4.10.5 nspektr==0.5.0 numba==0.58.1 numexpr==2.8.8 numpy==1.26.3 oauth2client==4.1.3 openpyxl==3.1.2 ordered-set==4.1.0 overrides==7.4.0 packaging==23.2 pacman_mirrors==4.24.0 pandas==1.5.3 pandas-datareader==0.10.0 pandocfilters==1.5.0 paramiko==2.11.1 parso==0.8.3 pathspec==0.12.1 patiencediff==0.2.14 pdftotext==2.2.2 pep517==0.13.0 pexpect==4.9.0 pickleshare==0.7.5 pillow==10.2.0 pipx==1.4.1 platformdirs==4.1.0 plotly==5.18.0 pluggy==1.3.0 ply==3.11 poetry-core==1.8.1 pooch==1.8.0 progress==1.6 prometheus-client==0.19.0 prompt-toolkit==3.0.43 protobuf==4.25.1 psutil==5.9.7 psycopg2==2.9.9 ptyprocess==0.7.0 pulsemixer==1.5.1 pure-eval==0.2.2 pwquality==1.4.5 py7zr==0.20.6 pyaml==23.9.0 pyarrow==14.0.2 pyasn1==0.4.8 pyasn1-modules==0.2.8 pybcj==1.0.1 pycairo==1.25.1 pychm==0.8.6 pycparser==2.21 pycryptodome==3.19.1 pycryptodomex==3.12.0 pydantic==2.5.3 pydantic_core==2.14.6 PyDrive==1.3.1 PyDrive2==1.17.0 Pygments==2.17.2 PyGObject==3.46.0 PyHamcrest==2.0.4 pyinotify==0.9.6 PyMySQL==1.1.0 PyNaCl==1.4.0 pynvim==0.5.0 pyOpenSSL==23.3.0 pyparsing==3.1.1 pyPEG2==2.15.2 pyperclip==1.8.2 pyppmd==1.0.0 pyproject_hooks==1.0.0 PyQt5==5.15.10 PyQt5-sip==12.13.0 PyQt6==6.6.1 PyQt6-sip==13.6.0 PyQt6-WebEngine==6.6.0 PyQtWebEngine==5.15.6 pyrsistent==0.19.3 pyserial==3.5 PySide2==5.15.12 PySide6==6.6.1 pysmbc==1.0.25.1 pytest==7.4.4 python-dateutil==2.8.2 python-distutils-extra==2.39 python-json-logger==2.0.7 python-snappy==0.6.1 python-xlib==0.33 pytz==2023.3.post1 pyxdg==0.28 PyYAML==6.0.1 pyzmq==25.1.2 pyzstd==0.15.9 QtPy==2.4.0 ranger-fm==1.9.3 rapidfuzz==3.6.0 rapidfuzz-capi==1.0.5 referencing==0.32.0 regex==2023.12.25 rencode==1.0.6 reportlab==3.6.12 requests==2.31.0 resolvelib==1.0.1 retrying==1.3.3 rich==13.7.0 rpds-py==0.15.2 rsa==4.9 ruamel.yaml==0.17.40 ruamel.yaml.clib==0.2.8 scikit-build==0.17.6 SciPy==1.11.4 scour==0.38.2 Send2Trash==1.8.2 service-identity==23.1.0 setproctitle==1.3.3 setuptools-scm==8.0.4 sgmllib3k==1.0.0 shiboken2==5.15.12 shiboken6==6.6.1 shiboken6-generator==6.6.1 six==1.16.0 sniffio==1.3.0 sortedcontainers==2.4.0 soupsieve==2.5 speedtest-cli==2.1.3 SQLAlchemy==1.4.44 stack-data==0.6.3 systemd-python==235 tables==3.7.0 tabulate==0.9.0 TBB==0.2 tenacity==8.2.3 testpath==0.6.0 texttable==1.7.0 tinycss2==1.2.1 TLPUI==1.6.1 toml==0.10.2 tomli==2.0.1 tomlkit==0.12.3 tornado==6.3.2 tqdm==4.66.1 traitlets==5.14.1 trove-classifiers==2024.1.9 Twisted==22.10.0 typing_extensions==4.9.0 uc-micro-py==1.0.2 ueberzug==18.2.1 ufw==0.36.2 unrardll==0.1.7 uritemplate==4.1.1 urllib3==1.26.18 userpath==1.9.1 validate==5.0.8 validate-pyproject==0.13.post1.dev0+gb752273.d20230520 virtualenv==20.25.0 wcwidth==0.2.12 webencodings==0.5.1 websocket-client==1.7.0 wxPython==4.2.1 xarray==2023.1.0 xlrd==2.0.1 XlsxWriter==3.1.9 xlwt==1.3.0 xxhash==3.2.0 yarl==1.9.4 zeroconf==0.63.0 zipfile-deflate64==0.2.0 zipp==3.17.0 zope.interface==6.1 zstandard==0.22.0

Expected Behavior

A clear and concise description of what you expected to happen. Being able to run python and R in the same quarto document and hopefully exchange object between the two with R:reticulate. That would be amazing.

andy941 commented 8 months ago

Another example of the error when running code in the "second" kernel:

Error executing vim.schedule lua callback: vim/_editor.lua:0: nvim_exec2()..function MoltenEvaluateRange[1]..remote#define#request, line 2: Vim(let):error caught in async handler '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenUpdateInterface [[]]'
stack traceback:
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    ...andrea/.local/share/nvim/lazy/molten-nvim/lua/prompt.lua:82: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

I also tried to setup a virtual environment for python:

utils.opt("o", "python3_host_program", vim.fn.expand("$HOME") .. "/.pyenv/versions/neovim/bin/python")

It did not fix the issue

benlubas commented 8 months ago

huh, that's weird. I'm able to run python and ruby cells just fine. I wonder if R is a little different. I will take a closer look at this later today.

It looks like you're running the latest version of main, just double check the plugin is up to date, I definitely have had issues like that in the past.

https://github.com/benlubas/molten-nvim/assets/56943754/228569a8-3d7c-4931-b03f-95f3794097b5

andy941 commented 8 months ago

I see, I use IRKernel for R. Thank you very much for looking into this, I am just starting to use quarto and molten and there are a lot of moving parts.

A side question: do you always have to specify the kernel to use? Or is there a way to make molten use the last one or figure out if there is one active already for the language in the cell? quarto.nvim leverages otter.nvim to get lsp features for different languages in the same quarto document so I would get that information is there. That would be another great improvement!

Andrea

benlubas commented 8 months ago

Automatically sending code to the right kernel would be something that quarto.nvim could do. I want to keep molten totally format agnostic, so code running integrations would have to handle language detection like that. It was something that I considered when I wrote the original code running integration for quarto, but it's a hard problem to solve, and I wasn't even sure what the best solution would look like. So I just left it for later.

I also don't use notebooks with more than one language very often, if ever.

But if you have ideas about what it would look like, feel free to leave them in a feature request over there.

andy941 commented 8 months ago

Cool yeah I understand. I might open a feature request, but as you said it might be more well suited for quarto.nvim to address.

benlubas commented 8 months ago

I'm really stumped here. I'm able to run the exact code you've run, and I can execute the cells multiple times and I'm not seeing any problems.

image

andy941 commented 8 months ago

I tried everything I could come up with, that is why I opened the ticket. It must be something specific to my setup. I see that on Linux and Marcos too (M1). Do those errors tell you anything? Something that could direct me to the solution.

benlubas commented 8 months ago

If you suspect that it's a setup or configuration problem the best thing to do it to try and use the minimal configuration supplied here. (note that you should read through that file, there are some values you have to change.)

This should completely isolate things. If you can still reproduce the issue with a minimal config I can run the same config and that makes it easier to figure out what's going wrong.

If you can't reproduce with the minimal config, then we know it's a config problem and we can take a closer look at your config.

andy941 commented 8 months ago

Minimal config:

-- Example for configuring Neovim to load user-installed installed Lua rocks:
package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?/init.lua"
package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?.lua"

--        You should specify your python3 path here \/.
--vim.g.python3_host_prog = vim.fn.expand("$HOME") .. "/.pyenv/versions/neovim/bin/python"

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 = {
    {
        "bluz71/vim-moonfly-colors",
        lazy = false,
        priority = 1000,
        config = function()
            vim.cmd.syntax("enable")
            vim.cmd.colorscheme("moonfly")

            vim.api.nvim_set_hl(0, "MoltenOutputBorder", { link = "Normal" })
            vim.api.nvim_set_hl(0, "MoltenOutputBorderFail", { link = "MoonflyCrimson" })
            vim.api.nvim_set_hl(0, "MoltenOutputBorderSuccess", { link = "MoonflyBlue" })
        end,
    },
    {
        "benlubas/molten-nvim",
        dependencies = { "3rd/image.nvim" },
        build = ":UpdateRemotePlugins",
        init = function()
            vim.g.molten_image_provider = "image.nvim"
            vim.g.molten_use_border_highlights = true
            -- add a few new things

            -- don't change the mappings (unless it's related to your bug)
            vim.keymap.set("n", "<localleader>mi", ":MoltenInit<CR>")
            vim.keymap.set("n", "<localleader>e", ":MoltenEvaluateOperator<CR>")
            vim.keymap.set("n", "<localleader>rr", ":MoltenReevaluateCell<CR>")
            vim.keymap.set("v", "<localleader>r", ":<C-u>MoltenEvaluateVisual<CR>gv")
            vim.keymap.set("n", "<localleader>os", ":noautocmd MoltenEnterOutput<CR>")
            vim.keymap.set("n", "<localleader>oh", ":MoltenHideOutput<CR>")
            vim.keymap.set("n", "<localleader>md", ":MoltenDelete<CR>")
        end,
    },
    {
        "3rd/image.nvim",
        opts = {
            backend = "kitty",
            integrations = {},
            max_width = 100,
            max_height = 12,
            max_height_window_percentage = math.huge,
            max_width_window_percentage = math.huge,
            window_overlap_clear_enabled = true,
            window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
        },
        version = "1.1.0", -- or comment out for latest
    },
    {
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        config = function()
            require("nvim-treesitter.configs").setup({
                ensure_installed = {
                    "markdown",
                    "markdown_inline",
                    "python",
                },
                highlight = {
                    enable = true,
                    additional_vim_regex_highlighing = false,
                },
            })
        end,
    },
    -- add any additional plugins here
    {
        "jmbuhr/otter.nvim",
        lazy = true,
        dev = false,
        opts = {
            buffers = { set_filetype = true, write_to_disk = true },
        },
    },
    {
        "quarto-dev/quarto-nvim",
        dev = false,
        dependencies = {
            "neovim/nvim-lspconfig",
            "jmbuhr/otter.nvim",
            "3rd/image.nvim",
            "benlubas/molten-nvim",
        },
        ft = { "markdown", "quarto" },
        config = function()
            require("quarto").setup({
                debug = true,
                closePreviewOnExit = true,
                lspFeatures = {
                    enabled = false,
                    languages = { "r", "python", "julia", "sh", "zsh", "bash", "c", "cpp" },
                    chunks = "all", -- 'curly' or 'all'
                    diagnostics = {
                        enabled = false,
                        triggers = { "BufWritePost" },
                    },
                    completion = {
                        enabled = false,
                    },
                },
                codeRunner = {
                    enabled = true,
                    default_method = "molten", -- 'molten' or 'slime'
                    --ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
                    -- Takes precedence over `default_method`
                    never_run = { "yaml" }, -- filetypes which are never sent to a code runner
                },
                keymap = {
                    hover = "K",
                    definition = "gD",
                    rename = "<leader>rn",
                    references = "gr",
                },
            })

            local runner = require("quarto.runner")

            vim.keymap.set("n", "<C-s>", runner.run_cell, { desc = "run cell", silent = true })
            -- vim.keymap.set("n", "<>ra", runner.run_above, { desc = "run cell and above", silent = true })
            vim.keymap.set("n", "<localleader>a", runner.run_all, { desc = "run all cells", silent = true })
            vim.keymap.set("n", "<localleader>l", runner.run_line, { desc = "run line", silent = true })
            vim.keymap.set("v", "<C-s>", runner.run_range, { desc = "run visual range", silent = true })
            vim.keymap.set("n", "<localleader>A", function()
                runner.run_all(true)
            end, { desc = "run all cells of all languages", silent = true })
            vim.keymap.set("n", "<leader>rs", "<cmd>QuartoActivate<cr>", opts)
            vim.keymap.set("n", "<leader>vv", "<cmd>QuartoPreview<cr>", opts)
            vim.keymap.set("n", "<leader>cB", "<cmd>Telescope bibtex format=markdown<cr>", opts)
        end,
    },
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

The situation here is even worse, I can activate the kernels but then when I run the cell I get the error: [Quarto] it seems that the code runner isn't initialized for this buffer. Kernels are both active though: image

I tried with the pyenv too but no luck. Is there a specific way to install kernels that would be "recommended"?

andy941 commented 8 months ago

I can do a MoltenEvaluateLine in R cells and it seems to work. In the python code however I get the same On Hold I was getting, like it is stuck. I get also similar errors:

Error invoking '/home/andrea/tmp_qmd/.repro/plugins/molten-nvim/rplugin/python3/molten:command:MoltenEvaluateVisual' on channel 3 (python3-rplugin-host):                   
error caught in request handler '/home/andrea/tmp_qmd/.repro/plugins/molten-nvim/rplugin/python3/molten:command:MoltenEvaluateVisual [[]]':                                 
Traceback (most recent call last):                                                                                                                                          
  File "/home/andrea/tmp_qmd/.repro/plugins/molten-nvim/rplugin/python3/molten/utils.py", line 11, in inner                                                                 
    func(self, *args, **kwargs)                                                                                                                                             
  File "/home/andrea/tmp_qmd/.repro/plugins/molten-nvim/rplugin/python3/molten/__init__.py", line 487, in command_evaluate_visual                                           
    self.kernel_check("MoltenEvaluateVisual %k", self.nvim.current.buffer)                                                                                                  
  File "/home/andrea/tmp_qmd/.repro/plugins/molten-nvim/rplugin/python3/molten/__init__.py", line 576, in kernel_check                                                      
    self.nvim.command(c)                                                                                                                                                    
  File "/usr/lib/python3.11/site-packages/pynvim/api/nvim.py", line 311, in command                                                                                         
    return self.request('nvim_command', string, **kwargs)                                                                                                                   
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                   
  File "/usr/lib/python3.11/site-packages/pynvim/api/nvim.py", line 199, in request                                                                                         
    res = self._session.request(name, *args, **kwargs)                                                                                                                      
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                      
  File "/usr/lib/python3.11/site-packages/pynvim/msgpack_rpc/session.py", line 137, in request                                                                              
    raise self.error_wrapper(err)               

The error comes first and the the subsequent times i run a line from anywhere it hangs.

benlubas commented 8 months ago

[Quarto] it seems that the code runner isn't initialized for this buffer.

This happens when you haven't run :QuartoActivate I think.

Is there a specific way to install kernels that would be "recommended"?

There are instructions for installing python kernels into venvs in the docs. I would say that's recommended, but shouldn't be necessary.

I've found that installing python kernels can be finicky, the longest standing open bug in this project is related to python kernels that hang (but normally they freeze up neovim). It's never been pinned down though, and installing a new kernel seems to always fix the problem.

I'm surprised this is something you're seeing on multiple systems b/c I can't seem to recreate it.

andy941 commented 8 months ago

I was careful to QuartoActivate every time but no luck. I tried different combinations of kernels with julia, still getting the same error when trying to run multiple kernels. Same error:

Error executing vim.schedule lua callback: vim/_editor.lua:0: nvim_exec2()..function MoltenEvaluateRange[1]..remote#define#request, line 2: Vim(let):error caught in async handler '/home/andrea/.local/share/nvim/lazy/molten-nvim/rplugin/python3/molten:function:MoltenUpdateInterface [[]]'
stack traceback:
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    ...andrea/.local/share/nvim/lazy/molten-nvim/lua/prompt.lua:82: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

I don't understand where that error is coming from or how to debug it but I am happy to do more digging.

benlubas commented 8 months ago

I'd start with printing some things from molten-nvim/lua/prompt.lua:82

That vim.ui.prompt callback might be causing problems.

benlubas commented 8 months ago

You might also have luck with making the UpdateInterface function sync. Just change False to True on this line: https://github.com/benlubas/molten-nvim/blob/21d766c2d60e5f6e03f507e7f3e382a2a927ad41/rplugin/python3/molten/__init__.py#L875

I know that some functions, when marked async, can cause problems, but that one never gave me issues.

andy941 commented 8 months ago

You might also have luck with making the UpdateInterface function sync. Just change False to True on this line:

https://github.com/benlubas/molten-nvim/blob/21d766c2d60e5f6e03f507e7f3e382a2a927ad41/rplugin/python3/molten/__init__.py#L875

I know that some functions, when marked async, can cause problems, but that one never gave me issues.

Good news, this completely solves the bug for me. I can run all the kernels I want! I also don't see any slowdowns which is good.

I still see the barrage of errors if I move too fast when MoltenInit whihc forces me to close the file (it's an infinite loop of errors), maybe that is an async problem too (it says something about cursormoved).

Now I can run python and R together in the same quiarto document but R::reticulate does not work to exchange objects so as of now it's useless. It's not a problem with Molten though so I am happy with the fix. We can keep the ticket open if you want to investigate the other bug, maybe making something else as sync will solve that as well.

Again marking that line as sync makes no difference to responsiveness for me.

benlubas commented 8 months ago

Glad you fixed that first one. I can change that back. I think I changed as many functions to async as I could get away with when I added a second tick function (those functions need to be async iirc). But yeah I don't think update interface needs to be.

I still see the barrage of errors if I move too fast when MoltenInit whihc forces me to close the file (it's an infinite loop of errors), maybe that is an async problem too (it says something about cursormoved).

Yeah, it's unfortunately very easy to trigger an error on every cursor moved event once one part of the plugin fails. If you want to leave that error as well I can try and help you resolve it (but also I'm able to initialize two kernels with the same keybind and don't have problems, so I doubt I'll be able to replicate it).

andy941 commented 8 months ago

Yep this one is very "random" so it's difficult to even give you a reproducible example. It happens when I reformat with formatter.nvim too. When I try to run a cell it crashes with a similar error, I'll try to send something tomorrow. It is not the infinite loop but still bad because the whole plugin just crashes and does not respond anymore.

Thank you for the help so far, it's great to have them working at least. I opened an issue about reticulate on quarto.nvim.

benlubas commented 7 months ago

@andy941 any luck reproducing this?

andy941 commented 7 months ago

No sorry, I moved to vim-slime to be able to run reticulate (I use it a lot at work) and have not used molten in a while. It wasn't a huge problem though!

Thanks for the help!

benlubas commented 7 months ago

cool, I'll close this for now then