2

We have base LUA file ~/.hammerspoon/init.lua which can load a spoon package:

hs.loadSpoon("Foo") 

Now we have our package file init.lua by path: ~/.hammerspoon/Spoons/Foo.Spoon/init.lua

It works fine.

I create a file my/bar.lua inside this package. Full path:

`~/.hammerspoon/Spoons/Foo.Spoon/my/bar.lua` 

with code:

return {} 

and it should be included in ~/.hammerspoon/Spoons/Foo.Spoon/init.lua:

local bar = require("Foo.Spoon.my.bar") 

We will see similar error:

Error loading Foo:..poon.app/Contents/Resources/extensions/hs/_coresetup.lua:662: module 'Foo.Spoon/my/bar' not found: no field package.preload['Foo.Spoon/my/bar'] no file '/Users/me/.hammerspoon/Foo/Spoon/my/bar.lua' no file '/Users/me/.hammerspoon/Foo/Spoon/my/bar/init.lua' no file /Users/me/.hammerspoon/Spoons/Foo/Spoon/my/bar.spoon/init.luat26: no file opt/homebrew/share/lua/5.4/Foo/Spoon/my/bar.lua' ... 

I played with various options like

  • require("my.bar")
  • require("Foo.my.bar")

all of them fail.

Problem

In case of using require("Foo.Spoon.my.bar"), it looks like the loader the loader always replaces "." with "/" which breaks options to mention the package folder Foo.Spoon.
But it suppose to keep "Foo.Spoon" instead of "Foo/Spoon".
And it does not:

no file '/Users/me/.hammerspoon/Foo/Spoon/my/bar.lua' no file 

According to logs it obviously walks around, but does not look at the Foo.Spoon directory (tried .spoon it in lowercase too), which is strange.

Any advice/workaround (but without changing "require" function, or using absolute paths)?..

P.S. I took a look at related other posts.

2 Answers 2

3

Since the init script works fine, you can append the directory of the init script to package.searchpath, so that lua is able to find the module from this directory.

-- modulePath will be resolved as '~/.hammerspoon/Spoons/Foo.Spoon/init.lua' local modulePath = package.searchpath('Foo', package.path) -- Remove 'init.lua' modulePath = modulePath:match('(.*/)') -- Append ';~/.hammerspoon/Spoons/Foo.Spoon/?.lua' to package.path package.path = package.path .. ';' .. modulePath .. '?.lua' -- Now the following code will resolve the module path as -- '~/.hammerspoon/Spoons/Foo.Spoon/my/bar.lua' local bar = require("my.bar") 
Sign up to request clarification or add additional context in comments.

Comments

0

According to the Hammerspoon docs on spoon development you can not use require and should instead use the following:

dofile(hs.spoons.resourcePath("someCode.lua")) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.