From ee7b427c291788db066c7cfb49ba2ce128d826b0 Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Sat, 18 Jan 2025 16:30:47 +0100 Subject: [PATCH] [HotFix] Fix an issue with Gradle 8.10 and worker threads realising values on Properties (#261) --- .../runs/ide/IdeRunIntegrationManager.java | 24 ++++++++++++------- .../gradle/common/util/run/RunsUtil.java | 19 +++++++++------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/net/neoforged/gradle/common/runs/ide/IdeRunIntegrationManager.java b/common/src/main/java/net/neoforged/gradle/common/runs/ide/IdeRunIntegrationManager.java index 71b4b29e..652e45de 100644 --- a/common/src/main/java/net/neoforged/gradle/common/runs/ide/IdeRunIntegrationManager.java +++ b/common/src/main/java/net/neoforged/gradle/common/runs/ide/IdeRunIntegrationManager.java @@ -187,6 +187,15 @@ private void createIdeaRun(Project project, Run run, RunConfigurationContainer i ideBeforeRunTask.configure(task -> copyProcessResourcesTasks.forEach(task::dependsOn)); } + //We can eagerly initialize here, as we are in an afterEvaluate after the run configuration has happened. + final List jvmArguments = runImpl.realiseJvmArguments(); + final List programArguments = runImpl.getArguments().get(); + final Map productionEnvironment = adaptEnvironment(runImpl, multimapProvider -> RunsUtil.buildRunWithIdeaModClasses(multimapProvider, RunsUtil.IdeaCompileType.Production)); + final Map testEnvironment = adaptEnvironment(runImpl, + multimapProvider -> RunsUtil.buildRunWithIdeaModClasses(multimapProvider, RunsUtil.IdeaCompileType.Production), + multimapProvider -> RunsUtil.buildRunWithIdeaModClasses(multimapProvider, RunsUtil.IdeaCompileType.Test) + ); + //Do not generate a run configuration for unit tests if (!runImpl.getIsJUnit().get()) { ideaRuns.register(runName, Application.class, ideaRun -> { @@ -194,10 +203,10 @@ private void createIdeaRun(Project project, Run run, RunConfigurationContainer i ideaRun.setMainClass(runImpl.getMainClass().get()); ideaRun.setWorkingDirectory(runImpl.getWorkingDirectory().get().getAsFile().getAbsolutePath()); - ideaRun.setJvmArgs(RunsUtil.escapeAndJoin(RunsUtil.deduplicateElementsFollowingEachOther(runImpl.realiseJvmArguments().stream()).toList())); + ideaRun.setJvmArgs(RunsUtil.escapeAndJoin(RunsUtil.deduplicateElementsFollowingEachOther(jvmArguments.stream()).toList())); ideaRun.setModuleName(RunsUtil.getIntellijModuleName(run.getExtensions().getByType(IdeaRunExtension.class).getPrimarySourceSet().get())); - ideaRun.setProgramParameters(RunsUtil.escapeAndJoin(RunsUtil.deduplicateElementsFollowingEachOther(runImpl.getArguments().get().stream()).toList())); - ideaRun.setEnvs(adaptEnvironment(runImpl, multimapProvider -> RunsUtil.buildRunWithIdeaModClasses(multimapProvider, RunsUtil.IdeaCompileType.Production))); + ideaRun.setProgramParameters(RunsUtil.escapeAndJoin(RunsUtil.deduplicateElementsFollowingEachOther(programArguments.stream()).toList())); + ideaRun.setEnvs(productionEnvironment); ideaRun.setShortenCommandLine(ShortenCommandLine.ARGS_FILE); ideaRun.beforeRun(beforeRuns -> { @@ -212,7 +221,7 @@ private void createIdeaRun(Project project, Run run, RunConfigurationContainer i }); } else { ideaRuns.register(runName, JUnitWithBeforeRun.class, ideaRun -> { - final RunsUtil.PreparedUnitTestEnvironment preparedUnitTestEnvironment = RunsUtil.prepareUnitTestEnvironment(run); + final RunsUtil.PreparedUnitTestEnvironment preparedUnitTestEnvironment = RunsUtil.prepareUnitTestEnvironment(run, jvmArguments, programArguments); ideaRun.setWorkingDirectory(runImpl.getWorkingDirectory().get().getAsFile().getAbsolutePath()); ideaRun.setModuleName(RunsUtil.getIntellijModuleName(run.getExtensions().getByType(IdeaRunExtension.class).getPrimarySourceSet().get())); @@ -225,14 +234,11 @@ private void createIdeaRun(Project project, Run run, RunConfigurationContainer i ideaRun.setCategory(runImpl.getTestScope().getCategory().getOrNull()); ideaRun.setWorkingDirectory(runImpl.getWorkingDirectory().get().getAsFile().getAbsolutePath()); - ideaRun.setVmParameters(RunsUtil.escapeAndJoin(runImpl.realiseJvmArguments(), + ideaRun.setVmParameters(RunsUtil.escapeAndJoin(jvmArguments, "-Dfml.junit.argsfile=%s".formatted(preparedUnitTestEnvironment.programArgumentsFile().getAbsolutePath()), "@%s".formatted(preparedUnitTestEnvironment.jvmArgumentsFile().getAbsolutePath()) )); - ideaRun.setEnvs(adaptEnvironment(runImpl, - multimapProvider -> RunsUtil.buildRunWithIdeaModClasses(multimapProvider, RunsUtil.IdeaCompileType.Production), - multimapProvider -> RunsUtil.buildRunWithIdeaModClasses(multimapProvider, RunsUtil.IdeaCompileType.Test)) - ); + ideaRun.setEnvs(testEnvironment); ideaRun.setShortenCommandLine(ShortenCommandLine.ARGS_FILE); ideaRun.beforeRun(beforeRuns -> { diff --git a/common/src/main/java/net/neoforged/gradle/common/util/run/RunsUtil.java b/common/src/main/java/net/neoforged/gradle/common/util/run/RunsUtil.java index 348f5d1e..5e26ddb5 100644 --- a/common/src/main/java/net/neoforged/gradle/common/util/run/RunsUtil.java +++ b/common/src/main/java/net/neoforged/gradle/common/util/run/RunsUtil.java @@ -391,15 +391,16 @@ private static String escape(String arg) { public record PreparedUnitTestEnvironment(File programArgumentsFile, File jvmArgumentsFile) { } - public static PreparedUnitTestEnvironment prepareUnitTestEnvironment(Run run) { - + public static PreparedUnitTestEnvironment prepareUnitTestEnvironment(Run run, + List jvmArguments, + List programArguments) { return new PreparedUnitTestEnvironment( - createArgsFile(run.getWorkingDirectory().file("%s_test_args.txt".formatted(run.getName())), run.getArguments()), - createArgsFile(run.getWorkingDirectory().file("%s_jvm_args.txt".formatted(run.getName())), run.getJvmArguments()) + createArgsFile(run.getWorkingDirectory().file("%s_test_args.txt".formatted(run.getName())), jvmArguments), + createArgsFile(run.getWorkingDirectory().file("%s_jvm_args.txt".formatted(run.getName())), programArguments) ); } - private static File createArgsFile(Provider outputFile, ListProperty inputs) { + private static File createArgsFile(Provider outputFile, List inputs) { final File output = outputFile.get().getAsFile(); if (!output.getParentFile().exists()) { @@ -408,7 +409,7 @@ private static File createArgsFile(Provider outputFile, ListPropert } } try { - final List value = deduplicateElementsFollowingEachOther(inputs.get().stream()).toList(); + final List value = deduplicateElementsFollowingEachOther(inputs.stream()).toList(); if (output.exists()) { if (Files.readAllLines(output.toPath()).equals(value)) { return output; @@ -429,7 +430,11 @@ private static File createArgsFile(Provider outputFile, ListPropert private static void configureTestTask(Project project, TaskProvider testTaskProvider, Run run) { testTaskProvider.configure(testTask -> { - PreparedUnitTestEnvironment preparedEnvironment = prepareUnitTestEnvironment(run); + PreparedUnitTestEnvironment preparedEnvironment = prepareUnitTestEnvironment( + run, + run.getJvmArguments().get(), + run.getArguments().get() + ); addRunSourcesDependenciesToTask(testTask, run, true); testTask.getDependsOn().add(run.getDependsOn());