Skip to content

Commit 0927b9a

Browse files
bcorsoDagger Team
authored and
Dagger Team
committed
Add flag to control LegacyBindingGraphFactory usage.
For now, the flag's default is set to enabled to keep the functionality the same as before. However, in a future release (likely 2.55) we will flip the default to `disabled`. The new `BindingGraphFactory` contains many bug fixes that still exist in the `LegacyBindingGraphFactory`. However, the new `BindingGraphFactory` does have some behavior changes that may affect users. The biggest change is that we no longer allow a module binding installed in a component to float to a subcomponent to satisfy missing bindings. See the javadoc in `CompilerOptions` for more details. RELNOTES=N/A PiperOrigin-RevId: 710805160
1 parent 1620e92 commit 0927b9a

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

java/dagger/internal/codegen/binding/BindingGraphFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.common.base.Preconditions.checkState;
2222
import static com.google.common.base.Predicates.not;
2323
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.isAssistedFactoryType;
24+
import static dagger.internal.codegen.binding.LegacyBindingGraphFactory.useLegacyBindingGraphFactory;
2425
import static dagger.internal.codegen.extension.DaggerCollectors.onlyElement;
2526
import static dagger.internal.codegen.extension.DaggerGraphs.unreachableNodes;
2627
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
@@ -106,7 +107,7 @@ public final class BindingGraphFactory {
106107
*/
107108
public BindingGraph create(
108109
ComponentDescriptor componentDescriptor, boolean createFullBindingGraph) {
109-
return LegacyBindingGraphFactory.useLegacyBindingGraphFactory(componentDescriptor)
110+
return useLegacyBindingGraphFactory(compilerOptions, componentDescriptor)
110111
? legacyBindingGraphFactory.create(componentDescriptor, createFullBindingGraph)
111112
: createBindingGraph(componentDescriptor, createFullBindingGraph);
112113
}

java/dagger/internal/codegen/binding/LegacyBindingGraphFactory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
/** A factory for {@link BindingGraph} objects. */
6969
public final class LegacyBindingGraphFactory {
7070

71-
static boolean useLegacyBindingGraphFactory(ComponentDescriptor componentDescriptor) {
72-
return true;
71+
static boolean useLegacyBindingGraphFactory(
72+
CompilerOptions compilerOptions, ComponentDescriptor componentDescriptor) {
73+
return compilerOptions.useLegacyBindingGraphFactory();
7374
}
7475

7576
static boolean hasStrictMultibindingsExemption(

java/dagger/internal/codegen/compileroption/CompilerOptions.java

+20
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ public final boolean doCheckForNulls() {
118118
*/
119119
public abstract boolean generatedClassExtendsComponent();
120120

121+
/**
122+
* Returns {@code true} if Dagger should use the legacy binding graph factory.
123+
*
124+
* <p>Note: This flag is only intended to give users time to migrate to the new binding graph
125+
* factory. New users should not enable this flag. This flag will be removed in a future release.
126+
*
127+
* <p>The legacy binding graph factory contains a number of bugs which can lead to an incorrect
128+
* binding graph (e.g. missing multibindings), can be difficult to debug, and are often dependent
129+
* on the ordering of bindings/dependency requests in the user's code.
130+
*
131+
* <p>The new binding graph factory fixes many of these issues by switching to a well known graph
132+
* data structure and algorithms to avoid many of the subtle bugs that plagued the legacy binding
133+
* graph factory. However, note that the new binding graph factory also has a behavior change that
134+
* could cause issues for some users. Specifically, a module binding is no longer allowed to float
135+
* from its installed component into one of its subcomponents in order to satisfy a missing
136+
* dependency. Thus, any (transitive) dependencies of the module binding that are missing from the
137+
* installed component will now be reported as an error.
138+
*/
139+
public abstract boolean useLegacyBindingGraphFactory();
140+
121141
/**
122142
* Returns {@code true} if the key for map multibinding contributions contain a framework type.
123143
*

java/dagger/internal/codegen/compileroption/ProcessingEnvironmentCompilerOptions.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.STRICT_MULTIBINDING_VALIDATION;
3636
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.STRICT_SUPERFICIAL_VALIDATION;
3737
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.USE_FRAMEWORK_TYPE_IN_MAP_MULTIBINDING_CONTRIBUTION_KEY;
38+
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.USE_LEGACY_BINDING_GRAPH_FACTORY;
3839
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.VALIDATE_TRANSITIVE_COMPONENT_DEPENDENCIES;
3940
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.WARN_IF_INJECTION_FACTORY_NOT_GENERATED_UPSTREAM;
4041
import static dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions.Feature.WRITE_PRODUCER_NAME_IN_TOKEN;
@@ -203,6 +204,11 @@ public boolean generatedClassExtendsComponent() {
203204
return isEnabled(GENERATED_CLASS_EXTENDS_COMPONENT);
204205
}
205206

207+
@Override
208+
public boolean useLegacyBindingGraphFactory() {
209+
return isEnabled(USE_LEGACY_BINDING_GRAPH_FACTORY);
210+
}
211+
206212
@Override
207213
public int keysPerComponentShard(XTypeElement component) {
208214
if (options.containsKey(KEYS_PER_COMPONENT_SHARD)) {
@@ -238,15 +244,14 @@ private ProcessingEnvironmentCompilerOptions checkValid() {
238244
noLongerRecognized(FLOATING_BINDS_METHODS);
239245
noLongerRecognized(EXPERIMENTAL_AHEAD_OF_TIME_SUBCOMPONENTS);
240246
noLongerRecognized(USE_GRADLE_INCREMENTAL_PROCESSING);
241-
if (!isEnabled(IGNORE_PROVISION_KEY_WILDCARDS)) {
242-
if (processingEnv.getBackend() == XProcessingEnv.Backend.KSP) {
243-
processingEnv.getMessager().printMessage(
244-
Diagnostic.Kind.ERROR,
245-
String.format(
246-
"When using KSP, you must also enable the '%s' compiler option (see %s).",
247-
"dagger.ignoreProvisionKeyWildcards",
248-
"https://dagger.dev/dev-guide/compiler-options#ignore-provision-key-wildcards"));
249-
}
247+
if (processingEnv.getBackend() == XProcessingEnv.Backend.KSP
248+
&& !isEnabled(IGNORE_PROVISION_KEY_WILDCARDS)) {
249+
processingEnv.getMessager().printMessage(
250+
Diagnostic.Kind.ERROR,
251+
String.format(
252+
"When using KSP, you must also enable the '%s' compiler option (see %s).",
253+
"dagger.ignoreProvisionKeyWildcards",
254+
"https://dagger.dev/dev-guide/compiler-options#ignore-provision-key-wildcards"));
250255
}
251256
return this;
252257
}
@@ -337,6 +342,8 @@ enum Feature implements EnumOption<FeatureStatus> {
337342

338343
GENERATED_CLASS_EXTENDS_COMPONENT,
339344

345+
USE_LEGACY_BINDING_GRAPH_FACTORY(ENABLED),
346+
340347
USE_FRAMEWORK_TYPE_IN_MAP_MULTIBINDING_CONTRIBUTION_KEY,
341348

342349
IGNORE_PROVISION_KEY_WILDCARDS(ENABLED),

java/dagger/internal/codegen/javac/JavacPluginCompilerOptions.java

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public boolean experimentalDaggerErrorMessages() {
111111
return false;
112112
}
113113

114+
@Override
115+
public boolean useLegacyBindingGraphFactory() {
116+
return true;
117+
}
118+
114119
@Override
115120
public boolean useFrameworkTypeInMapMultibindingContributionKey() {
116121
return false;

0 commit comments

Comments
 (0)