Page 1 of 2

Getting a project to work with moonscript 0.5.0-1

Posted: Sun May 20, 2018 1:48 pm
by grump
Anyone here know how to use moonscript with love? I have trouble getting a simple moonscript project to work.

I've attached a zip file with two identical versions of example code: one is written in plain Lua, the other written in moonscript with a main.lua file that is supposed to bootstrap the moonscript project. The Lua project works as expected and prints 'hello' on the screen, the moonscript version does not.

Moonscript stuff works fine when compiled with moonc, but I'm tired of the cumbersome debugging process and want to be able to run the moon files directly. It immediately fails however at the require('game') line, complaining that it can't find the module. According to the documentation, the package loader should be able to recognize and load .moon files after moonscript is required:
After moonscript is required, Lua’s package loader is updated to search for .moon files on any subsequent calls to require. [...] Any search paths in package.path ending in .lua are copied, rewritten to end in .moon, and then inserted in package.moonpath.
That doesn't seem to work at all:

Code: Select all

$ love11 .
Error: main.lua:2: module 'game' not found:
	no field package.preload['game']
	no 'game' in LOVE game directories.
	no file 'game' in LOVE paths.
	no file './game.lua'
	no file '/usr/share/luajit-2.0.5/game.lua'
	no file '/usr/local/share/lua/5.1/game.lua'
	no file '/usr/local/share/lua/5.1/game/init.lua'
	no file '/usr/share/lua/5.1/game.lua'
	no file '/usr/share/lua/5.1/game/init.lua'
	no file './game.so'
	no file '/usr/local/lib/lua/5.1/game.so'
	no file '/usr/lib/lua/5.1/game.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
I tried adding this to the bootstrapper:

Code: Select all

package.path = package.path .. ";./?.moon;./?/init.moon"
But:

Code: Select all

$ love11 .
Error: main.lua:3: module 'game' not found:
	no field package.preload['game']
	no 'game' in LOVE game directories.
	no file 'game' in LOVE paths.
	no file './game.lua'
	no file '/usr/share/luajit-2.0.5/game.lua'
	no file '/usr/local/share/lua/5.1/game.lua'
	no file '/usr/local/share/lua/5.1/game/init.lua'
	no file '/usr/share/lua/5.1/game.lua'
	no file '/usr/share/lua/5.1/game/init.lua'
	no file './game.moon'
	no file './game/init.moon'
	no file './game.so'
	no file '/usr/local/lib/lua/5.1/game.so'
	no file '/usr/lib/lua/5.1/game.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
...it says 'no file ./game.moon'

I also tried running it with moon instead of love, but then it doesn't find the required module:

Code: Select all

$ moon game.moon
moon: game.moon:1: (1) module 'module' not found:No LuaRocks module found for module
	no field package.preload['module']
	no file '/root/.luarocks/share/lua/5.1/module.lua'
	no file '/root/.luarocks/share/lua/5.1/module/init.lua'
	no file '/usr/local/share/lua/5.1/module.lua'
	no file '/usr/local/share/lua/5.1/module/init.lua'
	no file './module.lua'
	no file '/usr/local/lib/lua/5.1/module.lua'
	no file '/usr/local/lib/lua/5.1/module/init.lua'
	no file '/usr/share/lua/5.1/module.lua'
	no file '/usr/share/lua/5.1/module/init.lua'
	no file '/home/grump/.luarocks/share/lua/5.1/module.lua'
	no file '/home/grump/.luarocks/share/lua/5.1/module/init.lua'
	no file '/root/.luarocks/lib/lua/5.1/module.so'
	no file '/usr/local/lib/lua/5.1/module.so'
	no file './module.so'
	no file '/usr/lib/x86_64-linux-gnu/lua/5.1/module.so'
	no file '/usr/lib/lua/5.1/module.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
	no file '/home/grump/.luarocks/lib/lua/5.1/module.so'
So I tried the package hack in game.moon. That makes it find the module, but it still seems to try to run it as plain Lua:

Code: Select all

$ moon game.moon
moon: error loading module 'module' from file './module/init.moon':
	./module/init.moon:1: '=' expected near 'fn'
What am I doing wrong?

Edit: it just occured to me that even if the moon files are recognized correctly, it may fail because it doesn't know about the package loader that love installs. Is it even possible to run uncompiled moonscript files with love?

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 7:29 am
by s-ol
Hm, im not sure it love 11 changed something, but I was always runnig it like that:

main.lua

Code: Select all

require(“moonscript”)
require(“game”)
I cant check your attached file unfortunately on my phone. You are not attempting to require “game.moon” right? Also, is your capitalization the same on disk and in the require call?

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 8:02 am
by grump

Code: Select all

-- main.lua
require('moonscript')
require('game')

Code: Select all

-- game.moon
package.path = package.path .. ';./?/init.moon' -- this seems to be required to make modules work
require('module')

love.draw = ->
	fn('hello')

Code: Select all

-- module/init.moon
export fn = (x) -> love.graphics.print(x, 0, 0)
s-ol wrote: Mon May 21, 2018 7:29 am Also, is your capitalization the same on disk and in the require call?
Yes. Everything is correctly named, everything is in lower case.

