Repository navigation
Scan files of all languages in a single opengrep run TAR-155 - #40
Conversation
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 6 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
Opengrep applies each rule only to the files of the languages it supports, and the language was never passed to it (-l was commented out), so grouping files by language only multiplied the number of opengrep processes, each re-loading the full ruleset. Run one batched scan over every file instead.
Pass -j to opengrep so a single pod does not burst past its CPU allocation.
bb40256 to
c836c19
Compare
There was a problem hiding this comment.
Pull Request Overview
👀 Worth a closer look
- The cgroup-based job calculation may ignore the process's actual cgroup and inherited quotas, causing excessive parallelism on some hosts.
- The shared file list is not reset between executions, so repeated runs can rescan prior files and duplicate results.
Codacy also found:
- No new static-analysis or duplication issues.
- Complexity increased in
internal/tool/jobs.go; tests were added. - Coverage data was unavailable for this PR.
Test suggestions
- Files from multiple languages and an unmapped extension are retained in the shared analysis list.
- Valid and invalid
OPENGREP_JOBSvalues are handled, including cgroup quota rounding and fallback behavior. - File lists larger than 1000 entries are split into multiple Opengrep invocations.
- A complete run invokes Opengrep once per batch with all languages represented and aggregates results.
- Repeated tool executions do not retain files from a previous execution.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. File lists larger than 1000 entries are split into multiple Opengrep invocations.
2. A complete run invokes Opengrep once per batch with all languages represented and aggregates results.
3. Repeated tool executions do not retain files from a previous execution.
Make this review better
TIP Add Codacy review.md instructions or an AGENTS.md so the reviewer knows your conventions.
TIP How was this review? Give us feedback
| if jobs, err := strconv.Atoi(os.Getenv(jobsEnvVar)); err == nil && jobs > 0 { | ||
| return jobs | ||
| } | ||
| if jobs, ok := cgroupJobs(cgroupV2CPUMax); ok { |
There was a problem hiding this comment.
🟡 MEDIUM RISK
Resolve the process's cgroup from /proc/self/cgroup and account for effective quotas across its ancestors before selecting the job count.
TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.
| func populateFilesToAnalyseFromFiles(toolExecutionFiles []string) error { | ||
| for _, file := range toolExecutionFiles { | ||
| addFileToFilesByLanguage(file) | ||
| addFileToAnalyse(file) |
There was a problem hiding this comment.
🟡 MEDIUM RISK
Reset the shared file list before each execution, or make it local to the execution, so repeated tool runs remain isolated.
TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.
| //"--error-recovery", | ||
| "--max-memory", "5000", | ||
| //"-j", strconv.Itoa(runtime.NumCPU()), | ||
| "-j", strconv.Itoa(opengrepJobs()), |
There was a problem hiding this comment.
Do we have ideia why was commented? Maybe something that existed on semgrep that was not available yet?
machadoit
left a comment
There was a problem hiding this comment.
Sounds fun to me! Maybe someone else has any input regarding passing the languages explicitly?
Summary
languageparameter,extensionToLanguageMapanddetectLanguage.-jto opengrep:OPENGREP_JOBSenv var if set, else cgroup v2 CPU quota (rounded up), elseruntime.NumCPU().Why
The language was never passed to opengrep (
-lwas commented out) and each rule only applies to files of its own languages, so grouping by language only multiplied opengrep processes, each re-loading the full ruleset. Companion change in codacy-worker makes it send all languages in one task. Without-j, a single pod could burst past its CPU request.Batching at 1000 files per invocation (ARG_MAX protection) is unchanged.
Note: files with unmapped extensions are no longer filtered out by the wrapper; every file the worker sends is scanned (opengrep still picks rules by language).
Linear: TAR-155
Test plan
go build ./...andgo test ./internal/tool/