How to exclude in Gradle 7?

I created a project like this:

plugins {

group = "org.glavo"
version = "1.0-SNAPSHOT"

tasks.compileJava {

    logger.quiet("Sources: " + source.asPath)

In Gradle 6, exclude("") can avoid compiling, but this is not working in Gradle 7.

By printing the log, I can see that does not exist in the source path of compileJava task. However, it still always compiles in Gradle 7 (I tried 7.0~7.4.2 with the same result).

Am I doing something wrong? Why is there such a difference between Gradle 6 and Gradle 7?

After investigation, I found out that this is the result brought by modularity.inferModulePath.

It defaults to true and works in a very unusual way, even excluding from the SourceSet, it still finds and forces to be compiled.

Is this expected behavior?