Skip to content

Commit

Permalink
Limit external configuration sources parsing in native-mode
Browse files Browse the repository at this point in the history
`SmallryeConfigBuilder` tries to access properties files from various
sources, despite them not being available at run-time nor supported in
native-mode, see #41994

This patch also avoids getting a `MissingRegistrationError` when using
`-H:+ThrowMissingRegistrationErrors` or `--exact-reachability-metadata`.

Related to #41994 and
#41995
  • Loading branch information
zakkak committed Jul 25, 2024
1 parent ec8e234 commit fc678ed
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +64,16 @@ void systemOnlySources(BuildProducer<StaticInitConfigBuilderBuildItem> staticIni
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<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(FileSystemOnlySourcesConfigBuilder.class.getName()));
}

private static class SystemOnlySources implements BooleanSupplier {
ConfigBuildTimeConfig configBuildTimeConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -391,6 +392,17 @@ public void setupConfigOverride(

@BuildStep
public void watchConfigFiles(BuildProducer<HotDeploymentWatchedFileBuildItem> watchedFiles) {
for (String configWatchedFile : getConfigFiles()) {
watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile));
}
}

@BuildStep
public NativeImageResourceBuildItem nativeConfigFiles() {
return new NativeImageResourceBuildItem(getConfigFiles());
}

private List<String> getConfigFiles() {
List<String> configWatchedFiles = new ArrayList<>();

SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
Expand Down Expand Up @@ -432,10 +444,7 @@ public void watchConfigFiles(BuildProducer<HotDeploymentWatchedFileBuildItem> wa
}
}
});

for (String configWatchedFile : configWatchedFiles) {
watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile));
}
return configWatchedFiles;
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
@@ -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.withSources(
inFileSystem(Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(),
260, Thread.currentThread().getContextClassLoader()));
}

@Override
public int priority() {
return Integer.MAX_VALUE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.smallrye.config.SmallRyeConfig;

public final class ConfigYamlProcessor {
Expand All @@ -34,10 +35,23 @@ public void yamlConfig(

@BuildStep
void watchYamlConfig(BuildProducer<HotDeploymentWatchedFileBuildItem> watchedFiles) {
for (String configWatchedFile : getConfigWatchedFiles()) {
watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(configWatchedFile));
}
}

@BuildStep
NativeImageResourceBuildItem nativeYamlConfig() {
return new NativeImageResourceBuildItem(getConfigWatchedFiles());
}

private static List<String> getConfigWatchedFiles() {
List<String> 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());
Expand All @@ -53,9 +67,6 @@ void watchYamlConfig(BuildProducer<HotDeploymentWatchedFileBuildItem> 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;
}
}

0 comments on commit fc678ed

Please sign in to comment.