Skip to content

Commit

Permalink
Add a dagger.experimentalKsp compiler option.
Browse files Browse the repository at this point in the history
This CL adds the `dagger.experimentalKsp` compiler option, and by default sets it to `DISABLED`. The intention is to require users to explicitly enable the flag when using Dagger's KSP processors to make it clear that they are opting into something experimental. Once Dagger's KSP processor support goes stable we will remove the flag.

RELNOTES=N/A
PiperOrigin-RevId: 557588065
  • Loading branch information
bcorso authored and Dagger Team committed Aug 16, 2023
1 parent 2c31d66 commit 3ac7207
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import dagger.testing.compile.CompilerTests;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -211,7 +212,7 @@ public abstract static class HiltCompiler {
static Builder builder() {
return new AutoValue_HiltCompilerTests_HiltCompiler.Builder()
// Set the builder defaults.
.processorOptions(ImmutableMap.of())
.processorOptions(CompilerTests.DEFAULT_PROCESSOR_OPTIONS)
.additionalJavacProcessors(ImmutableList.of())
.additionalKspProcessors(ImmutableList.of())
.processingSteps(ImmutableList.of())
Expand All @@ -235,7 +236,11 @@ static Builder builder() {

/** Returns a new {@link HiltCompiler} instance with the annotation processors options. */
public HiltCompiler withProcessorOptions(ImmutableMap<String, String> processorOptions) {
return toBuilder().processorOptions(processorOptions).build();
// Add default processor options first to allow overridding with new key-value pairs.
Map<String, String> newProcessorOptions =
new HashMap<>(CompilerTests.DEFAULT_PROCESSOR_OPTIONS);
newProcessorOptions.putAll(processorOptions);
return toBuilder().processorOptions(ImmutableMap.copyOf(newProcessorOptions)).build();
}

/** Returns the processing steps suppliers. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public int keysPerComponentShard(XTypeElement component) {
return 3500;
}

/** Returns {@code true} if the Dagger experimental KSP processors are enabled. */
public abstract boolean experimentalKsp();

/**
* This option enables a fix to an issue where Dagger previously would erroneously allow
* multibinding contributions in a component to have dependencies on child components. This will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.EXPERIMENTAL_AHEAD_OF_TIME_SUBCOMPONENTS;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.EXPERIMENTAL_ANDROID_MODE;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.EXPERIMENTAL_DAGGER_ERROR_MESSAGES;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.EXPERIMENTAL_KSP;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.FAST_INIT;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.FLOATING_BINDS_METHODS;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.FORMAT_GENERATED_SOURCE;
Expand Down Expand Up @@ -114,6 +115,11 @@ private boolean fastInitInternal(XTypeElement component) {
return isEnabled(FAST_INIT);
}

@Override
public boolean experimentalKsp() {
return isEnabled(EXPERIMENTAL_KSP);
}

@Override
public boolean formatGeneratedSource() {
return isEnabled(FORMAT_GENERATED_SOURCE);
Expand Down Expand Up @@ -254,6 +260,14 @@ private ProcessingEnvironmentCompilerOptions checkValid() {
"https://dagger.dev/dev-guide/compiler-options#ignore-provision-key-wildcards"));
}
}
if (!isEnabled(EXPERIMENTAL_KSP) && processingEnv.getBackend() == XProcessingEnv.Backend.KSP) {
messager.printMessage(
Diagnostic.Kind.ERROR,
String.format(
"Dagger's KSP processor is still in experimental. The '%s=ENABLED' compiler option "
+ "must be set when using KSP.",
optionName(EXPERIMENTAL_KSP)));
}
return this;
}

Expand Down Expand Up @@ -314,6 +328,8 @@ public String toString() {
*/
enum Feature implements EnumOption<FeatureStatus> {
FAST_INIT,

EXPERIMENTAL_KSP,

EXPERIMENTAL_ANDROID_MODE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ public boolean generatedClassExtendsComponent() {
public boolean ignoreProvisionKeyWildcards() {
return false;
}

@Override
public boolean experimentalKsp() {
return false;
}
}
3 changes: 2 additions & 1 deletion java/dagger/testing/compile/CompilerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public final class CompilerTests {
new XProcessingEnvConfig.Builder().disableAnnotatedElementValidation(true).build();

// TODO(bcorso): Share this with javatests/dagger/internal/codegen/Compilers.java
private static final ImmutableMap<String, String> DEFAULT_PROCESSOR_OPTIONS =
public static final ImmutableMap<String, String> DEFAULT_PROCESSOR_OPTIONS =
ImmutableMap.of(
"dagger.experimentalKsp", "enabled",
"dagger.experimentalDaggerErrorMessages", "enabled");

/** Returns the {@link XProcessingEnv.Backend} for the given {@link CompilationResultSubject}. */
Expand Down

0 comments on commit 3ac7207

Please sign in to comment.