Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
xtask: Add method for walking the GPUI crate graph (#64247)
Finn Eversc2451489b78f
diff --git a/Cargo.lock b/Cargo.lockindex 12e299d226d..cff1793dbf2 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2703,9 +2703,9 @@ dependencies = [[[package]]name = "camino"-version = "1.2.1"+version = "1.2.6"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "276a59bf2b2c967788139340c9f0c5b12d7fd6630315c15c217e559de85d2609"+checksum = "bbbad30e4b4c14a39e3cc8aed085a12a327257c316619c93581e017bc52be591"dependencies = ["serde_core",]@@ -4827,6 +4827,12 @@ dependencies = ["windows-sys 0.59.0",]+[[package]]+name = "debug-ignore"+version = "1.0.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ffe7ed1d93f4553003e20b629abe9085e1e81b1429520f897f8f8860bc6dfc21"+[[package]]name = "debug_adapter_extension"version = "0.1.0"@@ -7853,6 +7859,39 @@ dependencies = ["subtle",]+[[package]]+name = "guppy"+version = "0.18.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "790eaa3f2acc0210783d4af5ab8fe019ab07935d8dc0e64324120bbee9b142b7"+dependencies = [+ "ahash 0.8.12",+ "camino",+ "cargo_metadata 0.23.1",+ "cfg-if",+ "debug-ignore",+ "fixedbitset",+ "guppy-workspace-hack",+ "indexmap 2.14.0",+ "itertools 0.15.0",+ "nested",+ "once_cell",+ "pathdiff",+ "petgraph",+ "semver",+ "serde",+ "serde_json",+ "smallvec",+ "static_assertions",+ "target-spec",+]++[[package]]+name = "guppy-workspace-hack"+version = "0.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "92620684d99f750bae383ecb3be3748142d6095760afd5cbcf2261e9a279d780"+[[package]]name = "h2"version = "0.3.27"@@ -11301,6 +11340,12 @@ dependencies = ["jni-sys 0.3.0",]+[[package]]+name = "nested"+version = "0.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ca2b420f638f07fe83056b55ea190bb815f609ec5a35e7017884a10f78839c9e"+[[package]]name = "net"version = "0.1.0"@@ -12670,6 +12715,9 @@ name = "pathdiff"version = "0.2.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"+dependencies = [+ "camino",+][[package]]name = "pathfinder_geometry"@@ -16265,9 +16313,9 @@ checksum = "b12e76d157a900eb52e81bc6e9f3069344290341720e9178cde2407113ac8d89"[[package]]name = "semver"-version = "1.0.27"+version = "1.0.28"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2"+checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"dependencies = ["serde","serde_core",@@ -16275,9 +16323,9 @@ dependencies = [[[package]]name = "serde"-version = "1.0.228"+version = "1.0.229"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"+checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"dependencies = ["serde_core","serde_derive",@@ -16295,22 +16343,22 @@ dependencies = [[[package]]name = "serde_core"-version = "1.0.228"+version = "1.0.229"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"+checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"dependencies = ["serde_derive",][[package]]name = "serde_derive"-version = "1.0.228"+version = "1.0.229"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"+checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"dependencies = ["proc-macro2","quote",- "syn 2.0.117",+ "syn 3.0.3",][[package]]@@ -16971,9 +17019,9 @@ dependencies = [[[package]]name = "smallvec"-version = "1.15.1"+version = "1.16.1"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"+checksum = "ba467056f1b547ed52077911161fc86985becbc60e8e1857c8a144dab0def891"dependencies = ["serde",]@@ -18119,6 +18167,17 @@ version = "0.13.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca"+[[package]]+name = "target-spec"+version = "3.7.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4e117cdea67a83bf392bc69fb07cfc37e948e95991eca900c413a2bb1b494807"+dependencies = [+ "cfg-expr",+ "guppy-workspace-hack",+ "target-lexicon",+]+[[package]]name = "task"version = "0.1.0"@@ -22574,6 +22633,7 @@ dependencies = ["clap","compliance","gh-workflow",+ "guppy","indexmap 2.14.0","indoc","itertools 0.14.0",@@ -22585,6 +22645,7 @@ dependencies = ["tokio","toml 0.8.23","toml_edit 0.22.27",+ "url",][[package]]diff --git a/tooling/xtask/Cargo.toml b/tooling/xtask/Cargo.tomlindex f9628dfa639..5b6b5ecabc0 100644--- a/tooling/xtask/Cargo.toml+++ b/tooling/xtask/Cargo.toml@@ -17,6 +17,7 @@ cargo_toml.workspace = trueclap = { workspace = true, features = ["derive"] }compliance = { workspace = true, features = ["octo-client"] }gh-workflow.workspace = true+guppy = "0.18"indoc.workspace = trueindexmap.workspace = trueitertools.workspace = true@@ -28,3 +29,4 @@ strum.workspace = truetokio = { workspace = true, features = ["rt", "rt-multi-thread"] }toml.workspace = truetoml_edit.workspace = true+url.workspace = truediff --git a/tooling/xtask/src/main.rs b/tooling/xtask/src/main.rsindex f9db590d23c..e763c7b1cb4 100644--- a/tooling/xtask/src/main.rs+++ b/tooling/xtask/src/main.rs@@ -19,8 +19,8 @@ enum CliCommand {Licenses(tasks::licenses::LicensesArgs),/// Checks that packages conform to a set of standards.PackageConformity(tasks::package_conformity::PackageConformityArgs),- /// Publishes GPUI and its dependencies to crates.io.- PublishGpui(tasks::publish_gpui::PublishGpuiArgs),+ /// Prints the GPUI crate graph in dependency-first order.+ Gpui,/// Runs the Linux Bubblewrap sandboxing NixOS VM tests.SandboxTests(tasks::sandbox_tests::SandboxTestsArgs),/// Runs the Windows WSL Bubblewrap sandbox behavior tests.@@ -43,7 +43,7 @@ fn main() -> Result<()> {CliCommand::PackageConformity(args) => {tasks::package_conformity::run_package_conformity(args)}- CliCommand::PublishGpui(args) => tasks::publish_gpui::run_publish_gpui(args),+ CliCommand::Gpui => tasks::gpui::run_gpui(),CliCommand::SandboxTests(args) => tasks::sandbox_tests::run_sandbox_tests(args),CliCommand::WslSandboxTests(args) => tasks::wsl_sandbox_tests::run_wsl_sandbox_tests(args),CliCommand::SetupWebrtc(args) => tasks::setup_webrtc::run_setup_webrtc(args),diff --git a/tooling/xtask/src/tasks.rs b/tooling/xtask/src/tasks.rsindex fba583b7614..1f0552b2cf0 100644--- a/tooling/xtask/src/tasks.rs+++ b/tooling/xtask/src/tasks.rs@@ -1,8 +1,8 @@pub mod clippy;pub mod compliance;+pub mod gpui;pub mod licenses;pub mod package_conformity;-pub mod publish_gpui;pub mod sandbox_tests;pub mod setup_webrtc;pub mod web_examples;diff --git a/tooling/xtask/src/tasks/publish_gpui.rs b/tooling/xtask/src/tasks/gpui.rssimilarity index 93%rename from tooling/xtask/src/tasks/publish_gpui.rsrename to tooling/xtask/src/tasks/gpui.rsindex 2740f75a484..483c9153698 100644--- a/tooling/xtask/src/tasks/publish_gpui.rs+++ b/tooling/xtask/src/tasks/gpui.rs@@ -1,12 +1,16 @@#![allow(clippy::disallowed_methods, reason = "tooling is exempt")]++pub mod crate_graph;+use std::io::{self, Write};use std::process::{Command, Output, Stdio};use anyhow::{Context as _, Result, bail};use clap::Parser;+use guppy::graph::DependencyDirection;#[derive(Parser)]-pub struct PublishGpuiArgs {+pub struct GpuiArgs {/// Perform a dry-run and wait for user confirmation before each publish#[arg(long)]dry_run: bool,@@ -16,7 +20,26 @@ pub struct PublishGpuiArgs {skip_to: Option<String>,}-pub fn run_publish_gpui(args: PublishGpuiArgs) -> Result<()> {+pub fn run_gpui() -> Result<()> {+ let graph = crate_graph::load_workspace_graph()?;+ let crates = crate_graph::gpui_crates(&graph)?;++ println!("GPUI crate graph ({} crates):", crates.len());+ for package in crates.packages(DependencyDirection::Reverse) {+ println!("{} ({})", package.name(), package.source());+ for link in package.direct_links().filter(|link| !link.dev_only()) {+ match crates.contains(link.to().id()) {+ Ok(true) => println!(" -> {}", link.to().name()),+ Ok(false) => {}+ Err(e) => eprintln!(" error checking dependency {}: {}", link.to().name(), e),+ }+ }+ }+ Ok(())+}++#[allow(unused, reason = "retained while GPUI releases are reworked")]+fn run_publish_gpui(args: GpuiArgs) -> Result<()> {println!("Starting GPUI publish process{}...",if args.dry_run { " (with dry-run)" } else { "" }diff --git a/tooling/xtask/src/tasks/gpui/crate_graph.rs b/tooling/xtask/src/tasks/gpui/crate_graph.rsnew file mode 100644index 00000000000..a2137677f74--- /dev/null+++ b/tooling/xtask/src/tasks/gpui/crate_graph.rs@@ -0,0 +1,390 @@+use anyhow::{Context as _, Result, ensure};+use guppy::{+ MetadataCommand,+ graph::{DependencyDirection, ExternalSource, PackageGraph, PackageSet, PackageSource},+};+use url::Url;++pub fn load_workspace_graph() -> Result<PackageGraph> {+ MetadataCommand::new()+ .build_graph()+ .context("failed to load the workspace crate graph")+}++/// Returns all GPUI crates in the current workspace+pub fn gpui_crates(graph: &PackageGraph) -> Result<PackageSet<'_>> {+ let roots = graph+ .workspace()+ .iter_by_name()+ .filter(|(name, _)| name.starts_with("gpui"))+ .map(|(_, package)| package.id())+ .collect::<Vec<_>>();+ ensure!(!roots.is_empty(), "no GPUI crates found in the workspace");++ let dependencies = graph+ .query_forward(roots)?+ // we do not need dev dependencies for releases+ .resolve_with_fn(|_, link| !link.dev_only());++ // Keep all workspace crates and Zed forks+ Ok(+ dependencies.filter(DependencyDirection::Reverse, |package| {+ package.in_workspace() || is_zed_fork(package.source())+ }),+ )+}++fn is_zed_fork(source: PackageSource<'_>) -> bool {+ let Some(ExternalSource::Git { repository, .. }) = source.parse_external() else {+ return false;+ };++ Url::parse(repository).is_ok_and(|repository| {+ repository+ .host_str()+ .is_some_and(|host| host.eq_ignore_ascii_case("github.com"))+ && repository.path_segments().is_some_and(|mut segments| {+ segments+ .next()+ .is_some_and(|owner| owner.eq_ignore_ascii_case("zed-industries"))+ && segments.next().is_some_and(|name| !name.is_empty())+ })+ })+}++#[cfg(test)]+mod tests {+ use serde_json::{Value, json};++ use super::*;+ use DependencyKind::{Build, Development, Normal};+ use DependencyRequirement::{Optional, Required};+ use DependencyTarget::{All, Platform};+ use PackageOrigin::{CratesIo, Git, Path, Workspace};++ struct FixturePackage {+ name: &'static str,+ origin: PackageOrigin,+ dependencies: &'static [FixtureDependency],+ }++ enum PackageOrigin {+ Workspace,+ Path,+ CratesIo,+ Git(&'static str),+ }++ struct FixtureDependency {+ to: &'static str,+ kind: DependencyKind,+ target: DependencyTarget,+ }++ enum DependencyKind {+ Normal(DependencyRequirement),+ Build(DependencyRequirement),+ Development,+ }++ enum DependencyRequirement {+ Required,+ Optional,+ }++ enum DependencyTarget {+ All,+ Platform(&'static str),+ }++ #[test]+ fn selects_release_dependencies_in_topological_order() -> Result<()> {+ let graph = fixture(&[+ FixturePackage {+ name: "gpui",+ origin: Workspace,+ dependencies: &[+ FixtureDependency {+ to: "gpui_platform",+ kind: Development,+ target: All,+ },+ FixtureDependency {+ to: "gpui_macros",+ kind: Normal(Required),+ target: All,+ },+ FixtureDependency {+ to: "collections",+ kind: Normal(Required),+ target: All,+ },+ FixtureDependency {+ to: "collections",+ kind: Development,+ target: All,+ },+ FixtureDependency {+ to: "build_helper",+ kind: Build(Required),+ target: All,+ },+ FixtureDependency {+ to: "optional_helper",+ kind: Normal(Optional),+ target: Platform("cfg(windows)"),+ },+ FixtureDependency {+ to: "test_helper",+ kind: Development,+ target: All,+ },+ FixtureDependency {+ to: "local_dependency",+ kind: Normal(Required),+ target: All,+ },+ FixtureDependency {+ to: "registry",+ kind: Normal(Required),+ target: All,+ },+ FixtureDependency {+ to: "upstream",+ kind: Normal(Required),+ target: All,+ },+ ],+ },+ FixturePackage {+ name: "gpui_platform",+ origin: Workspace,+ dependencies: &[FixtureDependency {+ to: "gpui",+ kind: Normal(Required),+ target: All,+ }],+ },+ FixturePackage {+ name: "gpui_macros",+ origin: Workspace,+ dependencies: &[FixtureDependency {+ to: "gpui",+ kind: Development,+ target: All,+ }],+ },+ FixturePackage {+ name: "collections",+ origin: Workspace,+ dependencies: &[],+ },+ FixturePackage {+ name: "build_helper",+ origin: Workspace,+ dependencies: &[],+ },+ FixturePackage {+ name: "optional_helper",+ origin: Workspace,+ dependencies: &[],+ },+ FixturePackage {+ name: "test_helper",+ origin: Workspace,+ dependencies: &[],+ },+ FixturePackage {+ name: "zed",+ origin: Workspace,+ dependencies: &[FixtureDependency {+ to: "gpui",+ kind: Normal(Required),+ target: All,+ }],+ },+ FixturePackage {+ name: "local_dependency",+ origin: Path,+ dependencies: &[],+ },+ FixturePackage {+ name: "registry",+ origin: CratesIo,+ dependencies: &[FixtureDependency {+ to: "fork",+ kind: Normal(Required),+ target: All,+ }],+ },+ FixturePackage {+ name: "fork",+ origin: Git("git+https://github.com/zed-industries/fork?rev=abc#abc"),+ dependencies: &[FixtureDependency {+ to: "collections",+ kind: Normal(Required),+ target: All,+ }],+ },+ FixturePackage {+ name: "upstream",+ origin: Git("git+https://github.com/other/upstream#abc"),+ dependencies: &[],+ },+ FixturePackage {+ name: "gpui_unrelated",+ origin: Git("git+https://github.com/zed-industries/unrelated#abc"),+ dependencies: &[],+ },+ ])?;+ let crates = gpui_crates(&graph)?;+ let names = crates+ .packages(DependencyDirection::Reverse)+ .map(|package| package.name())+ .collect::<Vec<_>>();+ assert_eq!(+ names+ .iter()+ .copied()+ .collect::<std::collections::BTreeSet<_>>(),+ [+ "gpui",+ "gpui_platform",+ "gpui_macros",+ "collections",+ "build_helper",+ "optional_helper",+ "fork"+ ]+ .into_iter()+ .collect(),+ );+ for (dependency, dependent) in [+ ("gpui", "gpui_platform"),+ ("gpui_macros", "gpui"),+ ("collections", "fork"),+ ("fork", "gpui"),+ ("build_helper", "gpui"),+ ("optional_helper", "gpui"),+ ] {+ let dependency_index = names+ .iter()+ .position(|name| *name == dependency)+ .context("missing dependency")?;+ let dependent_index = names+ .iter()+ .position(|name| *name == dependent)+ .context("missing dependent")?;+ assert!(+ dependency_index < dependent_index,+ "{dependency} must precede {dependent}"+ );+ }+ assert!(crates.links(DependencyDirection::Reverse).all(|link| {+ names.contains(&link.from().name()) && names.contains(&link.to().name())+ }));+ Ok(())+ }++ #[test]+ fn requires_workspace_gpui_roots() -> Result<()> {+ let graph = fixture(&[FixturePackage {+ name: "zed",+ origin: Workspace,+ dependencies: &[],+ }])?;+ assert!(gpui_crates(&graph).is_err());+ Ok(())+ }++ #[test]+ fn identifies_forks_by_git_source_not_repository_metadata() {+ for source in [+ "git+https://github.com/zed-industries/fork?rev=abc#abc",+ "git+https://github.com/zed-industries/fork.git?branch=main#abc",+ "git+ssh://[email protected]/zed-industries/fork?tag=v1#abc",+ "git+https://github.com/Zed-Industries/fork#abc",+ ] {+ assert!(is_zed_fork(PackageSource::External(source)), "{source}");+ }+ for source in [+ PackageSource::CRATES_IO_REGISTRY,+ "sparse+https://index.crates.io/",+ "git+https://github.com/other/fork#abc",+ "git+https://github.com/zed-industries-other/fork#abc",+ "git+https://github.com.example.org/zed-industries/fork#abc",+ "git+https://example.org/github.com/zed-industries/fork#abc",+ "git+https://github.com/zed-industries/#abc",+ ] {+ assert!(!is_zed_fork(PackageSource::External(source)), "{source}");+ }+ }++ fn fixture(packages: &[FixturePackage]) -> Result<PackageGraph> {+ let mut nodes = Vec::new();+ let mut metadata = Vec::new();+ for package in packages {+ let name = package.name;+ let source = match package.origin {+ Workspace | Path => None,+ CratesIo => Some(PackageSource::CRATES_IO_REGISTRY),+ Git(source) => Some(source),+ };+ let mut dependencies = Vec::new();+ let mut resolved = std::collections::BTreeMap::<&str, Value>::new();+ for dependency in package.dependencies {+ let to = dependency.to;+ let (kind, optional) = match &dependency.kind {+ Normal(requirement) => (None, matches!(requirement, Optional)),+ Build(requirement) => (Some("build"), matches!(requirement, Optional)),+ Development => (Some("dev"), false),+ };+ let target = match dependency.target {+ All => None,+ Platform(target) => Some(target),+ };+ dependencies.push(json!({+ "name": to, "source": null, "req": "*", "kind": kind,+ "optional": optional, "uses_default_features": true,+ "features": [], "target": target, "registry": null,+ }));+ let link = resolved.entry(to).or_insert_with(|| {+ json!({+ "name": to, "pkg": to, "dep_kinds": [],+ })+ });+ link["dep_kinds"]+ .as_array_mut()+ .context("expected dependency kinds")?+ .push(json!({"kind": kind, "target": target}));+ }+ nodes.push(json!({+ "id": name, "dependencies": resolved.keys().collect::<Vec<_>>(),+ "deps": resolved.values().collect::<Vec<_>>(), "features": [],+ }));+ metadata.push(json!({+ "name": name, "version": "0.1.0", "id": name, "source": source,+ "dependencies": dependencies, "features": {},+ "targets": [{+ "name": name, "kind": ["lib"], "crate_types": ["lib"],+ "src_path": format!("/workspace/{name}/src/lib.rs"),+ "edition": "2024", "doctest": false, "test": false, "doc": false,+ }],+ "manifest_path": format!("/workspace/{name}/Cargo.toml"),+ "repository": "https://github.com/zed-industries/fixture",+ }));+ }+ Ok(PackageGraph::from_json(+ json!({+ "packages": metadata,+ "workspace_members": packages.iter()+ .filter(|package| matches!(package.origin, Workspace))+ .map(|package| package.name).collect::<Vec<_>>(),+ "resolve": { "nodes": nodes, "root": null },+ "workspace_root": "/workspace", "target_directory": "/workspace/target",+ "version": 1,+ })+ .to_string(),+ )?)+ }+}