Skip to content

Add command requirements (requires) - #940

Merged
rafaelfranca merged 14 commits into
rails:mainfrom
Shopify:requires
Nov 23, 2026
Merged

rafaelfranca merged 14 commits into
rails:mainfrom
Shopify:requires

Conversation

@paulspringett

Copy link
Copy Markdown
Contributor

🌈
Closes #939

Why

A Thor command can call invoke to run another command, but there's no declarative way to say a command needs others to have run first, with what inputs. The relationship doesn't show in help, and a failure doesn't name which prerequisite failed.

Shopify maintains a large set of Rake tasks for local development. Many build on each other (set something up, then seed it, then use it), and we want to move them to Thor for its typed options and help. Requirements are the piece Thor is missing for that. I've discussed this with @rafaelfranca already and we're aligned on the direction.

What

A requires class macro declares that a command needs other commands to have run first, with optional args:/options: mappings. A Thor::Requirement value object owns name resolution and satisfaction; Thor::Invocation#invoke_command gets a one-line hook that calls it before the command body runs. A failure raises the new Thor::RequirementError, with the original error as cause. Thor.validate_requirements! resolves every requirement on a class, for use in a test. help COMMAND gets a Requirements: section listing the direct names.

Requirements run through invoke, so Thor's existing once-per-run dedup applies: a command already run in this invocation is skipped by name, whatever the mapped args. Calling invoke directly in a command body is still the way to run a command again with different inputs.

The arity check on the command's own arguments runs before its requirements, so a usage error doesn't run any prerequisites first.

Options declared by both commands auto-pass by name. An explicit options: mapping merges on top of the auto-passed set.

Compatibility

No behaviour change for classes that don't use requires. Thor::Command.new gains one optional trailing argument. _shared_configuration gains one new key, for cycle detection.

Notes for reviewers

  • requires has no method_requires form, unlike method_option/option and method_exclusive/exclusive. Should we add this?
  • A required command whose own body raised stays in the once-per-run record – as that matches the existing invoke rule. A command whose requirement failed is not recorded, so a later invoke retries it.
  • Auto-pass sends the caller's current option value, default included, because parsed options don't record which values the user typed.

Thor::Requirement is the value object and the engine behind the requires
macro: a command name plus optional args:/options: mappings, validated
when declared, with the name resolution, invocation, error wrapping,
cycle detection, and the graph walk behind validate_requirements!.
Thor::RequirementError < Thor::Error is raised when a requirement cannot
be satisfied. Thor::Command gains a trailing requirements member that
defaults to an empty list, so existing callers of Command.new are
unaffected.
requires declares one or more commands that must run before the next
command. Requirements run through invoke from Invocation#invoke_command,
so they share its once-per-invocation rule, compose recursively, and
run depth-first in declaration order. Options declared by both commands
auto-pass by name; args:/options: mappings (static or callable) map the
current invocation into a single required command. A malformed command
line raises its usage error before any requirement runs, and a command
whose requirement failed is not recorded as run.

A missing command, a cycle, or a Thor::Error from a required command
raises Thor::RequirementError with the original error as cause. Other
exceptions propagate unchanged. validate_requirements! resolves every
reachable requirement and reports unresolved names and cycles, so a test
suite can assert them. help COMMAND lists the command's requirements.
Make the mapping helpers private, use Thor's type-check idiom and error
wording, and match the suite's describe style. Split the arity example
that chained two raising calls, so both cases run. Add examples for a
splat parameter, two classes sharing a namespace, the default namespace,
and an explicit options: key the required command does not declare.
@paulspringett
paulspringett marked this pull request as ready for review October 19, 2026 12:09
@rafaelfranca
rafaelfranca merged commit f37315b into rails:main Nov 23, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Declarative command requirements (requires)

2 participants