Skip to content

GitHub Actions Validator — Rust source

Check a workflow YAML before you push: on-shape, jobs, runs-on, steps with run or uses, and the needs graph including cycle detection — every issue reported at its path. Runs locally in your browser.

This is the Rust implementation — the same logic the interactive tool runs, in a shareable, citable form.

// gh-actions-validator — GitHub Actions workflow validator. Rust port
// (canonical TS: src/lib/gh-actions-validator.ts; Go twin: cli/gh-actions-validator).
// Minimal YAML-subset parser (block maps, lists, scalars — stdlib has no
// YAML) then the rules: on/jobs presence, runs-on (or reusable uses), steps
// run-xor-uses, needs known + acyclic. Issues print as "path — message".
use std::collections::HashMap;
use std::io::Read;

#[derive(Default, Clone)]
struct Node {
    map: HashMap<String, Node>,
    list: Vec<Node>,
    scalar: Option<String>,
}

fn parse(lines: &[&str], i: &mut usize, indent: usize) -> Node {
    let mut n = Node::default();
    while *i < lines.len() {
        let raw = lines[*i];
        let t = raw.trim();
        if t.is_empty() || t.starts_with('#') {
            *i += 1;
            continue;
        }
        let ind = raw.len() - raw.trim_start().len();
        if ind < indent {
            break;
        }
        if let Some(rest) = t.strip_prefix("- ") {
            *i += 1;
            let mut child = Node::default();
            child.scalar = Some(rest.trim().trim_matches(|c| c == '"' || c == '\'').to_string());
            n.list.push(child);
            continue;
        }
        let Some(colon) = t.find(':') else {
            *i += 1;
            continue;
        };
        let key = t[..colon].trim().to_string();
        let rest = t[colon + 1..].trim();
        if rest.is_empty() {
            *i += 1;
            let child = parse(lines, i, indent + 2);
            n.map.insert(key, child);
        } else {
            *i += 1;
            let mut child = Node::default();
            child.scalar = Some(rest.trim_matches(|c| c == '"' || c == '\'').to_string());
            n.map.insert(key, child);
        }
    }
    n
}

fn needs_cycle(jobs: &HashMap<String, Node>) -> Option<String> {
    fn visit(id: &str, jobs: &HashMap<String, Node>, color: &mut HashMap<String, u8>, stack: &mut Vec<String>) -> Option<String> {
        if *color.get(id).unwrap_or(&0) == 2 {
            return None;
        }
        if *color.get(id).unwrap_or(&0) == 1 {
            let from = stack.iter().position(|s| s == id)?;
            let chain: Vec<String> = stack[from..].iter().chain(std::iter::once(&id.to_string())).cloned().collect();
            return Some(chain.join(" -> "));
        }
        color.insert(id.to_string(), 1);
        stack.push(id.to_string());
        if let Some(job) = jobs.get(id) {
            let mut needs: Vec<String> = Vec::new();
            if let Some(n) = job.map.get("needs") {
                if !n.list.is_empty() {
                    needs = n.list.iter().filter_map(|c| c.scalar.clone()).collect();
                } else if let Some(s) = &n.scalar {
                    needs = vec![s.clone()];
                }
            }
            for need in needs {
                if jobs.contains_key(&need) {
                    if let Some(c) = visit(&need, jobs, color, stack) {
                        return Some(c);
                    }
                }
            }
        }
        stack.pop();
        color.insert(id.to_string(), 2);
        None
    }
    let mut color = HashMap::new();
    let mut stack = Vec::new();
    for id in jobs.keys() {
        if let Some(c) = visit(id, jobs, &mut color, &mut stack) {
            return Some(c);
        }
    }
    None
}

fn main() {
    let mut src = String::new();
    std::io::stdin().read_to_string(&mut src).unwrap();
    let lines: Vec<&str> = src.lines().collect();
    let mut i = 0;
    let root = parse(&lines, &mut i, 0);
    let mut issues: Vec<(String, String)> = Vec::new();

    if !root.map.contains_key("on") {
        issues.push(("$".into(), "missing required key \"on\"".into()));
    }
    let jobs = root.map.get("jobs");
    if jobs.is_none() {
        issues.push(("$".into(), "missing required key \"jobs\"".into()));
    } else {
        let jobs = jobs.unwrap();
        if jobs.map.is_empty() {
            issues.push(("$.jobs".into(), "jobs must not be empty".into()));
        }
        for (id, job) in &jobs.map {
            let at = format!("$.jobs.{}", id);
            let reusable = job.map.get("uses").and_then(|u| &u.scalar).is_some();
            if !reusable && !job.map.contains_key("runs-on") {
                issues.push((format!("{}.runs-on", at), "job needs runs-on (or a reusable-workflow uses)".into()));
            }
            if !reusable && !job.map.contains_key("steps") {
                issues.push((format!("{}.steps", at), "job needs steps".into()));
            }
            if let Some(steps) = job.map.get("steps") {
                if !steps.list.is_empty() {
                    for (k, step) in steps.list.iter().enumerate() {
                        let has_run = step.map.get("run").and_then(|r| &r.scalar).is_some();
                        let has_uses = step.map.get("uses").and_then(|u| &u.scalar).is_some();
                        if !has_run && !has_uses {
                            issues.push((format!("{}.steps[{}]", at, k), "step needs run or uses".into()));
                        }
                        if has_run && has_uses {
                            issues.push((format!("{}.steps[{}]", at, k), "step has both run and uses - pick one".into()));
                        }
                    }
                }
            }
            if let Some(needs) = job.map.get("needs") {
                let names: Vec<String> = if !needs.list.is_empty() {
                    needs.list.iter().filter_map(|c| c.scalar.clone()).collect()
                } else if let Some(s) = &needs.scalar {
                    vec![s.clone()]
                } else {
                    vec![]
                };
                for (k, n) in names.iter().enumerate() {
                    if !jobs.map.contains_key(n) {
                        issues.push((format!("{}.needs[{}]", at, k), format!("unknown job \"{}\"", n)));
                    }
                }
            }
        }
        if let Some(cycle) = needs_cycle(&jobs.map) {
            issues.push(("$.jobs".into(), format!("needs cycle: {}", cycle)));
        }
    }
    for (p, m) in &issues {
        println!("{} — {}", p, m);
    }
    if issues.is_empty() {
        println!("valid");
    }
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →