mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
xtask: Add validation layer for GPUI commands (#64869)
## Summary This PR adds a validation step to the GPUI publishing plan, ensuring that crates there have proper licenses and that we do not face duplicate crate names (this one is acutally currently violated but something to fix separately). Release Notes: - N/A
This commit is contained in:
Generated
+2
@@ -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",
|
||||
|
||||
@@ -26,7 +26,11 @@ serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
serde_yaml = "0.9.34"
|
||||
strum.workspace = true
|
||||
thiserror.workspace = true
|
||||
tokio = { workspace = true, features = ["rt", "rt-multi-thread"] }
|
||||
toml.workspace = true
|
||||
toml_edit.workspace = true
|
||||
url.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile.workspace = true
|
||||
|
||||
@@ -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 list
|
||||
Why {
|
||||
/// 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}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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"}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user