premake / premake/premake-core

Add a "conditional require" call

Open
#1,344 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C
Stars
3.6k
Forks
654
Avg merge
1d 1h
Merged PRs (30d)
13

Description

What problem will this solve?

Provide a way to require a module if it exists. Return nil if the module does not exist (or does not meet any provided version requirements). This makes it easier to add conditional logic or overrides to third-party modules, which may or may not be installed.

What might be a solution?

local myModule = tryRequire('my-module')

if myModule then
    p.override(myModule, 'someFeature', function(base) ... end)
end

This call must:

  • Return nil if the module does not exist
  • If a version requirement is specified, return nil if the requirement is not met, without loading the module (which might have side effects)
  • Any errors which occur while loading the module must be returned to the called (just as with require()

What other alternatives have you already considered?

#624 was a first attempt at solving this. However it needed to load the module to check the version, and could leave side effects if the version check failed, or the module failed to load. We'll need a way to check for the existence of a module, and to check its version, without actually loading it.

Anything else we should know?

Originally logged as #38; I decided it would be better to open a new issue rather than edit @TurkeyMan's description in place.

(You can now support Premake on our OpenCollective. Your contributions help us spend more time responding to requests like these!)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the existing require() behavior and the approach discussed in #624. Determine how module existence and version requirements can be checked without loading the module. Done means a conditional require returns nil for missing or incompatible modules, avoids loading them in those cases, and returns loading errors to the caller.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua
Domain
cli, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.