Files
oak-gpui/tooling/xtask/src/tasks/package_conformity.rs
T
Kirill Bulatov 16366cf9f2 Use anyhow more idiomatically (#31052)
https://github.com/zed-industries/zed/issues/30972 brought up another
case where our context is not enough to track the actual source of the
issue: we get a general top-level error without inner error.

The reason for this was `.ok_or_else(|| anyhow!("failed to read HEAD
SHA"))?; ` on the top level.

The PR finally reworks the way we use anyhow to reduce such issues (or
at least make it simpler to bubble them up later in a fix).
On top of that, uses a few more anyhow methods for better readability.

* `.ok_or_else(|| anyhow!("..."))`, `map_err` and other similar error
conversion/option reporting cases are replaced with `context` and
`with_context` calls
* in addition to that, various `anyhow!("failed to do ...")` are
stripped with `.context("Doing ...")` messages instead to remove the
parasitic `failed to` text
* `anyhow::ensure!` is used instead of `if ... { return Err(...); }`
calls
* `anyhow::bail!` is used instead of `return Err(anyhow!(...));`

Release Notes:

- N/A
2025-05-20 23:06:07 +00:00

83 lines
2.4 KiB
Rust

use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
use anyhow::{Context as _, Result};
use cargo_toml::{Dependency, Manifest};
use clap::Parser;
use crate::workspace::load_workspace;
#[derive(Parser)]
pub struct PackageConformityArgs {}
pub fn run_package_conformity(_args: PackageConformityArgs) -> Result<()> {
let workspace = load_workspace()?;
let mut non_workspace_dependencies = BTreeMap::new();
for package in workspace.workspace_packages() {
let is_extension = package
.manifest_path
.parent()
.and_then(|parent| parent.parent())
.map_or(false, |grandparent_dir| {
grandparent_dir.ends_with("extensions")
});
let cargo_toml = read_cargo_toml(&package.manifest_path)?;
let is_using_workspace_lints = cargo_toml.lints.map_or(false, |lints| lints.workspace);
if !is_using_workspace_lints {
eprintln!(
"{package:?} is not using workspace lints",
package = package.name
);
}
// Extensions should not use workspace dependencies.
if is_extension || package.name == "zed_extension_api" {
continue;
}
// Ignore `workspace-hack`, as it produces a lot of false positives.
if package.name == "workspace-hack" {
continue;
}
for dependencies in [
&cargo_toml.dependencies,
&cargo_toml.dev_dependencies,
&cargo_toml.build_dependencies,
] {
for (name, dependency) in dependencies {
if let Dependency::Inherited(_) = dependency {
continue;
}
non_workspace_dependencies
.entry(name.to_owned())
.or_insert_with(Vec::new)
.push(package.name.clone());
}
}
}
for (dependency, packages) in non_workspace_dependencies {
eprintln!(
"{dependency} is being used as a non-workspace dependency: {}",
packages.join(", ")
);
}
Ok(())
}
/// Returns the contents of the `Cargo.toml` file at the given path.
fn read_cargo_toml(path: impl AsRef<Path>) -> Result<Manifest> {
let path = path.as_ref();
let cargo_toml_bytes = fs::read(path)?;
Manifest::from_slice(&cargo_toml_bytes)
.with_context(|| format!("reading Cargo.toml at {path:?}"))
}