Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not discover properties resource in the classpath to avoid missing resources registration in native mode #44910

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import io.quarkus.runtime.configuration.ConfigDiagnostic;
import io.quarkus.runtime.configuration.ConfigRecorder;
import io.quarkus.runtime.configuration.DisableableConfigSource;
import io.quarkus.runtime.configuration.NativeConfigBuilder;
import io.quarkus.runtime.configuration.QuarkusConfigValue;
import io.quarkus.runtime.configuration.RuntimeConfigBuilder;
import io.quarkus.runtime.configuration.RuntimeOverrideConfigSource;
Expand Down Expand Up @@ -162,6 +163,14 @@ void buildTimeRunTimeConfig(
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(builderClassName));
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void nativeConfig(
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(NativeConfigBuilder.class.getName()));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(NativeConfigBuilder.class.getName()));
}

@BuildStep(onlyIfNot = { IsNormal.class }) // for dev or test
void runtimeOverrideConfig(
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.runtime.configuration;

import static io.smallrye.config.PropertiesConfigSourceLoader.inFileSystem;

import java.nio.file.Paths;

import io.smallrye.config.SmallRyeConfigBuilder;

/**
* Do not register classpath resources lookup in native mode to avoid missing resources registration errors, which
* became a strict check during native image execution.
* <p>
* Configuration coming from classpath resources is recoded during build time in a low ordinal source, so the
* configuration will still be available. If the users change the ordinals of the sources in runtime, it may
* cause unexpected values to be returned by the config, but this has always been the case. The classpath configuration
* resources were never registered in the native image.
*/
public class NativeConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
builder
.setAddDefaultSources(false)
.setAddSystemSources(true)
.setAddPropertiesSources(false);

builder.withSources(inFileSystem(
Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(), 260,
builder.getClassLoader()));

return builder;
}

@Override
public int priority() {
return Integer.MIN_VALUE + 100;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.quarkus.runtime.configuration;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
Expand All @@ -9,6 +14,7 @@
import com.oracle.svm.core.annotate.TargetElement;

import io.smallrye.common.constraint.Assert;
import io.smallrye.config.AbstractLocationConfigSourceLoader;
import io.smallrye.config.ConfigMappingInterface;
import io.smallrye.config.ConfigMappingLoader;
import io.smallrye.config.ConfigMappingMetadata;
Expand Down Expand Up @@ -99,4 +105,12 @@ public byte[] getClassBytes() {
return null;
}
}

@TargetClass(value = AbstractLocationConfigSourceLoader.class)
static final class Target_AbstractLocationConfigSourceLoader {
@Substitute
protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
public class SystemOnlySourcesConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
return builder.setAddDefaultSources(false).addSystemSources();
return builder.setAddDefaultSources(false)
.setAddPropertiesSources(false)
.addSystemSources();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.config.yaml.runtime.YamlConfigBuilder;
import io.quarkus.config.yaml.runtime.YamlInClassPathConfigBuilder;
import io.quarkus.config.yaml.runtime.YamlInFileSystemConfigBuilder;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
import io.quarkus.deployment.pkg.NativeConfig;
import io.smallrye.config.SmallRyeConfig;

public final class ConfigYamlProcessor {
Expand All @@ -24,12 +26,18 @@ public FeatureBuildItem feature() {
}

@BuildStep
public void yamlConfig(
void yamlConfig(
NativeConfig nativeConfig,
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {

staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(YamlConfigBuilder.class));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlConfigBuilder.class));
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(YamlInFileSystemConfigBuilder.class));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlInFileSystemConfigBuilder.class));

if (!nativeConfig.enabled()) {
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(YamlInClassPathConfigBuilder.class));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlInClassPathConfigBuilder.class));
}
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.source.yaml.YamlConfigSourceLoader;

public class YamlConfigBuilder implements ConfigBuilder {
/**
* Only for JVM mode.
*
* @see io.quarkus.runtime.configuration.RuntimeConfigBuilder
*/
public class YamlInClassPathConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
return builder.withSources(new YamlConfigSourceLoader.InFileSystem())
.withSources(new YamlConfigSourceLoader.InClassPath());
return builder.withSources(new YamlConfigSourceLoader.InClassPath());
}

@Override
public int priority() {
return Integer.MIN_VALUE + 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.config.yaml.runtime;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

import io.quarkus.runtime.configuration.ConfigBuilder;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.source.yaml.YamlConfigSourceLoader;

public class YamlInFileSystemConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
// Removes Yaml source providers added by the ServiceLoader to avoid double registration
List<ConfigSourceProvider> toRemove = new ArrayList<>();
for (ConfigSourceProvider sourceProvider : builder.getSourceProviders()) {
if (sourceProvider instanceof YamlConfigSourceLoader) {
toRemove.add(sourceProvider);
}
}
builder.getSourceProviders().removeAll(toRemove);
return builder.withSources(new YamlConfigSourceLoader.InFileSystem());
}
}
Loading