Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#3,133 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@maxandersen ci sta già lavorando.

Dal 6/10/2026.

  • #3134 di @maxandersen — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
java
Ambito
build-system

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Java
Stelle
5.7k
Fork
565
Merge medio
2g 2h
PR unite (30g)
57

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di diffplug/spotless

Tutte le issue di diffplug/spotless

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.