desruisseaux commented on code in PR #3392:
URL: https://github.com/apache/maven-surefire/pull/3392#discussion_r3613762836


##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ModularClasspathForkConfiguration.java:
##########
@@ -224,4 +240,76 @@ File createArgsFile(
             return surefireArgs;
         }
     }
+
+    /**
+     * Searches for module-info-patch.args generated by maven-compiler-plugin 
4.x.
+     * The file is expected at {@code 
target/test-classes/META-INF/maven/module-info-patch.args}
+     * or within a module subdirectory at
+     * {@code 
target/test-classes/<module>/META-INF/maven/module-info-patch.args}.
+     *
+     * @param patchFile the test classes directory (may be the module 
subdirectory)
+     * @return the module-info-patch.args file, or null if not found
+     */
+    @Nullable
+    private static File findModuleInfoPatchArgs(File patchFile) {
+        // Direct location: 
target/test-classes/<module>/META-INF/maven/module-info-patch.args
+        File argsFile = new File(patchFile, 
"META-INF/maven/module-info-patch.args");
+        if (argsFile.isFile()) {
+            return argsFile;
+        }
+        // Parent location: 
target/test-classes/META-INF/maven/module-info-patch.args
+        File parent = patchFile.getParentFile();
+        if (parent != null) {
+            argsFile = new File(parent, 
"META-INF/maven/module-info-patch.args");
+            if (argsFile.isFile()) {
+                return argsFile;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Reads the module-info-patch.args file and appends its directives (e.g. 
--add-exports,
+     * --add-opens) to the args builder. Exceptions are --add-reads and 
--add-modules, which
+     * are skipped: they may reference named modules that surefire places on 
the classpath
+     * rather than the module path. Surefire appends its own
+     * {@code --add-reads <module>=ALL-UNNAMED} instead, and {@code 
--add-modules
+     * ALL-MODULE-PATH} is generated by the caller.
+     *
+     * @param args the args builder to append to
+     * @param patchArgs the module-info-patch.args file
+     * @param moduleName the module name for surefire's own --add-reads
+     * @throws IOException if the file cannot be read
+     */
+    private static void appendModuleInfoPatchArgs(StringBuilder args, File 
patchArgs, String moduleName)
+            throws IOException {
+        // explicit charset: the compiler writes the args file as UTF-8, the 
platform default may differ
+        try (BufferedReader reader = 
Files.newBufferedReader(patchArgs.toPath(), StandardCharsets.UTF_8)) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                line = line.trim();
+                if (line.isEmpty() || line.startsWith("#")) {
+                    continue;
+                }
+                // Skip --add-reads and --add-modules from the file — surefire 
manages these itself.
+                // The compiler-generated args may reference named modules 
that surefire places on
+                // the classpath rather than the module-path, causing boot 
layer errors.
+                if (line.startsWith("--add-reads") || 
line.startsWith("--add-modules")) {
+                    if (line.equals("--add-reads") || 
line.equals("--add-modules")) {
+                        // two-line form: the value is on the following line
+                        reader.readLine();
+                    }
+                    continue;
+                }
+                args.append(line).append(NL);
+            }
+        }
+        // Surefire always needs --add-reads <module>=ALL-UNNAMED for its 
classpath-based runner
+        args.append("--add-reads")

Review Comment:
   See above reminder: may need to be removed after class-path versus 
module-path problem is resolved.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to