11local h = require (" null-ls.helpers" )
22local methods = require (" null-ls.methods" )
3- local u = require (" null-ls.utils" )
43
54local DIAGNOSTICS_ON_SAVE = methods .internal .DIAGNOSTICS_ON_SAVE
65
@@ -12,14 +11,35 @@ local severities = {
1211 UNKNOWN = h .diagnostics .severities [" information" ],
1312}
1413
14+ -- NOTE: (vkhitrin) custom logic to derive the directory name for trivy execution:
15+ -- * If buffer is inside a helm chart, attempt to set the directory to the directory
16+ -- containing Chart.yaml.
17+ -- * Otherwise, set the directory to none-ls' '$DIRNAME'.
18+ local trivy_working_dir = function ()
19+ local filetype = vim .bo .filetype
20+ if filetype == " helm" then
21+ local dir = vim .fn .expand (" %:p:h" )
22+ while dir ~= " /" do
23+ local chart_path = dir .. " /Chart.yaml"
24+ if vim .fn .filereadable (chart_path ) == 1 then
25+ return dir
26+ end
27+ dir = vim .fn .fnamemodify (dir , " :h" )
28+ end
29+ return dir
30+ else
31+ return " $DIRNAME"
32+ end
33+ end
34+
1535return h .make_builtin ({
1636 name = " trivy" ,
1737 meta = {
1838 url = " https://github.com/aquasecurity/trivy" ,
1939 description = " Find misconfigurations and vulnerabilities" ,
2040 },
2141 method = DIAGNOSTICS_ON_SAVE ,
22- filetypes = { " terraform" , " tf" , " terraform-vars" },
42+ filetypes = { " terraform" , " tf" , " terraform-vars" , " helm " , " dockerfile " },
2343 generator_opts = {
2444 command = " trivy" ,
2545 timeout = 30000 , -- Trivy can be slow, so increase timeout
@@ -29,7 +49,7 @@ return h.make_builtin({
2949 " --format" ,
3050 " json" ,
3151 " --quiet" ,
32- " $DIRNAME " ,
52+ trivy_working_dir () ,
3353 }
3454
3555 local config_file_path = vim .fs .find (" trivy.yaml" , {
@@ -55,8 +75,8 @@ return h.make_builtin({
5575 cwd = h .cache .by_bufnr (function (params )
5676 return vim .fs .dirname (params .bufname )
5777 end ),
58- from_stderr = false , -- Trivy outputs logs to stderr that even --quiet doesn't silence
59- ignore_stderr = true ,
78+ from_stderr = true , -- https://github.com/aquasecurity/trivy/pull/2289
79+ ignore_stderr = false ,
6080 to_stdin = false ,
6181 multiple_files = true ,
6282 format = " json" ,
@@ -82,12 +102,14 @@ return h.make_builtin({
82102 for _ , result in pairs (params .output .Results or {}) do
83103 for _ , misconfiguration in ipairs (result .Misconfigurations or {}) do
84104 local rewritten_diagnostic = {
85- message = misconfiguration .ID .. " - " .. misconfiguration .Title ,
105+ code = misconfiguration .ID ,
106+ message = misconfiguration .Title ,
86107 row = misconfiguration .CauseMetadata .StartLine ,
108+ end_row = misconfiguration .CauseMetadata .EndLine ,
87109 col = 0 ,
88110 source = " trivy" ,
89111 severity = severities [misconfiguration .Severity ],
90- filename = u . path . join ( params . cwd , result .Target ) ,
112+ filename = result .Target ,
91113 }
92114 table.insert (combined_diagnostics , rewritten_diagnostic )
93115 end
0 commit comments