Skip to content

Commit c2ff69b

Browse files
committed
feat(lint): add stderr and ignore_exit_code options
Some linters (e.g., cpplint) output to stderr and exit non-zero when lint issues are found, which guard treats as a failure. This adds two options to handle such tools: - `stderr`: capture stderr instead of stdout - `ignore_exit_code`: don't treat non-zero exit as error Example usage: ```lua ft('cpp'):lint({ cmd = 'cpplint', fname = true, stderr = true, ignore_exit_code = true, parse = ..., }) ```
1 parent addb8d2 commit c2ff69b

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

lua/guard/_meta.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
---@field env table<string, string>?
2929
---@field timeout integer?
3030
---@field health function?
31+
---@field stderr boolean?
32+
---@field ignore_exit_code boolean?
3133

3234
---@alias LintConfig LintConfigTable|fun(): LintConfigTable
3335

lua/guard/lint.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local custom_ns = {}
1212
---@async
1313
---@param cmd string[]
1414
---@param cwd string
15-
---@param config {env: table?, timeout: integer?}
15+
---@param config {env: table?, timeout: integer?, stderr: boolean?, ignore_exit_code: boolean?}
1616
---@param input string|string[]
1717
---@return string output
1818
---@return {code: integer, stderr: string, cmd: string}? error
@@ -31,15 +31,15 @@ local function exec_linter(cmd, cwd, config, input)
3131
handle:write(nil)
3232
end)
3333

34-
if result.code ~= 0 and #result.stderr > 0 then
34+
if not config.ignore_exit_code and result.code ~= 0 and #result.stderr > 0 then
3535
return '', {
3636
code = result.code,
3737
stderr = result.stderr,
3838
cmd = cmd[1],
3939
}
4040
end
4141

42-
return result.stdout, nil
42+
return config.stderr and result.stderr or result.stdout, nil
4343
end
4444

4545
---@param buf number?

lua/guard/util.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ function M.toolcopy(c)
176176
timeout = c.timeout,
177177
parse = c.parse,
178178
health = c.health,
179+
stderr = c.stderr,
180+
ignore_exit_code = c.ignore_exit_code,
179181
}
180182
end
181183

0 commit comments

Comments
 (0)