Skip to content

Commit 0decae5

Browse files
committed
error out when GOMOD env value is /dev/null
1 parent 59c8d3f commit 0decae5

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

gomodguard.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gomodguard
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"go/parser"
89
"go/token"
@@ -18,8 +19,8 @@ import (
1819

1920
const (
2021
goModFilename = "go.mod"
21-
errReadingGoModFile = "unable to read go mod file %s: %w"
22-
errParsingGoModFile = "unable to parsing go mod file %s: %w"
22+
errReadingGoModFile = "unable to read module file %s: %w"
23+
errParsingGoModFile = "unable to parse module file %s: %w"
2324
)
2425

2526
var (
@@ -64,15 +65,15 @@ func (r *BlockedVersion) Message(lintedModuleVersion string) string {
6465
var sb strings.Builder
6566

6667
// Add version contraint to message.
67-
fmt.Fprintf(&sb, "version `%s` is blocked because it does not meet the version constraint `%s`.",
68+
_, _ = fmt.Fprintf(&sb, "version `%s` is blocked because it does not meet the version constraint `%s`.",
6869
lintedModuleVersion, r.Version)
6970

7071
if r.Reason == "" {
7172
return sb.String()
7273
}
7374

7475
// Add reason to message.
75-
fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
76+
_, _ = fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
7677

7778
return sb.String()
7879
}
@@ -110,13 +111,13 @@ func (r *BlockedModule) Message() string {
110111
for i := range r.Recommendations {
111112
switch {
112113
case len(r.Recommendations) == 1:
113-
fmt.Fprintf(&sb, "`%s` is a recommended module.", r.Recommendations[i])
114+
_, _ = fmt.Fprintf(&sb, "`%s` is a recommended module.", r.Recommendations[i])
114115
case (i+1) != len(r.Recommendations) && (i+1) == (len(r.Recommendations)-1):
115-
fmt.Fprintf(&sb, "`%s` ", r.Recommendations[i])
116+
_, _ = fmt.Fprintf(&sb, "`%s` ", r.Recommendations[i])
116117
case (i + 1) != len(r.Recommendations):
117-
fmt.Fprintf(&sb, "`%s`, ", r.Recommendations[i])
118+
_, _ = fmt.Fprintf(&sb, "`%s`, ", r.Recommendations[i])
118119
default:
119-
fmt.Fprintf(&sb, "and `%s` are recommended modules.", r.Recommendations[i])
120+
_, _ = fmt.Fprintf(&sb, "and `%s` are recommended modules.", r.Recommendations[i])
120121
}
121122
}
122123

@@ -126,9 +127,9 @@ func (r *BlockedModule) Message() string {
126127

127128
// Add reason to message
128129
if sb.Len() == 0 {
129-
fmt.Fprintf(&sb, "%s.", strings.TrimRight(r.Reason, "."))
130+
_, _ = fmt.Fprintf(&sb, "%s.", strings.TrimRight(r.Reason, "."))
130131
} else {
131-
fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
132+
_, _ = fmt.Fprintf(&sb, " %s.", strings.TrimRight(r.Reason, "."))
132133
}
133134

134135
return sb.String()
@@ -473,9 +474,13 @@ func loadGoModFile() ([]byte, error) {
473474
return ioutil.ReadFile(goModFilename)
474475
}
475476

476-
if _, err := os.Stat(goEnv["GOMOD"]); os.IsNotExist(err) {
477+
if _, err = os.Stat(goEnv["GOMOD"]); os.IsNotExist(err) {
477478
return ioutil.ReadFile(goModFilename)
478479
}
479480

481+
if goEnv["GOMOD"] == "/dev/null" {
482+
return nil, errors.New("current working directory must have a go.mod file")
483+
}
484+
480485
return ioutil.ReadFile(goEnv["GOMOD"])
481486
}

0 commit comments

Comments
 (0)