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
Changes from 1 commit
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
Next Next commit
Do not discover properties resource in the classpath to avoid missing…
… resources registration in native mode
radcortez authored and zakkak committed Jan 14, 2025
commit f08210039f0b4dd5c96b637598130cbfe32de51c
Original file line number Diff line number Diff line change
@@ -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;
@@ -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,
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,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;
@@ -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;
@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

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

If what my previous comment is right, this is a breaking (for quite a corner case I admit) change, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this could be a breaking change if a user has a custom source with a custom resource. If they are registering the resource manually they would need to process the file and generate some sort of representation (like us) for runtime.

Unfortunately, I don't see any good alternative. Having to forcely register all the resources is going to throw away all of our optimizations. If we leave it as is, you get the error that we are trying to prevent. (Well I guess we could add a try / catch there instead).

I still think it doesn't make sense to apply that validation to an API like ClassLoader#getResources. That API never fails in JVM mode, no matter what you are looking for. Granted that you can obtain different results from JVM to native if the registrations are not done properly, but the native image shouldn't try to outsmart the library or integrator by changing API behaviours.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, I don't see any good alternative.

I am personally fine with this, since it's a corner case. I am not even sure we need to document this kind of "breakage".

Copy link
Contributor

Choose a reason for hiding this comment

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

@radcortez I went on with adding a try/catch. Please have a look.

}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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
Original file line number Diff line number Diff line change
@@ -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());
}
}