Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This keeps a stack of
require
"paths", and uses the topmost one when resolving relative requires to calculate the "absolute path".From inside
"path_1.path_2"
:require ".b"
will require"path_1.path_2.b"
.require "..b"
will require"path_1.b"
.This has a few caveats:
require
must be called "inside" the parentrequire
; this means thatrequire
s called "later" will have the wrong path.So:
core.plugins
and the relative path is.......plugin_name
, the resulting path isplugin_name
.(2) might be considered a feature, to limit the require range and not go outside our managed directories (even thought a plugin could still add its own custom search path).
To provide a workaround for (1), I added
get_current_require_path
which will return the topmost path in the stack. This still needs to be called inside the parentrequire
, but its result can be used later.So:
Suggestions for a better name for
get_current_require_path
are welcome.Should we also add a
level
parameter toget_current_require_path
, so that path traversal doesn't need to be implemented by each plugin that uses it?EDIT: If we want to avoid using
pcall
, we could use a to-be-closed object that does the stack popping.