Skip to content

Build: Integrate Scalafix to auto-fix unused Scala imports#15676

Closed
jackylee-ch wants to merge 2 commits intoapache:mainfrom
jackylee-ch:apply_scalafix_to_autofix_unused_imports
Closed

Build: Integrate Scalafix to auto-fix unused Scala imports#15676
jackylee-ch wants to merge 2 commits intoapache:mainfrom
jackylee-ch:apply_scalafix_to_autofix_unused_imports

Conversation

@jackylee-ch
Copy link
Copy Markdown
Contributor

Integrate Scalafix with the RemoveUnused rule to automatically fix unused Scala imports via ./gradlew scalafix.
Fix #14342

@jackylee-ch jackylee-ch force-pushed the apply_scalafix_to_autofix_unused_imports branch from beaf75d to b007c87 Compare March 19, 2026 01:24
For Scala code, unused imports are treated as compilation errors. To automatically remove unused imports, run [Scalafix](https://scalacenter.github.io/scalafix/):

```bash
./gradlew scalafix -DsparkVersions=4.0 -DscalaVersion=2.13
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also work with Spark 3.5 scala 2.12?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works.

@huaxingao
Copy link
Copy Markdown
Contributor

LGTM from my side. Any other reviewers interested in taking a look? Maybe @RussellSpitzer ?

@RussellSpitzer
Copy link
Copy Markdown
Member

I'm not familiar with this tool, I would just double check we can't already support this with the existing plugins we have. If not then I don't have a problem. I can review more seriously when I get to a computer since I'll need to learn more about this tool

@jackylee-ch
Copy link
Copy Markdown
Contributor Author

remind @RussellSpitzer, PTAL

@RussellSpitzer
Copy link
Copy Markdown
Member

@jackylee-ch Did you have a response for my comment?

@jackylee-ch
Copy link
Copy Markdown
Contributor Author

@jackylee-ch Did you have a response for my comment?

Oh, sorry, I misunderstood. Just as I replied in the PR of Build: Apply spotless for scala code, there's currently no other solution to the unused import problem.
image


// When running scalafix, downgrade unused import errors to warnings so that
// compilation succeeds and SemanticDB is produced for Scalafix to process.
boolean scalafixRun = gradle.startParameter.taskNames.any {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really feels like a bug in the cosmic-silence plugin. It should be automatically be downgrading the tasks on it's own.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its working principle is to handle the issue only after a Scala compile warning occurs, since it doesn't perform the unused check itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, introducing Scalafix is ​​a bit heavy, but we lack this component.
Alternatively, do you think we can maintain the current state and let users handle this code style issue themselves when they receive compilation failures related to unused imports?
If so, the PR and the issue can be closed now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are better off just not adding anything to the build, I've grown very paranoid of adding things to the build with all the supply chain attacks lately and even though I think this would be relatively safe, I don't think we really gain that much by giving folks an auto fix command. We aren't really a scala project so the benefits here feel very low compared to adding complexity to the build and a new plugin.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, would close current PR and Issue. Thanks for your clarify.

@jackylee-ch jackylee-ch closed this Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unused imports check/fix for Scala Code

3 participants