syncable_cli/analyzer/hadolint/rules/
dl3029.rs

1//! DL3029: Use --platform flag with FROM for cross-architecture builds
2//!
3//! When building for multiple architectures, use --platform to be explicit.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
7use crate::analyzer::hadolint::shell::ParsedShell;
8use crate::analyzer::hadolint::types::Severity;
9
10pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
11    simple_rule(
12        "DL3029",
13        Severity::Warning,
14        "Do not use --platform flag with FROM unless you're building cross-platform images.",
15        |instr, _shell| {
16            // This rule is informational - it's the inverse of what you might expect
17            // It warns when --platform IS used, suggesting it may not be necessary
18            // unless specifically building cross-platform images
19
20            // For now, we'll make this a no-op and always pass
21            // The original hadolint rule is more nuanced about when to warn
22            match instr {
23                Instruction::From(_base) => {
24                    // Always pass - this is an informational rule about explicit platform use
25                    true
26                }
27                _ => true,
28            }
29        },
30    )
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use crate::analyzer::hadolint::config::HadolintConfig;
37    use crate::analyzer::hadolint::lint::{LintResult, lint};
38
39    fn lint_dockerfile(content: &str) -> LintResult {
40        lint(content, &HadolintConfig::default())
41    }
42
43    #[test]
44    fn test_from_without_platform() {
45        let result = lint_dockerfile("FROM ubuntu:20.04\nRUN echo hello");
46        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3029"));
47    }
48
49    #[test]
50    fn test_from_with_platform() {
51        let result = lint_dockerfile("FROM --platform=linux/amd64 ubuntu:20.04\nRUN echo hello");
52        // This is informational, not an error
53        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3029"));
54    }
55}