Lua require does not work but file is in the trace - lua

I'm trying to require files in Lua, in one case it is working, but when I want to simplify the requirements in updating the LUA PATH the file is not found, but it is in the trace!
To reproduce my require problem I did the test with the package.searchpath function, which takes the required key and the Lua path in arguments.
So the code :
print('MY LUA PATH')
local myLuaPath = "?;?.lua;D:\\Projets\\wow-addon\\HeyThere\\?;D:\\Projets\\wow-addon\\HeyThere\\src\\HeyThere\\?;D:\\Projets\\wow-addon\\HeyThere\\test\\HeyThere\\?"
print(myLuaPath)
print('package search core.add-test')
print(package.searchpath('core.add-test', myLuaPath))
print('package search test.HeyThere.core.add-test')
print(package.searchpath('test.HeyThere.core.add-test', myLuaPath))
The result :
MY LUA PATH
?;?.lua;D:\Projets\wow-addon\HeyThere\?;D:\Projets\wow-addon\HeyThere\src\HeyThere\?;D:\Projets\wow-addon\HeyThere\test\HeyThere\?
package search core.add-test
nil no file 'core\add-test'
no file 'core\add-test.lua'
no file 'D:\Projets\wow-addon\HeyThere\core\add-test'
no file 'D:\Projets\wow-addon\HeyThere\src\HeyThere\core\add-test'
no file 'D:\Projets\wow-addon\HeyThere\test\HeyThere\core\add-test'
package search test.HeyThere.core.add-test
test\HeyThere\core\add-test.lua
So the first try with 'core.add-test' should work with the 'D:\Projets\wow-addon\HeyThere\test\HeyThere\?' value in the path but fails...
In the trace, there is the file I want!
no file 'D:\Projets\wow-addon\HeyThere\test\HeyThere\core\add-test'
But with the same LUA PATH but starting in a parent folder the file is found... Second test with 'test.HeyThere.core.add-test' found from the 'D:\Projets\wow-addon\HeyThere\?'
-> test\HeyThere\core\add-test.lua
Can someone explains to me why it doesn't work the first time?
EDIT :
My current directory is D:\Projets\wow-addon\HeyThere
My lua.exe is in D:\Projets\wow-addon\HeyThere\bin\lua but is added to my PATH variable (I'm on Windows)
I set the LUA_PATH environment variable and execute
lua "test\test-suite.lua" -v
The code inside test-suite.lua is the test code described above

As #EgorSkriptunoff suggested, adding file extansion in the path resolve the problem...
Ex:
Wrong path D:\Projets\wow-addon\HeyThere\?
Good path D:\Projets\wow-addon\HeyThere\?.lua
The extension should be in the path variable because in the require the dot is replace and used as a folder separator.

Related

Can you require a file directly from the /after/plugin folder?

I decided to spring-clean and update my nvim config files/plugins, and thought I’d make proper use of the after/plug folder.
While setting up LSP (with mason, mason-lspconfig, and lspconfig), I wanted to move all the lsp language server settings out from after/plugin/lsp/init.lua to their own files (now in after/plugin/lsp/settings).
The problem is I don’t seem to be able to require them into the init.lua file.
Things I’ve tried to no avail:
require(‘after/plugin/lsp/settings/sumneko_lua.lua’)
require(vim.fn.stdpath("config") .. "/after/plugin/lsp/settings/sumneko_lua”)
require(vim.fn.expand('%:h').. ‘/settings/sumneko_lua’)
The attempt using expand works when I resource the file in nvim; but causes an error when starting nvim.
I understand that all the files in after/plugin are automagically sourced at startup. So if I had a file shared.lua:
local M = {}
function M.greet()
vim.notify("Hello!”)
end
return M
in the same folder as after/plugin/lsp/init.lua, how can I get access to the greet() function from init.lua?
Any pointers would be greatly appreciated.
It turned out to be quite a simple solution in the end: I simply updated the paths to be searched in init.lua
-- nvim/init.lua
-- Allow require to look in after/plugin folder
local home_dir = os.getenv("HOME”)
package.path = home_dir .. "/.config/nvim/after/plugin/?.lua;" .. package.path
And then I can require any file inside the after/plugin folder
e.g - require(‘lsp/settings/sumneko’) or require(‘lsp/shared’).greet()

Lua Nested Require Path

I'm writing a tool to parse lua plugins created by other users. The only guarentee about the plugin is that it has a data.lua file in a known directory. Inside there users are free to do anything they wish. This particular plugin using require to load a file and that file loads another file. Both are relative paths but the second is relative to the location of the first file.
data.lua
foo/bar.lua
foo/baz.lua
data.lua:
require("foo.bar")
foo/bar.lua:
require("baz")
When I try to execute data.lua I get an error when foo/bar.lua tries to require "baz". None of the paths it tries are ./foo/.
Any idea how I can fix this? I could find any documentation specifically about this case, it seemed like I need to hard code /foo/ into the path but I don't know it ahead of time. This seems like something that should be automatic is there a setting I'm missing or am I running the wrong version of lua? I'm using NLua 4.0
Thanks
I tested this script using node-lua and it fixes the issue for me!
https://gist.github.com/hoelzro/1299679
Relavent code:
local oldrequire = require
function require(modname)
local regular_loader = package.loaders[2]
local loader = function(inner)
if string.match(modname, '(.*)%.') then
return regular_loader(string.match(modname, '(.*)%.') .. '.' .. inner)
end
end
table.insert(package.loaders, 1, loader)
local retval = oldrequire(modname)
table.remove(package.loaders, 1)
return retval
end
To get this to work with Lua 5.2 change all uses of package.loaders to package.searchers.
Also if you want to override the global require function you need this snippet as well:
_G.require = require
You can alter the search behavior of require by changing the package.path variable.
Resources on package.path:
https://www.lua.org/manual/5.3/manual.html#pdf-package.path
http://lua-users.org/wiki/PackagePath
Example adding foo folder to search locations:
package.path = package.path .. ';./foo/?.lua'
the ? character will be where the string passed to require is placed.
Alternatively you can add a default file to load, to the package.path:
package.path = package.path .. ';./nested_require.lua'
Then define the behavior you would like within this file. You can use the global variable _REQUIREDNAME to reference the value passed to the require function.
The documentation for this method can be found here at the bottom of the page: https://www.lua.org/pil/8.1.html

How to get the path where the library is installed

I am working in Linux and I have a library written in Fortran 90 (written by 3rd party), that is reading from a file in the current working directory. I would like to be able to call the resulting library from other folders, hence I need to read the path where the library is installed. How can I know the path to the compiled library within the Fortran code?
I need to store in a variable the path within the code.
For who knows python, I want to achieve the same as
import os
os.path.dirname(os.path.abspath(__file__))
but in f90 (see Get location of the .py source file)
Using the suggestions in the comment I have done the following:
export DATAPATH=`pwd`
make
in the Makefile
ifort -O3 -fpic -fpp -DDATAPATH -c mysource.f90
in mysource.f90
subroutine mysub
character(len=100)::dpath
#ifdef DATAPATH
dpath=DATAPATH
#endif
open(10,file=trim(dpath)//'initialise.dat')
....
....
the problem is that at compile time I get
mysource.f90(42): error #6054: A CHARACTER data type is required in this context. [1]
dpath=1
----------^
compilation aborted for mysource.f90 (code 1)
If you wish you can fix the path at compile time. Something like
gfortran -cpp mylib.f90 -DPREFIX=\"/usr/local/\"
open(newunit=u,file=PREFIX//'mylib/initialise.dat')
You must than make sure the library is indeed installed in that place PREFIX/mylib/
You can create an environment variable containing the path of your data. This variable can be set by hand, in your .bashrc or .bash_profile or in the system /etc/profile.d/ or /etc/bash.bashrc, there are manyways and they depend if the library is just for one user or for all users of some large computer.
For example
export MYLIB_PATH='/usr/local/mylib'
Then you can read the variable in Fortran as
CALL get_environment_variable("MYLIB_PATH", mylib_path, status=stat)
and the path is now in variable mylib_path. You can check the success by checking if stat==0.
This is not the only possible approach. You can also have a configuration file for your library in your home directory:
mkdir $HOME/.config/mylib/
echo "/usr/local/mylib" > $HOME/.config/mylib/path
and then you can try to read the path from this file if the environment variable was not set
if (stat/=0) then
CALL get_environment_variable("HOME", home_dir)
open(newunit=path_unit, file=home_dir//'/.config/mylib/path',status='old',action='read',iostat=stat)
if (stat/=0) complain
read(path_unit,'(a)',iostat=stat) mylib_path
if (stat/=0) complain
end if
So when you compiled with -DDATAPATH you have not passed the variable DATAPATH into your code only declared a symbol called DATAPATH as being true, so ifort will substitute DATAPATH as 1. What you need to do is pass it as a value:
-DDATAPATH="$(DATAPATH)"
For the compilation to work.

Lua 5.3 thinks socket.lua doesn't exist in a directory where it actually does exist

Here's a screenshot:
https://drive.google.com/open?id=0B4Dqo44FM648VTVKVzBKSERldGc
I would like to have fun contributing to the development of an open source game but this problem is preventing me from doing this.
My LuaRocks install is configured for Lua 5.3 and is set to use MinGW's build tools. My OS is Windows 10 64 bit.
My Lua 5.3.2 binaries were provided by this website (lua-5.3.2_Win64_bin.zip).
If you have an idea for a possible cause please post it.
You have to resolve environment variables manually:
os.getenv (varname)
Returns the value of the process environment variable varname, or nil
if the variable is not defined.
So, in your case you need to obtain the %APPDATA% path first:
os.getenv("APPDATA")
Returns: C:\Users\USERNAME\AppData\Roaming
When searching for packages to load, Lua uses a pre-defined list of folders to search in. This list is defined in package.path which is a semicolon-delimited (;) string. To add a new folder, you must append it's path to package.path:
package.path = package.path .. ";" .. NEWFOLDER .. "\\?.lua"
Finally, your complete solution is this:
package.path = package.path .. ";" .. os.getenv("APPDATA") .. "\\luarocks\\share\\lua\\5.3\\?.lua"
local s = require("socket")
"Lua does not expand environment variable references in package.path, so %APPDATA% won't work. You need the real path. The LuaRocks installation script should have said that: "Note that the %APPDATA% element in the paths above is user specific and it MUST be replaced by its actual value." - siffiejoe

Can't set OBJECTS_DIR from .pri file

I want to have one directory for all object files and create Common.pri file that set OBJECTS_DIR like that
OBJECTS_DIR = $$PWD/../
But when build project i can't find obj file in given directory.If I write this direct in .pro file I get the expected result.I successfully include Common.pri file. I checked that with
!include( ../../Common.pri)::warning(Fail to include Common.pri)
How to achieve what i want.I can't find anything in google
The PWD variable specifies the full path leading to the directory containing the current file being parsed, that is, in your case the full path leading to the Common.pri file and NOT the .pro file. I would place a warning($$OBJECTS_DIR) function in both the .pri and the .pro file to verify the value of OBJECTS_DIR variable.

Resources