Repository: Hammerspoon/hammerspoon

Issue #2478: hs.configdir resolution change in 0.9.79 breaks module loading for symlinked configs
Created: 2020-09-20T21:07:21Z
Thanks @asmagill, @cmsj, @latenitefilms — and to everyone else who contributed to this new release! 🚀
I've been keeping my configs in sync between multiple Macs by symlinking init.lua (and other modules) into ~/.hammerspoon from a central config dir (~/Sync/Settings/Hammerpoon) using Resilio Sync.
My config failed to load when upgrading from 0.9.78. In the console, the following errors were logged:
...app/Contents/Resources/extensions/hs/_coresetup/init.lua:651: module 'keybinds' not found:
	no field package.preload['keybinds']
	no file '/Users/luke/Sync/Settings/Hammerspoon/keybinds.lua'
	no file '/Users/luke/Sync/Settings/Hammerspoon/keybinds/init.lua'
	no file '/Users/luke/Sync/Settings/Hammerspoon/Spoons/keybinds.spoon/init.lua'
        < ...snip... >
stack traceback:
	[C]: in function 'rawrequire'
	...app/Contents/Resources/extensions/hs/_coresetup/init.lua:651: in function 'require'
	(...tail calls...)
	[C]: in function 'xpcall'
	...app/Contents/Resources/extensions/hs/_coresetup/init.lua:514: in function <...app/Contents/Resources/extensions/hs/_coresetup/init.lua:494>

I eventually figured out that this was due to a change in the way hs.configdir is resolved. Previously, it seems like it would resolve to ~/.hammerspoon but now, if init.lua is itself a symlink, then hs.configdir is set to the parent directory of the absolute path of the real file. This is probably fine for 99.9% of users— but in my case, it broke my configuration.
I have the following symlinks:
~/.hammerspoon/init.lua       →  ~/Sync/Settings/Hammerspoon/init-XXX-YYY.lua
~/.hammerspoon/keybinds.lua   →  ~/Sync/Settings/Hammerspoon/keybinds-XXX-YYY.lua

(where XXX=hostname and YYY=machine serial number)

The reason I do this is so I can share chunks of configuration in init.lua between multiple Macs, but have the keybinds/module settings be specific to the machine that's loading them. I symlink a custom file for each machine but they are all named e.g. "keybinds.lua".
I "fixed" it by adding a function to my init.lua:
function require_safe(m)
  local rs_abspath = hs.fs.pathToAbsolute(os.getenv('HOME') .. '/.hammerspoon/' .. m .. '.lua')
  if not rs_abspath then return end
  local rs_basename = string.gsub(rs_abspath, "(.*/)(.*)", "%2")
  local rs_modname = rs_basename:sub(1, -5)
  return require(rs_modname)
end

-- instead of require('keybinds')
require_safe('keybinds')
This has gotten things working again for me. But I'm guessing there's a cleaner way to handle this...?

Duplicate issue #2495: SpoonInstall fails with 0.9.80 and 0.9.79 – attempt to index a nil value (global 'spoon')
Created: 2020-09-22T10:53:54Z
I have the following lines at the beginning of my init.lua (which seem very common across various people's configs):
hs.loadSpoon("SpoonInstall")
spoon.SpoonInstall.use_syncinstall = true
Install=spoon.SpoonInstall

-- Various Install:andUse(...) lines follow
With both the 0.9.79 and 0.9.80 releases, my config fails to load with this error:
2020-09-22 20:49:05: -- Lazy extension loading enabled
2020-09-22 20:49:05: -- Loading ~/.hammerspoon/init.lua
2020-09-22 20:49:05: -- Loading Spoon: SpoonInstall
2020-09-22 20:49:05: *** ERROR: Unable to load Spoon: SpoonInstall
2020-09-22 20:49:05: *** ERROR: /Users/tim/.dotfiles/hammerspoon/init.lua:4: attempt to index a nil value (global 'spoon')
stack traceback:
	/Users/tim/.dotfiles/hammerspoon/init.lua:4: in main chunk
	[C]: in function 'xpcall'
	...app/Contents/Resources/extensions/hs/_coresetup/init.lua:702: in function 'hs._coresetup.setup'
	(...tail calls...)

This might be related to some of the other errors with the recent two releases, but I wasn't sure, so I thought filing a separate issue might be best (and might be easy for other people to find).
I double checked the Spoons repo to see if there might have been some recent compatibility-related changes there, but SpoonInstall.spoon hasn't been updated since 13 months ago.
Thanks for your work on Hammerspoon! Please let me know if I can provide any more information to help diagnose this problem ❤️

Closing comment (2020-09-22T14:10:43Z): Based on the location of your init.lua, I suspect this is a dupe of #2478 (and can be solved in a similar way, I personally recommend adding
-- Needed until  https://github.com/Hammerspoon/hammerspoon/issues/2478 is fixed
hs.configdir = os.getenv('HOME') .. '/.hammerspoon'
package.path = hs.configdir .. '/?.lua;' .. hs.configdir .. '/?/init.lua;' .. hs.configdir .. '/Spoons/?.spoon/init.lua;' .. package.path
to the top of your init.lua, as that worked for me