Skip to content

Bug: dynamicNode crashes with "bad argument #2 to 'rep'" in sql snippet #41

@tilin94

Description

@tilin94

Introduction

Hi!

A bug report along with a proposed fix. I'm happy to discuss alternative approaches or make any adjustments based on your feedback. Please let me know if you need any additional information or clarification.
Then after reading the code I also found that it was documented with a FIXME comment.

It would be nice contributing to the codebase. Looking forward for feedback! 🙂

Description

When expanding the sql quasiquote snippet, neovim throws an error:

Error while evaluating dynamicNode@3 for snippet 'sql':
...lazy/haskell-snippets.nvim/lua/haskell-snippets/util.lua:50: bad argument #2 to 'rep' (number expected, got nil)

Root Cause

In lua/haskell-snippets/util.lua, the _indent_newline function uses pcall to safely call parent:get_buf_position(), but doesn't properly handle the failure case:

local _, pos = pcall(function()
  return parent:get_buf_position()
end)
local indent_count = pos[2]  -- crashes if pos is nil

When pcall fails or returns nil, accessing pos[2] yields nil, and string.rep(' ', nil) throws the error.

Affected Snippets

Any snippet using util.indent_newline_text() or util.indent_newline_insert()

Proposed Fix

Check pcall success and pos validity before accessing pos[2], defaulting to 0 indent:

local ok, pos = pcall(function()
return parent:get_buf_position()
end)
local indent_count = (ok and pos and pos[2]) or 0

Environment

  • Neovim: v0.11.2
  • LuaSnip: v2.4.1
  • OS: Linux 6.12.47-1.qubes.fc37.x86_64

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