From eb9899b7ddc98ecebdf52b5989a49ef47a253f1a Mon Sep 17 00:00:00 2001 From: dmillzilla <37204972+dmillzilla@users.noreply.github.com> Date: Sat, 7 Nov 2026 12:57:56 -0600 Subject: [PATCH] Honor task ignore_errors in risky-shell-pipe checks --- CHANGELOG.md | 3 + src/extra_rules.rs | 14 +- tests/conditional_checks.rs | 38 ++++ tests/fixtures/conditional_checks.json | 291 +++++++++++++++++++++++++ 4 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 tests/conditional_checks.rs create mode 100644 tests/fixtures/conditional_checks.json diff --git a/CHANGELOG.md b/CHANGELOG.md index db08e86..7bc2744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Exempt shell pipelines with truthy task-level `ignore_errors` from + `risky-shell-pipe`, preserving independent failure-handling checks. + - Resolve role-relative task includes with Ansible role and playbook fallbacks, including targets already prefixed with `tasks/`. - Keep quoted text and shell comments from faking an enabled `pipefail`. diff --git a/src/extra_rules.rs b/src/extra_rules.rs index 6e512de..044cbb7 100644 --- a/src/extra_rules.rs +++ b/src/extra_rules.rs @@ -208,7 +208,19 @@ pub fn task<'a>(task: &'a Node, action: &Action<'a>, emit: &mut impl FnMut(&str, let powershell = action .argument("executable") .is_some_and(|value| value.contains("pwsh") || value.contains("powershell")); - if !powershell && has_pipeline(&command) && !has_pipefail { + // The pinned rule tests task-level Python truthiness, including + // nonempty template strings; it does not evaluate the condition. + let ignores_errors = task + .get("ignore_errors") + .is_some_and(|node| match node.to_json() { + serde_json::Value::Null => false, + serde_json::Value::Bool(value) => value, + serde_json::Value::String(value) => !value.is_empty(), + serde_json::Value::Number(value) => value.as_f64() != Some(0.0), + serde_json::Value::Array(value) => !value.is_empty(), + serde_json::Value::Object(value) => !value.is_empty(), + }); + if !powershell && !ignores_errors && has_pipeline(&command) && !has_pipefail { emit( "risky-shell-pipe", "Set pipefail so an earlier pipeline failure is not hidden", diff --git a/tests/conditional_checks.rs b/tests/conditional_checks.rs new file mode 100644 index 0000000..dc92417 --- /dev/null +++ b/tests/conditional_checks.rs @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +use serde_json::Value; +use std::{fs, process::Command}; + +#[test] +fn ignored_shell_pipelines_and_implicit_conditionals_keep_scoped_checks() { + let cases: Value = + serde_json::from_str(include_str!("fixtures/conditional_checks.json")).unwrap(); + for case in cases.as_array().unwrap() { + let directory = tempfile::tempdir().unwrap(); + let file = case["file"].as_str().unwrap(); + let path = directory.path().join(file); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, case["source"].as_str().unwrap()).unwrap(); + fs::write( + directory.path().join(".ansible-lint"), + case["config"].as_str().unwrap(), + ) + .unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_oxible")) + .current_dir(directory.path()) + .env("PATH", "") + .args([file, "--offline", "--no-cache", "-f", "json"]) + .output() + .unwrap(); + let issues: Value = serde_json::from_slice(&output.stdout).unwrap(); + // Retain implicit task-file spacing: pinned ansible-lint checks the + // singular kind "task" instead of its actual "tasks" classification. + // Playbook checks confirm these are useful, valid formatting findings. + assert_eq!(issues, case["expected"]["issues"], "{}", case["case"]); + assert_eq!( + output.status.code().map(i64::from), + case["expected"]["exit"].as_i64(), + "{}", + case["case"] + ); + } +} diff --git a/tests/fixtures/conditional_checks.json b/tests/fixtures/conditional_checks.json new file mode 100644 index 0000000..250b3e0 --- /dev/null +++ b/tests/fixtures/conditional_checks.json @@ -0,0 +1,291 @@ +[ + { + "case": "pipe-true", + "file": "tasks/main.yml", + "source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: true\n changed_when: false\n", + "config": "offline: true\n", + "expected": { + "exit": 2, + "issues": [ + { + "type": "issue", + "check_name": "ignore-errors", + "categories": [ + "unpredictability" + ], + "url": "https://docs.ansible.com/projects/lint/rules/ignore-errors/", + "severity": "major", + "description": "Use failed_when and specify error conditions instead of using ignore_errors.", + "fingerprint": "404f3e389d3edc8dc3b8c5fa40e1cafd9b6b79fa1006e8e2a8154970fc1f14f1", + "location": { + "path": "tasks/main.yml", + "lines": { + "begin": 2 + } + }, + "content": { + "body": "Task/Handler: Check for a matching entry" + } + } + ] + } + }, + { + "case": "pipe-false", + "file": "tasks/main.yml", + "source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: false\n changed_when: false\n", + "config": "offline: true\n", + "expected": { + "exit": 2, + "issues": [ + { + "type": "issue", + "check_name": "risky-shell-pipe", + "categories": [ + "command-shell" + ], + "url": "https://docs.ansible.com/projects/lint/rules/risky-shell-pipe/", + "severity": "major", + "description": "Shells that use pipes should set the pipefail option.", + "fingerprint": "ee7cba85ddf560b3ee5ac65fa53e264a18350f22ea071bc96fb472f0866683ce", + "location": { + "path": "tasks/main.yml", + "lines": { + "begin": 2 + } + }, + "content": { + "body": "Task/Handler: Check for a matching entry" + } + } + ] + } + }, + { + "case": "pipe-omitted", + "file": "tasks/main.yml", + "source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n changed_when: false\n", + "config": "offline: true\n", + "expected": { + "exit": 2, + "issues": [ + { + "type": "issue", + "check_name": "risky-shell-pipe", + "categories": [ + "command-shell" + ], + "url": "https://docs.ansible.com/projects/lint/rules/risky-shell-pipe/", + "severity": "major", + "description": "Shells that use pipes should set the pipefail option.", + "fingerprint": "ee7cba85ddf560b3ee5ac65fa53e264a18350f22ea071bc96fb472f0866683ce", + "location": { + "path": "tasks/main.yml", + "lines": { + "begin": 2 + } + }, + "content": { + "body": "Task/Handler: Check for a matching entry" + } + } + ] + } + }, + { + "case": "pipe-check-mode", + "file": "tasks/main.yml", + "source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: \"{{ ansible_check_mode }}\"\n changed_when: false\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [] + } + }, + { + "case": "pipe-registered", + "file": "tasks/main.yml", + "source": "---\n- name: Check for a matching entry\n ansible.builtin.shell: printf '%s\\n' example | grep example\n ignore_errors: true\n register: result\n changed_when: false\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [] + } + }, + { + "case": "implicit-tasks-gt-False", + "file": "tasks/main.yml", + "source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int > 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [ + { + "categories": [ + "formatting" + ], + "check_name": "jinja[spacing]", + "content": { + "body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int > 13`." + }, + "description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int > 13 -> ansible_distribution_major_version | int > 13", + "fingerprint": "e6e716ebd95046c182ff363a1496e84dbe6c26d38265218407c49fdd51ee6e4e", + "location": { + "path": "tasks/main.yml", + "positions": { + "begin": { + "column": 9, + "line": 5 + } + } + }, + "severity": "minor", + "type": "issue", + "url": "https://docs.ansible.com/projects/lint/rules/jinja/" + } + ] + } + }, + { + "case": "implicit-tasks-gt-True", + "file": "tasks/main.yml", + "source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int > 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [] + } + }, + { + "case": "implicit-playbook-gt-False", + "file": "site.yml", + "source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int > 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [ + { + "type": "issue", + "check_name": "jinja[spacing]", + "categories": [ + "formatting" + ], + "url": "https://docs.ansible.com/projects/lint/rules/jinja/", + "severity": "minor", + "description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int > 13 -> ansible_distribution_major_version | int > 13", + "fingerprint": "7cdf934691bce1eb1228c8cbe9fe03c72538f5e2d1cf5a226d345840562b3d2c", + "location": { + "path": "site.yml", + "positions": { + "begin": { + "line": 9, + "column": 13 + } + } + }, + "content": { + "body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int > 13`." + } + } + ] + } + }, + { + "case": "implicit-playbook-gt-True", + "file": "site.yml", + "source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int > 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [] + } + }, + { + "case": "implicit-tasks-ge-False", + "file": "tasks/main.yml", + "source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int >= 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [ + { + "categories": [ + "formatting" + ], + "check_name": "jinja[spacing]", + "content": { + "body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int >= 13`." + }, + "description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int >= 13 -> ansible_distribution_major_version | int >= 13", + "fingerprint": "c6b957d9f56d62fd7193c925219acb1f3f5ebb3d4745862ebc3fb6a61522e4c5", + "location": { + "path": "tasks/main.yml", + "positions": { + "begin": { + "column": 9, + "line": 5 + } + } + }, + "severity": "minor", + "type": "issue", + "url": "https://docs.ansible.com/projects/lint/rules/jinja/" + } + ] + } + }, + { + "case": "implicit-tasks-ge-True", + "file": "tasks/main.yml", + "source": "---\n- name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int >= 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [] + } + }, + { + "case": "implicit-playbook-ge-False", + "file": "site.yml", + "source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version|int >= 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [ + { + "type": "issue", + "check_name": "jinja[spacing]", + "categories": [ + "formatting" + ], + "url": "https://docs.ansible.com/projects/lint/rules/jinja/", + "severity": "minor", + "description": "Jinja2 spacing could be improved: ansible_distribution_major_version|int >= 13 -> ansible_distribution_major_version | int >= 13", + "fingerprint": "644cb48fc09ed8ba0310093bb5e0545054855e2905f00a1cae4e7c83ff7a232f", + "location": { + "path": "site.yml", + "positions": { + "begin": { + "line": 9, + "column": 13 + } + } + }, + "content": { + "body": "Jinja2 template rewrite recommendation: `ansible_distribution_major_version | int >= 13`." + } + } + ] + } + }, + { + "case": "implicit-playbook-ge-True", + "file": "site.yml", + "source": "---\n- name: Test conditionals\n hosts: localhost\n gather_facts: false\n tasks:\n - name: Show a message conditionally\n ansible.builtin.debug:\n msg: hello\n when: ansible_distribution_major_version | int >= 13\n", + "config": "offline: true\n", + "expected": { + "exit": 0, + "issues": [] + } + } +]