-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Spotless Task #518
Comments
Thanks for the reply! Is there a way to do this directly from a custom gradle task? The goal is for our engineers to be able to just run a task without having to do any additional configuration. Or is this what you're working on? |
Also, I'm unable to get that to work...either with |
More specifically, I run |
Ah, sorry, spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java Lines 282 to 289 in e870e78
And then it takes effect here: spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java Lines 190 to 211 in e870e78
And here is the gradle plugin test which makes sure that it still works: So that's the first thing to figure out - why |
Excellent. I'll dig around, thank you! And yes, the capital J was a typo. I double checked after you mentioned it. |
Ok, so here's the progress I've made: I played around with printing both the Also note, it is pretty important to me to be able to configure the Spotless logic outside of the base build script. I want to share this logic between projects. I still can't make it work even by using the base script, but if at all possible I want to make it work with the dependency script. |
That's pretty strange. Can you provide a verbatim command-line that you're executing? For example, one of the commands used from the test linked above is:
I wonder if perhaps there is a missing
Almost everywhere that I use Spotless, I configure it exclusively through |
Okay, I am now able to get |
One important thing is that Depending on when you are doing |
I mess around with it here and there to see what happens, but the main command I'm using is: ./gradlew spotlessCheck -PspotlessFiles=Test.java I'm running this from the root directory of the project, and have confirmed that And you were correct about why filePatterns was not showing up in my print statements...I added them to a |
Okay, so we know that |
What it's doing now is not checking any of the files. I'm playing around with it to see if it's that particular file, but I don't think so. I tried Test.java both with the indent I expect and with another commonly expected indent, then I tried putting in a file that the full, normal run of Spotless returned errors from. I've also tried using both absolute and relative paths. |
I would try this:
|
Sorry for the delay again. I will take a look at your most recent suggestions by Monday. In particular, your last comment gave me the idea that maybe there's something going on with |
I don't, I think that my instructions above would work as just as well with
Start with a toy example! Delete every file except "Test.java", git makes that easy and safe to do. "A complex system that works is invariably found to have evolved from a simple system that worked." |
Ok. I deleted all source files, just leaving a short
For reference, here are the contents of public class Test {
int wrongIndent = 0;
// Too many newlines
String tooLong = "thislineiswaytoolongandshouldbebrokenbyalinteroratleastflaggedbysaidlinterifitdoesntwanttobeopinionated";
} When I run ConfigsSince it seems pretty likely this is playing a role here, I'm gonna go ahead and include my configuration files.
//TODO: Remove this file and depend on styleguide repo after cross-repo sharing is ready
import com.diffplug.gradle.spotless.SpotlessExtension
import com.diffplug.gradle.spotless.SpotlessPlugin
import com.diffplug.gradle.spotless.SpotlessTask
// Must use legacy plugin syntax for importing external files to build.gradle
buildscript {
repositories {
maven(url = "https://plugins.gradle.org/m2/")
}
dependencies {
classpath("com.diffplug.spotless:spotless-plugin-gradle:3.27.1")
}
}
apply(plugin = "checkstyle")
apply(plugin = "java")
apply<SpotlessPlugin>()
configure<CheckstyleExtension> {
toolVersion = "8.28"
configFile = rootProject.file("$projectDir/gradle/nextraq_checkstyle.xml")
}
configure<SpotlessExtension> {
java {
eclipse().configFile("$projectDir/gradle/eclipse-java-nextraq-style.xml")
endWithNewline()
}
}
// Checkstyle and Spotless felt the need to add themselves to the java plugin's build process without asking. How rude.
if (gradle.startParameter.taskNames
.intersect(
listOf("check",
*tasks.filter { it.dependsOn.contains("check") }.map { it.name }.toTypedArray()))
.isNotEmpty()) {
tasks.withType(Checkstyle::class) {
enabled = false
}
tasks.withType(SpotlessTask::class) {
enabled = false
}
}
import com.diffplug.gradle.spotless.SpotlessExtension
buildscript {
repositories {
maven(url = "https://plugins.gradle.org/m2/")
}
dependencies {
classpath("com.diffplug.spotless:spotless-plugin-gradle:3.27.1")
}
}
apply(from = "$projectDir/gradle/style.gradle.kts")
configure<SpotlessExtension> {
java {
target(
fileTree(
mapOf(
"dir" to ".",
"include" to listOf("**/*.java")
)
)
)
}
} And, in apply(from = "$projectDir/gradle/lint.gradle.kts") I can't thank you enough for all your help here. |
There's a lot that can be commented out of this to make the test case simpler, especially the |
No dice on What alternative to that |
Regardless of that, my goal is just to figure out why a simple unit test is passing, but failing for your build. There are two possibilities - A) the unit test is actually broken and we just don't realize it (possible, but I don't see how). B) Your build is different in some surprising way (I see lots of ways it is different). Removing complexity helps narrow things down. You can always add them back later. |
Ah cool! Thanks for the tip on disabling Spotless in the Java plugin build process! I tried commenting out that code and it still does not work as expected. I know there's a lot of weird stuff in my configs, but those were the only solutions to issues I was having. In particular, using Kotlin and imported build scripts seemed to cause issues that needed me to do weird stuff to get spotless included in the build. Any other ideas? I'll also keep playing around with it and let you know what I find. |
Nope, my only ideas are that the unit test is broken in a surprising way, or that your build is complicated in a surprising way. The path to figuring out which are small toy projects which eliminate confounding variables. Best of luck. |
Ok. I've tried quite a variety of different configurations. I've tried removing a number of things from my configs, leaving only things that removing breaks Spotless. I've also tried messing around with the way that I import Spotless and the way that I import What specifically would you say is unusual about my configuration, and can you point me to the particular unit test you're referring to? |
I've also been playing around with doing this directly in my styleguide project with a dummy |
I'm guessing this is the test? It doesn't look like it tests Kotlin configurations. Yeah, that "shouldn't" matter, but these things often do. Kotlin Gradle has been different from Groovy Gradle in many surprising ways, in my experience. I'll try rewriting my It also doesn't test an imported Spotless configuration like I'm using, which has some extra nuances due to deficiencies in Gradle (primarily the thing where legacy plugin syntax is required). There is one more issue where While most of these cases ideally wouldn't need to be tested, they do end up being relevant in practice. Even if it doesn't make sense to test them in the long run, they still could be causing the issue I'm seeing. I'll let you know what happens with trying the Groovy configuration. |
Yup, that's the one.
I don't have any specific recommendations, except that the unit test works, and the build doesn't. The only tool I can recommend is to make the build more like the unit test until it works, or (as you note) to make the unit test more the like build until it breaks. I don't have a hunch that your build has a bug, nor that Spotless has a bug. I have no hunches whatsoever, beyond a working unit test and a general-purpose debugging methodology to find the gap between a working test-case and a broken use-case. FWIW, the vast majority of Spotless users in the wild are using |
Ok that makes sense. Thank you for all the input you've given up to this point! I sincerely appreciate it; it has been quite helpful. I will keep looking into this and report back to let you and anyone else who is curious what I find. For now I'll close the ticket. Feel free to reopen if you like. |
See #528 |
FYI, |
Hello!
I'm trying to create a custom spotless task that only acts on files changed in git. I would also like the option to act on all files, so ideally I would have two separate tasks. For example,
spotlessJavaCheck
should check the pattern**/*.java
, whilespotlessChangedCheck
should check only the files changed in git.I've already written a function in the build script which identifies the changed files, and I would like to set the spotless target to these files.
Any advice?
The text was updated successfully, but these errors were encountered: