-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[MNG-8768] Add executable() function for conditional profile activation based on PATH #12332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hiteshsai007
wants to merge
2
commits into
apache:master
Choose a base branch
from
Hiteshsai007:mng-8768-executable-profile-activation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ExecutableFinder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.maven.impl.model.profile; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Locale; | ||
|
|
||
| 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> | ||
| * | ||
| * @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 ExecutableFinder() {} | ||
|
|
||
| /** | ||
| * Returns {@code true} when {@code name} resolves to an executable file. | ||
| * | ||
| * @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 | ||
| */ | ||
| 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); | ||
| return isExecutableFile(candidate, isWindows); | ||
| } | ||
|
|
||
| // --- plain name: search PATH --- | ||
| String pathValue = getPathValue(context); | ||
| if (pathValue == null || pathValue.isBlank()) { | ||
| return false; | ||
| } | ||
|
|
||
| String[] dirs = pathValue.split(File.pathSeparator, -1); | ||
| for (String dir : dirs) { | ||
| if (dir.isBlank()) { | ||
| continue; | ||
| } | ||
| Path base = Path.of(dir).resolve(name); | ||
| if (isExecutableFile(base, isWindows)) { | ||
| return true; | ||
| } | ||
| // On Windows also try known executable extensions (unless already present). | ||
| if (isWindows && !hasWindowsExtension(name)) { | ||
| for (String ext : WINDOWS_EXTENSIONS) { | ||
| Path withExt = Path.of(dir).resolve(name + ext); | ||
| if (isExecutableFile(withExt, isWindows)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // Package-private helpers (visible to tests) | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Retrieves the PATH value from the activation context. | ||
| * | ||
| * <p>Maven places env vars in system properties as {@code env.<NAME>}. | ||
| * On Windows, env var names are normalised to upper-case (e.g. {@code env.PATH}). | ||
| * | ||
| * @param context the profile activation context | ||
| * @return the raw PATH string, or {@code null} if not available | ||
| */ | ||
| static String getPathValue(ProfileActivationContext context) { | ||
| return context.getSystemProperty(ENV_PATH_KEY); | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // Private utilities | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| private static boolean isWindows(ProfileActivationContext context) { | ||
| String osName = context.getSystemProperty("os.name"); | ||
| return osName != null && osName.toLowerCase(Locale.ROOT).contains("windows"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} if {@code path} is a regular file that the JVM considers executable. | ||
| * On Windows, any regular file is treated as potentially executable (the OS itself uses the | ||
| * extension to decide); the {@link Files#isExecutable} check is still applied so that | ||
| * read-only / locked files are excluded. | ||
| */ | ||
| private static boolean isExecutableFile(Path path, boolean isWindows) { | ||
| if (!Files.isRegularFile(path)) { | ||
| return false; | ||
| } | ||
| // On Windows Files.isExecutable() always returns true for regular files – that is fine | ||
| // because we are already filtering by extension in the caller. On Unix we rely on the | ||
| // execute bit. | ||
| return isWindows || Files.isExecutable(path); | ||
| } | ||
|
|
||
| private static boolean hasWindowsExtension(String name) { | ||
| String lower = name.toLowerCase(Locale.ROOT); | ||
| for (String ext : WINDOWS_EXTENSIONS) { | ||
| if (lower.endsWith(ext)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -490,4 +490,80 @@ protected ProfileActivationContext newFileContext(Path path) { | |
| protected ProfileActivationContext newFileContext() { | ||
| return newFileContext(tempDir); | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // executable() tests (MNG-8768) | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Puts a fake executable into a temporary directory and activates a profile only when | ||
| * that directory is on the PATH – confirming the PATH-search logic end-to-end. | ||
| */ | ||
| @Test | ||
| void testExecutablePresentInPath() throws Exception { | ||
| // Create a fake executable in the temp dir | ||
| Path fakeExec = tempDir.resolve("my-fake-tool"); | ||
| Files.createFile(fakeExec); | ||
| // Make it executable on POSIX systems; on Windows Files.isExecutable() returns true anyway | ||
| fakeExec.toFile().setExecutable(true); | ||
|
|
||
| String pathValue = tempDir.toAbsolutePath().toString(); | ||
| Map<String, String> sysProps = Map.of("env.PATH", pathValue); | ||
|
|
||
| Profile profile = newProfile("executable('my-fake-tool')"); | ||
| assertActivation(true, profile, newContext(null, sysProps)); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that the function returns false for a name that is definitely not on PATH. | ||
| */ | ||
| @Test | ||
| void testExecutableNotInPath() { | ||
| // Use an empty/nonexistent PATH so that nothing can be found | ||
| Map<String, String> sysProps = Map.of("env.PATH", ""); | ||
|
|
||
| Profile profile = newProfile("executable('this-tool-does-not-exist-anywhere-42')"); | ||
| assertActivation(false, profile, newContext(null, sysProps)); | ||
| } | ||
|
|
||
| /** | ||
| * An absolute path to an existing executable file must resolve to true. | ||
| */ | ||
| @Test | ||
| void testExecutableWithAbsolutePath() throws Exception { | ||
| Path fakeExec = tempDir.resolve("abs-tool"); | ||
| Files.createFile(fakeExec); | ||
| fakeExec.toFile().setExecutable(true); | ||
|
|
||
| String absPath = fakeExec.toAbsolutePath().toString().replace('\\', '/'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| Profile profile = newProfile("executable('" + absPath + "')"); | ||
|
|
||
| // PATH content does not matter for absolute paths | ||
| assertActivation(true, profile, newContext(null, Map.of("env.PATH", ""))); | ||
| } | ||
|
|
||
| /** | ||
| * An absolute path to a non-existent file must resolve to false. | ||
| */ | ||
| @Test | ||
| void testExecutableAbsolutePathMissing() { | ||
| Profile profile = newProfile("executable('/no/such/executable/path/42/bin/tool')"); | ||
| assertActivation(false, profile, newContext(null, Map.of("env.PATH", ""))); | ||
| } | ||
|
|
||
| /** | ||
| * not(executable(...)) must invert the result correctly. | ||
| */ | ||
| @Test | ||
| void testExecutableNegated() throws Exception { | ||
| Path fakeExec = tempDir.resolve("neg-tool"); | ||
| Files.createFile(fakeExec); | ||
| fakeExec.toFile().setExecutable(true); | ||
|
|
||
| String pathValue = tempDir.toAbsolutePath().toString(); | ||
| Map<String, String> sysProps = Map.of("env.PATH", pathValue); | ||
|
|
||
| assertActivation(false, newProfile("not(executable('neg-tool'))"), newContext(null, sysProps)); | ||
| assertActivation(true, newProfile("not(executable('no-such-neg-tool'))"), newContext(null, sysProps)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment above (lines 136-138) says "we are already filtering by extension in the caller" — but that's only true for the PATH-search branch (lines 88-95). For direct paths (line 69), the caller doesn't filter by extension, so on Windows
isExecutableFilereturnstruefor any regular file regardless of extension.Also, for direct paths on Windows, the extension probing (
name + ".exe", etc.) is not applied. Consider whetherexecutable('/path/to/tool')on Windows should also try/path/to/tool.exe.