Skip to content

[2.x] fix: Support inline comments in .jvmopts and .sbtopts files#8758

Merged
eed3si9n merged 2 commits intosbt:developfrom
njlazzar-su:fix/inline-comments-8755
Feb 17, 2026
Merged

[2.x] fix: Support inline comments in .jvmopts and .sbtopts files#8758
eed3si9n merged 2 commits intosbt:developfrom
njlazzar-su:fix/inline-comments-8755

Conversation

@njlazzar-su
Copy link
Copy Markdown
Contributor

fixes #8755

Problem

After PR #8730 (commit 921efce), inline comments in .jvmopts and .sbtopts files cause errors.
For example, --add-opens=java.base/java.util=ALL-UNNAMED # comment results in:

Error: Could not find or load main class #

The # and everything after it is now parsed as separate arguments instead of being stripped as a comment.

Solution

Update the sed command in outputConfigFileTokens() to strip inline comments (everything from # to end of line) before parsing tokens:

-    line=$(printf '%s' "$line" | sed $'/^\#/d;s/\r$//')
+    line=$(printf '%s' "$line" | sed $'/^\#/d;s/\s*\#.*//;s/\r$//')

The new s/\s*\#.*// pattern:

  • \s* - matches optional whitespace before #
  • \# - matches the # character
  • .* - matches everything to end of line

This means --add-opens=java.base/java.util=ALL-UNNAMED # comment becomes --add-opens=java.base/java.util=ALL-UNNAMED before token parsing.

Testing

  • Added integration test verifying inline comments are stripped from .jvmopts
  • Manually tested with .jvmopts containing inline comments - no errors
  • Full line comments (starting with #) still work correctly

Manual test output:

# Input .jvmopts:
--add-opens=java.base/java.util=ALL-UNNAMED # This is a comment
-Xmx2g # Memory setting
-Dtest.key=value # inline comment here too

# Parsed tokens (comments stripped):
--add-opens=java.base/java.util=ALL-UNNAMED
-Xmx2g
-Dtest.key=value

AI Disclosure

Generated-by: Claude Sonnet 4.5

All code has been manually reviewed, tested, and verified to solve issue #8755.

Nick and others added 2 commits February 17, 2026 19:06
**Problem**
After PR sbt#8730 (commit 921efce), inline comments in .jvmopts and .sbtopts files cause errors.
For example, `--add-opens=java.base/java.util=ALL-UNNAMED # comment` results in:
Error: Could not find or load main class #

The # and everything after it is now parsed as separate arguments instead of being stripped as a comment.

**Solution**
Update the sed command in outputConfigFileTokens() to strip inline comments (everything from # to end of line) before parsing tokens. Changed:
  sed $'/^\#/d;s/\r$//'
To:
  sed $'/^\#/d;s/\s*\#.*//;s/\r$//'

The new s/\s*\#.*// pattern matches optional whitespace + # + rest of line and removes it.

**Testing**
- Added integration test verifying inline comments are stripped from .jvmopts
- Manually tested with .jvmopts containing inline comments - no errors
- Full line comments (starting with #) still work correctly

Generated-by: Claude Sonnet 4.5
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!

@njlazzar-su
Copy link
Copy Markdown
Contributor Author

Thanks!

You’re welcome! Just signed CLA

@eed3si9n
Copy link
Copy Markdown
Member

I appreciate the details on the PR description. I wish everyone did this.

@eed3si9n eed3si9n merged commit 02d9d4c into sbt:develop Feb 17, 2026
15 of 16 checks passed
@tpetillot
Copy link
Copy Markdown
Contributor

@eed3si9n this PR introduce unexpected deletion, for example:

$ echo "-Dlog4j2.configurationFile=file:/etc/myapp/log4j2#prod.xml" | sed $'/^\#/d;s/\s*\#.*//;s/\r$//' 
-Dlog4j2.configurationFile=file:/etc/myapp/log4j2

@eed3si9n
Copy link
Copy Markdown
Member

I guess the expression immediately before \# should be \s+ if we want at least one whitespace character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[1.x] regression in JVM parameters parsing

3 participants