Skip to content

Commit ba826d9

Browse files
committed
Improve Java command line parsing
By ignoring argument files. Fixes: #139
1 parent aae85bd commit ba826d9

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎px/px_commandline.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ def get_java_command(commandline: str) -> Optional[str]:
570570
continue
571571
if component.startswith("--add-modules="):
572572
continue
573+
if component.startswith("@"):
574+
# Argument file, skip and hope its contents doesn't break the
575+
# rest of our parsing
576+
continue
573577
if component == "--add-modules":
574578
state = "skip next"
575579
continue

‎tests/px_commandline_test.py‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def test_get_command_java_equinox():
397397
)
398398

399399

400-
# Ref:https://github.com/walles/px/issues/137
400+
# Ref: https://github.com/walles/px/issues/137
401401
def test_get_command_line_jdk_java_options():
402402
commandline = """
403403
/opt/homebrew/Cellar/openjdk/24.0.1/libexec/openjdk.jdk/Contents/Home/bin/java
@@ -446,6 +446,40 @@ def test_get_command_line_jdk_java_options():
446446
assert px_commandline.get_command(commandline) == "Launcher"
447447

448448

449+
# Ref: https://github.com/walles/px/issues/139
450+
def test_get_command_line_java_issue_139():
451+
commandline = """
452+
/opt/homebrew/Cellar/openjdk@21/21.0.8/libexec/openjdk.jdk/Contents/Home/bin/java
453+
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
454+
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
455+
--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
456+
--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
457+
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
458+
--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
459+
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
460+
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
461+
--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
462+
--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
463+
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
464+
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
465+
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
466+
@/Users/johan/.gradle/.tmp/gradle-worker-classpath12697647132064750531txt
467+
-Xmx2g
468+
-Dfile.encoding=UTF-8
469+
-Duser.country=SE
470+
-Duser.language=sv
471+
-Duser.variant
472+
worker.org.gradle.process.internal.worker.GradleWorkerMain
473+
'Gradle
474+
Worker
475+
Daemon
476+
3'
477+
""".strip().replace("\n", " ")
478+
commandline = re.sub(r"\s+", " ", commandline)
479+
480+
assert px_commandline.get_command(commandline) == "GradleWorkerMain"
481+
482+
449483
def test_prettify_java_class():
450484
assert (
451485
px_commandline.prettify_fully_qualified_java_class("com.example.MyClass")
@@ -458,7 +492,7 @@ def test_prettify_java_class():
458492
assert px_commandline.prettify_fully_qualified_java_class("") is None
459493

460494

461-
# Ref:https://github.com/walles/px/issues/126
495+
# Ref: https://github.com/walles/px/issues/126
462496
def test_get_command_flutter():
463497
# Candidate contains no slashes => subcommand
464498
assert (

0 commit comments

Comments
 (0)