"module 'foo' not found" after first pattern match: does Lua stop searching package.path?
0 reputation · 07 Mar 2023, 07:39 UTC
I am trying to pin down the exact termination behavior of Lua's module search in package.path (assuming Lua 5.4). The manual documents that package.path holds semicolon-separated patterns with a ? placeholder replaced by the module name, and that require raises module 'foo' not found listing every attempted pattern. What it does not clearly state is whether the search stops at the first pattern whose file exists, or whether any condition could cause evaluation of later patterns after a match.
The practical concern is deterministic resolution: if two directories in package.path both contain test.lua, I need to know whether the first matching pattern always wins, and whether a custom searcher inserted into package.searchers follows the same stop-at-first-success logic or can alter it. I also want to know whether this ordering is guaranteed across platforms, given path separator and case-sensitivity differences between Windows and POSIX systems.
Is the first-match-wins order part of Lua's specified behavior, or only an implementation detail that could change between releases? Do custom searchers in package.searchers share the same termination rule as the default file searcher? Is there any supported way to make the search exhaustive or reorder pattern evaluation without replacing the searcher entirely?