|
| 1 | +//! Tier violation checks for visibility-based release tiers |
| 2 | +//! |
| 3 | +//! Detects violations where lower-tier releases (e.g., OSS) depend on |
| 4 | +//! higher-tier crates (e.g., enterprise), which would break the release. |
| 5 | +
|
| 6 | +use crate::checks::trait_def::{Check, CheckContext, CheckResult, Severity}; |
| 7 | +use crate::core::config::{RailConfig, Visibility}; |
| 8 | +use crate::core::error::RailResult; |
| 9 | +use crate::graph::workspace_graph::WorkspaceGraph; |
| 10 | +use std::collections::HashSet; |
| 11 | + |
| 12 | +/// Check for tier violations in release configurations. |
| 13 | +/// |
| 14 | +/// A tier violation occurs when: |
| 15 | +/// - An OSS release includes or depends on an internal/enterprise crate |
| 16 | +/// - An internal release depends on an enterprise crate |
| 17 | +/// |
| 18 | +/// This ensures releases can be published without exposing higher-tier code. |
| 19 | +pub struct TierViolationCheck; |
| 20 | + |
| 21 | +impl Check for TierViolationCheck { |
| 22 | + fn name(&self) -> &str { |
| 23 | + "tier-violations" |
| 24 | + } |
| 25 | + |
| 26 | + fn description(&self) -> &str { |
| 27 | + "Checks for tier violations (OSS depending on internal/enterprise)" |
| 28 | + } |
| 29 | + |
| 30 | + fn run(&self, ctx: &CheckContext) -> RailResult<CheckResult> { |
| 31 | + // Load config |
| 32 | + let config = match RailConfig::load(&ctx.workspace_root) { |
| 33 | + Ok(c) => c, |
| 34 | + Err(_) => { |
| 35 | + // No config = no releases = no tier violations |
| 36 | + return Ok(CheckResult { |
| 37 | + check_name: self.name().to_string(), |
| 38 | + passed: true, |
| 39 | + message: "No rail.toml found (tier checks require releases config)".to_string(), |
| 40 | + suggestion: None, |
| 41 | + severity: Severity::Info, |
| 42 | + details: None, |
| 43 | + }); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // If no releases configured, skip check |
| 48 | + if config.releases.is_empty() { |
| 49 | + return Ok(CheckResult { |
| 50 | + check_name: self.name().to_string(), |
| 51 | + passed: true, |
| 52 | + message: "No releases configured (tier checks require releases)".to_string(), |
| 53 | + suggestion: None, |
| 54 | + severity: Severity::Info, |
| 55 | + details: None, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + // Load graph with config to get visibility annotations |
| 60 | + let graph = WorkspaceGraph::load_with_config(&ctx.workspace_root, Some(&config))?; |
| 61 | + |
| 62 | + let mut violations = Vec::new(); |
| 63 | + |
| 64 | + // Check each release for tier violations |
| 65 | + for release in &config.releases { |
| 66 | + // Get the primary crate for this release |
| 67 | + let primary_crate = release |
| 68 | + .crate_path |
| 69 | + .file_name() |
| 70 | + .and_then(|n| n.to_str()) |
| 71 | + .unwrap_or(&release.name); |
| 72 | + |
| 73 | + // Collect all crates in this release (primary + includes) |
| 74 | + let mut release_crates = HashSet::new(); |
| 75 | + release_crates.insert(primary_crate.to_string()); |
| 76 | + for included in &release.includes { |
| 77 | + release_crates.insert(included.clone()); |
| 78 | + } |
| 79 | + |
| 80 | + // For each crate in the release, check its dependencies |
| 81 | + for crate_name in &release_crates { |
| 82 | + // Get all transitive dependencies (what this crate depends on) |
| 83 | + let mut to_check = vec![crate_name.clone()]; |
| 84 | + let mut checked = HashSet::new(); |
| 85 | + let mut all_deps = HashSet::new(); |
| 86 | + |
| 87 | + while let Some(current) = to_check.pop() { |
| 88 | + if checked.contains(¤t) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + checked.insert(current.clone()); |
| 92 | + |
| 93 | + // Get direct dependencies |
| 94 | + if let Ok(deps) = graph.direct_dependencies(¤t) { |
| 95 | + for dep in deps { |
| 96 | + if !all_deps.contains(&dep) && graph.workspace_members().contains(&dep) { |
| 97 | + all_deps.insert(dep.clone()); |
| 98 | + to_check.push(dep); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Check each transitive dependency's visibility |
| 105 | + for dep in all_deps { |
| 106 | + let dep_visibilities = graph.crate_visibilities(&dep); |
| 107 | + |
| 108 | + // Skip if dependency has no visibility (not in any release) |
| 109 | + if dep_visibilities.is_empty() { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + // Skip self-references |
| 114 | + if dep == *crate_name { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + // Check for violations based on release visibility |
| 119 | + match release.visibility { |
| 120 | + Visibility::Oss => { |
| 121 | + // OSS can't depend on internal or enterprise |
| 122 | + if dep_visibilities.contains(&Visibility::Internal) || dep_visibilities.contains(&Visibility::Enterprise) |
| 123 | + { |
| 124 | + violations.push(format!( |
| 125 | + "Release '{}' (OSS) includes '{}' which depends on '{}' ({:?})", |
| 126 | + release.name, crate_name, dep, dep_visibilities |
| 127 | + )); |
| 128 | + } |
| 129 | + } |
| 130 | + Visibility::Internal => { |
| 131 | + // Internal can't depend on enterprise |
| 132 | + if dep_visibilities.contains(&Visibility::Enterprise) { |
| 133 | + violations.push(format!( |
| 134 | + "Release '{}' (internal) includes '{}' which depends on '{}' (enterprise)", |
| 135 | + release.name, crate_name, dep |
| 136 | + )); |
| 137 | + } |
| 138 | + } |
| 139 | + Visibility::Enterprise => { |
| 140 | + // Enterprise can depend on anything |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if violations.is_empty() { |
| 148 | + Ok(CheckResult { |
| 149 | + check_name: self.name().to_string(), |
| 150 | + passed: true, |
| 151 | + message: "No tier violations found".to_string(), |
| 152 | + suggestion: None, |
| 153 | + severity: Severity::Info, |
| 154 | + details: None, |
| 155 | + }) |
| 156 | + } else { |
| 157 | + Ok(CheckResult { |
| 158 | + check_name: self.name().to_string(), |
| 159 | + passed: false, |
| 160 | + message: format!( |
| 161 | + "Found {} tier violation(s):\n - {}", |
| 162 | + violations.len(), |
| 163 | + violations.join("\n - ") |
| 164 | + ), |
| 165 | + suggestion: Some( |
| 166 | + "Review release configurations and remove higher-tier dependencies, or adjust release visibility".to_string(), |
| 167 | + ), |
| 168 | + severity: Severity::Error, |
| 169 | + details: None, |
| 170 | + }) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +mod tests { |
| 177 | + use super::*; |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_check_name() { |
| 181 | + let check = TierViolationCheck; |
| 182 | + assert_eq!(check.name(), "tier-violations"); |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn test_check_without_config() { |
| 187 | + use std::env; |
| 188 | + |
| 189 | + let temp_dir = env::temp_dir().join("cargo-rail-test-tier-no-config"); |
| 190 | + let _ = std::fs::remove_dir_all(&temp_dir); |
| 191 | + std::fs::create_dir_all(&temp_dir).unwrap(); |
| 192 | + |
| 193 | + let ctx = CheckContext { |
| 194 | + workspace_root: temp_dir.clone(), |
| 195 | + crate_name: None, |
| 196 | + thorough: false, |
| 197 | + }; |
| 198 | + |
| 199 | + let check = TierViolationCheck; |
| 200 | + let result = check.run(&ctx).unwrap(); |
| 201 | + |
| 202 | + assert!(result.passed); |
| 203 | + assert!(result.message.contains("No rail.toml")); |
| 204 | + |
| 205 | + let _ = std::fs::remove_dir_all(&temp_dir); |
| 206 | + } |
| 207 | +} |
0 commit comments