Skip to content

bug: nested injections produce Index out of bounds errors #817

@KNnut

Description

@KNnut

Neovim version

NVIM v0.11.5

Operating system

Linux

Log file

[DEBUG] Running formatters on /tmp/repro.md: { "injected" }
[INFO] Run injected on /tmp/repro.md
[TRACE] Input lines: { "```js", "sql(`", "selecT 1", "`)", "```" }
[TRACE] Injected formatter regions { { "javascript", 2, 0, 5, 0 } }
[DEBUG] Injected format javascript:2:5: { "injected" }
[TRACE] Injected format lines { "sql(`", "selecT 1", "`)", "" }
[INFO] Run injected on /tmp/repro.md.1.js
[TRACE] Input lines: { "sql(`", "selecT 1", "`)" }
[ERROR] Formatter 'injected' error: ...im/lazy/conform.nvim/lua/conform/formatters/injected.lua:116: Index out of bounds
[TRACE] Injected javascript:2:5 formatted lines { "sql(`", "selecT 1", "`)" }
[TRACE] Applying formatting to /tmp/repro.md
[TRACE] Comparing lines { "```js", "sql(`", "selecT 1", "`)", "```" } and { "```js", "sql(`", "selecT 1", "`)", "```" }
[TRACE] Diff indices {}
[TRACE] Applying text edits: {}
[TRACE] Done formatting /tmp/repro.md
[ERROR] error formatting log line: '{
  code = 6,
  message = "Formatter 'injected' error: ...im/lazy/conform.nvim/lua/conform/formatters/injected.lua:116: Index out of bounds"
}' args {
  n = 0
}

Describe the bug

Using the injected formatter for both markdown and another language (nested injection) produces Index out of bounds errors.

What is the severity of this bug?

breaking (some functionality is broken)

Steps to reproduce

cat <<\EOF >repro.md
```js
sql(`
selecT 1
`)
```
EOF

cat <<\EOF >repro.lua
vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    {
      "nvim-treesitter/nvim-treesitter",
      lazy = false,
      branch = "main",
      build = ":TSUpdate",
      init = function()
        vim.api.nvim_create_autocmd("FileType", {
          pattern = { "javascript", "markdown", "sql" },
          callback = function()
            vim.treesitter.start()
          end,
        })
      end,
      config = function()
        require("nvim-treesitter").install({ "javascript", "markdown", "sql" }):wait()
      end,
    },
    {
      "stevearc/conform.nvim",
      opts = {
        log_level = vim.log.levels.TRACE,
        formatters_by_ft = {
          javascript = { "injected" },
          markdown = { "injected" },
          sql = { "sqruff" },
        },
      },
    },
  },
  rocks = {
    enabled = false,
    hererocks = false,
  },
})
EOF

mkdir -p .repro/data/nvim/site
nvim -u repro.lua -c "lua require('conform').format()" +ConformInfo repro.md

Additional context

It only happens when the SQL code is on more than one line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions