.love file package/require path

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
jerluc
Prole
Posts: 13
Joined: Fri Mar 25, 2016 9:59 pm
Location: San Francisco, CA
Contact:

.love file package/require path

Post by jerluc »

I've read a few discussions regarding similar problems, but I haven't really been able to find any definitive answer, so here goes:

I have a LOVE project with the following directory structure:

Code: Select all

lib/
  middleclass/
    middleclass.lua
  module1/
    init.lua
    a.lua
    b.lua
    ...
conf.lua
main.lua
module1 relies on the middleclass module, and so it requires it like so:

Code: Select all

local class = require('middleclass')
-- ... module1 contents ...
And in my main.lua, I have something along these lines:

Code: Select all

-- This seems to be required for it to work from the command-line
package.path = 'lib/?.lua;lib/?/?.lua;lib/?/init.lua;?.lua;?/init.lua;' .. package.path
-- This honestly seems to do absolutely nothing
love.filesystem.setRequirePath(package.path)
local module1 = require('module1')
-- ... main.lua contents ...
When I run from the command-line:

Code: Select all

$ love .
or even the ZIPed version of the game source:

Code: Select all

$ love my_game.love
everything works as expected.

However, when I drag and drop the .love file onto the Love.app program (I'm on OS X) or even when I repackage the .app file per the documentation, the game loads module1 properly, but fails to lookup the middleclass module when it is required by module1.

Has anyone else run into a similar problem or have a similar directory structure? I'd love to hear any suggestions (though I'd really like to keep my files organized this way if at all possible).
User avatar
jerluc
Prole
Posts: 13
Joined: Fri Mar 25, 2016 9:59 pm
Location: San Francisco, CA
Contact:

Re: .love file package/require path

Post by jerluc »

For reference, this is the stack trace I get when running it from the .app file:

Image

As you can see, this stack trace is coming from the successfully loaded module "lovemachine", but for some reason, it can't find the middleclass module?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: .love file package/require path

Post by zorg »

Lose the package.path modification (at least, i never needed that, grain of salt with this one), and require 'middleclass.middleclass" because the lua file named middleclass is inside a folder named middleclass.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
jerluc
Prole
Posts: 13
Joined: Fri Mar 25, 2016 9:59 pm
Location: San Francisco, CA
Contact:

Re: .love file package/require path

Post by jerluc »

Thanks for the quick response, zorg! And yes, that worked perfectly. Alternatively, it looks like renaming the middleclass.lua file to the standard init.lua filename works as well.

I wonder, do you know why it would not be able to find it with that filename? "lib/?/?.lua" is being set as part of the require path, and even in the stack trace you can see it is trying to load "lib/middleclass/middleclass.lua", so it's odd that it would require the use of init.lua.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: .love file package/require path

Post by Positive07 »

Well there is no "lib" folder in your path, the "middleclass" folder is in the root folder, putting middleclass inside of a folder called lib would have worked.

I must also recommend to keep the package path as clean as possible, it would help when your paths becomes too complex and you may find multiple versions of the same libs all over the place or maybe libs or files with the same name.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
jerluc
Prole
Posts: 13
Joined: Fri Mar 25, 2016 9:59 pm
Location: San Francisco, CA
Contact:

Re: .love file package/require path

Post by jerluc »

Thanks for the reply, Positive7. However, there is a "lib" folder in the root of the project directory (see above file structure, the indentation signifies a child directory or file). That's where both the "middleclass" and the "module1" directories live. The problem I was encountering was that the "module1" was being loaded properly (it has an init.lua file), but the "middleclass" couldn't be found, despite the stack trace looking in the correct file location.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: .love file package/require path

Post by zorg »

Hmm, you're right... the image of the error does show lib/middleclass/middleclass.lua being tried... weird. Well, maybe someone else more knowledgeable may have an answer :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: .love file package/require path

Post by Positive07 »

Could you provide a LÖVE file? There may be many reasons, wrong packaging, wrong directory lookup, case sensitive... If you provided a LÖVE file we could find what the error is exactly

Maybe (and just maybe since it's not documented and I haven't tried it) you can use ? twice in a path... I'm not sure and would have to test
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: .love file package/require path

Post by bartbes »

Here's a hint as to why it doesn't work: love.filesystem doesn't use package.path, see [wiki]love.filesystem.setRequirePath[/wiki].
User avatar
jerluc
Prole
Posts: 13
Joined: Fri Mar 25, 2016 9:59 pm
Location: San Francisco, CA
Contact:

Re: .love file package/require path

Post by jerluc »

@Positive07, I can provide a .love shortly, but please please please reread my posts more carefully before responding. Several of the things you have asked are already answered in my previous correspondence. Also, to answer your curiosities around using ? twice in a path, I have and currently do use it twice in the path, and you can see from the stack trace I posted above that it is actually working as intended (it replaces each instance of ? with the name of the module used in the require() call).

@bartbes, thanks for the reply! Can you provide more information around your assertion that it doesn't use package.path? The link you've provided to the LOVE wiki (which I've been to at least a bajillion times by now :p) says nothing whatsoever about whether or not it makes use of Lua's package.path. However, simple experimentation shows that if you set package.path, it does in fact have an effect on the way modules are looked up in LOVE projects.

Perhaps if you have a better link to the source code, that would be most helpful for me :)

------
Kind of as an aside, do any of you know any better way for me to dive into the LOVE source to find where each of the love.* modules are implemented? I'd imagine they are mostly in C/C++, but nonetheless it may help me debug problems in the future if I can better navigate the actual source tree itself.
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests