Skip to content

lua: fix longstanding bug in lua envHook causing relative module imports to stop working#289135

Merged
teto merged 1 commit intoNixOS:stagingfrom
lolbinarycat:luajit-relative-modules
Feb 21, 2024
Merged

lua: fix longstanding bug in lua envHook causing relative module imports to stop working#289135
teto merged 1 commit intoNixOS:stagingfrom
lolbinarycat:luajit-relative-modules

Conversation

@lolbinarycat
Copy link
Contributor

Description of changes

originally thought to only affect luajit, this bug can also affect lua5, such as when running via
nix-shell -p lua luaPackages.toml.

this bug was caused by the lua envHook overriding the default package path whenever it found a module.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: lua Lua is a powerful, efficient, lightweight, embeddable scripting language. label Feb 15, 2024
@lolbinarycat
Copy link
Contributor Author

Fixes #113110

@lolbinarycat
Copy link
Contributor Author

see extended commit message for more info

@lolbinarycat lolbinarycat changed the title fix longstanding bug in lua envHook causing relative module imports to stop working lua: fix longstanding bug in lua envHook causing relative module imports to stop working Feb 15, 2024
@lolbinarycat
Copy link
Contributor Author

Superceeds #273342

@lolbinarycat lolbinarycat force-pushed the luajit-relative-modules branch from 8f563ca to 9260e79 Compare February 15, 2024 21:45
@ofborg ofborg bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 2501-5000 This PR causes many rebuilds on Darwin and should target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Feb 15, 2024
@teto
Copy link
Member

teto commented Feb 15, 2024

Could you add a test of what you want in pkgs/development/interpreters/lua-5/tests please ?

#!/usr/bin/env nix-shell
#! nix-shell -i sh -p lua luaPackages.toml
lua -e 'print(package.path)'
echo "lua path: $LUA_PATH"
source ./assert.sh
# here adds some tests (lua path contains XX) and exit 1 when fails

output:

./share/lua/5.2/?.lua;./?.lua;./?/init.lua

NB: this will eventually need to target staging considering the number of rebuilds

@lolbinarycat
Copy link
Contributor Author

@teto

Could you add a test of what you want in pkgs/development/interpreters/lua-5/tests please ?

#!/usr/bin/env nix-shell
#! nix-shell -i sh -p lua luaPackages.toml
lua -e 'print(package.path)'
echo "lua path: $LUA_PATH"
source ./assert.sh
# here adds some tests (lua path contains XX) and exit 1 when fails

the existing test for luajit basically already does this (it was broken before, and now it works), if i was to add more tests it would be to test the actual inclusion of modules instead of just checking the path.

i don't mind writing some more unit tests for lua, as it could clearly use them, but i would want to do that as a separate PR as the scope would be larger than just this 1 thing that is already tested for.

NB: this will eventually need to target staging considering the number of rebuilds

do i need to do something about that? i'm fairly new to nixpkgs.

@lolbinarycat
Copy link
Contributor Author

also, my intent with this PR was to keep it as minimal as possible so that it can easily be reviewed. this is 4 year old bug that affects most multi-file lua projects, so my main priority was to just get it fixed.

@teto
Copy link
Member

teto commented Feb 17, 2024

it fixes nix-build -A luajit.tests so that's cool but
nix-shell -p lua luaPackages.argparse still ignores local files

$ nix-shell -p lua luaPackages.argparse
$ echo $LUA_PATH
/nix/store/p8rnf25wx45kggkm7c2dcxk6svsn0hbh-lua5.2-argparse-0.7.1-1/share/lua/5.2/?.lua;/nix/store/p8rnf25wx45kggkm7c2dcxk6svsn0hbh-lua5.2-argparse-0.7.1-1/share/lua/5.2/?/init.lua

@lolbinarycat
Copy link
Contributor Author

it fixes nix-build -A luajit.tests so that's cool but nix-shell -p lua luaPackages.argparse still ignores local files

$ nix-shell -p lua luaPackages.argparse
$ echo $LUA_PATH
/nix/store/p8rnf25wx45kggkm7c2dcxk6svsn0hbh-lua5.2-argparse-0.7.1-1/share/lua/5.2/?.lua;/nix/store/p8rnf25wx45kggkm7c2dcxk6svsn0hbh-lua5.2-argparse-0.7.1-1/share/lua/5.2/?/init.lua

@teto nix-shell -p always uses the system channel.

run NIX_PATH=nixpkgs=$PWD/default.nix nix-shell -p luaPackages.argparse lua to use the local branch of nixpkgs.

@teto
Copy link
Member

teto commented Feb 18, 2024

Indeed sry ! when the number of rebuilds > 2000, we usually target the "staging" branch, which becomes available in nixos-unstable 1/2 weeks later. Can you target staging (convert the PR first to draft avoids pinging dozens of people when making a mistake so I recommand that) https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#rebasing-between-branches-ie-from-master-to-staging and then I will merge.

