Skip to content

Commit

Permalink
app.require_here: follow symlink'd main module
Browse files Browse the repository at this point in the history
If a Lua application has, say, a main.lua and a lib.lua side-by-side,
and someone symlinks main.lua into (e.g.) ~/bin, we'd like
require_here() to look in the original directory, not ~/bin.

Symlink-following behavior can be disabled using the (new) second
argument to resolve_here.
  • Loading branch information
nwf committed Mar 12, 2022
1 parent c000fe7 commit de3644f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ deprecation policy.
see [CONTRIBUTING.md](CONTRIBUTING.md#release-instructions-for-a-new-version) for release instructions

## 1.13.0 (unreleased)
- feat: `app.require_here` now follows symlink'd main modules to their directory
- fix: `compat.warn` raised write guard warning in OpenResty
[#414](https://github.com/lunarmodules/Penlight/pull/414)
- feat: `utils.enum` now accepts hash tables, to enable better error handling
Expand Down
16 changes: 14 additions & 2 deletions lua/pl/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,24 @@ end
--
-- Note: the path is prefixed, so it is searched first when requiring modules.
-- @string base optional base directory (absolute, or relative path).
-- @bool nofollow always use the invocation's directory, even if the main file is a symlink
-- @treturn string the current script's path with a trailing slash
function app.require_here (base)
local p = path.dirname(app.script_name())
function app.require_here (base, nofollow)
local p = app.script_name()
if not path.isabs(p) then
p = path.join(path.currentdir(),p)
end
if not nofollow then
local t = path.link_attrib(p)
if t.mode == 'link' then
t = t.target
if not path.isabs(t) then
t = path.join(path.dirname(p), t)
end
p = t
end
end
p = path.normpath(path.dirname(p))
if p:sub(-1,-1) ~= path.sep then
p = p..path.sep
end
Expand Down

0 comments on commit de3644f

Please sign in to comment.