I just checked, and there is indeed a difference between LÖVE 0.10.2 and 11.1:

11.1:

Code: Select all

$ love11 .
Error: main.lua:2: module 'game' not found:
	no field package.preload['game']
	no 'game' in LOVE game directories.
	no file 'game' in LOVE paths.
	no file './game.lua'
	no file '/usr/share/luajit-2.0.5/game.lua'
	no file '/usr/local/share/lua/5.1/game.lua'
	no file '/usr/local/share/lua/5.1/game/init.lua'
	no file '/usr/share/lua/5.1/game.lua'
	no file '/usr/share/lua/5.1/game/init.lua'
	no file './game.so'
	no file '/usr/local/lib/lua/5.1/game.so'
	no file '/usr/lib/lua/5.1/game.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
game.moon is not found (or even looked for), though it sits right there alongside with main.lua.

0.10.2:

Code: Select all

$ love .
Error: error loading module 'module' from file './module/init.moon':
	./module/init.moon:1: '=' expected near 'fn'
game.moon is being found in 0.10.2, so is module/init.moon. It still doesn't recognize module/init.moon as a moonscript file, and tries to interpret it as Lua code.

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 9:06 am
by bartbes
That kinds of makes me wonder what 'love11' refers to...

Anyway, there are 2 issues: the documentation claims moonscript doesn't use package.path, but package.moonpath. As you can see, you're telling lua to load your moonscript files as lua files.
Additionally, I doubt the moonscript loader is using love.filesystem, so it's probably sensitive to things like the working directory. You can probably create your own loader, which would look something like this:

Code: Select all

local moonscript = require "moonscript.base"
table.insert(package.loaders, function(modname)
    modname = modname:gsub("%.", "/")
    local filename1 = ("%.moon"):gsub("%%", modname)
    local filename2 = ("%/init.moon"):gsub("%%", modname)
    local contents
    if love.filesystem.getInfo(filename1, "file") then
        contents = love.filesystem.read(filename1)
    elseif love.filesystem.getInfo(filename2, "file") then
        contents = love.filesystem.read(filename2)
    else
        return nil, "Moonscript file not found"
    end
    return moonscript.loadstring(contents)
end)

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 9:19 am
by grump
bartbes wrote: Mon May 21, 2018 9:06 am That kinds of makes me wonder what 'love11' refers to...
It's the 11.1-AppImage that I use so I can have both 11.1 and 0.10.2 installed.

Thank you for your help, I got it working now with the loader you provided.

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 9:59 am
by bartbes
grump wrote: Mon May 21, 2018 9:19 am It's the 11.1-AppImage that I use so I can have both 11.1 and 0.10.2 installed.
I wonder if it someone trips up moonscript's (plain) loader. As far as I know it now correctly sets the working directory, so that shouldn't mess with it. Oh well.

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 10:55 am
by grump
I can now successfully use moonscript with love, with the downside that I still don't getter proper error locations, because by using loadstring there are no filenames associated with error messages; all errors are shown as moonscript.loadstring as the error location. That makes debugging even more cumbersome than using moonc compiled files.
bartbes wrote: Mon May 21, 2018 9:59 am I wonder if it someone trips up moonscript's (plain) loader. As far as I know it now correctly sets the working directory, so that shouldn't mess with it. Oh well.
Using moonscript.loadfile would be better to get correct error locations (it just has to work on the plain filesystem for debugging, I don't care about .love files working), but yes, that does trip up because the working directory is the usr directory in the tmp mount path.

The AppImage provided on love2d.org does not have the working directory fix yet, does it? love.filesystem.getWorkingDirectory() returned /tmp/.mount_87ko1w/usr.

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 11:14 am
by bartbes
grump wrote: Mon May 21, 2018 10:55 am I can now successfully use moonscript with love, with the downside that I still don't getter proper error locations, because by using loadstring there are no filenames associated with error messages; all errors are shown as moonscript.loadstring as the error location.
You can pass a filename (or any name) as second argument to loadstring.
grump wrote: Mon May 21, 2018 10:55 am The AppImage provided on love2d.org does not have the working directory fix yet, does it? love.filesystem.getWorkingDirectory() returned /tmp/.mount_87ko1w/usr.
Oh yeah, it wouldn't. I never quite manage to remember what made it into what release.

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 11:38 am
by grump
bartbes wrote: Mon May 21, 2018 11:14 am You can pass a filename (or any name) as second argument to loadstring.
Right, that kind of works - it's still printing bogus line numbers though. It's the line numbers from the transpiled file, not the source line numbers. Right back to square one, sigh.

Re: Getting a project to work with moonscript 0.5.0-1

Posted: Mon May 21, 2018 11:43 am
by s-ol
grump wrote: Mon May 21, 2018 11:38 am
bartbes wrote: Mon May 21, 2018 11:14 am You can pass a filename (or any name) as second argument to loadstring.
Right, that kind of works - it's still printing bogus line numbers though. It's the line numbers from the transpiled file, not the source line numbers. Right back to square one, sigh.
it has always been this way for me. I have a shortcut in my editor that runs the buffer through moonc and shows me the compiled source for reference in a temporary window for cases that require it.

This is something that you should bring up to leafo, though I imagine he is aware.