Skip to content

Commit

Permalink
feat: godebug directives
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 1, 2024
1 parent c1282b5 commit b1a06e9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cmd/gomoddirectives/gomoddirectives.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type config struct {
RetractAllowNoExplanation bool
ToolchainForbidden bool
ToolForbidden bool
GoDebugForbidden bool
}

func main() {
Expand All @@ -40,6 +41,7 @@ func main() {
flag.BoolVar(&cfg.RetractAllowNoExplanation, "retract-no-explanation", false, "Allow to use retract directives without explanation")
flag.BoolVar(&cfg.ToolchainForbidden, "toolchain", false, "Forbid the use of toolchain directive")
flag.BoolVar(&cfg.ToolForbidden, "tool", false, "Forbid the use of tool directives")
flag.BoolVar(&cfg.GoDebugForbidden, "godebug", false, "Forbid the use of godebug directives")

help := flag.Bool("h", false, "Show this help.")

Expand All @@ -51,12 +53,13 @@ func main() {
}

results, err := gomoddirectives.Analyze(gomoddirectives.Options{
ExcludeForbidden: cfg.ExcludeForbidden,
ReplaceAllowList: cfg.ReplaceAllowList,
ReplaceAllowLocal: cfg.ReplaceAllowLocal,
ExcludeForbidden: cfg.ExcludeForbidden,
RetractAllowNoExplanation: cfg.RetractAllowNoExplanation,
ToolchainForbidden: cfg.ToolchainForbidden,
ToolForbidden: cfg.ToolForbidden,
GoDebugForbidden: cfg.GoDebugForbidden,
})
if err != nil {
log.Fatal(err)
Expand Down
15 changes: 15 additions & 0 deletions gomoddirectives.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
reasonExclude = "exclude directive is not allowed"
reasonToolchain = "toolchain directive is not allowed"
reasonTool = "tool directive is not allowed"
reasonGoDebug = "godebug directive is not allowed"
reasonReplaceLocal = "local replacement are not allowed"
reasonReplace = "replacement are not allowed"
reasonReplaceIdentical = "the original module and the replacement are identical"
Expand Down Expand Up @@ -50,6 +51,7 @@ type Options struct {
RetractAllowNoExplanation bool
ToolchainForbidden bool
ToolForbidden bool
GoDebugForbidden bool
}

// AnalyzePass analyzes a pass.
Expand Down Expand Up @@ -94,6 +96,7 @@ func AnalyzeFile(file *modfile.File, opts Options) []Result {
results = append(results, checkToolDirectives(file, opts)...)
results = append(results, checkReplaceDirectives(file, opts)...)
results = append(results, checkToolchainDirective(file, opts)...)
results = append(results, checkGoDebugDirectives(file, opts)...)

return results
}
Expand Down Expand Up @@ -193,6 +196,18 @@ func checkToolchainDirective(file *modfile.File, opts Options) []Result {
return results
}

func checkGoDebugDirectives(file *modfile.File, opts Options) []Result {
var results []Result

if opts.GoDebugForbidden {
for _, e := range file.Godebug {
results = append(results, NewResult(file, e.Syntax, reasonGoDebug))
}
}

return results
}

// Filesystem paths found in "replace" directives are represented by a path with an empty version.
// https://github.com/golang/mod/blob/bc388b264a244501debfb9caea700c6dcaff10e2/module/module.go#L122-L124
func isLocal(r *modfile.Replace) bool {
Expand Down
31 changes: 31 additions & 0 deletions gomoddirectives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,37 @@ func TestAnalyzeFile(t *testing.T) {
ToolchainForbidden: false,
},
},
{
desc: "godebug: don't allow",
modulePath: "godebug/go.mod",
opts: Options{
GoDebugForbidden: true,
},
expected: []Result{
{
Reason: "godebug directive is not allowed",
Start: token.Position{Filename: "go.mod", Offset: 0, Line: 5, Column: 1},
End: token.Position{Filename: "go.mod", Offset: 0, Line: 5, Column: 23},
},
{
Reason: "godebug directive is not allowed",
Start: token.Position{Filename: "go.mod", Offset: 0, Line: 7, Column: 5},
End: token.Position{Filename: "go.mod", Offset: 0, Line: 7, Column: 15},
},
{
Reason: "godebug directive is not allowed",
Start: token.Position{Filename: "go.mod", Offset: 0, Line: 8, Column: 5},
End: token.Position{Filename: "go.mod", Offset: 0, Line: 8, Column: 21},
},
},
},
{
desc: "godebug: allow",
modulePath: "godebug/go.mod",
opts: Options{
GoDebugForbidden: false,
},
},
}

for _, test := range testCases {
Expand Down
22 changes: 17 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
[![Sponsor](https://img.shields.io/badge/Sponsor%20me-%E2%9D%A4%EF%B8%8F-pink)](https://github.com/sponsors/ldez)
[![Build Status](https://github.com/ldez/gomoddirectives/workflows/Main/badge.svg?branch=master)](https://github.com/ldez/gomoddirectives/actions)

A linter that handle [`replace`](https://golang.org/ref/mod#go-mod-file-replace),
[`tool`](https://golang.org/ref/mod#go-mod-file-tool),
[`toolchain`](https://golang.org/ref/mod#go-mod-file-toolchain),
[`retract`](https://golang.org/ref/mod#go-mod-file-retract),
[`exclude`](https://golang.org/ref/mod#go-mod-file-exclude) directives into `go.mod`.
A linter that handle directives into `go.mod`.

## [`retract`](https://golang.org/ref/mod#go-mod-file-retract) directives

Expand Down Expand Up @@ -92,3 +88,19 @@ go 1.22

toolchain go1.23.3
```

## [`godebug`](https://go.dev/ref/mod#go-mod-file-godebug) directives

- Ban `godebug` directive.

```go
module example.com/foo

go 1.22

godebug default=go1.21
godebug (
panicnil=1
asynctimerchan=0
)
```
9 changes: 9 additions & 0 deletions testdata/godebug/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/ldez/gomoddirectives/testdata/godebug

go 1.22

godebug default=go1.21
godebug (
panicnil=1
asynctimerchan=0
)

0 comments on commit b1a06e9

Please sign in to comment.