-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Config: injection verification during the native image build #36169
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.arc.processor.DotNames; | ||
import io.quarkus.arc.processor.InjectionPointInfo; | ||
import io.quarkus.arc.runtime.NativeBuildConfigCheck; | ||
import io.quarkus.arc.runtime.NativeBuildConfigCheckInterceptor; | ||
import io.quarkus.arc.runtime.NativeBuildConfigContext; | ||
import io.quarkus.arc.runtime.NativeBuildConfigContextCreator; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.builditem.ConfigurationBuildItem; | ||
import io.quarkus.deployment.pkg.steps.NativeBuild; | ||
|
||
public class NativeBuildConfigSteps { | ||
|
||
@BuildStep(onlyIf = NativeBuild.class) | ||
SyntheticBeanBuildItem registerNativeBuildConfigContext(ConfigurationBuildItem config, | ||
BeanDiscoveryFinishedBuildItem beanDiscoveryFinished) { | ||
|
||
// Collect all @ConfigProperty injection points | ||
Set<String> injectedProperties = new HashSet<>(); | ||
for (InjectionPointInfo injectionPoint : beanDiscoveryFinished.getInjectionPoints()) { | ||
if (injectionPoint.hasDefaultedQualifier()) { | ||
continue; | ||
} | ||
AnnotationInstance configProperty = injectionPoint.getRequiredQualifier(ConfigBuildStep.MP_CONFIG_PROPERTY_NAME); | ||
if (configProperty != null) { | ||
injectedProperties.add(configProperty.value("name").asString()); | ||
} | ||
} | ||
|
||
// Retain only BUILD_AND_RUN_TIME_FIXED properties | ||
injectedProperties.retainAll(config.getReadResult().getBuildTimeRunTimeValues().keySet()); | ||
|
||
return SyntheticBeanBuildItem.configure(NativeBuildConfigContext.class) | ||
.param( | ||
"buildAndRunTimeFixed", | ||
injectedProperties.toArray(String[]::new)) | ||
.creator(NativeBuildConfigContextCreator.class) | ||
.scope(Singleton.class) | ||
.done(); | ||
|
||
} | ||
|
||
@BuildStep(onlyIf = NativeBuild.class) | ||
AdditionalBeanBuildItem registerBeans() { | ||
return AdditionalBeanBuildItem.builder().addBeanClasses(NativeBuildConfigCheckInterceptor.class, | ||
NativeBuildConfigCheck.class).build(); | ||
} | ||
|
||
@BuildStep(onlyIf = NativeBuild.class) | ||
AnnotationsTransformerBuildItem transformConfigProducer() { | ||
DotName configProducerName = DotName.createSimple("io.smallrye.config.inject.ConfigProducer"); | ||
|
||
return new AnnotationsTransformerBuildItem(AnnotationsTransformer.appliedToMethod().whenMethod(m -> { | ||
// Apply to all producer methods declared on io.smallrye.config.inject.ConfigProducer | ||
return m.declaringClass().name().equals(configProducerName) | ||
&& m.hasAnnotation(DotNames.PRODUCES) | ||
&& m.hasAnnotation(ConfigBuildStep.MP_CONFIG_PROPERTY_NAME); | ||
}).thenTransform(t -> { | ||
t.add(NativeBuildConfigCheck.class); | ||
})); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.quarkus.arc.test.config.nativebuild; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@Singleton | ||
public class Fail { | ||
|
||
@ConfigProperty(name = "foo", defaultValue = "bar") | ||
String value; | ||
|
||
// triggers init during static init of native build | ||
void init(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.arc.test.config.nativebuild; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusProdModeTest; | ||
|
||
public class NativeBuildConfigInjectionFailTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest TEST = new QuarkusProdModeTest() | ||
.setBuildNative(true) | ||
.withApplicationRoot( | ||
root -> root.addClass(Fail.class)) | ||
// ImageGenerationFailureException is private | ||
.setExpectedException(RuntimeException.class); | ||
|
||
@Test | ||
void test() { | ||
fail(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.quarkus.arc.test.config.nativebuild; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusProdModeTest; | ||
|
||
public class NativeBuildConfigInjectionOkTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest TEST = new QuarkusProdModeTest() | ||
.setBuildNative(true) | ||
.withApplicationRoot( | ||
root -> root.addClass(Ok.class)); | ||
|
||
@Test | ||
void test() { | ||
} | ||
} | ||
yrodiere marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.arc.test.config.nativebuild; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
import io.quarkus.arc.config.NativeBuildTime; | ||
|
||
@Singleton | ||
public class Ok { | ||
|
||
@NativeBuildTime | ||
@ConfigProperty(name = "foo", defaultValue = "bar") | ||
String value; | ||
|
||
// triggers init during static init of native build | ||
void init(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||||||
package io.quarkus.arc.config; | ||||||||||
|
||||||||||
import static java.lang.annotation.ElementType.FIELD; | ||||||||||
import static java.lang.annotation.ElementType.PARAMETER; | ||||||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||||||||||
|
||||||||||
import java.lang.annotation.Retention; | ||||||||||
import java.lang.annotation.Target; | ||||||||||
|
||||||||||
/** | ||||||||||
* A config property injected during the static initialization phase of a native image build may result in unexpected errors | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should improve the wording here, as it's not only about injecting properties. |
||||||||||
* because the injected value was obtained at build time and cannot be updated at runtime. | ||||||||||
* <p> | ||||||||||
* If it's intentional and expected then use this annotation to eliminate the false positive. | ||||||||||
*/ | ||||||||||
@Retention(RUNTIME) | ||||||||||
@Target({ FIELD, PARAMETER }) | ||||||||||
public @interface NativeBuildTime { | ||||||||||
|
||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import jakarta.enterprise.inject.spi.Annotated; | ||
import jakarta.enterprise.inject.spi.InjectionPoint; | ||
|
@@ -23,6 +24,10 @@ public Object create(SyntheticCreationalContext<Object> context) { | |
throw new IllegalStateException("No current injection point found"); | ||
} | ||
|
||
if ((boolean) context.getParams().get("nativeBuild")) { | ||
NativeBuildConfigCheckInterceptor.verifyCurrentImageMode(Set.of()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC the Set is empty here because we assume none of the properties being injected are "build and runtime fixed"... but can't a config mapping be completely "build and runtime fixed"? If so we'd want to skip this check, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, a Because we do register a synthetic bean for any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI it's not a problem because a config root implemented as a config mapping is not considered a |
||
} | ||
|
||
Class<?> interfaceType = (Class<?>) context.getParams().get("type"); | ||
String prefix = (String) context.getParams().get("prefix"); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.arc.runtime; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.interceptor.InterceptorBinding; | ||
|
||
/** | ||
* Interceptor binding for {@link NativeBuildConfigCheckInterceptor}. | ||
*/ | ||
@InterceptorBinding | ||
@Retention(RUNTIME) | ||
@Target({ TYPE, METHOD }) | ||
public @interface NativeBuildConfigCheck { | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this earlier version of the check took into account the type being injected in the error message (i.e. it would suggest
Instance<MyConfig>
instead ofInstance<String>
, IIUC).Would it make sense to still do that with the new approach, or is it too complex to be worth it?