Skip to content

Commit 91e3d5c

Browse files
authored
Remove use of toRealPath (#11250) (#11257)
As this makes us "escape" from paths that are symbolic links, and also causes inconsistencies among paths (like maven home, and system settings and system toolchains). Backport of 92d53cb
1 parent 08325be commit 91e3d5c

File tree

7 files changed

+24
-33
lines changed

7 files changed

+24
-33
lines changed

compat/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,11 +1723,7 @@ private static String stripLeadingAndTrailingQuotes(String str) {
17231723
}
17241724

17251725
private static Path getCanonicalPath(Path path) {
1726-
try {
1727-
return path.toRealPath();
1728-
} catch (IOException e) {
1729-
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
1730-
}
1726+
return path.toAbsolutePath().normalize();
17311727
}
17321728

17331729
static class ExitException extends Exception {

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CliUtils.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.maven.cling.invoker;
2020

21-
import java.io.IOException;
2221
import java.nio.file.Path;
2322
import java.util.HashMap;
2423
import java.util.Map;
@@ -60,11 +59,7 @@ public static String stripLeadingAndTrailingQuotes(String str) {
6059
@Nonnull
6160
public static Path getCanonicalPath(Path path) {
6261
requireNonNull(path, "path");
63-
try {
64-
return path.toRealPath();
65-
} catch (IOException e) {
66-
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
67-
}
62+
return path.toAbsolutePath().normalize();
6863
}
6964

7065
@Nonnull

impl/maven-executor/src/main/java/org/apache/maven/api/cli/ExecutorRequest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.maven.api.cli;
2020

21-
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.io.OutputStream;
2423
import java.nio.file.Path;
@@ -401,9 +400,13 @@ private Impl(
401400
this.cwd = getCanonicalPath(requireNonNull(cwd));
402401
this.installationDirectory = getCanonicalPath(requireNonNull(installationDirectory));
403402
this.userHomeDirectory = getCanonicalPath(requireNonNull(userHomeDirectory));
404-
this.jvmSystemProperties = jvmSystemProperties != null ? Map.copyOf(jvmSystemProperties) : null;
405-
this.environmentVariables = environmentVariables != null ? Map.copyOf(environmentVariables) : null;
406-
this.jvmArguments = jvmArguments != null ? List.copyOf(jvmArguments) : null;
403+
this.jvmSystemProperties = jvmSystemProperties != null && !jvmSystemProperties.isEmpty()
404+
? Map.copyOf(jvmSystemProperties)
405+
: null;
406+
this.environmentVariables = environmentVariables != null && !environmentVariables.isEmpty()
407+
? Map.copyOf(environmentVariables)
408+
: null;
409+
this.jvmArguments = jvmArguments != null && !jvmArguments.isEmpty() ? List.copyOf(jvmArguments) : null;
407410
this.stdIn = stdIn;
408411
this.stdOut = stdOut;
409412
this.stdErr = stdErr;
@@ -510,10 +513,6 @@ static Path discoverUserHomeDirectory() {
510513
@Nonnull
511514
static Path getCanonicalPath(Path path) {
512515
requireNonNull(path, "path");
513-
try {
514-
return path.toRealPath();
515-
} catch (IOException e) {
516-
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
517-
}
516+
return path.toAbsolutePath().normalize();
518517
}
519518
}

impl/maven-executor/src/main/java/org/apache/maven/cling/executor/embedded/EmbeddedMavenExecutor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,12 @@ protected Context doCreate(Path mavenHome, ExecutorRequest executorRequest) {
208208
getClass().getSimpleName() + " does not support command " + executorRequest.command());
209209
}
210210
if (executorRequest.environmentVariables().isPresent()) {
211-
throw new IllegalArgumentException(getClass().getSimpleName() + " does not support environment variables");
211+
throw new IllegalArgumentException(getClass().getSimpleName() + " does not support environment variables: "
212+
+ executorRequest.environmentVariables().get());
212213
}
213214
if (executorRequest.jvmArguments().isPresent()) {
214-
throw new IllegalArgumentException(getClass().getSimpleName() + " does not support jvmArguments");
215+
throw new IllegalArgumentException(getClass().getSimpleName() + " does not support jvmArguments: "
216+
+ executorRequest.jvmArguments().get());
215217
}
216218
Path boot = mavenHome.resolve("boot");
217219
Path m2conf = mavenHome.resolve("bin/m2.conf");

impl/maven-executor/src/main/java/org/apache/maven/cling/executor/internal/HelperImpl.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package org.apache.maven.cling.executor.internal;
2020

2121
import java.nio.file.Path;
22-
import java.util.Collections;
2322
import java.util.HashMap;
2423
import java.util.concurrent.ConcurrentHashMap;
2524

@@ -94,8 +93,7 @@ protected Executor getExecutor(Mode mode, ExecutorRequest request) throws Execut
9493
}
9594

9695
private Executor getExecutorByRequest(ExecutorRequest request) {
97-
if (request.environmentVariables().orElse(Collections.emptyMap()).isEmpty()
98-
&& request.jvmArguments().orElse(Collections.emptyList()).isEmpty()) {
96+
if (request.environmentVariables().isEmpty() && request.jvmArguments().isEmpty()) {
9997
return getExecutor(Mode.EMBEDDED, request);
10098
} else {
10199
return getExecutor(Mode.FORKED, request);

impl/maven-impl/src/main/java/org/apache/maven/impl/model/rootlocator/DefaultRootLocator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ protected Optional<Path> getRootDirectoryFallback() {
9898
}
9999

100100
protected Path getCanonicalPath(Path path) {
101-
try {
102-
return path.toRealPath();
103-
} catch (IOException e) {
104-
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
105-
}
101+
return path.toAbsolutePath().normalize();
106102
}
107103
}

its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8181CentralRepoTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ public void testitModel() throws Exception {
4747
verifier.addCliArgument("--settings=settings.xml");
4848
verifier.addCliArgument("-Dmaven.repo.local=" + testDir.toPath().resolve("target/local-repo"));
4949
verifier.addCliArgument("-Dmaven.repo.local.tail=target/null");
50-
verifier.addCliArgument("-Dmaven.repo.central=http://repo1.maven.org/");
50+
// note: intentionally bad URL, we just want tu ensure that this bad URL is used
51+
verifier.addCliArgument("-Dmaven.repo.central=https://repo1.maven.org");
5152
verifier.addCliArgument("validate");
52-
verifier.setHandleLocalRepoTail(false); // we want isolation to have Maven fail due non-HTTPS repo
53+
verifier.setHandleLocalRepoTail(false); // we want isolation to have Maven fail due bad URL
5354
assertThrows(VerificationException.class, verifier::execute);
54-
verifier.verifyTextInLog("central (http://repo1.maven.org/, default, releases)");
55+
// error is
56+
// PluginResolutionException: Plugin eu.maveniverse.maven.mimir:extension3:XXX or one of its dependencies could
57+
// not be resolved:
58+
// Could not find artifact eu.maveniverse.maven.mimir:extension3:jar:XXX in central (https://repo1.maven.org)
59+
verifier.verifyTextInLog("central (https://repo1.maven.org)");
5560
}
5661
}

0 commit comments

Comments
 (0)