Skip to content

require override drops the return value for non-bundled modules #19

Description

@MintTee

The require replacement at the top of basalt.lua has a bug in its fallback path. Here's the line as it currently ships (minified bundle, lib/basalt.lua / basalt.lua):

require = function(path) if(project[path..".lua"])then if(loadedProject[path]==nil)then loadedProject[path] = project[path..".lua"]() end return loadedProject[path] end baseRequire(path) end

Reformatted:

local baseRequire = require
require = function(path)
    if project[path..".lua"] then
        if loadedProject[path] == nil then
            loadedProject[path] = project[path..".lua"]()
        end
        return loadedProject[path]
    end
    baseRequire(path)   -- missing return
end

If path is one of basalt's bundled modules, everything's fine — the if branch returns properly. But if path is an external module (anything loaded from package.path, i.e. any user code that isn't part of the basalt bundle), the fallback calls baseRequire(path) and discards the result. The module is loaded and cached in package.loaded as usual, but the caller gets nil.

I ran into this trying to structure a project with a few plain Lua modules loaded via require("app"), require("src.comms"), etc., after basalt had been loaded. Every one of them came back nil, which was pretty confusing until I looked at package.loaded and saw they were all there. Basalt itself never hits this branch, since it only ever requires its own bundled files, so it doesn't show up in normal use.

Fix is one word — add return:

return baseRequire(path)

I worked around it on my end by wrapping the overridden require right after loading basalt:

local bas = require("lib.basalt")

local basalt_require = require
require = function(path)
    local result = basalt_require(path)
    if result ~= nil then return result end
    return package.loaded[path]
end

That works, but it'd be nicer if the bundle itself did the right thing. Happy to send a PR against the source if the minified bundle isn't the canonical place to patch.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions