Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
xtask: Add validation layer for GPUI commands (#64869)
Finn Evers447f72e5167e
diff --git a/Cargo.lock b/Cargo.lockindex 47b48dc74d8..12aae028775 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -22562,6 +22562,8 @@ dependencies = ["serde_json","serde_yaml","strum 0.28.0",+ "tempfile",+ "thiserror 2.0.17","tokio","toml 0.8.23","toml_edit 0.22.27",diff --git a/tooling/xtask/Cargo.toml b/tooling/xtask/Cargo.tomlindex 5b6b5ecabc0..591ef5c0abe 100644--- a/tooling/xtask/Cargo.toml+++ b/tooling/xtask/Cargo.toml@@ -26,7 +26,11 @@ serde.workspace = trueserde_json.workspace = trueserde_yaml = "0.9.34"strum.workspace = true+thiserror.workspace = truetokio = { workspace = true, features = ["rt", "rt-multi-thread"] }toml.workspace = truetoml_edit.workspace = trueurl.workspace = true++[dev-dependencies]+tempfile.workspace = truediff --git a/tooling/xtask/src/tasks/gpui.rs b/tooling/xtask/src/tasks/gpui.rsindex 76bd52803bf..5ace1481541 100644--- a/tooling/xtask/src/tasks/gpui.rs+++ b/tooling/xtask/src/tasks/gpui.rs@@ -2,6 +2,7 @@pub mod crate_graph;pub mod publish_plan;+pub mod validation;use std::io::{self, Write};use std::process::{Command, Output, Stdio};@@ -15,7 +16,7 @@ use crate::tasks::workflows::GitSha;#[derive(Subcommand)]pub enum GpuiCommand {/// Print the publish plan in dependency-first order- List(GpuiArgs),+ Plan(GpuiArgs),/// Show the dependency paths that put a crate in the publish listWhy {/// Original Cargo package name@@ -45,8 +46,9 @@ struct PublishGpuiArgs {pub fn run_gpui(command: GpuiCommand) -> Result<()> {let graph = crate_graph::load_workspace_graph()?;match command {- GpuiCommand::List(args) => {+ GpuiCommand::Plan(args) => {let plan = publish_plan::build_publish_plan(&graph)?;+ validation::validate(&plan)?;println!("GPUI publish plan ({} crates, dependencies first):",plan.len()@@ -408,23 +410,23 @@ mod tests {use super::*;#[test]- fn gpui_list_accepts_an_explicit_workspace_sha() -> Result<()> {+ fn gpui_plan_accepts_an_explicit_workspace_sha() -> Result<()> {let sha = "0123456789abcdef0123456789abcdef01234567";- let args = crate::Args::try_parse_from(["xtask", "gpui", "list", "--sha", sha])?;- let crate::CliCommand::Gpui(GpuiCommand::List(args)) = args.command else {- bail!("expected the GPUI list command");+ let args = crate::Args::try_parse_from(["xtask", "gpui", "plan", "--sha", sha])?;+ let crate::CliCommand::Gpui(GpuiCommand::Plan(args)) = args.command else {+ bail!("expected the GPUI plan command");};assert_eq!(args.sha.as_ref(), sha);Ok(())}#[test]- fn gpui_list_requires_a_full_workspace_sha() {+ fn gpui_plan_requires_a_full_workspace_sha() {assert!(crate::Args::try_parse_from(["xtask", "gpui"]).is_err());- assert!(crate::Args::try_parse_from(["xtask", "gpui", "list"]).is_err());+ assert!(crate::Args::try_parse_from(["xtask", "gpui", "plan"]).is_err());for sha in ["HEAD", "abc", "g123456789abcdef0123456789abcdef01234567"] {assert!(- crate::Args::try_parse_from(["xtask", "gpui", "list", "--sha", sha]).is_err(),+ crate::Args::try_parse_from(["xtask", "gpui", "plan", "--sha", sha]).is_err(),"{sha}");}diff --git a/tooling/xtask/src/tasks/gpui/validation.rs b/tooling/xtask/src/tasks/gpui/validation.rsnew file mode 100644index 00000000000..b25a772ebe5--- /dev/null+++ b/tooling/xtask/src/tasks/gpui/validation.rs@@ -0,0 +1,112 @@+//! Rules that the crates selected for a GPUI release have to satisfy.+//!+//! To add a rule, implement [`Rule`] in a submodule and list it in [`RULES`].++mod apache_licensed;+mod unique_publish_names;++use std::fmt;++use thiserror::Error;++use crate::tasks::gpui::publish_plan::PublishPlanEntry;++const RULES: &[&dyn Rule] = &[+ &apache_licensed::ApacheLicensed,+ &unique_publish_names::UniquePublishNames,+];++/// A check over the crates selected for a GPUI release.+pub(crate) trait Rule {+ /// What the rule guarantees, used to label the violations it reports.+ fn name(&self) -> &'static str;++ /// Returns one violation per crate that breaks the rule.+ fn check(&self, plan: &[PublishPlanEntry<'_>]) -> Vec<Violation>;+}++/// Why a crate does not satisfy a [`Rule`].+pub(crate) type Violation = Box<dyn std::error::Error + Send + Sync>;++/// Runs every rule and reports all violations at once, so that a single run+/// surfaces everything that needs fixing.+pub fn validate(plan: &[PublishPlanEntry<'_>]) -> Result<(), ValidationError> {+ validate_with(RULES, plan)+}++fn validate_with(+ rules: &[&dyn Rule],+ plan: &[PublishPlanEntry<'_>],+) -> Result<(), ValidationError> {+ let violations = rules+ .iter()+ .flat_map(|rule| {+ rule.check(plan)+ .into_iter()+ .map(|violation| (rule.name(), violation))+ })+ .collect::<Vec<_>>();++ if violations.is_empty() {+ return Ok(());+ }+ Err(ValidationError { violations })+}++#[derive(Debug, Error)]+pub struct ValidationError {+ violations: Vec<(&'static str, Violation)>,+}++impl fmt::Display for ValidationError {+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {+ write!(+ formatter,+ "found {count} violation(s) in the GPUI crate graph:",+ count = self.violations.len()+ )?;+ for (rule, violation) in &self.violations {+ write!(formatter, "\n [{rule}] {violation}")?;+ }+ Ok(())+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[derive(Debug, Error)]+ #[error("{0} is held together with tape")]+ struct Ramshackle(&'static str);++ struct Failing;++ impl Rule for Failing {+ fn name(&self) -> &'static str {+ "crates are structurally sound"+ }++ fn check(&self, _: &[PublishPlanEntry<'_>]) -> Vec<Violation> {+ vec![+ Violation::from(Ramshackle("util")),+ Violation::from(Ramshackle("gpui")),+ ]+ }+ }++ #[test]+ fn labels_each_violation_with_the_rule_that_found_it() {+ assert!(validate_with(&[], &[]).is_ok());++ let error =+ validate_with(&[&Failing], &[]).expect_err("expected the failing rule to be reported");+ assert_eq!(+ error.to_string(),+ indoc::indoc! {"+ found 2 violation(s) in the GPUI crate graph:+ [crates are structurally sound] util is held together with tape+ [crates are structurally sound] gpui is held together with tape"}+ );+ }+}diff --git a/tooling/xtask/src/tasks/gpui/validation/apache_licensed.rs b/tooling/xtask/src/tasks/gpui/validation/apache_licensed.rsnew file mode 100644index 00000000000..73cae379096--- /dev/null+++ b/tooling/xtask/src/tasks/gpui/validation/apache_licensed.rs@@ -0,0 +1,155 @@+use std::fs;+use std::path::Path;++use thiserror::Error;++use super::{Rule, Violation};+use crate::tasks::gpui::publish_plan::PublishPlanEntry;++const APACHE_LICENSE: &str = "Apache-2.0";+const APACHE_LICENSE_FILE: &str = "LICENSE-APACHE";++/// License files that contradict [`APACHE_LICENSE_FILE`] when present alongside it.+const CONFLICTING_LICENSE_FILES: &[&str] = &["LICENSE-GPL", "LICENSE-AGPL"];++pub struct ApacheLicensed;++impl Rule for ApacheLicensed {+ fn name(&self) -> &'static str {+ "workspace crates are Apache-2.0 licensed"+ }++ fn check(&self, plan: &[PublishPlanEntry<'_>]) -> Vec<Violation> {+ plan.iter()+ .filter(|entry| entry.package.in_workspace())+ .filter_map(|entry| {+ let directory = entry.package.manifest_path().as_std_path().parent()?;+ let violation =+ license_violation(entry.package.name(), entry.package.license(), directory)?;+ Some(Violation::from(violation))+ })+ .collect()+ }+}++#[derive(Debug, Error)]+enum LicenseViolation {+ #[error("{crate_name}: expected `license = \"{APACHE_LICENSE}\"`, found `{license}`")]+ WrongLicense { crate_name: String, license: String },+ #[error("{crate_name}: missing a `license` field, expected `{APACHE_LICENSE}`")]+ MissingLicense { crate_name: String },+ #[error("{crate_name}: `{APACHE_LICENSE_FILE}` must be a symlink to the repository license")]+ UnlinkedLicenseFile { crate_name: String },+ #[error("{crate_name}: `{conflicting}` contradicts `license = \"{APACHE_LICENSE}\"`")]+ ConflictingLicenseFile {+ crate_name: String,+ conflicting: &'static str,+ },+}++fn license_violation(+ crate_name: &str,+ license: Option<&str>,+ directory: &Path,+) -> Option<LicenseViolation> {+ let crate_name = crate_name.to_owned();+ match license {+ Some(APACHE_LICENSE) => {}+ Some(license) => {+ return Some(LicenseViolation::WrongLicense {+ crate_name,+ license: license.to_owned(),+ });+ }+ None => return Some(LicenseViolation::MissingLicense { crate_name }),+ }++ if !is_symlinked_license(&directory.join(APACHE_LICENSE_FILE)) {+ return Some(LicenseViolation::UnlinkedLicenseFile { crate_name });+ }++ let conflicting = CONFLICTING_LICENSE_FILES+ .iter()+ .copied()+ .find(|license_file| directory.join(license_file).exists())?;+ Some(LicenseViolation::ConflictingLicenseFile {+ crate_name,+ conflicting,+ })+}++fn is_symlinked_license(license_file: &Path) -> bool {+ fs::symlink_metadata(license_file).is_ok_and(|metadata| metadata.is_symlink())+ && fs::canonicalize(license_file)+ .is_ok_and(|target| target.file_name() == Some(APACHE_LICENSE_FILE.as_ref()))+}++#[cfg(all(test, unix))]+mod tests {+ use anyhow::Result;++ use super::*;++ #[cfg(unix)]+ #[test]+ fn requires_an_apache_license_field_and_symlink() -> Result<()> {+ use std::os::unix::fs::symlink;++ let root = tempfile::tempdir()?;+ let root = root.path();+ fs::write(root.join(APACHE_LICENSE_FILE), "Apache License")?;+ fs::write(root.join("LICENSE-GPL"), "GPL")?;++ let crate_directory = |name: &str, license_link: Option<&str>| -> Result<_> {+ let directory = root.join(name);+ fs::create_dir(&directory)?;+ if let Some(license_link) = license_link {+ symlink(license_link, directory.join(APACHE_LICENSE_FILE))?;+ }+ Ok(directory)+ };++ let linked = crate_directory("linked", Some("../LICENSE-APACHE"))?;+ assert!(license_violation("linked", Some(APACHE_LICENSE), &linked).is_none());+ assert!(matches!(+ license_violation("linked", Some("MIT"), &linked),+ Some(LicenseViolation::WrongLicense { license, .. }) if license == "MIT"+ ));+ assert!(matches!(+ license_violation("linked", None, &linked),+ Some(LicenseViolation::MissingLicense { .. })+ ));++ for (name, license_link) in [+ ("missing", None),+ ("broken", Some("../nowhere")),+ ("mismatched", Some("../LICENSE-GPL")),+ ] {+ let directory = crate_directory(name, license_link)?;+ assert!(+ matches!(+ license_violation(name, Some(APACHE_LICENSE), &directory),+ Some(LicenseViolation::UnlinkedLicenseFile { .. })+ ),+ "{name}"+ );+ }++ let copied = crate_directory("copied", None)?;+ fs::write(copied.join(APACHE_LICENSE_FILE), "Apache License")?;+ assert!(matches!(+ license_violation("copied", Some(APACHE_LICENSE), &copied),+ Some(LicenseViolation::UnlinkedLicenseFile { .. })+ ));++ fs::write(linked.join("LICENSE-GPL"), "GPL")?;+ assert!(matches!(+ license_violation("linked", Some(APACHE_LICENSE), &linked),+ Some(LicenseViolation::ConflictingLicenseFile {+ conflicting: "LICENSE-GPL",+ ..+ })+ ));+ Ok(())+ }+}diff --git a/tooling/xtask/src/tasks/gpui/validation/unique_publish_names.rs b/tooling/xtask/src/tasks/gpui/validation/unique_publish_names.rsnew file mode 100644index 00000000000..f94f2ee146c--- /dev/null+++ b/tooling/xtask/src/tasks/gpui/validation/unique_publish_names.rs@@ -0,0 +1,89 @@+use std::collections::BTreeMap;++use thiserror::Error;++use super::{Rule, Violation};+use crate::tasks::gpui::publish_plan::PublishPlanEntry;++pub struct UniquePublishNames;++impl Rule for UniquePublishNames {+ fn name(&self) -> &'static str {+ "published crate names are unique"+ }++ fn check(&self, plan: &[PublishPlanEntry<'_>]) -> Vec<Violation> {+ duplicate_publish_names(plan.iter().map(|entry| {+ (+ entry.repository.target_name(entry.package.name()),+ entry.package.name(),+ )+ }))+ .into_iter()+ .map(Violation::from)+ .collect()+ }+}++#[derive(Debug, Error)]+#[error("`{crate_name}` and `{other}` would both be published as `{published_name}`")]+struct DuplicatePublishName {+ crate_name: String,+ other: String,+ published_name: String,+}++fn duplicate_publish_names<'a>(+ published: impl IntoIterator<Item = (String, &'a str)>,+) -> Vec<DuplicatePublishName> {+ let mut crates_by_published_name = BTreeMap::<String, Vec<&str>>::new();+ for (published_name, crate_name) in published {+ crates_by_published_name+ .entry(published_name)+ .or_default()+ .push(crate_name);+ }++ let mut duplicates = Vec::new();+ for (published_name, mut crates) in crates_by_published_name {+ crates.sort_unstable();+ for &[crate_name, other] in crates.array_windows() {+ duplicates.push(DuplicatePublishName {+ crate_name: crate_name.to_owned(),+ other: other.to_owned(),+ published_name: published_name.clone(),+ });+ }+ }+ duplicates+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn reports_crates_that_would_be_published_under_the_same_name() {+ let duplicates = duplicate_publish_names([+ ("gpui_util".to_owned(), "util"),+ ("gpui_collections".to_owned(), "collections"),+ ("gpui_util".to_owned(), "gpui_util"),+ ("zed-fork".to_owned(), "fork"),+ ]);+ assert_eq!(+ duplicates+ .iter()+ .map(ToString::to_string)+ .collect::<Vec<_>>(),+ ["`gpui_util` and `util` would both be published as `gpui_util`"]+ );++ assert!(+ duplicate_publish_names([+ ("gpui_util".to_owned(), "util"),+ ("gpui_collections".to_owned(), "collections"),+ ])+ .is_empty()+ );+ }+}