Closed rayanamal closed 4 months ago
My guess is this could be because you have ?
in some of your $env.LUA_PATH/CPATH and lua is expecting something to expand those illegal paths into "real" paths. Just a guess though.
No, this is not the case. I updated the issue with detailed information. You can see package.path
variable in both LUA_REPLs include ? characters.
I think you need to set an environment conversion to convert the environment variables from nushell values back into strings. https://www.nushell.sh/book/environment.html#environment-variable-conversions
Thank you, this was the culprit. The updated code to make it work:
env.nu
:
$env.ENV_CONVERSIONS = {
...
"LUA_PATH": {
from_string: { |s| $s | split row ';' | path expand --no-symlink }
to_string: { |v| $v | path expand --no-symlink | str join ';' }
}
"LUA_CPATH": {
from_string: { |s| $s | split row ';' | path expand --no-symlink }
to_string: { |v| $v | path expand --no-symlink | str join ';' }
}
}
config.nu
:
def --env load_luarocks_path_vars [] {
def make_record [] { reduce -f {} {|it, acc| $acc| merge $it }}
# 'luarocks path' command gives 3 bash export commands
luarocks path | split row "\n"
| each {|cmd|
let env_var = $cmd | parse "export {name}='{value}'"
let var_name = $env_var.name.0
# Assumption: multiple consecutive colon/semicolons in path string doesn't mean anything.
let paths = $env_var.value.0
| split row -r ';+|:+' | each {{$in: true}} | make_record
let old_value = $env
| get -i $var_name
| if $in == null {{}} else { split row -r ';+|:+' | each {{$in: true}} | make_record }
let new_value = $old_value | merge $paths | columns
{$var_name: $new_value}
}
| make_record
| load-env
}
load_luarocks_path_vars
Is it possible to put this solution somewhere more permanent than Github issues? Idk whether there is a wiki or something. I just buried several hours on this issue (and learned the nu lang along the way ;) )
Describe the bug
Lua doesn't read environment variables
LUA_PATH
andLUA_CPATH
correctly from nushell while it reads them when run under bash, in the exact same environment. These variables are needed for lua to find packages installed via luarocks lua package manager.How to reproduce
luarocks install lpeg
. Non-sudo installation to user's home directory.LUA_PATH
,LUA_CPATH
andPATH
with the output ofluarocks path
. Here's a script to do that:~> bash 05/13/2024 06:30:35 PM username@textadept:~$ eval $(luarocks path) username@textadept:~$ echo $LUA_PATH /usr/local/share/lua/5.4/?.lua;/usr/local/share/lua/5.4/?/init.lua;/usr/local/lib/lua/5.4/?.lua;/usr/local/lib/lua/5.4/?/init.lua;/usr/share/lua/5.4/?.lua;/usr/share/lua/5.4/?/init.lua;./?.lua;./?/init.lua;/home/username/.luarocks/share/lua/5.4/?.lua;/home/username/.luarocks/share/lua/5.4/?/init.lua username@textadept:~$ echo $LUA_CPATH /usr/local/lib/lua/5.4/?.so;/usr/lib/x86_64-linux-gnu/lua/5.4/?.so;/usr/lib/lua/5.4/?.so;/usr/local/lib/lua/5.4/loadall.so;./?.so;/home/username/.luarocks/lib/lua/5.4/?.so username@textadept:~$ lua Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
Notice that package.path variable in Lua REPL when invoked from nushell vs when invoked from bash is not the same, even though environment variables for both shells are the same. When invoked from nushell, lua loads default variables and doesn't see the environment variable for some reason.
Expected behavior
The package.path output of Lua REPL shouldn't change when invoked from nushell, and lpeg module should be able to load without error as a result.
Configuration
Additional context