Skip to content
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

gradle: excluding spotlessJava should also exclude spotlessApply #2097

Open
tkruse opened this issue Apr 15, 2024 · 2 comments
Open

gradle: excluding spotlessJava should also exclude spotlessApply #2097

tkruse opened this issue Apr 15, 2024 · 2 comments
Labels

Comments

@tkruse
Copy link

tkruse commented Apr 15, 2024

When running gradle compileJava -xspotlessJava, spotlessApply will still run. Since spotlessApply copies files from build/spotless/spotlessJava to the source tree, this can overwrite unsaved work in progress, making teams lose valuable work due to a simple misunderstanding. Since this is a silent overwrite, team might also not notice that changes have been made, allowing bugs to enter the codebase.

spotlessApply should only ever run when the files it copies were created as part of the same build, to ensure no work is lost.

This happened on a company project, so I cannot quickly produce a public repo, but I could if need be. The steps to reproduce would be (in a java project using the spotless plugin...):

git clone ...
cd ...
// assuming this will have some file that can be formatted
 ./gradlew compileJava
  git reset --hard
  git checkout other_branch
  // assuming some file from earlier branch is different here
  
  ./gradlew compileJava -xspotlessJava
  git diff

The last git diff should not have any changes. However, currently for me this overwrites the source file with the spotless output from the ./gradlew compileJava task.

Using gradle8, java17, spotless 6.25.0 on MacOs.

@nedtwigg nedtwigg added the bug label Apr 15, 2024
@nedtwigg nedtwigg changed the title excluding spotlessJava should also exclude spotlessApply gradle: excluding spotlessJava should also exclude spotlessApply Apr 15, 2024
@punitdarira
Copy link

@tkruse @nedtwigg
I can pick this up if no one else has already started working on it.
I think instead of copying only the files which were created as part of the current build, we can instead clean the build/spotless/spotlessJava directory before every compileJava task.

@nedtwigg
Copy link
Member

nedtwigg commented Jul 4, 2024

The tasks are:

  • spotlessJava
  • spotlessJavaApply
  • spotlessApply

I think -xspotlessJavaApply will do what you want already, that's probably the best answer. But spotlessJava is the slow task that you see in the console, so I think it makes sense to cancel spotlessJavaApply and spotlessJavaCheck if -xspotlessJava is specified.

There's already some "did this task run" tracking, but I don't know how the task can know whether a task was excluded vs didn't run because it was up-to-date.

public void performAction(InputChanges inputs) throws Exception {
SpotlessTaskService taskService = getTaskService().get();
taskService.registerSourceAlreadyRan(this);

public abstract class SpotlessApply extends SpotlessTaskService.ClientTask {
@TaskAction
public void performAction() {
getTaskService().get().registerApplyAlreadyRan(this);

I'm happy to take a simple PR, but if it's complicated it's not worth it, just use -xspotlessJavaApply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants