Skip to content

Support overlapping source sets #427

@vorburger

Description

@vorburger

What version of OpenRewrite are you using?

I am using Gradle plugin id("org.openrewrite.rewrite") version "7.28.0".

How are you running OpenRewrite?

In a single module Gradle project.

But I'm using a non-standard directory layout in my build.gradle.kts, because I like to keep my *Test in java/:

sourceSets {
    main {
        java.srcDir("java")
        java.srcDir("build/generated/sources/annotationProcessor/java/main")
        java.exclude("**/*Test.java")
        java.exclude("**/testlib/**")
        resources.srcDir("resources")
    }
    test {
        java.srcDir("java")
        java.srcDir("build/generated/sources/annotationProcessor/java/test")
        java.include("**/*Test.java")
        java.include("**/testlib/**")
        resources.srcDir("test")
    }
}

What did you expect to see?

In a perfect world, the rewrite-gradle-plugin should work well with such "overlapping" source sets.

What did you see instead?

It has also sorts of problems, including:

  • completely breaking gradle test, which I have worked around by isolating into a if (hasProperty("runRewrite")) { and running it gradle rewriteRun -PrunRewrite
  • formatting my main sources but ignoring my test source, which I have worked around with an ugly hack
// gradle rewriteRun -PrunRewrite
// NB: The OpenRewrite Gradle plugin *BREAKS* "gradle test" in this project
// (possibly due to our joint main and test in the same java/ folder), so we
// keep it in a separate profile and only apply it when really needed:
if (hasProperty("runRewrite")) {
    apply(plugin = "org.openrewrite.rewrite")
    configure<org.openrewrite.gradle.RewriteExtension> {
        activeRecipe("org.openrewrite.java.ShortenFullyQualifiedTypeReferences")
        exclusion(".direnv/**")
        exclusion(".devcontainer/**")
        exclusion(".gradle/**")
        exclusion(".git/**")
        exclusion("typescript/**")
        exclusion("build/**")
        exclusion("**/*.kts")
        exclusion("build.gradle.kts")
    }

    // Workaround for OpenRewrite ignoring the 'test' sourceSet because it shares
    // the same 'java/' srcDir with 'main'. We temporarily include tests in 'main'.
    sourceSets.main {
        java.setExcludes(emptyList<String>())
    }
    configurations.named("compileOnly") {
        extendsFrom(configurations.named("testImplementation").get())
    }
}

Are you interested in contributing a fix to OpenRewrite?

I unfortunately wouldn't have the bandwidth (time), but still wanted to let you know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions