Skip to content

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

Description

@aoto-tech

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

Spotless configuration

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

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

Input

gradle/libs.versions.toml:

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

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

Steps to reproduce

./gradlew spotlessApply --stacktrace

Actual output file

[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:

> 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:

List<String> pendingComments = new ArrayList<>();
StringBuilder multiLineAccumulator = null;
int lineNumber = 0;
int entryStartLine = 0;
for (String line : raw.split("\n", -1)) {
lineNumber++;
String trimmed = line.trim();
if (multiLineAccumulator != null) {
multiLineAccumulator.append('\n').append(line);
if (isBalanced(multiLineAccumulator.toString())) {
Entry entry = new Entry(multiLineAccumulator.toString(), new ArrayList<>(pendingComments));
currentEntries.add(entry);
pendingComments.clear();
multiLineAccumulator = null;
}
continue;
}
if (currentHeader == null && !TABLE_HEADER.matcher(trimmed).matches()) {
continue;
}
Matcher headerMatcher = TABLE_HEADER.matcher(trimmed);
if (headerMatcher.matches()) {
currentHeader = "[" + headerMatcher.group(1) + "]";
currentEntries = new ArrayList<>();
sections.put(currentHeader, currentEntries);
pendingComments.clear();
} else if (currentEntries != null) {
if (trimmed.isEmpty() || trimmed.startsWith("#")) {
pendingComments.add(trimmed);
} else if (!isBalanced(trimmed)) {
multiLineAccumulator = new StringBuilder(line.stripLeading());
entryStartLine = lineNumber;
} else {
Entry entry = new Entry(trimmed, new ArrayList<>(pendingComments));
currentEntries.add(entry);
pendingComments.clear();
}
}
}

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions