Skip to content

Fix #12749: Do not report a zero exit code from mvnsh as an error - #12750

Open
HDPark95 wants to merge 1 commit into
apache:masterfrom
HDPark95:fix/12749-mvnsh-zero-exit-code
Open

Fix #12749: Do not report a zero exit code from mvnsh as an error#12750
HDPark95 wants to merge 1 commit into
apache:masterfrom
HDPark95:fix/12749-mvnsh-zero-exit-code

Conversation

@HDPark95

Copy link
Copy Markdown

helpOrVersionAndMayExit() throws InvokerException.ExitException(0) for --help and --version, so a successful command reaches the shell's catch block exactly like a failing one. The mvn, mvnenc and mvnup handlers logged an error without looking at the code:

mvnsh> mvn --version
Apache Maven 4.0.0-rc-6 ...
[ERROR] mvn command exited with exit code 0

The three handlers now share reportExitCode(), which logs only a non-zero code.

Two unit tests cover both branches. With the guard removed, zeroExitCodeIsNotReported fails on the exact message from the report; impl/maven-cli is green at 637 tests with checkstyle and spotless enabled.

Fixes #12749

Commands invoked from mvnsh terminate by throwing
InvokerException.ExitException, and they do so on success too:
helpOrVersionAndMayExit() throws ExitException(0) for --help and
--version. The mvn, mvnenc and mvnup handlers caught that exception and
logged an error without looking at the code, so a plain "mvn --version"
inside the shell printed

  [ERROR] mvn command exited with exit code 0

right after the version banner. Route the three handlers through a
shared reportExitCode() that only logs when the code is non-zero.

Fixes apache#12749

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVE — Clean, correct fix that stops the mvnsh shell from reporting exit code 0 as an error.

The root cause is well-identified: helpOrVersionAndMayExit() throws ExitException(0) for --help and --version, and the old catch blocks unconditionally logged an error regardless of exit code. The shell() method (external process execution) already had the correct if (exitCode != 0) guard, confirming this is the expected pattern.

Strengths:

  • The refactoring into reportExitCode() as a shared helper is well-scoped and reduces duplication.
  • Package-private static placement makes it accessible from the inner class and tests without exposing it publicly.
  • The RecordingLogger test double cleanly implements the Logger interface and verifies both the error path (non-zero exit code) and the no-error path (zero exit code).

Minor observation (non-blocking):

  1. The existing shell() method still implements its own inline if (exitCode != 0) check with a slightly different message format ("exited with code" vs "exited with exit code"). Pre-existing inconsistency, not a blocker — a future cleanup could unify both paths through reportExitCode.

This review was generated by an AI agent (Claude Code) and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

}

/**
* Reports the exit code of a command invoked from the shell. Commands terminate by throwing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc comment is at least confusing and perhaps wrong. Rewrite.

* {@code --version} exit with code 0. Only a non-zero code is an error worth reporting.
*/
static void reportExitCode(Logger logger, String commandName, int exitCode) {
if (exitCode != 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name says it reports an exit code, but it doesn't always do that. Rename the method of perhaps move the if block to where this method is invoked.

gnodet added a commit to gnodet/maven that referenced this pull request Aug 18, 2026
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.

Using mvnsh - ERROR shown while no error happens - "mvn --version", "mvnenc

3 participants