From b007c8737487b7e0b0805eea44d8e146ab8fa67b Mon Sep 17 00:00:00 2001 From: Jacky Lee Date: Thu, 19 Mar 2026 00:26:56 +0800 Subject: [PATCH] Build: Integrate Scalafix to auto-fix unused Scala imports (#14342) --- .baseline/scala/.scalafix.conf | 23 +++++++++++++++++++++++ baseline.gradle | 19 +++++++++++++++++-- build.gradle | 1 + site/docs/contribute.md | 7 +++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 .baseline/scala/.scalafix.conf diff --git a/.baseline/scala/.scalafix.conf b/.baseline/scala/.scalafix.conf new file mode 100644 index 000000000000..740b2cb5572c --- /dev/null +++ b/.baseline/scala/.scalafix.conf @@ -0,0 +1,23 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +rules = [RemoveUnused] +RemoveUnused.imports = true +RemoveUnused.privates = false +RemoveUnused.locals = false +RemoveUnused.patternvars = false +RemoveUnused.params = false diff --git a/baseline.gradle b/baseline.gradle index 4efbd89eda02..81919bcb9fcf 100644 --- a/baseline.gradle +++ b/baseline.gradle @@ -187,6 +187,21 @@ subprojects { } pluginManager.withPlugin('scala') { + apply plugin: 'io.github.cosmicsilence.scalafix' + + scalafix { + configFile = file("${rootDir}/.baseline/scala/.scalafix.conf") + semanticdb { + autoConfigure = true + } + } + + // 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 { + it.toLowerCase().contains('scalafix') + } + tasks.withType(ScalaCompile).configureEach { scalaCompile -> if (scalaVersion?.startsWith("2.12")) { scalaCompile.scalaCompileOptions.additionalParameters = [ @@ -196,8 +211,8 @@ subprojects { ] } else if (scalaVersion?.startsWith("2.13")) { scalaCompile.scalaCompileOptions.additionalParameters = [ - "-Wconf:cat=unused:error", - "-Wunused:imports" + "-Wunused:imports", + scalafixRun ? "-Wconf:cat=unused:warning" : "-Wconf:cat=unused:error", ] } } diff --git a/build.gradle b/build.gradle index 52d25bc33b51..3ddde79a04e0 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,7 @@ buildscript { classpath 'me.champeau.jmh:jmh-gradle-plugin:0.7.3' classpath 'gradle.plugin.io.morethan.jmhreport:gradle-jmh-report:0.9.6' classpath "com.github.alisiikh:gradle-scalastyle-plugin:3.5.0" + classpath 'io.github.cosmicsilence:gradle-scalafix:0.2.6' classpath 'org.revapi:gradle-revapi:1.8.0' classpath 'com.gorylenko.gradle-git-properties:gradle-git-properties:2.5.7' classpath 'com.palantir.gradle.gitversion:gradle-git-version:4.3.0' diff --git a/site/docs/contribute.md b/site/docs/contribute.md index 3094cb5874b2..37dbaba22954 100644 --- a/site/docs/contribute.md +++ b/site/docs/contribute.md @@ -111,6 +111,7 @@ Iceberg is built using Gradle with Java 17 or 21. * To invoke a build and run tests: `./gradlew build` * To skip tests: `./gradlew build -x test -x integrationTest` * To fix code style: `./gradlew spotlessApply` +* To fix Scala unused imports: `./gradlew scalafix` * To build particular Spark/Flink Versions: `./gradlew build -DsparkVersions=3.4,3.5 -DflinkVersions=1.14` Iceberg table support is organized in library modules: @@ -303,6 +304,12 @@ In order to automatically fix Java code style issues, please use `./gradlew spot since that version is compatible with JDK 8. When formatting the code in the IDE, there is a slight chance that it will produce slightly different results. In such a case please run `./gradlew spotlessApply` as CI will check the style against **google-java-format** 1.7. +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 +``` + ### Copyright Each file must include the Apache license information as a header.