Skip to content

Conversation

@BobVul
Copy link
Contributor

@BobVul BobVul commented Jul 21, 2025

Fixes #10937 by introducing an additional INTERNAL_MAVEN_OPTS for any arguments that need to be inserted by the script. Parsing the externally-defined MAVEN_OPTS variable can lead to incorrect processing of quotes and special characters, so use the separate variable to avoid doing so.

Additionally JVM_CONFIG_MAVEN_OPTS is introduced as its own variable to preserve the append behaviour.

Specifically, this fixes this case:

$env:MAVEN_OPTS='"-Dfoo=bar|baz"'
mkdir .mvn
New-Item .mvn\jvm.config
mvn

by implementing proposed fix 2 from #10937

The related fix for 3.9.x branch is in PR #10969


Following this checklist to help us incorporate your
contribution quickly and easily:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the Core IT successfully.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

Fixes apache#10937 by introducing an additional INTERNAL_MAVEN_OPTS for any arguments that need to be inserted by the script. Parsing the externally-defined MAVEN_OPTS variable can lead to incorrect processing of quotes and special characters, so use the separate variable to avoid doing so.

Additionally JVM_CONFIG_MAVEN_OPTS is introduced as its own variable to preserve the append behaviour.
@BobVul BobVul marked this pull request as draft July 21, 2025 07:52
@slawekjaranowski slawekjaranowski added the bug Something isn't working label Jul 21, 2025
Remove quotes from the new JVM_CONFIG_MAVEN_OPTS to also allow quoted pipes to work from jvm.config
This is a follow-up to apache#10937, where the extra layer of quotes causes parsing issues in Windows cmd
@BobVul BobVul marked this pull request as ready for review July 21, 2025 10:25
Test that adding pipes to either MAVEN_OPTS or jvm.config does not break anything
Note: it is important that a jvm.config exists for the MAVEN_OPTS portion of the test to work
@BobVul
Copy link
Contributor Author

BobVul commented Jul 21, 2025

Sorry, I think I did a rename of the folder before the last push that moved it over the line length limit. Fixed now.

@BobVul BobVul mentioned this pull request Jul 21, 2025
8 tasks
@BobVul
Copy link
Contributor Author

BobVul commented Jul 21, 2025

Looks like in 4.x the bash script also changed, such that it now evals the command and introduces a similar pipe parsing error in a completely different way.

The changes made here (purely to the Windows .cmd!) should have no impact on that, but the new tests do expose that issue.

I'll consolidate findings/discussion back in issue #10937 but I'm honestly not too sure what to do here -- complex bash scripts are not my forte.

@BobVul BobVul marked this pull request as draft July 21, 2025 15:49
Fixes apache#10937 by introducing an additional INTERNAL_MAVEN_OPTS for any arguments that need to be inserted by the script. Parsing the externally-defined MAVEN_OPTS variable can lead to incorrect processing of quotes and special characters, so use the separate variable to avoid doing so.
@BobVul
Copy link
Contributor Author

BobVul commented Jul 21, 2025

@gnodet Since you made the original change in #2213, could I run this by you, as I'm a bit unsure.

The existing behaviour is:

  1. The change there updated concat_lines to perform more advanced parsing of jvm.config.
  2. In the process it runs the lines through xargs, which processes quotes specially, in the process stripping them from the input
  3. Later, quotes are added back in if the line is not surrounded by them and contains spaces (i.e. first and last char only)

But the behaviour seen with pipes specifically is that the quotes are being stripped but not added back in. i.e. -Dopt="foo|bar" is transformed into -Dopt=foo|bar and causing problems on Linux. I can see two ways of handling this:

  1. Add pipes to the "check for spaces" bit, which will then re-surround the entire line in quotes. This would probably work but seems a tad hacky?
  2. Change out the xargs -n 1 command for tr '\n' '\0' | xargs -n 1 -0, i.e. use xargs in NUL mode. This prevents xargs from handling quotes specially, and therefore quotes are not stripped from the input. However, this also means the code that re-adds quotes must be removed else it will cause e.g. -Dopt="foo bar" to become "-Dopt="foo bar"" which effectively leaves the space unquoted again (much like the original issue on Windows!). I think this is probably fine since in this case xargs won't strip initial quotes, though it does force the user to add quotes manually.

I'm leaning towards using option 2, but I'm not familiar enough with either this area of code nor bash to be certain that it won't cause other issues. Do you have any suggestions here?

Proposed new concat_lines:

concat_lines() {
  if [ -f "$1" ]; then
    # First convert all CR to LF using tr
    tr '\r' '\n' < "$1" | \
    sed -e '/^$/d' -e 's/#.*$//' | \
    # Replace LF with NUL for xargs
    tr '\n' '\0' | \
    # Split into words and process each argument
    # Use -0 with NUL to avoid special behaviour on quotes
    xargs -n 1 -0 | \
    while read -r arg; do
      # Replace variables first
      arg=$(echo "$arg" | sed \
        -e "s@\${MAVEN_PROJECTBASEDIR}@$MAVEN_PROJECTBASEDIR@g" \
        -e "s@\$MAVEN_PROJECTBASEDIR@$MAVEN_PROJECTBASEDIR@g")

      echo "$arg"
    done | \
    tr '\n' ' '
  fi
}

e: This proposed new version passes both the new gh10937 test and the old mng4559 tests. So as far as what's tested, it appears to work correctly. I'll push the change to the PR but happy to rollback if it's the wrong approach.

e2: I just realised xargs -0 might not be available in POSIX xargs but it looks like it was added in issue 8 / 2024. And it's currently available in gnu/macos/(net|free|open)bsd/busybox so that's about as universal as I can find

By default xargs handles quotes specially. To avoid this behaviour, `-0` must be used instead, but first we need to convert LF to NUL.
Since quotes are no longer being stripped by xargs, we should also stop trying to add them back in otherwise nested quotes cause further issues
@BobVul BobVul marked this pull request as ready for review July 21, 2025 17:04
@BobVul
Copy link
Contributor Author

BobVul commented Jul 23, 2025

So um... what's the next step here? Is there anything else I should do here or on the related 3.9.x PR?

Also, what's the best way to get this into 4.0.x branch? I think the changes should be identical, should I wait for this one to be reviewed/merged first?

Copy link
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

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

LGTM, thx for this nice patch ! I'll do the backport to maven-4.0.x branch.

@gnodet gnodet merged commit aeff353 into apache:master Jul 24, 2025
19 checks passed
@github-actions github-actions bot added this to the 4.1.0 milestone Jul 24, 2025
gnodet pushed a commit to gnodet/maven that referenced this pull request Jul 24, 2025
Fixes apache#10937 by introducing an additional INTERNAL_MAVEN_OPTS for any arguments that need to be inserted by the script. Parsing the externally-defined MAVEN_OPTS variable can lead to incorrect processing of quotes and special characters, so use the separate variable to avoid doing so.

Additionally JVM_CONFIG_MAVEN_OPTS is introduced as its own variable to preserve the append behaviour.

Remove quotes from the new JVM_CONFIG_MAVEN_OPTS to also allow quoted pipes to work from jvm.config
This is a follow-up to apache#10937, where the extra layer of quotes causes parsing issues in Windows cmd

Test that adding pipes to either MAVEN_OPTS or jvm.config does not break anything
Note: it is important that a jvm.config exists for the MAVEN_OPTS portion of the test to work

By default xargs handles quotes specially. To avoid this behaviour, `-0` must be used instead, but first we need to convert LF to NUL.
Since quotes are no longer being stripped by xargs, we should also stop trying to add them back in otherwise nested quotes cause further issues

---------

Co-authored-by: Bob <BobVul@users.noreply.github.com>
(cherry picked from commit aeff353)
@gnodet
Copy link
Contributor

gnodet commented Jul 24, 2025

💚 All backports created successfully

Status Branch Result
maven-4.0.x

Questions ?

Please refer to the Backport tool documentation

@BobVul
Copy link
Contributor Author

BobVul commented Jul 24, 2025

LGTM, thx for this nice patch ! I'll do the backport to maven-4.0.x branch.

Thank you!

Should the 3.9.x IT also be merged? apache/maven-integration-testing#407

gnodet added a commit that referenced this pull request Jul 24, 2025
Fixes #10937 by introducing an additional INTERNAL_MAVEN_OPTS for any arguments that need to be inserted by the script. Parsing the externally-defined MAVEN_OPTS variable can lead to incorrect processing of quotes and special characters, so use the separate variable to avoid doing so.

Additionally JVM_CONFIG_MAVEN_OPTS is introduced as its own variable to preserve the append behaviour.

Remove quotes from the new JVM_CONFIG_MAVEN_OPTS to also allow quoted pipes to work from jvm.config
This is a follow-up to #10937, where the extra layer of quotes causes parsing issues in Windows cmd

Test that adding pipes to either MAVEN_OPTS or jvm.config does not break anything
Note: it is important that a jvm.config exists for the MAVEN_OPTS portion of the test to work

By default xargs handles quotes specially. To avoid this behaviour, `-0` must be used instead, but first we need to convert LF to NUL.
Since quotes are no longer being stripped by xargs, we should also stop trying to add them back in otherwise nested quotes cause further issues

---------


(cherry picked from commit aeff353)

Co-authored-by: Bob <1674237+BobVul@users.noreply.github.com>
Co-authored-by: Bob <BobVul@users.noreply.github.com>
gnodet pushed a commit to gnodet/maven that referenced this pull request Jul 24, 2025
Fixes apache#10937 by introducing an additional INTERNAL_MAVEN_OPTS for any arguments that need to be inserted by the script. Parsing the externally-defined MAVEN_OPTS variable can lead to incorrect processing of quotes and special characters, so use the separate variable to avoid doing so.

Additionally JVM_CONFIG_MAVEN_OPTS is introduced as its own variable to preserve the append behaviour.

Remove quotes from the new JVM_CONFIG_MAVEN_OPTS to also allow quoted pipes to work from jvm.config
This is a follow-up to apache#10937, where the extra layer of quotes causes parsing issues in Windows cmd

Test that adding pipes to either MAVEN_OPTS or jvm.config does not break anything
Note: it is important that a jvm.config exists for the MAVEN_OPTS portion of the test to work

By default xargs handles quotes specially. To avoid this behaviour, `-0` must be used instead, but first we need to convert LF to NUL.
Since quotes are no longer being stripped by xargs, we should also stop trying to add them back in otherwise nested quotes cause further issues

---------

Co-authored-by: Bob <BobVul@users.noreply.github.com>
@slawekjaranowski
Copy link
Member

Should the 3.9.x IT also be merged? apache/maven-integration-testing#407

should 😄 done

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

Labels

backport-to-4.0.x bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quoted pipes in MAVEN_OPTS throw errors in 3.9.10+, 4.0.0-rc

3 participants