micronaut-projects / micronaut-aot

Experimental Ahead-of-time optimizations for Micronaut
Apache License 2.0
17 stars 5 forks source link

Micronaut AOT not respecting Bean Configurations #25

Closed sdelamo closed 2 years ago

sdelamo commented 2 years ago

Issue description

I am trying to use micronaut-aot with the micronaut application I am developing as part of Micronaut Live.

Steps to reproduce.

https://github.com/micronaut-advocacy/micronaut-live-newsletter/blob/master/README.md#local-development

java.lang.NullPointerException: null
        at java.base/java.util.Objects.requireNonNull(Objects.java:208)
        at java.base/java.util.Optional.of(Optional.java:113)
        at io.micronaut.views.csp.CspConfiguration.getPolicyDirectives(CspConfiguration.java:105)
        at io.micronaut.views.csp.CspFilter.lambda$doFilter$1(CspFilter.java:88)
        at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:185)
        at io.micronaut.reactive.reactor.instrument.ReactorSubscriber.onNext(ReactorSubscriber.java:57)
        at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
        at io.micronaut.reactive.reactor.instrument.ReactorSubscriber.onNext(ReactorSubscriber.java:57)
        at reactor.core.publisher.FluxFlatMap$FlatMapMain.drainLoop(FluxFlatMap.java:712)
        at reactor.core.publisher.FluxFlatMap$FlatMapMain.drain(FluxFlatMap.java:588)
        at reactor.core.publisher.FluxFlatMap$FlatMapInner.onSubscribe(FluxFlatMap.java:955)
        at io.micronaut.reactive.reactor.instrument.ReactorSubscriber.onSubscribe(ReactorSubscriber.java:50)
        at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:68)
        at reactor.core.publisher.Flux.subscribe(Flux.java:8468)

The CSP beans should not be loaded unless micronaut.views.csp.enabled is set to true

https://github.com/micronaut-projects/micronaut-views/blob/master/views-core/src/main/java/io/micronaut/views/csp/package-info.java#L23

Beans Configurations is a pattern we use heavily through the framework and I think micronaut-aot is not compatible with it yet.

This is my build config:

plugins {
...
    id("io.micronaut.application") version "3.1.0-M1"
    id("io.micronaut.aot") version "3.1.0-M1"
}

...
micronaut {
   ...
    aot {
        version = "1.0.0-M2"
        //configFile = file("aot.properties")
        sealEnvironment =  true
        optimizeServiceLoading = true
        optimizeClassLoading = true
         // convertYamlToJava = true
        precomputeOperations = true
    }
}
melix commented 2 years ago

Given that the bean requirements should be evaluated at build time, I would expect this to work. Let me check.

sdelamo commented 2 years ago

maybe we are not evaluating package-info.java ?

melix commented 2 years ago

The problem isn't with the package-info.java class as I can see that the bean is discarded properly (you can check the logs in build/native/jit/logs and see that there's a line:

Skipping class io.micronaut.views.csp.$BeanConfiguration because it doesn't match bean requirements

Which corresponds to the evaluation of the @Requires condition of that bean. The problem is more that CspConfiguration$Definition on its side is evaluated to true so the bean is loaded.

This happens at https://github.com/micronaut-projects/micronaut-aot/blob/d822944fa402bc4e323268f21c1e49f06bc14af1/aot-core/src/main/java/io/micronaut/aot/core/context/ApplicationContextAnalyzer.java#L196

sdelamo commented 2 years ago

please note that if you run the app with run and visit localhost:8080 it behaves fine

sdelamo commented 2 years ago

are you suggesting this

@Configuration
@Requires(property = CspConfiguration.PREFIX + ".enabled", value = StringUtils.TRUE)
package io.micronaut.views.csp;

Should be


@Configuration
@Requires(property = CspConfiguration.PREFIX + ".enabled", value = StringUtils.TRUE, defaultValue=StringUtils.FALSE)
package io.micronaut.views.csp;

with defaultValue set to false

melix commented 2 years ago

I am not suggesting anything, I'm simply describing what the problem is. Most likely AOT doesn't reproduce a behavior which exists when running the app normally. I'm trying to understand what I need to add to replicate this behavior.