From a225258ebfef642b56bb562db7d9ba819ece93a1 Mon Sep 17 00:00:00 2001 From: Daniel Chao Date: Thu, 27 Feb 2025 12:54:49 -0800 Subject: [PATCH] Make Truffle use fallback runtime in Gradle plugin (#995) Using native libraries in Gradle plugins is problematic; this adds some flags that causes Truffle to use a fallback runtime that avoids loading native libraries. --- .../java/org/pkl/gradle/task/BasePklTask.java | 26 +++++++++++++++++-- .../java/org/pkl/gradle/task/ModulesTask.java | 4 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkl-gradle/src/main/java/org/pkl/gradle/task/BasePklTask.java b/pkl-gradle/src/main/java/org/pkl/gradle/task/BasePklTask.java index 08ba07f6..0e23d446 100644 --- a/pkl-gradle/src/main/java/org/pkl/gradle/task/BasePklTask.java +++ b/pkl-gradle/src/main/java/org/pkl/gradle/task/BasePklTask.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,11 @@ import org.pkl.core.util.Nullable; import org.pkl.gradle.utils.PluginUtils; public abstract class BasePklTask extends DefaultTask { + private static final String TRUFFLE_USE_FALLBACK_RUNTIME_FLAG = "truffle.UseFallbackRuntime"; + + private static final String POLYGLOT_WARN_INTERPRETER_ONLY_FLAG = + "polyglot.engine.WarnInterpreterOnly"; + @Input public abstract ListProperty getAllowedModules(); @@ -137,9 +142,26 @@ public abstract class BasePklTask extends DefaultTask { @Optional public abstract ListProperty getHttpNoProxy(); + /** + * There are issues with using native libraries in Gradle plugins. As a workaround for now, make + * Truffle use an un-optimized runtime. + * + * @see https://discuss.gradle.org/t/loading-a-native-library-in-a-gradle-plugin/44854 + * @see https://github.com/apple/pkl/issues/988 + */ + // TODO: Remove this workaround when ugprading to Truffle 24.2+ (Truffle automatically falls back + // in this scenario). + protected void withFallbackTruffleRuntime(Runnable task) { + System.setProperty(TRUFFLE_USE_FALLBACK_RUNTIME_FLAG, "true"); + System.setProperty(POLYGLOT_WARN_INTERPRETER_ONLY_FLAG, "false"); + task.run(); + } + @TaskAction public void runTask() { - doRunTask(); + withFallbackTruffleRuntime(this::doRunTask); } protected abstract void doRunTask(); diff --git a/pkl-gradle/src/main/java/org/pkl/gradle/task/ModulesTask.java b/pkl-gradle/src/main/java/org/pkl/gradle/task/ModulesTask.java index 10a93b94..4ec12481 100644 --- a/pkl-gradle/src/main/java/org/pkl/gradle/task/ModulesTask.java +++ b/pkl-gradle/src/main/java/org/pkl/gradle/task/ModulesTask.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -133,7 +133,7 @@ public abstract class ModulesTask extends BasePklTask { if (getCliBaseOptions().getNormalizedSourceModules().isEmpty()) { throw new InvalidUserDataException("No source modules specified."); } - doRunTask(); + withFallbackTruffleRuntime(this::doRunTask); } @Internal