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

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

Merged
merged 8 commits into from
Jan 5, 2023
Merged

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

merged 8 commits into from
Jan 5, 2023

Conversation

justinmk
Copy link
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

  • tests for shada/swap/userconfig
  • exit 1 on lua error
  • exit without +q
  • update docs-gen CI job
  • support -l - (stdin)
  • need some sort of vim.is_main() magic function that detects whether the current module is the thing executed by nvim -l.
  • adjust places that test verbose==1
  • ensure final \n in output?
  • fix: os_msg, os_errmsg should not truncate based on screen width!

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
Member Author

justinmk commented Jan 1, 2023

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

@zeertzjq
Copy link
Member

zeertzjq commented Jan 3, 2023

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

@justinmk
Copy link
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
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.

src/nvim/message.c Outdated Show resolved Hide resolved
@@ -281,6 +283,8 @@ jobs:
DEPS_BUILD_DIR: ${{ github.workspace }}/nvim-deps
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
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
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
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...

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
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