Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce leading space on comments #5747

Merged
merged 6 commits into from
Mar 7, 2024
Merged

Conversation

Groxx
Copy link
Member

@Groxx Groxx commented Mar 7, 2024

What

Changes all comments like this:

//stuff
type thing struct {
  field string //comment
}
//go:embed directives are left alone

To this:

// stuff
type thing struct {
  field string // comment
}
//go:embed directives are left alone

And added a fairly basic lint check to prevent them from coming back.
The lint check is imprecise and doesn't catch comments at the ends of non-blank lines of code,
but fixing that seemed like much more work than it was worth.

And, since this will likely neutralize a //nolint comment that wasn't doing anything anyway,
I've removed that one comment entirely to avoid being misleading.

How

"Starts with X but not followed by Y" is famously hard for regex, so I only tried briefly with sed / perl before giving up.
So instead I just had Goland format the entire repository, and undid anything that wasn't a Go file.

Somewhat surprisingly, all it did was add these spaces. Which is perfect.
Oddly it missed a single obvious one, but I'm willing to forgive it for that.

Why

IDEs often do this when formatting files, which is thrashing a bit.

gofmt (and therefore goimports) in theory doesn't actually care... but despite their
protestations about not caring about comment formatting in github issues, Go's
source-printer and therefore gofmt does treat some comments specially, most notably
"directives" like go:build: https://cs.opensource.google/go/go/+/refs/tags/go1.22.1:src/go/printer/comment.go;l=15
and anything that might be a "doc comment" may get restructured too:

diff --git cmd/server/main.go cmd/server/main.go
index 6af56db58..b857f9211 100644
--- cmd/server/main.go
+++ cmd/server/main.go
@@ -35,6 +35,14 @@ import (
 )

 // main entry point for the cadence server
