Skip to content

feat(lua)!: execute Lua with "nvim -l"#18706

Merged
justinmk merged 8 commits intoneovim:masterfrom
justinmk:lua
Jan 5, 2023
Merged

feat(lua)!: execute Lua with "nvim -l"#18706
justinmk merged 8 commits intoneovim:masterfrom
justinmk:lua

Conversation

@justinmk
Copy link
Copy Markdown
Member

@justinmk justinmk commented May 22, 2022

Examples

nvim  bar.txt -l foo.lua
echo 'print(vim.inspect(vim.api.nvim_buf_get_text(1,0,0,-1,-1,{})))' | nvim +"put ='text'" -l -
echo "print(vim.inspect(_G.arg))" | nvim -l - --arg1 --arg2

closes #15749

Todo

Future

To run tests with nvim instead of lua or luajit, change .deps/usr/bin/busted script as follows:

#!/bin/sh

LUAROCKS_SYSCONFDIR='/…/.deps/usr/etc/luarocks' exec '/…/build/bin/nvim' -c 'lua package.path="/…/.deps/usr/share/lua/5.1/?.lua;/…/.deps/usr/share/lua/5.1/?/init.lua;"..package.path;package.cpath="/…/.deps/usr/lib/lua/5.1/?.so;"..package.cpath;local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("busted","2.0.0-1")' -l '/…/.deps/usr/lib/luarocks/rocks-5.1/busted/2.0.0-1/bin/busted' "$@"

With this PR, plus this patch:

diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua
index ca59eb31828b..a01d62af98e9 100644
--- a/test/functional/helpers.lua
+++ b/test/functional/helpers.lua
@@ -957,7 +957,7 @@ function module.mkdir_p(path)
     or 'mkdir -p '..path))
 end
 
-module = global_helpers.tbl_extend('error', module, global_helpers)
+module = global_helpers.tbl_extend('keep', module, global_helpers)
 
 return function(after_each)
   if after_each then
diff --git a/test/helpers.lua b/test/helpers.lua
index 3fe4322501a3..320b826b6da5 100644
--- a/test/helpers.lua
+++ b/test/helpers.lua
@@ -811,6 +811,6 @@ function module.read_nvim_log(logfile, ci_rename)
   return log
 end
 
-module = shared.tbl_extend('error', module, Paths, shared, require('test.deprecated'))
+module = shared.tbl_extend('keep', module, Paths, shared, require('test.deprecated'))
 
 return module

tests run successfully using nvim as the test runner:

|| -------- Running tests from test/functional/core/startup_spec.lua
|| RUN      T1 startup -l Lua failure modes: 23.30 ms OK
|| RUN      T2 startup -l Lua os.exit() sets Nvim exitcode: 39.30 ms OK
|| RUN      T3 startup -l Lua sets _G.arg: 189.28 ms OK
|| -------- 3 tests from test/functional/core/startup_spec.lua (262.84 ms total)
|| 
|| -------- Global test environment teardown.
|| ======== 3 tests from 1 test file ran. (263.25 ms total)
|| PASSED   3 tests.

and fail correctly:

|| ERROR    test/functional/core/startup_spec.lua @ 99: startup -l Lua failure modes
|| test/helpers.lua:85: Pattern does not match.
|| Pattern:
|| nvim: XArgument missing after: "%-l"
|| Actual:
|| nvim: Argument missing after: "-l"
|| More info with "nvim -h"

@justinmk
Copy link
Copy Markdown
Member Author

justinmk commented Jan 1, 2023

test failing on windows... is there a problem with getting output from system(['nvim', '-es', ...]) there?

@justinmk justinmk force-pushed the lua branch 3 times, most recently from 69787e2 to 2f102ad Compare January 3, 2023 01:55
@justinmk justinmk marked this pull request as ready for review January 3, 2023 09:45
@zeertzjq
Copy link
Copy Markdown
Member

zeertzjq commented Jan 3, 2023

Maybe some Vim patches are missing for os_msg() and os_errmsg() on Windows?

