diffplug / diffplug/spotless

versionCatalog() silently deletes standalone comments at section boundaries and EOF

クローズ
#3,048 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Java
スター
5.6k
フォーク
559
平均マージ
1日 14時間
マージ済み PR(30日)
43

説明

## Summary

`versionCatalog()` silently deletes standalone comments when they are either:

1. between the final entry of one table and the next table header, or
2. after the final entry at end of file.

The formatting task succeeds, so these comments are removed without an error or lint. This reproduces with the released Spotless Gradle plugin 8.10.2 and at commit `cd8b6e2b4532f6533bd78c0d0ffec8ee37cae0e6` (the merge commit for #3042).

## Environment

- Gradle: 9.7.1
- Spotless Gradle plugin: 8.10.2
- Also reproduced at commit: `cd8b6e2b4532f6533bd78c0d0ffec8ee37cae0e6` (the merge commit for #3042)
- OS: Windows 11 10.0 amd64
- JVM: 21.0.8

## Spotless configuration

```groovy
plugins {
id 'com.diffplug.spotless' version '8.10.2'
}

spotless {
toml {
target 'gradle/libs.versions.toml'
versionCatalog()
}
}
```

## Input

`gradle/libs.versions.toml`:

```toml
[versions]
zoo = "1.0"
# keep this trailing comment

[libraries]
foo = { module = "g:a", version.ref = "zoo" }
# keep this final comment
```

## Steps to reproduce

```shell
./gradlew spotlessApply --stacktrace
```

## Actual output file

```toml
[versions]
zoo = "1.0"

[libraries]
foo = { module = "g:a", version.ref = "zoo" }
```

Both comments are removed.

## Expected output

Both comments should be preserved. Their exact attachment to the preceding table or following header is less important than avoiding data loss.

## Console output

There is no error or lint. The task reports success:

```text
> Task :spotlessToml
> Task :spotlessTomlApply
> Task :spotlessApply

BUILD SUCCESSFUL in 20s
2 actionable tasks: 2 executed
```

The released-plugin reproduction above was run as a minimal Gradle project using version 8.10.2. The commit-level reproduction was verified separately with local JUnit tests which call `VersionCatalogStep.create()` through `StepHarness` using the same section-boundary and EOF inputs; it was not run through a source-built Gradle plugin.

## Suspected cause

`VersionCatalogStep.parseSections()` stores standalone comments in `pendingComments`, but only attaches them when a later entry is parsed. A new table header clears `pendingComments`, and pending comments are not flushed at EOF:

https://github.com/diffplug/spotless/blob/cd8b6e2b4532f6533bd78c0d0ffec8ee37cae0e6/lib/src/main/java/com/diffplug/spotless/toml/VersionCatalogStep.java#L127-L168

#3042 fixed other comment/quote-related data-loss cases, but these section-boundary and EOF cases remain reproducible at commit `cd8b6e2b4532f6533bd78c0d0ffec8ee37cae0e6` after that change.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

lib/src/main/java/com/diffplug/spotless/toml/VersionCatalogStep.java から始め、特に parseSections() と pendingComments の処理を確認してください。VersionCatalogStep.create() を StepHarness 経由で呼び出すローカルの JUnit テストで、セクション境界と EOF のケースを再現してください。両方の位置にある単独コメントが、エラーやデータ損失なしにフォーマット後も保持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
java
領域
build-system
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
明確に書かれている
初心者へのやさしさ
78/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。