gnodet-bot commented on code in PR #14063:
URL: https://github.com/apache/maven/pull/14063#discussion_r4613018778
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ExecutableFinder.java:
##########
@@ -20,144 +20,121 @@
import java.io.File;
import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.nio.file.Path;
+import java.util.LinkedHashSet;
+import java.util.List;
import java.util.Locale;
+import java.util.regex.Pattern;
import org.apache.maven.api.services.model.ProfileActivationContext;
/**
- * Helper that implements the OS-aware PATH search used by the {@code
executable()} condition function.
- *
- * <p>The search strategy is:
- * <ol>
- * <li>If {@code name} contains a path separator (i.e. it already looks like
a path), treat it as
- * an absolute or relative file path and check it directly.</li>
- * <li>Otherwise, retrieve the {@code PATH} value from the activation
context's system properties
- * (Maven normalises env vars to {@code env.PATH} / {@code env.Path}
etc.) and split it by the
- * platform path separator. Each directory is searched in order.</li>
- * <li>On Windows, when the candidate does not already have one of the known
executable extensions
- * ({@code .exe}, {@code .cmd}, {@code .bat}, {@code .com}), those
extensions are appended and
- * tried as well.</li>
- * </ol>
+ * OS-aware PATH search used by the {@code executable()} condition function.
+ * Explicit paths are checked directly; plain names are searched only in
nonempty PATH entries.
+ * Maven normalizes Windows environment variable names, including {@code
Path}, to {@code env.PATH}.
+ * On Windows, quoted PATH directories are supported and suffixes from {@code
env.PATHEXT} are
+ * tried in order, unless the name already has one of those suffixes. An
absent PATHEXT defaults
+ * to {@code .COM;.EXE;.BAT;.CMD}; an empty PATHEXT disables suffix probing.
+ * Exact files are checked before appending suffixes, preserving explicit-file
support.
+ * No executable is launched and no implicit current-directory search is
performed.
*
* @since 4.x
*/
class ExecutableFinder {
- /** Windows-specific executable file extensions, in search order. */
- private static final String[] WINDOWS_EXTENSIONS = {".exe", ".cmd",
".bat", ".com"};
-
- /** The system property key under which Maven exposes the {@code PATH}
environment variable. */
- private static final String ENV_PATH_KEY = "env.PATH";
+ private static final String DEFAULT_WINDOWS_EXTENSIONS =
".COM;.EXE;.BAT;.CMD";
private ExecutableFinder() {}
/**
- * Returns {@code true} when {@code name} resolves to an executable file.
+ * Checks for a regular executable file without running it.
*
- * @param name the executable name (e.g. {@code "musl-gcc"}) or an
absolute/relative path
- * @param context the current profile activation context
- * @return {@code true} if the executable is found and is a regular,
executable file
+ * @param name executable name or absolute/relative path
+ * @param context current activation context, including environment
properties
+ * @return whether the executable can be found
*/
- static boolean isExecutableInPath(String name, ProfileActivationContext
context) {
- boolean isWindows = isWindows(context);
-
- // If the name already contains a path separator treat it as a direct
path.
- if (name.contains("/") || name.contains(File.separator)) {
- Path candidate = Path.of(name);
- if (isExecutableFile(candidate, isWindows)) {
- return true;
- }
- // On Windows also try known executable extensions for direct
paths.
- if (isWindows && !hasWindowsExtension(name)) {
- for (String ext : WINDOWS_EXTENSIONS) {
- if (isExecutableFile(Path.of(name + ext), isWindows)) {
- return true;
- }
- }
- }
+ static boolean isExecutableInPath(final String name, final
ProfileActivationContext context) {
+ if (name == null || name.isBlank()) {
return false;
}
+ final var windows = isWindows(context);
+ final var extensions = windows ? windowsExtensions(context) :
List.<String>of();
+
+ if (name.contains("/") || (windows && name.contains("\\"))) {
+ return matches(null, name, windows, extensions);
+ }
- // --- plain name: search PATH ---
- String pathValue = getPathValue(context);
+ final var pathValue = context.getSystemProperty("env.PATH");
if (pathValue == null || pathValue.isBlank()) {
return false;
}
-
- String[] dirs = pathValue.split(File.pathSeparator, -1);
- for (String dir : dirs) {
- if (dir.isBlank()) {
- continue;
+ final var separator = context.getSystemProperty("path.separator");
+ final var pathSeparator = separator == null || separator.isEmpty() ?
File.pathSeparator : separator;
+ final var delimiter =
+ ":".equals(pathSeparator) || ";".equals(pathSeparator) ?
pathSeparator : Pattern.quote(pathSeparator);
+ for (String directory : pathValue.split(delimiter, -1)) {
Review Comment:
💬 **Design note:** `path.separator` is read from the activation context to
override the PATH-splitting delimiter. At runtime this always matches
`File.pathSeparator`; the override is primarily for cross-OS unit testing (the
PR description confirms this). The logic is correct but the motivation is
non-obvious. A one-line comment here would prevent future readers from
wondering why a JVM standard property is being re-read as a configurable input:
```suggestion
final var separator = context.getSystemProperty("path.separator");
// path.separator may be overridden in tests to simulate a
non-native host OS;
// at runtime it always matches File.pathSeparator.
final var pathSeparator = separator == null || separator.isEmpty() ?
File.pathSeparator : separator;
final var delimiter =
":".equals(pathSeparator) || ";".equals(pathSeparator) ?
pathSeparator : Pattern.quote(pathSeparator);
for (String directory : pathValue.split(delimiter, -1)) {
```
--
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]