Skip to content

Add javafmtFixImports tasks for import-only mode#268

Merged
mkurz merged 1 commit into
sbt:mainfrom
mkurz:fix-imports-only-tasks
Apr 15, 2026
Merged

Add javafmtFixImports tasks for import-only mode#268
mkurz merged 1 commit into
sbt:mainfrom
mkurz:fix-imports-only-tasks

Conversation

@mkurz
Copy link
Copy Markdown
Member

@mkurz mkurz commented Apr 15, 2026

Adds dedicated import-only tasks backed by google-java-format's --fix-imports-only CLI mode.

New tasks:

  • javafmtFixImports
  • javafmtFixImportsCheck
  • javafmtFixImportsAll
  • javafmtFixImportsCheckAll

Why tasks instead of a setting?

--fix-imports-only is not just another formatting preference. It changes the meaning of the run from "format this file" to "only touch imports and leave the rest of the file alone". Because of that, modeling it as a persistent setting would be surprising:
enabling it would silently change what javafmt and javafmtCheck do.

Dedicated tasks make that behavior explicit:

  • javafmt and javafmtCheck remain full formatting tasks
  • javafmtFixImports and javafmtFixImportsCheck are clearly import-only operations

This also fits better with how people use it in practice. Import-only cleanup is usually something you invoke intentionally for a specific workflow, not a mode you want to keep enabled all the time.

Behavior details:

  • javafmtFixImports... does not format the rest of the Java file
  • it only runs the import-fixing step from google-java-format
  • only javafmtRemoveUnusedImports and javafmtSortImports influence these tasks
  • other formatting-related settings do not matter in this mode, because full formatting is skipped

This PR also adds scripted coverage for the new import-only tasks and documents them in the README.

@mkurz mkurz merged commit 087c0ae into sbt:main Apr 15, 2026
2 checks passed
@mkurz mkurz deleted the fix-imports-only-tasks branch April 15, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant