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

problem with Go directives #205

Open
ccoVeille opened this issue May 27, 2024 · 5 comments
Open

problem with Go directives #205

ccoVeille opened this issue May 27, 2024 · 5 comments

Comments

@ccoVeille
Copy link

There are a few golang directives that can be used to alter the way Go behaves

These directives are provided in Go comments

Here are some information about them

https://go.dev/doc/comment#syntax
https://pkg.go.dev/go/build#hdr-Build_Constraints
https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494

//go:build linux
// +build:linux
//go:linkname

There is a small problem with the regexp used for detecting Go comments

line_comment = _{ "//" ~ (!(NEWLINE) ~ ANY)* }

For example if I launch autocorrect --lint on this file

https://github.com/Antonboom/testifylint/blob/eb1257817037b6c67234f07f310e7c2b4fc24cf6/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall_linux_ppc64x.go#L5-L7

it would report to change linux to Linux

The whole repository has many examples of code Go directives

while there is no problem to change that on a markdown file, Go string, or in regular Go comment, it would cause issue here

I think the regexp should be fixed.

You can find existing regexp about how to detect them

https://github.com/mgechev/revive/blob/bbe5eb74146a39769ec40e413790dc3975cf8bfe/rule/utils.go#L169
https://github.com/mvdan/gofumpt/blob/52739c56d0efd3647a627598a55e821324f8571b/format/format.go#L320-L334

@ccoVeille
Copy link
Author

@ccoVeille
Copy link
Author

I'm unsure what to do with the +build, but I don't think your tool should suggest things like this

antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/pagesize_unix.go:6:1
-// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
+// +build aix darwin dragonfly freebsd Linux netbsd openbsd solaris


antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall_linux_gccgo_386.go:5:1
-//go:build linux && gccgo && 386
+//go:build Linux && gccgo && 386


antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall_linux_mipsx.go:5:1
-//go:build linux && (mips || mipsle)
+//go:build Linux && (mips || mipsle)

antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall_linux_mipsx.go:6:1
-// +build linux
+// +build Linux


antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall_linux_gccgo_arm.go:5:1
-//go:build linux && gccgo && arm
+//go:build Linux && gccgo && arm


antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall.go:5:1
-//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
+//go:build aix || darwin || dragonfly || freebsd || Linux || netbsd || openbsd || solaris || zos

antonboom-testifylint/analyzer/testdata/src/vendor/golang.org/x/sys/unix/syscall.go:6:1
-// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
+// +build aix darwin dragonfly freebsd Linux netbsd openbsd solaris zos

@huacnlee
Copy link
Owner

You can disable spellcheck first, this case is hard to support right now.

@ccoVeille
Copy link
Author

Of course, it's what I did.

I also apply fixes then nuke the errors with a git restore -p

It's OK, but I would prefer the issue to be reported and maybe fixed

@ccoVeille
Copy link
Author

I'm wondering if the fix couldn't be as simple as replacing // here

https://github.com/huacnlee/autocorrect/blob/main/autocorrect%2Fgrammar%2Fgo.pest#L8

by // so the comment but only when a space is present. This is how to differ a comment from a Go directive after all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants