Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Unreleased

- 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`.
- Exempt Jinja blocks and comments from `no-tabs` while preserving Jinja checks.
- Check commands supplied through `_raw_params` for module recommendations.

## 0.1.2 — 2026-11-06

- Check templated `ignore_errors` settings while preserving the check-mode and
Expand Down
126 changes: 125 additions & 1 deletion src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,60 @@ fn literal(
.filter(|file| !file.is_empty() && file != "/dev/null")
{
result.push(Dependency {
path: path.parent().unwrap_or(Path::new(".")).join(file),
path: if kind == FileKind::Tasks {
task_path(path, &file, variables)
} else {
path.parent().unwrap_or(Path::new(".")).join(file)
},
kind,
position,
plugin_base: None,
});
}
}

/// Resolve static task targets without executing Ansible. Keep the current
/// include directory first for nested includes, then use the role/task and
/// loader-basedir fallbacks from DataLoader.path_dwim_relative.
pub(crate) fn task_path(path: &Path, file: &str, variables: &serde_json::Value) -> PathBuf {
let direct = path.parent().unwrap_or(Path::new(".")).join(file);
if Path::new(file).is_absolute() {
return direct;
}
let role = variables
.get("role_path")
.and_then(serde_json::Value::as_str)
.map(Path::new);
let cwd = std::env::current_dir().ok();
let base = variables
.get("playbook_dir")
.and_then(serde_json::Value::as_str)
.map(Path::new)
.or(cwd.as_deref());
let mut candidates = vec![direct.clone()];
if let Some(role) = role {
candidates.push(role.join("tasks").join(file));
}
// DataLoader's unfrackpath("tasks/<target>") uses the process working
// directory, whereas its later path_dwim fallback uses the loader basedir.
if role.is_some()
&& let Some(cwd) = &cwd
{
candidates.push(cwd.join("tasks").join(file));
}
if let Some(role) = role {
candidates.push(role.join(file));
}
if let Some(base) = base {
candidates.push(base.join("tasks").join(file));
candidates.push(base.join(file));
}
candidates
.into_iter()
.find(|p| p.exists())
.unwrap_or(direct)
}

fn local_role(
path: &Path,
role: Option<&str>,
Expand Down Expand Up @@ -237,3 +283,81 @@ fn local_role(
}
}
}

#[cfg(test)]
mod tests {
use super::task_path;
use serde_json::json;
use std::{fs, path::Path};

#[test]
fn role_task_search_order_matches_dataloader_without_execution() {
// Expected selections verified with ansible-core 2.21.5
// DataLoader.path_dwim_relative(role, "tasks", target, is_role=True).
for (target, files, selected) in [
(
"tasks/configure.yml",
vec!["roles/example/tasks/configure.yml"],
"roles/example/tasks/configure.yml",
),
(
"tasks/configure.yml",
vec![
"roles/example/tasks/tasks/configure.yml",
"roles/example/tasks/configure.yml",
],
"roles/example/tasks/tasks/configure.yml",
),
(
"tasks/configure.yml",
vec![
"tasks/tasks/configure.yml",
"roles/example/tasks/configure.yml",
],
"roles/example/tasks/configure.yml",
),
(
"configure.yml",
vec![
"roles/example/tasks/configure.yml",
"roles/example/configure.yml",
"configure.yml",
],
"roles/example/tasks/configure.yml",
),
(
"configure.yml",
vec!["tasks/configure.yml", "roles/example/configure.yml"],
"roles/example/configure.yml",
),
(
"configure.yml",
vec!["roles/example/configure.yml", "configure.yml"],
"roles/example/configure.yml",
),
("configure.yml", vec!["configure.yml"], "configure.yml"),
] {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
for file in files {
let path = root.join(file);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, "---\n[]\n").unwrap();
}
let vars = json!({"role_path": root.join("roles/example"), "playbook_dir": root});
assert_eq!(
task_path(&root.join("roles/example/tasks/main.yml"), target, &vars),
root.join(selected)
);
let absolute = root.join(selected);
assert_eq!(
task_path(
Path::new("unrelated/main.yml"),
absolute.to_str().unwrap(),
&vars
),
absolute
);
}
}
}
66 changes: 59 additions & 7 deletions src/extra_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,7 @@ pub fn task<'a>(task: &'a Node, action: &Action<'a>, emit: &mut impl FnMut(&str,
&& let Some(command) = action.command()
{
let command = crate::template::mask_expressions(command);
let has_pipefail = command.split([';', '\n']).any(|statement| {
let words: Vec<_> = statement.split_whitespace().collect();
words.first() == Some(&"set")
&& words.windows(2).any(|pair| {
pair[0].starts_with('-') && pair[0].ends_with('o') && pair[1] == "pipefail"
})
});
let has_pipefail = has_pipefail(&command);
let powershell = action
.argument("executable")
.is_some_and(|value| value.contains("pwsh") || value.contains("powershell"));
Expand Down Expand Up @@ -421,3 +415,61 @@ fn has_pipeline(text: &str) -> bool {
}
false
}

// Shell separators and comments only delimit statements outside quoted words.
// Preserve word boundaries so data containing `set -o pipefail` cannot disable
// the pipeline check. This parser never invokes a shell.
fn has_pipefail(text: &str) -> bool {
let mut quote = None;
let mut escape = false;
let mut comment = false;
let mut statement = String::new();
let enabled = |statement: &str| {
let Ok(words) = shell_words::split(statement) else {
return false;
};
words.first().is_some_and(|word| word == "set")
&& words.windows(2).any(|pair| {
pair[0].starts_with('-') && pair[0].ends_with('o') && pair[1] == "pipefail"
})
};
let chars: Vec<_> = text.chars().collect();
for (index, &ch) in chars.iter().enumerate() {
if comment {
if ch != '\n' {
continue;
}
comment = false;
}
if escape {
statement.push(ch);
escape = false;
continue;
}
if ch == '\\' && quote != Some('\'') {
statement.push(ch);
escape = true;
continue;
}
if quote.is_none()
&& ch == '#'
&& (index == 0 || chars[index - 1].is_whitespace() || chars[index - 1] == ';')
{
comment = true;
} else if quote == Some(ch) {
quote = None;
statement.push(ch);
} else if quote.is_none() && matches!(ch, '\'' | '"') {
quote = Some(ch);
statement.push(ch);
} else if quote.is_none() && matches!(ch, ';' | '\n') {
if enabled(&statement) {
return true;
}
statement.clear();
} else {
statement.push(ch);
}
}
enabled(&statement)
}
6 changes: 5 additions & 1 deletion src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,11 @@ impl Loader<'_> {
if let Some(file) =
crate::template::render_path(value, vars, &self.options.settings.collection_index)
{
let target = path.parent().unwrap_or(Path::new(".")).join(file);
let target = if kind == FileKind::Tasks {
crate::dependencies::task_path(path, &file, vars)
} else {
path.parent().unwrap_or(Path::new(".")).join(file)
};
if loaded && !target.is_file() {
self.graph.diagnostics.push(Diagnostic::error(
path,
Expand Down
1 change: 1 addition & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ impl<'a> Action<'a> {
}
})
.or_else(|| self.argument("cmd"))
.or_else(|| self.argument("_raw_params"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/static_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ fn tabs<'a>(
) {
match &node.kind {
crate::yaml::Kind::Scalar { value, .. }
if value.contains('\t') && !value.contains("{{") =>
if value.contains('\t') && !regex!(r"(?s)\{[\{%#].*[%#\}]\}").is_match(value) =>
{
emit("no-tabs", "Use spaces instead of tabs", node, container)
}
Expand Down
Loading
Loading