Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 4746789

Browse files
authored
Merge pull request #224 from sauyon/no-vendor
Skip vendor directories for go.mod extraction
2 parents b2fef01 + 2e73f3e commit 4746789

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+304
-223
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The extractor now only extracts go.mod files belonging to extracted packages. In particular, vendored go.mod files will no longer be extracted unless the vendored package is explicitly passed to the extractor. This will remove unexpected `GoModExpr` and similar expressions seen by queries.

extractor/extractor.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -178,55 +178,32 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
178178
}
179179

180180
extractPackage(pkg, &wg, goroutineSem, fdSem)
181-
return
182-
}
183181

184-
log.Printf("Skipping dependency package %s.", pkg.PkgPath)
185-
})
182+
if pkgRoots[pkg.PkgPath] != "" {
183+
modPath := filepath.Join(pkgRoots[pkg.PkgPath], "go.mod")
184+
if util.FileExists(modPath) {
185+
log.Printf("Extracting %s", modPath)
186+
start := time.Now()
186187

187-
wg.Wait()
188-
189-
log.Println("Done extracting packages.")
190-
log.Println("Starting to extract go.mod files.")
191-
192-
cwd, err := os.Getwd()
193-
if err != nil {
194-
log.Printf("Warning: unable to get working directory: %s", err.Error())
195-
log.Println("Skipping go.mod extraction")
196-
}
197-
rcwd, err := filepath.EvalSymlinks(cwd)
198-
if err == nil {
199-
cwd = rcwd
200-
}
201-
202-
goModPaths := make([]string, 0, 10)
188+
err := extractGoMod(modPath)
189+
if err != nil {
190+
log.Printf("Failed to extract go.mod: %s", err.Error())
191+
}
203192

204-
filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error {
205-
if filepath.Base(path) == "go.mod" && info != nil && info.Mode().IsRegular() {
206-
if err != nil {
207-
log.Printf("Found go.mod with path %s, but encountered error %s", path, err.Error())
193+
end := time.Since(start)
194+
log.Printf("Done extracting %s (%dms)", modPath, end.Nanoseconds()/1000000)
195+
}
208196
}
209197

210-
goModPaths = append(goModPaths, path)
198+
return
211199
}
212200

213-
return nil
201+
log.Printf("Skipping dependency package %s.", pkg.PkgPath)
214202
})
215203

216-
for _, path := range goModPaths {
217-
log.Printf("Extracting %s", path)
218-
start := time.Now()
219-
220-
err := extractGoMod(path)
221-
if err != nil {
222-
log.Printf("Failed to extract go.mod: %s", err.Error())
223-
}
224-
225-
end := time.Since(start)
226-
log.Printf("Done extracting %s (%dms)", path, end.Nanoseconds()/1000000)
227-
}
204+
wg.Wait()
228205

229-
log.Println("Done extracting go.mod files.")
206+
log.Println("Done extracting packages.")
230207

231208
return nil
232209
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
func main() {}

ql/test/library-tests/semmle/go/GoModExpr/ExcludeLines.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/ExcludeLines.ql

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
missingRequire
2+
missingExclude
3+
missingReplace
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import go
2+
3+
/**
4+
* Holds if there exists a comment on the same line as `l`
5+
* that contains the substring "`kind`,`dep`,`ver`".
6+
*/
7+
predicate metadata(Locatable l, string kind, string mod, string dep, string ver) {
8+
exists(string f, int line, Comment c, string text |
9+
l.hasLocationInfo(f, line, _, _, _) and
10+
c.hasLocationInfo(f, line, _, _, _)
11+
|
12+
text = c.getText().regexpFind("\\b([^,\\s]+,[^,]+,[^,]+,[^,\\s]+)", _, _) and
13+
kind = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 1) and
14+
mod = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 2) and
15+
dep = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 3) and
16+
ver = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 4)
17+
)
18+
}
19+
20+
query predicate missingRequire(string mod, string dep, string ver, int line) {
21+
exists(Locatable l | metadata(l, "RequireLine", mod, dep, ver) |
22+
l.hasLocationInfo(_, line, _, _, _)
23+
) and
24+
not exists(GoModRequireLine req |
25+
req.getModulePath() = mod and
26+
req.getPath() = dep and
27+
req.getVersion() = ver and
28+
metadata(req, "RequireLine", mod, dep, ver) and
29+
req.hasLocationInfo(_, line, _, _, _)
30+
)
31+
}
32+
33+
query predicate missingExclude(string mod, string dep, string ver, int line) {
34+
exists(Locatable l | metadata(l, "ExcludeLine", mod, dep, ver) |
35+
l.hasLocationInfo(_, line, _, _, _)
36+
) and
37+
not exists(GoModExcludeLine exc |
38+
exc.getModulePath() = mod and
39+
exc.getPath() = dep and
40+
exc.getVersion() = ver and
41+
metadata(exc, "ExcludeLine", mod, dep, ver) and
42+
exc.hasLocationInfo(_, line, _, _, _)
43+
)
44+
}
45+
46+
/**
47+
* Holds if there exists a comment on the same line as `l`
48+
* that contains the substring "ReplaceLine,`mod`,`dep`,`dver`,`rep`,`rver`".
49+
*/
50+
predicate repmetadata(Locatable l, string mod, string dep, string dver, string rep, string rver) {
51+
exists(string f, int line, Comment c, string text |
52+
l.hasLocationInfo(f, line, _, _, _) and
53+
c.hasLocationInfo(f, line, _, _, _)
54+
|
55+
text = c.getText().regexpFind("\\b(ReplaceLine,[^,]*,[^,]*,[^,]*,[^,]*,[^,\\s]*)", _, _) and
56+
mod = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 1) and
57+
dep = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 2) and
58+
dver = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 3) and
59+
rep = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 4) and
60+
rver = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 5)
61+
)
62+
}
63+
64+
query predicate missingReplace(string mod, string dep, string dver, string rep, string rver, int line) {
65+
exists(Locatable l | repmetadata(l, mod, dep, dver, rep, rver) |
66+
l.hasLocationInfo(_, line, _, _, _)
67+
) and
68+
not exists(GoModReplaceLine repl |
69+
(
70+
rver = repl.getReplacementVersion()
71+
or
72+
not exists(repl.getReplacementVersion()) and
73+
rver = ""
74+
) and
75+
(
76+
dver = repl.getOriginalVersion()
77+
or
78+
not exists(repl.getOriginalVersion()) and
79+
dver = ""
80+
)
81+
|
82+
repl.getModulePath() = mod and
83+
repl.getOriginalPath() = dep and
84+
repl.getReplacementPath() = rep and
85+
repmetadata(repl, mod, dep, dver, rep, rver) and
86+
repl.hasLocationInfo(_, line, _, _, _)
87+
)
88+
}

ql/test/library-tests/semmle/go/GoModExpr/ReplaceLines.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/ReplaceLines.ql

Lines changed: 0 additions & 18 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/RequireLines.expected

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)