From 0d62102d7e8cba5f8ca041600fbad5cf7c4177eb Mon Sep 17 00:00:00 2001 From: arimu1 <19286898+arimu1@users.noreply.github.com> Date: Fri, 7 Aug 2026 23:55:03 +0700 Subject: [PATCH] Improve error when Dagger compiler was not run over a module When a @Module from a dependency is missing its generated *Factory types (because dagger-compiler/KSP was not applied to that library), report a clear error instead of letting javac fail with a vague "does not exist" message on the generated component. Modules being compiled in the current compilation are skipped, since their factories are generated in-round by ModuleProcessingStep and may not yet be visible via findTypeElement. Fixes #5146 --- .../ComponentDescriptorValidator.java | 9 +- .../codegen/validation/ModuleValidator.java | 59 ++++++++ .../ModuleMissingFactoryErrorTest.java | 135 ++++++++++++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 javatests/dagger/internal/codegen/ModuleMissingFactoryErrorTest.java diff --git a/dagger-compiler/main/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java b/dagger-compiler/main/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java index 289d6c5835a..11e7ac68883 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java @@ -88,6 +88,7 @@ *
This typically means the Dagger compiler was not applied to the library that defines the + * module. Factories for modules in the current compilation are generated by {@code + * ModuleProcessingStep} and may not yet be visible via {@link XProcessingEnv#findTypeElement}, so + * those modules are skipped. + * + * @see dagger#5146 + */ + public void checkGeneratedFactoriesAvailable( + ModuleDescriptor module, ValidationReport.Builder report) { + XTypeElement moduleElement = module.moduleElement(); + if (isModuleFromCurrentCompilation(moduleElement)) { + return; + } + for (ContributionBinding binding : module.bindings()) { + if (binding.kind() != PROVISION && binding.kind() != PRODUCTION) { + continue; + } + if (!binding.bindingElement().isPresent()) { + continue; + } + XClassName factoryName = generatedClassNameForBinding(binding); + if (processingEnv.findTypeElement(factoryName) == null) { + report.addError( + String.format( + "The Dagger factory type %s is missing. This usually means the Dagger compiler was" + + " not applied to the module %s where the binding is defined. Ensure that the" + + " dependency that contains that module runs the Dagger annotation processor" + + " (or KSP plugin).", + factoryName.getCanonicalName(), moduleElement.getQualifiedName()), + moduleElement); + // One diagnostic per module is enough to point users at the root cause. + return; + } + } } /** Returns a validation report for a module type. */ diff --git a/javatests/dagger/internal/codegen/ModuleMissingFactoryErrorTest.java b/javatests/dagger/internal/codegen/ModuleMissingFactoryErrorTest.java new file mode 100644 index 00000000000..94b97ef46d1 --- /dev/null +++ b/javatests/dagger/internal/codegen/ModuleMissingFactoryErrorTest.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2026 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.room3.compiler.processing.util.Source; +import com.google.common.collect.ImmutableCollection; +import com.google.common.collect.ImmutableList; +import dagger.testing.compile.CompilerTests; +import java.io.File; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Tests that using a {@code @Module} from a library that was not processed by Dagger yields a + * clear error (https://github.com/google/dagger/issues/5146). + */ +@RunWith(Parameterized.class) +public class ModuleMissingFactoryErrorTest { + @Parameters(name = "{0}") + public static ImmutableCollection