Skip to content

[2.x] fix: sbt --client fails if -mem is provided#8831

Merged
eed3si9n merged 2 commits intosbt:developfrom
rockchris99:fix/client-mem-flag-6825
Mar 5, 2026
Merged

[2.x] fix: sbt --client fails if -mem is provided#8831
eed3si9n merged 2 commits intosbt:developfrom
rockchris99:fix/client-mem-flag-6825

Conversation

@rockchris99
Copy link
Copy Markdown
Contributor

Fixes #6825

Summary

sbt -mem 10000 -client fails because launcher-only flags leak through to the native thin client (sbtn), which doesn't understand them.

Problem

runNativeClient() in the bash launcher only strips --client and forwards everything else to sbtn. So sbtn receives -mem 10000, and NetworkClient.parseArgs() misclassifies them: -mem goes to sbtArguments, 10000 goes to commandArgs. The server start command becomes sbt -mem --detach-stdio --server, which fails because -mem has no value.

$ sbt -mem 10000 -client
[info] entering *experimental* thin client - BEEP WHIRR
[info] server was not detected. starting an instance
[error] failed to connect to server

Solution

1. Bash launcher (sbt): allowlist-based arg forwarding

Replace the naive --client stripping in runNativeClient() with an allowlist loop. Only -D* properties, -bsp, --sbt-launch-jar, and non-flag command args are forwarded to sbtn. All launcher-specific flags are categorized and skipped:

  • Value-taking flags (-mem, -jvm-debug, -java-home, etc.): skip flag + next arg
  • No-value flags (--client, -batch, -debug, etc.): skip flag
  • Eq-syntax flags (--supershell=false, --color=never): skip
  • -J* JVM flags: skip

2. Defense-in-depth in NetworkClient.parseArgs()

Added matching launcher flag sets and corresponding match cases so that even if launcher flags somehow reach sbtn, they are silently dropped instead of being misclassified as sbt arguments or commands.

3. Unit tests for NetworkClient.parseArgs()

18 test cases covering:

  • Value-taking flags (-mem 10000, -jvm-debug 5005, -java-home /path)
  • No-value flags (--client, -debug, -batch, -allow-empty)
  • Eq-syntax flags (--supershell=false, --color=never)
  • -J* JVM flags
  • Preserved flags (-Dfoo=bar, -bsp, --sbt-launch-jar, --sbt-script)
  • Combined/integration scenarios

Test results

sbt commandProj/test — 38/38 pass:

[info] sbt.internal.client.NetworkClientParseArgsTest
[info] - -mem 10000 compile drops -mem and its value
[info] - --mem 10000 compile drops --mem and its value
[info] - -jvm-debug 5005 compile drops both flag and port
[info] - -java-home /path/to/jdk compile drops both
[info] - -mem at end of args with no value does not crash
[info] - --client compile drops --client
[info] - -client compile drops -client
[info] - -debug is dropped
[info] - -batch is dropped
[info] - -allow-empty is dropped
[info] - --supershell=false is dropped
[info] - --color=never is dropped
[info] - -J-Xss4m is dropped
[info] - -Dfoo=bar compile forwards -D property to sbtArguments
[info] - -bsp is still recognized
[info] - --sbt-launch-jar is preserved
[info] - --sbt-script is preserved
[info] - combined: -mem 10000 -Dfoo=bar compile test
[info] - combined: --client -batch -java-home /jdk --color=never -Dfoo=bar compile
[info] Passed: Total 38, Failed 0, Errors 0, Passed 38

Verification

  • sbt commandProj/compile passes
  • sbt commandProj/test — 38/38 pass
  • sbt scalafmtCheckAll passes
  • sbt scalafmtSbtCheck passes

Generated-by: Claude Code (claude.ai/code)

@rockchris99
Copy link
Copy Markdown
Contributor Author

@eed3si9n This is ready for review. All tests pass and formatting checks are clean. Let me know if you'd like any changes.

@eed3si9n
Copy link
Copy Markdown
Member

Hi, thanks for the contribution! Please check out our Contributor's Guildeline and:

  1. Make sure you can reproduce the problem and the fix manually
  2. Include tests for the change
  3. Follow our AI assisted contributions guideline (please disclose AI usages in the PR description, if any)
  4. Multiple contributors might work on this issue in parallel
  5. Generally we recommend getting CI to pass on a forked repo before sending a pull request (pull requests that do not pass tests will be closed after a few days)
  6. Sign Scala CLA https://contribute.akka.io/contribute/cla/scala

Here's how to build sbtn locally:

$ sbt nativeImage

# or on ARM macOS
$ ARCHS=arm64 sbt nativeImage

The bash launcher's runNativeClient() passed all original CLI args to
sbtn, only stripping --client. This caused sbtn to receive launcher
flags like -mem 10000, which NetworkClient.parseArgs() misclassified:
-mem went to sbtArguments and 10000 went to commandArgs, resulting in
a broken server start command.

Fix runNativeClient() to use an allowlist: only forward -D* properties,
-bsp, --sbt-launch-jar, and sbt commands to sbtn. All launcher-specific
flags (-mem, -jvm-debug, -java-home, -batch, -debug, etc.) are skipped.

As defense-in-depth, add launcher flag filtering to
NetworkClient.parseArgs() so that even if launcher flags reach sbtn,
they are silently dropped instead of being misclassified.
Move all launcher flag filtering to NetworkClient.parseArgs() so the
fix applies uniformly across all launchers (sbt.sh, sbt.bat, sbtw).
@rockchris99 rockchris99 force-pushed the fix/client-mem-flag-6825 branch from d655ffa to 36d49b7 Compare March 5, 2026 05:07
@rockchris99 rockchris99 requested a review from eed3si9n March 5, 2026 06:10
Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

Thanks!

@eed3si9n eed3si9n merged commit dd66216 into sbt:develop Mar 5, 2026
15 checks passed
eed3si9n pushed a commit to eed3si9n/sbt that referenced this pull request Mar 23, 2026
The bash launcher's runNativeClient() passed all original CLI args to
sbtn, only stripping --client. This caused sbtn to receive launcher
flags like -mem 10000, which NetworkClient.parseArgs() misclassified:
-mem went to sbtArguments and 10000 went to commandArgs, resulting in
a broken server start command.
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.

sbt --client fails if -mem is provided

2 participants