@lolbinarycat lolbinarycat marked this pull request as draft February 21, 2024 17:17
previously, when the lua setup hook found a system lua module,
it would simply add that library to LUA_PATH, meaning the default
path would no longer be used.

for luajit, this bug would always occur, due to it having
several inbuilt libraries such as luabitop.

lua5 still passed unit tests, simply because the test
environment doesn't include any system lua libaries,
but the bug would still occur if lua5 was used in a derivation with
a buildInput from luaPackages, since that package would be found by
the envHook and overwrite the default path.

now, the setup hook will use any system module paths in addition to
the default path, instead of overriding it.
@lolbinarycat lolbinarycat force-pushed the luajit-relative-modules branch from 9260e79 to ca6452f Compare February 21, 2024 17:40
@github-actions github-actions bot added 6.topic: python Python is a high-level, general-purpose programming language. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: qt/kde Object-oriented framework for GUI creation 8.has: documentation This PR adds or changes documentation 8.has: changelog This PR adds or changes release notes 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: emacs Text editor 6.topic: golang Go is a high-level general purpose programming language that is statically typed and compiled. 6.topic: ruby A dynamic, open source programming language with a focus on simplicity and productivity. 6.topic: vim Advanced text editor labels Feb 21, 2024
@github-actions github-actions bot removed 6.topic: qt/kde Object-oriented framework for GUI creation 8.has: documentation This PR adds or changes documentation 8.has: changelog This PR adds or changes release notes 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: emacs Text editor 6.topic: golang Go is a high-level general purpose programming language that is statically typed and compiled. 6.topic: ruby A dynamic, open source programming language with a focus on simplicity and productivity. 6.topic: vim Advanced text editor 6.topic: stdenv Standard environment labels Feb 21, 2024
@lolbinarycat
Copy link
Contributor Author

@teto rebased!

@ofborg ofborg bot added 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. 8.has: clean-up This PR removes packages or removes other cruft 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. and removed 10.rebuild-darwin: 2501-5000 This PR causes many rebuilds on Darwin and should target the staging branches. labels Feb 21, 2024
@teto teto marked this pull request as ready for review February 21, 2024 22:42
@teto teto merged commit 90aaea8 into NixOS:staging Feb 21, 2024
@teto
Copy link
Member

teto commented Feb 21, 2024

ty

@infinisil
Copy link
Member

Fyi, I believe the pkgs/by-name check failed due to the base branch change, something relating to #282707 or so (not 100% sure). Shouldn't occur again in the future, but please ping me if you see the check failing. It generally shouldn't be ignored, I'm continuously improving it.

@vcunat
Copy link
Member

vcunat commented Mar 5, 2024

I now verified that this particular merge broke some lua* stuff (just reverting it fixes the builds), but I haven't investigated deeper. Examples:

/cc the corresponding staging-next PR #292500

@teto
Copy link
Member

teto commented Mar 6, 2024

@vcunat these errors look related to an invalid luarocks config. I've submitted some changes in that area so I'll have a look.

EDIT still it's weird, I usually run in my fork whatever I submitted to staging and have no issue..

@vcunat
Copy link
Member

vcunat commented Mar 7, 2024

@teto: perhaps we should revert this PR for now, so that there's not so much time pressure on this? The staging-next PR is basically waiting for this now, delaying other changes including security fixes.

@teto
Copy link
Member

teto commented Mar 7, 2024

I had a very quick look and yeah I could not find a link with my luarocks-related changes (might still be the case though, but I use these in my fork and it works well). If you are sure this is the responsible PR, let's revert it and put it back after more rigourous testing later.

lolbinarycat has prompted us to look into lua issues and we will do so in a more orderly manner #286822 . I have some CPU capacity to run nixpkgs-review on the PRs but we will have to slow down binary cat :)

anyway thanks for asking/letting us know about the status, maintaining staging-next is hard work and we'll try to be more careful on the next merge.

vcunat added a commit that referenced this pull request Mar 7, 2024
…stem modules

This reverts commit ca6452f.
Regressions appeared and they need time to get resolved; see:
#289135 (comment)
@lolbinarycat
Copy link
Contributor Author

nixpkgs-review probably would have caught this, unfortunately it's too much for my laptop, as is trying to test things on staging, as that requires rebuilding all of stdenv, and that deadlocks everything due to running out of memory

@vcunat
Copy link
Member

vcunat commented Mar 9, 2024

That sounds done wrong (well, maybe the tool could be blamed). Rebuilding together with staging changes. Either way, typical laptops aren't suitable for such things; it's good to have a remote more powerful machine at hand.

@lolbinarycat
Copy link
Contributor Author

@vcuncat yeah, that would be nice, but unless someone feels like giving me access to their personal build server, i think i'm fairly stuck with what i've got.

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

Labels

6.topic: lua Lua is a powerful, efficient, lightweight, embeddable scripting language. 8.has: clean-up This PR removes packages or removes other cruft 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants