google / google/google-java-format

unused import removal leaves extra blank line between package declaration and class Javadoc / declaration

Aperta
#1,436 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
6.2k
Fork
937
Merge medio
6m
PR unite (30g)
3

Descrizione

When `google-java-format` removes an unused import statement located between a `package` declaration and class Javadoc or class declaration, it does not collapse the vertical whitespace in the same formatting pass. As a result, the formatted output contains extra blank lines (two consecutive blank lines instead of one).

Running `google-java-format` a second time on the output collapses the extra blank line as expected. We observed this in generated code, more context here: https://github.com/googleapis/librarian/issues/7146

## Environment
* **`google-java-format` Version:** Tested on `1.25.2` through `1.35.0` (including recent builds).
* **Operating System:** Linux / macOS / Cross-platform

## Minimal Reproducible Example

### Input Code (`TestBug.java`)

```java
package com.example;

import static io.grpc.MethodDescriptor.generateFullMethodName;

/**
* Javadoc for class.
*/
public class TestBug {
}

### Command Executed

google-java-format TestBug.java

## Actual Output (First Pass)

package com.example;

/** Javadoc for class. */
public class TestBug {}

(Notice the two consecutive empty lines between package com.example; and /** Javadoc for class. */.)

## Expected Output

package com.example;

/** Javadoc for class. */
public class TestBug {}

## Additional Context & Workaround

• Second Pass Result: Executing google-java-format a second time on the first pass output (google-java-format TestBug.java | google-java-format -) produces the correct output with a single blank line.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start by reproducing the issue with the TestBug.java example and the google-java-format command. Trace the formatting pass that removes the unused import and handles vertical whitespace between the package declaration and class Javadoc. Done means the first formatting pass produces one blank line, matching the expected output, without needing a second pass.

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

Valutazione

Stack tecnologico
java
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
62/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.