+//go:build
+// did you know
+//go:link
+// that go special
+//cases:things
+// that use
+//go:embed
+// specific comment structures?
 func main() {
 	app := cadence.BuildCLI(metrics.ReleaseVersion, metrics.Revision)
 	app.Run(os.Args)

which gofmt -w turns into:

diff --git cmd/server/main.go cmd/server/main.go
index 6af56db58..2511eb414 100644
--- cmd/server/main.go
+++ cmd/server/main.go
@@ -18,6 +18,8 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.

+//go:build
+
 package main

 import (
@@ -35,6 +37,14 @@ import (
 )

 // main entry point for the cadence server
+// did you know
+// that go special
+// that use
+// specific comment structures?
+//
+//go:link
+//cases:things
+//go:embed
 func main() {
 	app := cadence.BuildCLI(metrics.ReleaseVersion, metrics.Revision)
 	app.Run(os.Args)

which IMO means we should explicitly avoid colliding with those kinds of comment patterns.
Linters like staticcheck generally use suppressing-comment structures like //nolint:sa123,
I suspect in part to fit with / avoid running afoul of this kind of reformatting.

Go has only claimed x:y as far as I can tell, so specifically x: is not a "directive"...
but that seems unnecessarily near-colliding to me, and it would allow //TODO: asdf which
are definitely not "tool directives" in any form, so I haven't allowed those.

# What

Changes all comments like this:
```go
//stuff
type thing struct {
  field string //comment
}
//go:embed directives are left alone
```
To this:
```go
// stuff
type thing struct {
  field string // comment
}
//go:embed directives are left alone
```
And added a fairly basic lint check to prevent them from coming back.
The lint check is imprecise and doesn't catch comments at the ends of non-blank lines of code,
but fixing that seemed like much more work than it was worth.

And, since this will likely neutralize a `//nolint` comment that wasn't doing anything anyway,
I've removed that one comment entirely to avoid being misleading.

# How

"Starts with X but not followed by Y" is famously hard for regex, so I only tried briefly with sed / perl before giving up.
So instead I just had Goland format the entire repository, and undid anything that wasn't a Go file.

Somewhat surprisingly, all it did was add these spaces.  Which is perfect.

# Why

IDEs often do this when formatting files, which is thrashing a bit.

gofmt (and therefore goimports) in theory doesn't actually care... but despite their protestations
about comment formatting in github issues, gofmt *does* treat some comments specially,
namely "directives" like `go:build`:
```diff
diff --git cmd/server/main.go cmd/server/main.go
index 6af56db58..b857f9211 100644
--- cmd/server/main.go
+++ cmd/server/main.go
@@ -35,6 +35,14 @@ import (
 )

 // main entry point for the cadence server
+//go:build
+// did you know
+//go:link
+// that go special
+//cases:things
+// that use
+//go:embed
+// specific comment structures?
 func main() {
 	app := cadence.BuildCLI(metrics.ReleaseVersion, metrics.Revision)
 	app.Run(os.Args)
```
which `gofmt -w` turns into:
```diff
diff --git cmd/server/main.go cmd/server/main.go
index 6af56db58..2511eb414 100644
--- cmd/server/main.go
+++ cmd/server/main.go
@@ -18,6 +18,8 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.

+//go:build
+
 package main

 import (
@@ -35,6 +37,14 @@ import (
 )

 // main entry point for the cadence server
+// did you know
+// that go special
+// that use
+// specific comment structures?
+//
+//go:link
+//cases:things
+//go:embed
 func main() {
 	app := cadence.BuildCLI(metrics.ReleaseVersion, metrics.Revision)
 	app.Run(os.Args)
```
which IMO means we should explicitly avoid colliding with those kinds of comment patterns.
Linters like staticcheck generally use suppressing-comment structures like `//nolint:sa123`,
I suspect in part to fit with / avoid running afoul of this kind of reformatting.

Go has only claimed `x:y` as far as I can tell, so specifically `x:` is not a "directive"...
but that seems unnecessarily near-colliding to me, and it would allow `//TODO: asdf` which
are definitely not "tool directives" in any form, so I haven't allowed those.
Copy link

codecov bot commented Mar 7, 2024

Codecov Report

Merging #5747 (6fe34d4) into master (b2db40e) will decrease coverage by 0.01%.
Report is 1 commits behind head on master.
The diff coverage is 90.00%.

❗ Current head 6fe34d4 differs from pull request most recent head cf982eb. Consider uploading reports for the commit cf982eb to get more accurate results

Additional details and impacted files
Files Coverage Δ
common/config/metrics.go 76.92% <ø> (ø)
common/config/tls.go 0.00% <ø> (ø)
common/domain/replication_queue.go 54.86% <ø> (ø)
common/elasticsearch/client/v6/client_bulk.go 37.16% <ø> (ø)
common/elasticsearch/esql/cadencesql.go 0.00% <ø> (ø)
common/elasticsearch/esql/esql.go 37.50% <ø> (ø)
common/membership/resolver.go 78.37% <ø> (ø)
common/messaging/kafka/partition_ack_manager.go 100.00% <ø> (ø)
common/persistence/dataStoreInterfaces.go 27.86% <ø> (ø)
common/persistence/data_manager_interfaces.go 98.38% <ø> (ø)
... and 25 more

... and 5 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4e86118...cf982eb. Read the comment docs.

@Groxx Groxx added the automerge label Mar 7, 2024
@coveralls
Copy link

coveralls commented Mar 7, 2024

Pull Request Test Coverage Report for Build 018e1a97-1575-4e1a-b18e-79512f9d46b7

Details

  • 22 of 46 (47.83%) changed or added relevant lines in 21 files are covered.
  • 89 unchanged lines in 16 files lost coverage.
  • Overall coverage decreased (-0.02%) to 63.938%

Changes Missing Coverage Covered Lines Changed/Added Lines %
common/pinot/responseUtility.go 0 1 0.0%
service/worker/parentclosepolicy/workflow.go 0 1 0.0%
tools/cli/domain.go 0 1 0.0%
common/elasticsearch/client/v6/client_bulk.go 0 2 0.0%
common/elasticsearch/client/v7/client_bulk.go 0 2 0.0%
service/history/engine/engineimpl/historyEngine.go 0 2 0.0%
common/persistence/persistence-tests/executionManagerTest.go 0 3 0.0%
common/persistence/persistence-tests/metadataPersistenceV2Test.go 0 3 0.0%
common/persistence/persistence-tests/persistenceTestBase.go 0 4 0.0%
service/history/execution/context.go 0 5 0.0%
Files with Coverage Reduction New Missed Lines %
common/task/weighted_round_robin_task_scheduler.go 1 88.06%
common/persistence/historyManager.go 2 66.67%
service/history/shard/context.go 2 66.67%
common/util.go 2 91.69%
service/history/queue/timer_gate.go 3 95.83%
common/persistence/nosql/nosqlplugin/cassandra/workflow.go 3 71.88%
common/persistence/statsComputer.go 3 94.64%
service/history/task/fetcher.go 3 85.57%
service/matching/taskListManager.go 4 79.7%
common/persistence/nosql/nosqlplugin/cassandra/tasks.go 4 76.09%
Totals Coverage Status
Change from base Build 018e1a84-60a7-4b3a-8891-4b57e89fe8f2: -0.02%
Covered Lines: 93733
Relevant Lines: 146599

💛 - Coveralls

@Groxx Groxx enabled auto-merge (squash) March 7, 2024 20:25
@@ -36,7 +36,6 @@ type (

// metricDefinition contains the definition for a metric
metricDefinition struct {
//nolint
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the one being removed.
removing it doesn't cause any lints to fail, and it doesn't really make sense here anyway, so I'm just getting rid of it.

@Groxx Groxx merged commit 233b3ef into cadence-workflow:master Mar 7, 2024
17 of 18 checks passed
@Groxx Groxx deleted the comments branch March 7, 2024 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants