Preserve orphan comments (e.g. kubebuilder scaffold marker) inside import blocks - #248
Open
devthedevil wants to merge 3 commits into
Open
Preserve orphan comments (e.g. kubebuilder scaffold marker) inside import blocks#248devthedevil wants to merge 3 commits into
devthedevil wants to merge 3 commits into
Conversation
A comment that sits on its own line inside an import block but isn't attached (as Doc or trailing Comment) to any *ast.ImportSpec was being silently dropped when the block was rewritten. This breaks tools like kubebuilder/controller-gen that rely on a standalone marker comment (e.g. `// +kubebuilder:scaffold:imports`) to know where to insert future imports. ParseFile now also returns the byte range of any such orphan comment found between the last recognized import spec and the closing paren.
LoadFormat now receives the orphan-comment byte range from parse.ParseFile and re-inserts it right before the closing paren of the rewritten import block, so standalone comments (e.g. a trailing `// +kubebuilder:scaffold:imports` marker) are no longer dropped.
Reproduces the kubebuilder scaffold marker case from daixiang0#135: a comment on its own line after the last import but before the closing paren must survive formatting instead of being dropped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #135. Since PR #120 ("optimize detect import blocks"),
gcisilently drops any comment inside an import block that isn't attached (asDocor trailingComment) to a specific*ast.ImportSpec. That PR's own description acknowledged this as a known breaking change ("introduce breaking change: remove isolated comments").The most common way to hit this is a standalone marker comment such as:
kubebuilder/controller-gen relies on that comment being present at that exact position to know where to insert future imports. Running
gcion such a file removes the marker entirely, which breaks the codegen workflow.Root cause
pkg/parse/parse.go'sParseFileonly records byte ranges for import specs (Start/Endderived from each spec'sDoc/Name/Path/Comment).pkg/gci/gci.go'sLoadFormatrebuilds the import block purely by concatenating those recorded byte ranges, so any comment that isn't formally attached to a spec never makes it into the rewritten output.Change
pkg/parse/parse.go: addedfindTailComment, which scansf.Commentsfor a comment group that sits after the last recognized import spec but before the import block's closing paren (i.e. not attached to any spec).ParseFilenow additionally returns that comment's byte range (-1, -1if none exists).pkg/gci/gci.go:LoadFormattakes the two new return values and, right before appending the closing), re-inserts the orphan comment's original bytes so it survives the rewrite.pkg/gci/testdata.go: added atrailing-marker-commenttest case reproducing the exact example from the issue.The change is intentionally scoped to the trailing-comment case described in the issue (a comment between the last import and the closing paren). A related but distinct case mentioned in the issue discussion — a leading orphan comment ending up merged onto the
import (line — is not addressed here to keep this change minimal and focused.Testing
Added
trailing-marker-commenttopkg/gci/testdata.go'stestCases, which is exercised by the existingTestRuninpkg/gci/gci_test.go:I don't have a Go toolchain available in the environment I drafted this in, so I hand-traced the AST byte offsets against this reproduction case and against the existing fixtures (multi-block imports, comments outside the import block, cgo blocks, etc.) to check for regressions, but please run CI/
go test ./...before merging.Closes #135