lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Sorry for the top post.

 But the question was what is the argument against the approach described. ‎ It always has seemed to me that Lua goes to great length to search relative paths and entirely negates to look in the application path for no reason whatsoever. It was one of the most confusing things when I first started looking at Lua.

Lua even interprets module names in require as directories and searches them for various patterns but doesn't try appending it to the application directory. I would love to know why because it seems so... missing? 


Sent from my BlackBerry 10 smartphone on the Virgin Mobile network.
  Original Message  
From: Sean Conner
Sent: Thursday, February 2, 2017 3:56 PM
To: Lua mailing list
Reply To: Lua mailing list
Subject: Re: require() relative to calling file

It was thus said that the Great once stated:
> Hi,
> that topic has been discussed on several places in the we 
> (stackoverflow, etc..) and while people came up with some solutions, I 
> haven't really seen anybody making a case against it. So I'm 
> wondering if that behaviour could be build into Lua ...
> What exactly do I mean?
> -----------------------
> consider the following layout:
> -basedir
> \
> -scripts
> \
> -a.lua
> -b.lua
> a.lua has a require('b')
> Now if I do the following:
> # cd basedir/scripts
> # lua a.lua
> everything runs smoothly. But if I
> # cd basedir
> # lua scripts/a.lua
> I get an error that module 'b' can't be found because the searchpath 
> is based of the current working directory.
> How to deal with it?
> --------------------

You could set LUA_PATH and LUA_CPATH before executing the program. You
could have a.lua do what you do below in C before the 'require "b"'
statement (the script name is in arg[0] by the way). You could wrap the
invocation of a.lua in a shell script that changes to the proper directory
to run it.