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

Add a dagger.experimentalKsp compiler option. #4020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class SPIPluginTest(val backend: Backend) {
gradleRunner.addPluginId("kotlin-kapt")
} else {
gradleRunner.addPluginId("com.google.devtools.ksp")
gradleRunner.addAdditionalClosure("""
|ksp {
| arg("dagger.allowKsp", "enabled")
|}
""".trimMargin())
}
gradleRunner.addAdditionalClosure("""
|kotlin {
Expand Down
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,14 @@ public int keysPerComponentShard(XTypeElement component) {
return 3500;
}

/**
* Returns {@code true} if the Dagger/Hilt KSP processors are allowed.
*
* <p>Note: the Dagger/Hilt KSP processors are still in alpha. Once the processors are stable, we
* will likely remove the need for this flag.
*/
public abstract boolean allowKsp();

/**
* 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 @@ -22,6 +22,7 @@
import static com.google.common.collect.Sets.immutableEnumSet;
import static dagger.internal.codegen.compileroption.FeatureStatus.DISABLED;
import static dagger.internal.codegen.compileroption.FeatureStatus.ENABLED;
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.ALLOW_KSP;
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;
Expand Down Expand Up @@ -114,6 +115,11 @@ private boolean fastInitInternal(XTypeElement component) {
return isEnabled(FAST_INIT);
}

@Override
public boolean allowKsp() {
return isEnabled(ALLOW_KSP);
}

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

Expand Down Expand Up @@ -315,6 +330,8 @@ public String toString() {
enum Feature implements EnumOption<FeatureStatus> {
FAST_INIT,

ALLOW_KSP,

EXPERIMENTAL_ANDROID_MODE,

FORMAT_GENERATED_SOURCE,
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 allowKsp() {
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.allowKsp", "enabled",
"dagger.experimentalDaggerErrorMessages", "enabled");

/** Returns the {@link XProcessingEnv.Backend} for the given {@link CompilationResultSubject}. */
Expand Down
7 changes: 6 additions & 1 deletion javatests/artifacts/dagger-ksp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ allprojects {
mavenCentral()
mavenLocal()
}

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'com.google.dagger'
Expand All @@ -47,3 +46,9 @@ allprojects {
}
}
}

subprojects {
ksp {
arg("dagger.allowKsp", "enabled")
}
}
6 changes: 6 additions & 0 deletions javatests/artifacts/hilt-android/simpleKotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ allprojects {
}
}
}

subprojects {
ksp {
arg("dagger.allowKsp", "enabled")
}
}
68 changes: 68 additions & 0 deletions javatests/dagger/internal/codegen/KspCompilerOptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2023 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.internal.codegen;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import dagger.testing.compile.CompilerTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class KspCompilerOptionTest {
@Parameters(name = "{0}")
public static ImmutableList<Object[]> parameters() {
return ImmutableList.copyOf(new Object[][] {{"enabled"}, {"disabled"}});
}

private final String allowKspValue;

public KspCompilerOptionTest(String allowKspValue) {
this.allowKspValue = allowKspValue;
}

@Test
public void testallowKsp() {
Source component =
CompilerTests.kotlinSource(
"test.MyComponent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component",
"interface MyComponent");
CompilerTests.daggerCompiler(component)
.withProcessingOptions(
ImmutableMap.of("dagger.allowKsp", allowKspValue))
.compile(
subject -> {
if (CompilerTests.backend(subject) == XProcessingEnv.Backend.KSP
&& allowKspValue.contentEquals("disabled")) {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"set the 'dagger.allowKsp=ENABLED' compiler option when using KSP");
} else {
subject.hasErrorCount(0);
}
});
}
}