@justinmk justinmk force-pushed the lua branch 9 times, most recently from 61753fc to 903fe3f Compare January 4, 2023 10:58
@justinmk justinmk force-pushed the lua branch 5 times, most recently from 9d4be15 to 6cf0892 Compare January 4, 2023 13:33
@justinmk
Copy link
Copy Markdown
Member Author

justinmk commented Jan 4, 2023

Maybe some Vim patches are missing for os_msg() and os_errmsg() on Windows?

thank you for the help! actually Nvim was crashing (bug in nlua_set_argv) in the nested system() call, which returned empty output. probably can make this better by checking v:shell_error in assert_l_out before checking the output.

@justinmk justinmk changed the title feat(lua)!: repurpose "-l" to execute Lua feat(lua)!: execute Lua with "nvim -l" Jan 4, 2023
@justinmk
Copy link
Copy Markdown
Member Author

justinmk commented Jan 4, 2023

after merging this,

  • regen-api-docs job will fail until nightly has this feature
  • we can use nvim as the test-runner instead of lua
    • eliminates most of the test util functions in helpers.lua.
    • see the "Future" note above.

Comment thread src/nvim/message.c Outdated
@justinmk justinmk force-pushed the lua branch 3 times, most recently from 216a3da to 64f0764 Compare January 4, 2023 16:13
Comment thread .github/workflows/ci.yml
CACHE_NVIM_DEPS_DIR: ${{ github.workspace }}/nvim-deps
DEPS_PREFIX: ${{ github.workspace }}/nvim-deps/usr
# TEST_FILE: test/functional/core/startup_spec.lua
# TEST_FILTER: foo
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this here and documented in CONTRIBUTING.md to help others.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put one set of environment variables at the top level instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't seem to work when I tried it...

@justinmk justinmk force-pushed the lua branch 2 times, most recently from ca337de to 8469218 Compare January 4, 2023 17:23
Problem:
Nvim has Lua but the "nvim" CLI can't easily be used to execute Lua
scripts, especially scripts that take arguments or produce output.

Solution:
- support "nvim -l [args...]" for running scripts. closes neovim#15749
- exit without +q
- remove lua2dox_filter
- remove Doxyfile. This wasn't used anyway, because the doxygen config
  is inlined in gen_vimdoc.py (`Doxyfile` variable).
- use "nvim -l" in docs-gen CI job

Examples:

    $ nvim -l scripts/lua2dox.lua --help
    Lua2DoX (0.2 20130128)
    ...

    $ echo "print(vim.inspect(_G.arg))" | nvim -l - --arg1 --arg2
    $ echo 'print(vim.inspect(vim.api.nvim_buf_get_text(1,0,0,-1,-1,{})))' | nvim +"put ='text'" -l -

TODO?
  -e executes Lua code
  -l loads a module
  -i enters REPL _after running the other arguments_.
Problem:
When "-l" is followed by "--", we stop sending args to the Lua script
and treat "--" in the usual way. This was for flexibility but didn't
have a strong use-case, and has these problems:
- prevents Lua "-l" scripts from handling "--" in their own way.
- complicates the startup logic (must call nlua_init before command_line_scan)

Solution:
Don't treat "--" specially if it follows "-l".
@justinmk
Copy link
Copy Markdown
Member Author

justinmk commented Jan 5, 2023

ci failure looks unrelated:

FAILED   test/functional/terminal/tui_spec.lua @ 2372: TUI as a client throws error when no server exists
test/functional/terminal/tui_spec.lua:2380: Row 2 did not match.
Expected:
  |Remote ui failed to start: {MATCH:.*}|
  |*                                                            |
  |*[Process exited 1]{1: }                                         |
  |*                                                            |
  |                                                            |
  |                                                            |
  |{3:-- TERMINAL --}                                              |
Actual:
  |Remote ui failed to start: failed to lookup host o          |
  |*r port                                                      |
  |*                                                            |
  |*[Process exited 1]{1: }                                         |
  |                                                            |
  |                                                            |
  |{3:-- TERMINAL --}                                              |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startup: "-l" enables Lua mode

4 participants