Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

shortenFullyQualifiedTypes corrupts method references on fields (x.field::m -> bogus import x.field)

未關閉
#3,133 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@maxandersen 已經在處理了。

開始於 2026年10月6日。

  • #3134 來自 @maxandersen —— 未關閉

評估

難度
3/5
預估耗時
1-2 天
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
java
領域
build-system

研究方向

Start by reading the implementation of shortenFullyQualifiedTypes() added in #3005 and the related tests; the issue does not name their file paths. Reproduce with the supplied Java example and run spotlessApply followed by compileJava. Done means catalog.aliases::get remains unchanged, no bogus import is added, and a regression test covers the case. A linked open pull request (#3134) indicates work is already underway.

由索引模型根據 Issue 內容生成。

描述

Summary

shortenFullyQualifiedTypes() (added in #3005) corrupts a method reference on a field of a local variable. It treats localVar.field::method as if localVar.field were a fully-qualified type, shortens it to field::method, and injects a bogus import localVar.field; — which does not compile.

Because it only manifests on spotlessApply (not the read-only spotlessCheck), a developer runs spotlessApply, commits, and then the project no longer builds.

Version
  • com.diffplug.spotless 8.10.3 (Gradle plugin)
  • JDK 11 / 17 (reproduces on both)
Minimal reproducer

build.gradle:

plugins {
    id 'java'
    id 'com.diffplug.spotless' version '8.10.3'
}
repositories { mavenCentral() }
spotless { java { shortenFullyQualifiedTypes() } }

src/main/java/Example.java:

import java.util.Map;
import java.util.function.Function;

public class Example {
    static class Catalog {
        Map<String, String> aliases;
    }

    String demo(Catalog catalog) {
        return find(catalog.aliases::get);   // method ref on a field of a local var
    }

    String find(Function<String, String> f) {
        return f.apply("x");
    }
}
Steps
./gradlew spotlessApply
./gradlew compileJava
Actual

spotlessApply rewrites the file to:

import java.util.Map;
import java.util.function.Function;
import catalog.aliases;          // <-- bogus import of a non-existent package/type

public class Example {
    ...
    String demo(Catalog catalog) {
        return find(aliases::get);   // <-- qualifier dropped, now unresolved
    }
    ...
}

and compilation fails:

Example.java: error: package catalog does not exist
Expected

catalog.aliases::get is a method reference on a field access of a local variable, not a fully-qualified type reference. It must be left untouched (no shortening, no import added).

Root cause (guess)

The qualifier-resolution doesn't verify that the left-hand side of a qualified name / method-reference scope actually resolves to a type before shortening. Field accesses on locals/params (x.y, x.y::m, x.y.z) are being misclassified as fully-qualified type names. Checking that the qualifier is a type (not a local variable / field expression) before rewriting should fix it.

Found while enabling the step on a real codebase (jbangdev/jbang), where catalog.aliases::get (a Catalog local + Map field) broke the build.

主要語言
Java
星號
5.7k
分支
565
平均合併
2 天 2 小時
30 天內合併 PR
57

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

diffplug/spotless 的其他 Issue

查看 diffplug/spotless 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。