From 6949f0bdd284f31652b28e60249ff3d8a47695f7 Mon Sep 17 00:00:00 2001 From: nolanpollack-stripe Date: Fri, 20 Nov 2026 13:29:55 -0800 Subject: [PATCH] Preserve all originating elements for KSP incremental processing `XTypeSpecs.Builder.addOriginatingElement()` currently overwrites the previous element. Components with multiple generated modules therefore lose input dependencies needed by KSP incremental processing. Editing only the component can make the next build fail because an unchanged generated module is missing. The fix keeps all originating elements. Fixes https://github.com/google/dagger/issues/5363 This was triggered when `TypeSpec` was migrated to `XTypeSpec` in https://github.com/google/dagger/commit/7c4622d3fa4cca3767d3eebac6f580588c848693. Javapoet's `addOriginatingElement` correctly tracks elements as a list. https://github.com/square/javapoet/blob/b9017a9503b76e11b4ad4c1a9f050e2d29112cb0/src/main/java/com/squareup/javapoet/TypeSpec.java#L429 Validation: - The new Gradle regression fails with the unpatched compiler on its second build: `ActivityModule_Activity` could not be resolved. It passes with this patch using Kotlin 2.3.21 and KSP 2.3.7. - Both tests in `IncrementalProcessingTest` pass against the patched local snapshot. - Bazel tests pass: `XTypeSpecsTest`, `XCodeBlocksTest`, `XMethodElementsTest`, `ComponentProcessorTest`, `ComponentShardTest`, and `KspComponentProcessorTest`. - `git diff --check` passes. Fixes https://github.com/google/dagger/pull/5360 RELNOTES=N/A COPYBARA_INTEGRATE_REVIEW=https://github.com/google/dagger/pull/5360 from nolanpollack-stripe:fix-originating-elements 8b3906915963b235b9c3d579813767c275b3e930 PiperOrigin-RevId: 1026714370 --- .../codegen/xprocessing/XTypeSpecs.java | 8 +-- .../buildtests/IncrementalProcessingTest.java | 14 ++--- .../internal/codegen/XTypeSpecsTest.java | 57 +++++++++++++++++++ 3 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 javatests/dagger/internal/codegen/XTypeSpecsTest.java diff --git a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java index c5bb4038bfc..4486a32e09b 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java @@ -105,7 +105,7 @@ private static enum Kind { private boolean isAbstract; private boolean isFunctional; private VisibilityModifier visibility = null; - private XElement originatingElement; + private final List originatingElements = new ArrayList<>(); private final Set alwaysQualifyNames = new LinkedHashSet<>(); private final List javadocs = new ArrayList<>(); private XTypeName superclass; @@ -171,10 +171,10 @@ public Builder addJavadoc(String format, Object... args) { return this; } - /** Sets the originating element of the type. */ + /** Adds an originating element of the type. */ @CanIgnoreReturnValue public Builder addOriginatingElement(XElement originatingElement) { - this.originatingElement = originatingElement; + originatingElements.add(originatingElement); return this; } @@ -420,7 +420,7 @@ public XTypeSpec build() { throw new AssertionError(); } - if (originatingElement != null) { + for (XElement originatingElement : originatingElements) { builder.addOriginatingElement(originatingElement); } diff --git a/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java b/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java index 6c488179286..1d42d814c33 100644 --- a/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java +++ b/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java @@ -275,9 +275,7 @@ public void testIncrementalProcessingWithGeneratedModule() throws IOException { + "'generated.Generated_AnnotatedClassModule' could not be resolved."); } - // This is a regression test for https://github.com/google/dagger/issues/4181 when multiple - // @ContributesAndroidInjector modules are implicitly included (see - // https://github.com/google/dagger/pull/5360). + // This is a regression test for https://github.com/google/dagger/issues/5363. @Test public void testIncrementalProcessingWithGeneratedAndroidInjectorModules() throws IOException { File projectDir = tmpFolder.getRoot(); @@ -369,12 +367,10 @@ public void testIncrementalProcessingWithGeneratedAndroidInjectorModules() throw // Leave both injector modules unchanged and regenerate only the component's source file. appModule.addSrcFile( "MyComponent.kt", componentContent.replace("dependency()", "dependency2()")); - BuildResult incrementalBuild = buildAndFail(projectDir); - assertThat(incrementalBuild.task(":app:kspKotlin").getOutcome()).isEqualTo(TaskOutcome.FAILED); - assertThat(incrementalBuild.getOutput()) - .contains( - "ComponentProcessingStep was unable to process 'app.MyComponent' because " - + "'app.ActivityModule_Activity' could not be resolved."); + BuildResult incrementalBuild = build(projectDir); + assertThat(incrementalBuild.task(":app:kspKotlin").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); + assertThat(getAllKspGeneratedFileNames(appModule.getDir())) + .containsExactlyElementsIn(initialGeneratedFiles); } private static BuildResult build(File projectDir) { diff --git a/javatests/dagger/internal/codegen/XTypeSpecsTest.java b/javatests/dagger/internal/codegen/XTypeSpecsTest.java new file mode 100644 index 00000000000..7418938bfec --- /dev/null +++ b/javatests/dagger/internal/codegen/XTypeSpecsTest.java @@ -0,0 +1,57 @@ +/* + * 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 static androidx.room3.compiler.codegen.compat.XConverters.toJavaPoet; +import static com.google.common.truth.Truth.assertThat; +import static dagger.testing.compile.CompilerTests.javaSource; + +import androidx.room3.compiler.codegen.XTypeSpec; +import androidx.room3.compiler.processing.XTypeElement; +import dagger.internal.codegen.xprocessing.XTypeSpecs; +import dagger.testing.compile.CompilerTests; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link XTypeSpecs}. */ +@RunWith(JUnit4.class) +public final class XTypeSpecsTest { + @Test + public void noOriginatingElements() { + assertThat(toJavaPoet(XTypeSpecs.classBuilder("Generated").build()).originatingElements) + .isEmpty(); + } + + @Test + public void addOriginatingElement_preservesAllElements() { + CompilerTests.invocationCompiler( + javaSource("test.First", "package test;", "class First {}"), + javaSource("test.Second", "package test;", "class Second {}")) + .compile(invocation -> { + XTypeElement first = invocation.getProcessingEnv().requireTypeElement("test.First"); + XTypeElement second = invocation.getProcessingEnv().requireTypeElement("test.Second"); + XTypeSpec type = + XTypeSpecs.classBuilder("Generated") + .addOriginatingElement(first) + .addOriginatingElement(second) + .build(); + + assertThat(toJavaPoet(type).originatingElements).hasSize(2); + }); + } +}