google / google/google-java-format

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

未关闭
#1,436 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Java
星标
6.2k
派生
937
平均合并
6 分钟
30 天内合并 PR
3

描述

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.

贡献指南

打开贡献指南

调研方向

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.

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
tooling
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
62/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。