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 2 commits 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 @@ -88,6 +88,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 @@ -168,6 +169,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.
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true?

In oracle/graal#10264 (reply in thread) we describe a scenario were using the classpath would work fine (assuming the user registers the resource for reflection and sets the ordinals accordingly).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With our stuff yes, we never registered the resources. The configuration is just included in a class file and we discard the resources, which may cause the issue described in #44652 (comment), but this has been an issue since day one and until now we never got any complaints about it.

*/
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,14 @@
package io.quarkus.runtime.configuration;

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

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;
import org.graalvm.nativeimage.MissingReflectionRegistrationError;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
Expand All @@ -9,9 +17,11 @@
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;
import io.smallrye.config._private.ConfigMessages;

/**
*/
Expand Down Expand Up @@ -99,4 +109,52 @@ public byte[] getClassBytes() {
return null;
}
}

@TargetClass(value = AbstractLocationConfigSourceLoader.class)
static final class Target_AbstractLocationConfigSourceLoader {
@Alias
protected native List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader);

@Alias
protected native List<ConfigSource> tryFileSystem(final URI uri, final int ordinal);

@Alias
protected native List<ConfigSource> tryJar(final URI uri, final int ordinal);

@Alias
protected native List<ConfigSource> tryHttpResource(final URI uri, final int ordinal);

@Alias
private static Converter<URI> URI_CONVERTER = null;

@Substitute
protected List<ConfigSource> loadConfigSources(final String[] locations, final int ordinal,
final ClassLoader classLoader) {
if (locations == null || locations.length == 0) {
return Collections.emptyList();
}

final List<ConfigSource> configSources = new ArrayList<>();
for (String location : locations) {
final URI uri = URI_CONVERTER.convert(location);
if (uri.getScheme() == null) {
configSources.addAll(tryFileSystem(uri, ordinal));
try {
configSources.addAll(tryClassPath(uri, ordinal, classLoader));
} catch (MissingReflectionRegistrationError e) {
// ignore
}
} else if (uri.getScheme().equals("file")) {
configSources.addAll(tryFileSystem(uri, ordinal));
} else if (uri.getScheme().equals("jar")) {
configSources.addAll(tryJar(uri, ordinal));
} else if (uri.getScheme().startsWith("http")) {
configSources.addAll(tryHttpResource(uri, ordinal));
} else {
throw ConfigMessages.msg.schemeNotSupported(uri.getScheme());
}
}
return configSources;
}
}
}
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