The Wayback Machine - https://web.archive.org/web/20200831080800/https://github.com/neovim/neovim/issues/12623
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

bad argument #1 to 'str_utfindex' (string expected, got nil) #12623

Open
DrGo opened this issue Jul 11, 2020 · 9 comments
Open

bad argument #1 to 'str_utfindex' (string expected, got nil) #12623

DrGo opened this issue Jul 11, 2020 · 9 comments

Comments

@DrGo
Copy link

@DrGo DrGo commented Jul 11, 2020

  • nvim --version:
    NVIM v0.5.0-nightly
    Build type: Release
    LuaJIT 2.0.5
    Compilation: /usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNDEBUG -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/tmp/neovim-20200711-88642-ii3lmc/build/config -I/tmp/neovim-20200711-88642-ii3lmc/src -I/usr/local/include -I/tmp/neovim-20200711-88642-ii3lmc/deps-build/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -I/usr/local/opt/gettext/include -I/tmp/neovim-20200711-88642-ii3lmc/build/src/nvim/auto -I/tmp/neovim-20200711-88642-ii3lmc/build/include
    Compiled by <>

Features: +acl +iconv +tui

  • Operating system/version:
    Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64

  • Terminal name/version:
    alacritty 0.4.3 (c723247)

  • $TERM:
    $TERM

Actual behaviour

This code

:lua << EOF
-- Synchronously organise (Go) imports.
function go_organize_imports_sync(timeout_ms)
  local context = { source = { organizeImports = true } }
  vim.validate { context = { context, 't', true } }
  local params = vim.lsp.util.make_range_params()
  params.context = context

  local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, timeout_ms)
  if not result then return end
  result = result[1].result
  if not result then return end
  edit = result[1].edit
  vim.lsp.util.apply_workspace_edit(edit)
end
go_organize_imports_sync(1000)
EOF

produce this error

E5108: Error executing lua ...vim/HEAD-a695da7/share/nvim/runtime/lua/vim/lsp /util.lua:1297: bad argument #1 to 'str_utfindex' (string expected, got nil)

Expected behaviour

no internal error.

@DrGo DrGo added the bug label Jul 11, 2020
@jamessan jamessan added the lua label Jul 11, 2020
@tjdevries
Copy link
Contributor

@tjdevries tjdevries commented Jul 13, 2020

I'm guessing this would be as simple as checking for nil here:

local function make_position_param()
  local row, col = unpack(api.nvim_win_get_cursor(0))
  row = row - 1
  local line = api.nvim_buf_get_lines(0, row, row+1, true)[1]
  -- vvvvvvvv
  col = str_utfindex(line, col)
  return { line = row; character = col; }
end

But I don't have time at the moment to test it.

@shoriminimoe
Copy link

@shoriminimoe shoriminimoe commented Aug 8, 2020

@tjdevries I am interested in working this issue. Do you know if anyone else has started?

@tjdevries
Copy link
Contributor

@tjdevries tjdevries commented Aug 8, 2020

I don't think anyone has started. Feel free to make a PR and tag me in it! Thanks!

@gysddn
Copy link

@gysddn gysddn commented Aug 11, 2020

I get a different error, can you perhaps post the go file you tried this script on, or a chunk of it.

@DrGo
Copy link
Author

@DrGo DrGo commented Aug 11, 2020

sorry cannot recall what go file was associated with this error.

@gysddn
Copy link

@gysddn gysddn commented Aug 17, 2020

Have you encountered this error again anytime, or are you able to reproduce it now?
Kinda running out of things to try really. I wasn't able to reproduce it. All I can think about is maybe "nvim_win_get_cursor(0)" returns something odd for a row value, which result line being nil.

@DrGo
Copy link
Author

@DrGo DrGo commented Aug 17, 2020

I get the exact error when I put the code above in my init.vim
my current config

Build type: Release
LuaJIT 2.1.0-beta3
Compilation: /Applications/Xcode-10.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -DNDEBUG -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/Users/travis/build/neovim/bot-ci/build/neovim/build/config -I/Users/travis/build/neovim/bot-ci/build/neovim/src -I/Users/travis/build/neovim/bot-ci/build/neovim/.deps/usr/include -I/usr/include -I/usr/local/opt/gettext/include -I/Users/travis/build/neovim/bot-ci/build/neovim/build/src/nvim/auto -I/Users/travis/build/neovim/bot-ci/build/neovim/build/include
Compiled by travis@Traviss-Mac-6.local

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

I think the nil check as suggested by @tjdevries is a good idea anyway.

@gysddn
Copy link

@gysddn gysddn commented Aug 18, 2020

I think I know what's going on, you said you put the exact code in your init.vim, and that includes this line

go_organize_imports_sync(1000)

so you're calling the function. But the problem is, as far as I know init script is run before any buffer is loaded (or present, I'm not sure) and there is no line at that moment. So it's really reasonable for this function to return nil.

  local line = api.nvim_buf_get_lines(0, row, row+1, true)[1]

As for a solution though, I'm not sure what to do, I mean it's reasonable but should we give a more informative error message or just, as @tjdevries mentioned, check for nil. Either way there is a logic error in script.

@DrGo
Copy link
Author

@DrGo DrGo commented Aug 18, 2020

I am calling the go_organize_imports_sync as a test.
I thinking checking for nil is the way to go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.