Skip to content

fix(rails): unknown command should exit 1 and write to stderr#25

Open
crossi-dev wants to merge 1 commit into
terminalwire:mainfrom
crossi-dev:fix/10-unknown-command-exit-code
Open

fix(rails): unknown command should exit 1 and write to stderr#25
crossi-dev wants to merge 1 commit into
terminalwire:mainfrom
crossi-dev:fix/10-unknown-command-exit-code

Conversation

@crossi-dev

Copy link
Copy Markdown

Fixes #10

Problem

An unknown command always exits 0. In gem/terminalwire-rails/lib/terminalwire/rails.rb, the rescue for Thor::UndefinedCommandError / Thor::InvocationError writes the message to stdout and never sets the exit code, so the ensure block always calls context.exit 0. Scripts and CI can't detect the failure.

rescue ::Thor::UndefinedCommandError, ::Thor::InvocationError => e
  context.stdout.puts e.message

Fix

Treat it like the StandardError handler right below it — error to stderr, exit code 1:

rescue ::Thor::UndefinedCommandError, ::Thor::InvocationError => e
  context.stderr.puts e.message
  exit_code = 1

Two-line change; mirrors the existing error-handling pattern in the same method.

Verified

Standalone repro of the rescue/ensure path (Ruby 3.3):

                stdout                                   stderr   exit_code
BEFORE   "Could not find command \"foobar\".\n"          ""        0   ← bug
AFTER    ""                          "Could not find command \"foobar\".\n"   1

stderr routing + non-zero exit confirmed; normal commands unaffected.


Came across Terminalwire and noticed #10 had been open a while, so I went ahead and fixed it. No strings attached — I'm with Choreless and we like contributing real fixes to projects we use. If it's useful and you ever want a hand with something larger, happy to help.

— Charles

When Thor raises UndefinedCommandError or InvocationError, the error
message was written to stdout and the process exited with code 0,
making it impossible for callers to detect the failure.

Write the error to stderr (consistent with the StandardError handler)
and set exit_code to 1 so the ensure block propagates the failure to
the client via context.exit.
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.

Unknown command always exits 0

1 participant