From 628ee635445496819b3fe4b513e4ab824d610608 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Wed, 24 Jul 2024 13:16:57 +0300 Subject: [PATCH] Limit external configuration sources parsing in native-mode `SmallryeConfigBuilder` tries to access properties files from various sources, despite them not being available at run-time nor supported in native-mode, see https://github.com/quarkusio/quarkus/issues/41994 This patch also avoids getting a `MissingRegistrationError` when using `-H:+ThrowMissingRegistrationErrors` or `--exact-reachability-metadata`. Related to https://github.com/quarkusio/quarkus/issues/41994 and https://github.com/quarkusio/quarkus/issues/41995 --- .../deployment/steps/ConfigBuildSteps.java | 14 +++++++- .../steps/ConfigGenerationBuildStep.java | 18 ++++++++--- .../FileSystemOnlySourcesConfigBuilder.java | 22 +++++++++++++ .../yaml/deployment/ConfigYamlProcessor.java | 32 ++++++++++++++++--- .../yaml/runtime/NativeYamlConfigBuilder.java | 12 +++++++ 5 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 core/runtime/src/main/java/io/quarkus/runtime/configuration/FileSystemOnlySourcesConfigBuilder.java create mode 100644 extensions/config-yaml/runtime/src/main/java/io/quarkus/config/yaml/runtime/NativeYamlConfigBuilder.java diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigBuildSteps.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigBuildSteps.java index 478b4b9d13ff4..a34fecb3273a9 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigBuildSteps.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigBuildSteps.java @@ -15,7 +15,9 @@ import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem; import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; +import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; import io.quarkus.deployment.util.ServiceUtil; +import io.quarkus.runtime.configuration.FileSystemOnlySourcesConfigBuilder; import io.quarkus.runtime.configuration.SystemOnlySourcesConfigBuilder; import io.quarkus.runtime.graal.InetRunTime; import io.smallrye.config.ConfigSourceInterceptor; @@ -55,13 +57,23 @@ RuntimeInitializedClassBuildItem runtimeInitializedClass() { return new RuntimeInitializedClassBuildItem(InetRunTime.class.getName()); } - @BuildStep(onlyIf = SystemOnlySources.class) + @BuildStep(onlyIf = SystemOnlySources.class, onlyIfNot = NativeOrNativeSourcesBuild.class) void systemOnlySources(BuildProducer staticInitConfigBuilder, BuildProducer runTimeConfigBuilder) { staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(SystemOnlySourcesConfigBuilder.class.getName())); runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(SystemOnlySourcesConfigBuilder.class.getName())); } + /** + * Limit external configuration sources parsing for native executables since they are not supported see + * https://github.com/quarkusio/quarkus/issues/41994 + */ + @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) + void nativeNoSources(BuildProducer staticInitConfigBuilder, + BuildProducer runTimeConfigBuilder) { + runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(FileSystemOnlySourcesConfigBuilder.class.getName())); + } + private static class SystemOnlySources implements BooleanSupplier { ConfigBuildTimeConfig configBuildTimeConfig; diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java index feb22a65981b2..7f3078ee0cf0e 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java @@ -58,6 +58,7 @@ import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem; import io.quarkus.deployment.builditem.SuppressNonRuntimeConfigChangedWarningBuildItem; +import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveMethodBuildItem; import io.quarkus.deployment.configuration.BuildTimeConfigurationReader; @@ -391,6 +392,17 @@ public void setupConfigOverride( @BuildStep public void watchConfigFiles(BuildProducer watchedFiles) { + for (String configWatchedFile : getConfigFiles()) { + watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile)); + } + } + + @BuildStep + public NativeImageResourceBuildItem nativeConfigFiles() { + return new NativeImageResourceBuildItem(getConfigFiles()); + } + + private List getConfigFiles() { List configWatchedFiles = new ArrayList<>(); SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); @@ -432,10 +444,7 @@ public void watchConfigFiles(BuildProducer wa } } }); - - for (String configWatchedFile : configWatchedFiles) { - watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile)); - } + return configWatchedFiles; } @BuildStep @@ -676,7 +685,6 @@ private static Set discoverService( // The discovery includes deployment modules, so we only include services available at runtime if (QuarkusClassLoader.isClassPresentAtRuntime(service)) { services.add(service); - reflectiveClass.produce(ReflectiveClassBuildItem.builder(service).build()); } } return services; diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/FileSystemOnlySourcesConfigBuilder.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/FileSystemOnlySourcesConfigBuilder.java new file mode 100644 index 0000000000000..2940b3b80af44 --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/FileSystemOnlySourcesConfigBuilder.java @@ -0,0 +1,22 @@ +package io.quarkus.runtime.configuration; + +import static io.smallrye.config.PropertiesConfigSourceLoader.inFileSystem; + +import java.nio.file.Paths; + +import io.smallrye.config.SmallRyeConfigBuilder; + +public class FileSystemOnlySourcesConfigBuilder implements ConfigBuilder { + @Override + public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) { + return builder.setAddDefaultSources(false).addSystemSources().withSources( + inFileSystem(Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(), + 260, builder.getClassLoader())); + } + + @Override + public int priority() { + return Integer.MAX_VALUE; + } + +} diff --git a/extensions/config-yaml/deployment/src/main/java/io/quarkus/config/yaml/deployment/ConfigYamlProcessor.java b/extensions/config-yaml/deployment/src/main/java/io/quarkus/config/yaml/deployment/ConfigYamlProcessor.java index c25fab1f0f91a..162ac2199c181 100644 --- a/extensions/config-yaml/deployment/src/main/java/io/quarkus/config/yaml/deployment/ConfigYamlProcessor.java +++ b/extensions/config-yaml/deployment/src/main/java/io/quarkus/config/yaml/deployment/ConfigYamlProcessor.java @@ -6,6 +6,7 @@ import org.eclipse.microprofile.config.ConfigProvider; +import io.quarkus.config.yaml.runtime.NativeYamlConfigBuilder; import io.quarkus.config.yaml.runtime.YamlConfigBuilder; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; @@ -14,6 +15,8 @@ import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem; import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem; import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem; +import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; +import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; import io.smallrye.config.SmallRyeConfig; public final class ConfigYamlProcessor { @@ -23,7 +26,7 @@ public FeatureBuildItem feature() { return new FeatureBuildItem(Feature.CONFIG_YAML); } - @BuildStep + @BuildStep(onlyIfNot = NativeOrNativeSourcesBuild.class) public void yamlConfig( BuildProducer staticInitConfigBuilder, BuildProducer runTimeConfigBuilder) { @@ -32,12 +35,34 @@ public void yamlConfig( runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlConfigBuilder.class)); } + /** + * Limit external configuration sources parsing for native executables since they are not supported see + * https://github.com/quarkusio/quarkus/issues/41994 + */ + @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) + void nativeNoSources(BuildProducer runTimeConfigBuilder) { + runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(NativeYamlConfigBuilder.class)); + } + @BuildStep void watchYamlConfig(BuildProducer watchedFiles) { + for (String configWatchedFile : getConfigWatchedFiles()) { + watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile)); + } + } + + @BuildStep + NativeImageResourceBuildItem nativeYamlConfig() { + return new NativeImageResourceBuildItem(getConfigWatchedFiles()); + } + + private static List getConfigWatchedFiles() { List configWatchedFiles = new ArrayList<>(); String userDir = System.getProperty("user.dir"); // Main files + configWatchedFiles.add("META-INF/microprofile-config.yaml"); + configWatchedFiles.add("META-INF/microprofile-config.yml"); configWatchedFiles.add("application.yaml"); configWatchedFiles.add("application.yml"); configWatchedFiles.add(Paths.get(userDir, "config", "application.yaml").toAbsolutePath().toString()); @@ -53,9 +78,6 @@ void watchYamlConfig(BuildProducer watchedFil configWatchedFiles.add( Paths.get(userDir, "config", String.format("application-%s.yml", profile)).toAbsolutePath().toString()); } - - for (String configWatchedFile : configWatchedFiles) { - watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile)); - } + return configWatchedFiles; } } diff --git a/extensions/config-yaml/runtime/src/main/java/io/quarkus/config/yaml/runtime/NativeYamlConfigBuilder.java b/extensions/config-yaml/runtime/src/main/java/io/quarkus/config/yaml/runtime/NativeYamlConfigBuilder.java new file mode 100644 index 0000000000000..3b8dd069511d7 --- /dev/null +++ b/extensions/config-yaml/runtime/src/main/java/io/quarkus/config/yaml/runtime/NativeYamlConfigBuilder.java @@ -0,0 +1,12 @@ +package io.quarkus.config.yaml.runtime; + +import io.quarkus.runtime.configuration.ConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.source.yaml.YamlConfigSourceLoader; + +public class NativeYamlConfigBuilder implements ConfigBuilder { + @Override + public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) { + return builder.withSources(new YamlConfigSourceLoader.InFileSystem()); + } +}