Skip to content

Gradle Plugin incompatible with mutli-project build and convention plugins  #747

Closed

Description

@marc-szalkiewicz-dd

Summary of problem

Thank you for your great work on spotless!

Gradle 6.7.1 reccommends convention plugins for sharing build logic across multi-project repositories.

In the official Gradle documentation, the root project often has no build.gradle - only a settings.gradle that includes other projects. Build logic is shared not through allprojects, but through convention plugins under buildSrc.

Currently, the Gradle Spotless plugin assumes it is running from the root project, which makes it incompatible with the guidance in the official Gradle documentation.

Gradle version

6.7.1

Spotless version

Plugin version 5.8.2

Operating system

Latest OSX

Spotless configuration block

In the following repository structure, with one subproject, a root settings file, and one convention plugin:

some-project
  settings.gradle
  subproject-a
    src/main/scala
        build.gradle.kts
  buildSrc
    src/main/kotlin
        scala-conventions.gradle.kts

The following configuration is inside a convention plugin:

// buildSrc/src/main/kotlin/scala-conventions.gradle.kts
plugins {
        id("com.diffplug.spotless")
}

spotless {
    scala {
        scalafmt("2.7.5").configFile("$rootDir/.scalafmt.conf")
    }
}

Full content of any console errors

 % ./gradlew build
> Task :spotlessInternalRegisterDependencies FAILED

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':spotlessInternalRegisterDependencies' (type 'RegisterDependenciesTask').
> Cannot write to file '.../some-project/build/tmp/spotless-register-dependencies' specified for property 'unitOutput', as ancestor '.../some-project/build' is not a directory.

Activity

  1. nedtwigg commented on Dec 10, 2020

    @nedtwigg
    Member

    Aha! Thanks, this is helpful diagnostic info. I this this might be a dupe of #741, but with a new cause which makes the bug easier to understand.

    The root project has special privileges when it comes to registering dynamic dependencies, so we have to use it for the "registerDepencies" task. I think we can fix it by ensuring that the build directory exists for the root project...

    PR's wanted!

  2. nedtwigg commented on Jan 4, 2021

    @nedtwigg
    Member

    Earlier I said:

    I think we can fix it by ensuring that the build directory exists for the root project

    but it turns out we were already doing that

    I tried to reproduce the issue (PR linked above), and I can only reproduce if I create a file (not a folder, but a file), in the build spot, which would be a bug outside of Spotless.

  3. upendrao commented on Feb 2, 2021

    @upendrao

    Hi Ned,
    I am using com.diffplug.spotless:spotless-plugin-gradle:5.9.0
    I still see a problem when used in Gradle mutli-module-project build environment (with convention plugins).
    'spotlessInternalRegisterDependencies' task fails reporting it cannot see an artifact in provided repositories.

    You probably need to add a repository containing the '[org.scalameta:scalafmt-core_2.11:2.0.1]' artifact in the 'build.gradle' of your root project. E.g.: 'buildscript { repositories { mavenCentral() }}' org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':detachedConfiguration1'. at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.rethrowFailure(DefaultConfiguration.java:1406) at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.access$3600(DefaultConfiguration.java:149) at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$DefaultResolutionHost.rethrowFailure(DefaultConfiguration.java:2011) at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.visitContents(DefaultConfiguration.java:1379) at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.visitContents(DefaultConfiguration.java:497) at org.gradle.api.internal.file.AbstractFileCollection.getFiles(AbstractFileCollection.java:126) at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated.getFiles(Unknown Source) at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.resolve(DefaultConfiguration.java:487) at com.diffplug.gradle.spotless.GradleProvisioner.lambda$fromRootBuildscript$1(GradleProvisioner.java:74) at com.diffplug.gradle.spotless.GradleProvisioner$RootProvisioner.provisionWithTransitives(GradleProvisioner.java:56) at com.diffplug.spotless.JarState.provisionWithTransitives(JarState.java:68) at com.diffplug.spotless.JarState.from(JarState.java:57)

    I have added mevenCentral() as suggested in the error message but of no help.
    Since there is no build.gradle file at the project root how is that spotless reading any repository declaration in a multi-project environment?

  4. nedtwigg commented on Feb 2, 2021

    @nedtwigg
    Member

    Since there is no build.gradle file at the project root

    Can you put one there? If not, why not? From the error message: need to add a repository containing the '[org.scalameta:scalafmt-core_2.11:2.0.1]' artifact in the 'build.gradle' of your root project.

    The reason for this is that Gradle has different rules for resolving dependencies in the root project than in child projects.

  5. upendrao commented on Feb 2, 2021

    @upendrao

    Ned, Appreciated your quick reply :-)

    Can you put one there?

    Yes. Then it works.

    If not, why not?

    Since(Gradle 6.7+) all the build logic has been moved to buildSrc module there is no need for one except in this occasion.

  6. nedtwigg commented on Feb 2, 2021

    @nedtwigg
    Member

    As stated earlier, Gradle has different rules for the root project than for child projects. Spotless cannot work unless you add the repositories block to the root project, and the error message is clear about that.

    If you want to do the buildscript { repositories {, etc with a buildSrc module, I think you can do that. But you'll have to apply that plugin to the root project, there is no way around that. e.g. apply: 'my-buildsrc-spotless-root-project'. There might be a way to accomplish this with the settings.gradle file, but I don't know what the magic phrase would be.

    Bad idea warning: You might be able to update the root project from a child project. You could do apply: 'my-buildsrc-plugin' in a subproject, and grab the root project and update its buildscript. That is bad practice and might not work, and you'd need to add some kind of flag to make sure that you only modify it once. If Gradle ever ships parallel project configuration (they already have parallel execution, but not yet configuration), then this will definitely not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions