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

fix: no autocommand file name to substitute for <afile> #416

Merged
merged 1 commit into from
Jun 9, 2024

Conversation

stevearc
Copy link
Contributor

@stevearc stevearc commented Jun 4, 2024

I encountered a bunch of error in Neotest when I started Neovim in verbose mode (nvim -V1), coming from these autocmds. Turns out, when expand() fails in verbose mode it will throw an error instead of expanding to the empty string (see neovim/neovim#29175). You can reproduce the issue using the minimal init file below:

-- save as repro.lua
-- run with nvim -V1 -u repro.lua
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

vim.g.mapleader = " "
-- 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 = {
  {
    "nvim-treesitter/nvim-treesitter",
    config = function()
      require("nvim-treesitter.configs").setup({
        ensure_installed = {
          "python",
        },
        auto_install = true,
      })
    end,
  },
  {
    "nvim-neotest/neotest",
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-neotest/neotest-python",
      "nvim-neotest/nvim-nio",
      "nvim-treesitter/nvim-treesitter",
    },
    keys = {
      {
        "<leader>tf",
        function()
          require("neotest").run.run({ vim.api.nvim_buf_get_name(0) })
        end,
        desc = "[T]est [F]ile",
      },
    },
    config = function()
      require("neotest").setup({
        adapters = {
          require("neotest-python")({}),
        },
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

To see the errors, do

  1. nvim -V1 -u repro.lua test.py (test.py is just some python file with a test)
  2. <leader>tf to run tests
  3. :tabnew
Error detected while processing BufAdd Autocommands for "*":
Error executing lua callback: Vim:E495: No autocommand file name to substitute for "<afile>"
stack traceback:
        [C]: in function 'expand'
        ...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:394: in function <...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:393>
Error executing lua callback: Vim:E495: No autocommand file name to substitute for "<afile>"
stack traceback:
        [C]: in function 'expand'
        ...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:450: in function <...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:449>
Error detected while processing BufEnter Autocommands for "*":
Error executing lua callback: Vim:E495: No autocommand file name to substitute for "<afile>"
stack traceback:
        [C]: in function 'expand'
        ...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:464: in function <...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:463>
Error executing lua callback: Vim:E495: No autocommand file name to substitute for "<afile>"
stack traceback:
        [C]: in function 'expand'
        ...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:476: in function <...e/tmp/.repro/plugins/neotest/lua/neotest/client/init.lua:475>

The workaround I'm applying here is to use the data in the autocmd callback to get the afile instead of calling expand()

@stevearc
Copy link
Contributor Author

stevearc commented Jun 4, 2024

Could not get the test failure to repro locally, is it possible that it's a flake?

@rcarriga
Copy link
Collaborator

rcarriga commented Jun 9, 2024

Thanks for the fix! Yeah it looks like a flaky test to me, so LGTM

@rcarriga rcarriga merged commit 2149a76 into nvim-neotest:master Jun 9, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants