From 0f9332a0fb64bb11f39fbeb30697c3fe3aafb5a5 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 19 May 2021 21:11:46 +0200 Subject: [PATCH] Make sure the current directory is changed when processing a new maven request, #397 --- .../org/apache/maven/cli/DaemonMavenCli.java | 17 ++++++++-- .../apache/maven/cli/DaemonMavenCliTest.java | 33 +++++++++++++++++++ .../org/mvndaemon/mvnd/nativ/CLibrary.java | 2 ++ .../org/mvndaemon/mvnd/sync/IpcClient.java | 19 ++++++++--- 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 daemon/src/test/java/org/apache/maven/cli/DaemonMavenCliTest.java diff --git a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java index 6d1fdb0dc..9db5baf06 100644 --- a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java +++ b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java @@ -206,7 +206,7 @@ public int main(List arguments, try { CliRequest req = new CliRequest(null, null); req.args = arguments.toArray(new String[0]); - req.workingDirectory = workingDirectory; + req.workingDirectory = new File(workingDirectory).getCanonicalPath(); req.multiModuleProjectDirectory = new File(projectDirectory); return doMain(req, clientEnv); } finally { @@ -684,8 +684,7 @@ private void environment(String workingDir, Map clientEnv) { CLibrary.setenv(key, vr); } setEnv(clientEnv); - CLibrary.chdir(workingDir); - System.setProperty("user.dir", workingDir); + chDir(workingDir); } catch (Exception e) { slf4jLogger.warn("Environment mismatch ! Could not set the environment (" + e + ")"); slf4jLogger.warn("A few environment mismatches have been detected between the client and the daemon."); @@ -699,6 +698,18 @@ private void environment(String workingDir, Map clientEnv) { } } + protected static void chDir(String workingDir) throws Exception { + CLibrary.chdir(workingDir); + System.setProperty("user.dir", workingDir); + Class fileClass = Class.forName("java.io.File"); + Field fsField = fileClass.getDeclaredField("fs"); + fsField.setAccessible(true); + Object fs = fsField.get(null); + Field userDirField = fs.getClass().getDeclaredField("userDir"); + userDirField.setAccessible(true); + userDirField.set(fs, workingDir); + } + @SuppressWarnings("unchecked") protected static void setEnv(Map newenv) throws Exception { try { diff --git a/daemon/src/test/java/org/apache/maven/cli/DaemonMavenCliTest.java b/daemon/src/test/java/org/apache/maven/cli/DaemonMavenCliTest.java new file mode 100644 index 000000000..cc2f79fa8 --- /dev/null +++ b/daemon/src/test/java/org/apache/maven/cli/DaemonMavenCliTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed 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.cli; + +import java.io.File; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DaemonMavenCliTest { + + @Test + void testChdir() throws Exception { + File d = new File("target/tstDir").getAbsoluteFile(); + d.mkdirs(); + DaemonMavenCli.chDir(d.toString()); + assertEquals(new File(d, "test").getAbsolutePath(), new File("test").getAbsolutePath()); + } + +} diff --git a/native/src/main/java/org/mvndaemon/mvnd/nativ/CLibrary.java b/native/src/main/java/org/mvndaemon/mvnd/nativ/CLibrary.java index 6ac1b8032..d2b1e75e0 100644 --- a/native/src/main/java/org/mvndaemon/mvnd/nativ/CLibrary.java +++ b/native/src/main/java/org/mvndaemon/mvnd/nativ/CLibrary.java @@ -15,6 +15,8 @@ */ package org.mvndaemon.mvnd.nativ; +import java.lang.reflect.Field; + /** * Interface to access some low level POSIX functions, loaded by * HawtJNI Runtime diff --git a/sync/src/main/java/org/mvndaemon/mvnd/sync/IpcClient.java b/sync/src/main/java/org/mvndaemon/mvnd/sync/IpcClient.java index 2fb4c93f5..7fb718566 100644 --- a/sync/src/main/java/org/mvndaemon/mvnd/sync/IpcClient.java +++ b/sync/src/main/java/org/mvndaemon/mvnd/sync/IpcClient.java @@ -39,7 +39,8 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -144,18 +145,28 @@ Socket createClient() throws IOException { .redirectError(discard) .start(); - Future future = ForkJoinPool.commonPool().submit(() -> { + ExecutorService es = Executors.newSingleThreadExecutor(); + Future future = es.submit(() -> { Socket s = ss.accept(); DataInputStream dis = new DataInputStream(s.getInputStream()); int rand2 = dis.readInt(); int port2 = dis.readInt(); return new int[] { rand2, port2 }; }); - int[] res = future.get(5, TimeUnit.SECONDS); + int[] res; + try { + res = future.get(5, TimeUnit.SECONDS); + } catch (Exception e) { + process.destroyForcibly(); + throw e; + } finally { + es.shutdownNow(); + ss.close(); + } if (rand != res[0]) { process.destroyForcibly(); + throw new IllegalStateException("IpcServer did not respond with the correct random"); } - ss.close(); int port = res[1]; Socket socket = new Socket();