From 6980c6bdc0f2f7fcdc646f675256d70c87bf0409 Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 8 Dec 2021 13:35:23 -0800 Subject: [PATCH] [cargo-hakari] go back to previous scheme of using unique workspace-hack crate names Unfortunately, cargo has a couple of bugs: https://github.com/facebookincubator/cargo-guppy/issues/524 --- tools/cargo-hakari/README.md | 4 +- tools/cargo-hakari/src/command.rs | 18 +++++-- tools/cargo-hakari/src/docs/publishing.rs | 60 +++++++---------------- tools/cargo-hakari/src/lib.rs | 4 +- 4 files changed, 35 insertions(+), 51 deletions(-) diff --git a/tools/cargo-hakari/README.md b/tools/cargo-hakari/README.md index 7441d26836c..e7f18484a27 100644 --- a/tools/cargo-hakari/README.md +++ b/tools/cargo-hakari/README.md @@ -49,10 +49,10 @@ There are three steps you *must* take for `cargo hakari` to work properly. #### 1. Initialize the workspace-hack -Initialize a workspace-hack crate for a workspace at path `workspace-hack`: +Initialize a workspace-hack crate for a workspace at path `my-workspace-hack`: ```sh -cargo hakari init workspace-hack +cargo hakari init my-workspace-hack ```

diff --git a/tools/cargo-hakari/src/command.rs b/tools/cargo-hakari/src/command.rs index cfd880bbb5a..165990e1539 100644 --- a/tools/cargo-hakari/src/command.rs +++ b/tools/cargo-hakari/src/command.rs @@ -7,7 +7,7 @@ use crate::{ publish::publish_hakari, }; use camino::{Utf8Path, Utf8PathBuf}; -use color_eyre::eyre::{eyre, Result, WrapErr}; +use color_eyre::eyre::{bail, eyre, Result, WrapErr}; use guppy::{ graph::{PackageGraph, PackageSet}, MetadataCommand, @@ -76,9 +76,9 @@ enum Command { /// Path to generate the workspace-hack crate at, relative to the current directory. path: Utf8PathBuf, - /// The name of the crate - #[structopt(long, short, default_value = "workspace-hack")] - package_name: String, + /// The name of the crate (default: derived from path) + #[structopt(long, short)] + package_name: Option, /// Skip writing a stub config to hakari.toml #[structopt(long)] @@ -116,10 +116,18 @@ impl Command { dry_run, yes, } => { + let package_name = match package_name.as_deref() { + Some(name) => name, + None => match path.file_name() { + Some(name) => name, + None => bail!("invalid path {}", path), + }, + }; + let workspace_path = cwd_rel_to_workspace_rel(&path, package_graph.workspace().root())?; - let mut init = HakariInit::new(&package_graph, &package_name, &workspace_path) + let mut init = HakariInit::new(&package_graph, package_name, &workspace_path) .with_context(|| "error initializing Hakari package")?; init.set_cargo_toml_comment(CARGO_TOML_COMMENT); if !skip_config { diff --git a/tools/cargo-hakari/src/docs/publishing.rs b/tools/cargo-hakari/src/docs/publishing.rs index 07b2c3e41dc..bec253507b8 100644 --- a/tools/cargo-hakari/src/docs/publishing.rs +++ b/tools/cargo-hakari/src/docs/publishing.rs @@ -9,10 +9,7 @@ //! registries. However, if you attempt to publish a crate from a Hakari-managed workspace, //! `cargo publish` may reject it for containing the local-only workspace-hack dependency. //! -//! `cargo hakari` provides three ways to handle this. **For most users publishing to crates.io, -//! [method B] is the easiest**. -//! -//! [method B]: #b-target-the-workspace-hack-crate-already-on-cratesio +//! `cargo hakari` provides two ways to handle this. //! //! # A. Temporarily remove the workspace-hack dependency before publishing //! @@ -30,41 +27,36 @@ //! If you don't have control over the commands run while publishing the package, it won't be //! possible to use this method. //! -//! # B. Target the "workspace-hack" crate already on crates.io -//! -//! Methods B and C preserve workspace-hack dependencies in `Cargo.toml`s by targeting a stub -//! crate on the registry. The crates.io registry already contains an empty package called -//! [workspace-hack](https://crates.io/crates/workspace-hack), meant just for this. -//! -//! Starting from cargo-hakari 0.9.9, `cargo hakari init`'s default configuration makes steps 1 and -//! 2 unnecessary, making this method zero-setup. +//! # B. Publish your own workspace-hack crate to the registry //! -//! However, if the workspace-hack crate was initialized by an older version of cargo-hakari, -//! perform the following actions. +//! This method preserves workspace-hack dependencies in `Cargo.toml`s by targeting a stub +//! crate on the registry. //! -//! ## 1. Ensure the local crate is called "workspace-hack" +//! ## 1. Ensure the local crate is unique on the registry //! -//! If your crate has a different name, rename it to `"workspace-hack"`. +//! Rename it to something unique if necessary. //! -//! > **TIP:** On Unix platforms, to rename `my-workspace-hack` to `workspace-hack` in other +//! > **TIP:** On Unix platforms, to rename `workspace-hack` to `my-workspace-hack` in other //! > `Cargo.toml` files: run this from the root of the workspace: //! > //! > ```sh -//! > git ls-files | grep Cargo.toml | xargs perl -p -i -e 's/^my-workspace-hack = /workspace-hack = /' +//! > git ls-files | grep Cargo.toml | xargs perl -p -i -e 's/^workspace-hack = /my-workspace-hack = /' //! > ``` //! > //! > If not in the context of a Git repository, run: //! > //! > ```sh -//! > find . -name Cargo.toml | xargs perl -p -i -e 's/^my-workspace-hack = /workspace-hack = /'` +//! > find . -name Cargo.toml | xargs perl -p -i -e 's/^workspace-hack = /my-workspace-hack = /'` //! > ``` //! //! Remember to update `.config/hakari.toml` (or `.guppy/hakari.toml`) with the new name. //! +//! The rest of this section assumes the crate is called `my-workspace-hack`. +//! //! ## 2. Ensure `dep-format-version = "2"` is set in `.config/hakari.toml` //! -//! `dep-format-version = "2"` adds the `version` field to the `workspace-hack = ...` lines in other -//! `Cargo.toml` files. `cargo publish` uses the `version` field to recognize published +//! `dep-format-version = "2"` adds the `version` field to the `my-workspace-hack = ...` lines in +//! other `Cargo.toml` files. `cargo publish` uses the `version` field to recognize published //! dependencies. //! //! This option is new in cargo-hakari 0.9.8. Configuration files created by older versions of @@ -82,25 +74,10 @@ //! //! After performing the above actions, simply run `cargo publish` as usual to publish the crate. //! -//! ## C. Publish your own workspace-hack crate to the registry -//! -//! If your crates need to be published to a different registry, or you wish to publish your own -//! version of the workspace-hack, follow these instructions. -//! -//! ## 1. Give the workspace-hack a unique name -//! -//! If your crate has a name that is already taken up on the registry, give it a unique name. -//! -//! ## 2. Ensure `dep-format-version = "2"` is set in `.config/hakari.toml` -//! -//! See [Method B] above for more about this. -//! -//! [Method B]: #2-ensure-dep-format-version--2-is-set-in-confighakaritoml -//! //! ## 3. Set options in the workspace-hack's `Cargo.toml` //! -//! In the workspace-hack's `Cargo.toml` file, set the `package.publish` option to anything other -//! than `false`. This enables publication of the workspace-hack crate. +//! In `my-workspace-hack`'s `Cargo.toml` file, set the `package.publish` option to anything other +//! than `false`. This enables its publication. //! //! ```toml //! [package] @@ -116,13 +93,12 @@ //! **This step is really important.** Not doing it will cause the full dependency set in the //! workspace-hack to be published, which is not what you want. //! -//! Run `cargo hakari disable` to disable the workspace-hack. +//! Run `cargo hakari disable` to disable the workspace-hack crate`. //! //! ## 5. Publish the stub workspace-hack crate //! -//! If the workspace-hack crate has been renamed to `my-workspace-hack`, run `cargo publish -p -//! my-workspace-hack --allow-dirty` to publish the crate to `crates.io`. For other registries, use -//! the `--registry` flag. +//! Run `cargo publish -p my-workspace-hack --allow-dirty` to publish the crate to `crates.io`. +//! For other registries, use the `--registry` flag. //! //! ## 6. Re-enable the workspace-hack crate //! diff --git a/tools/cargo-hakari/src/lib.rs b/tools/cargo-hakari/src/lib.rs index 061f77826e2..3a2b947e541 100644 --- a/tools/cargo-hakari/src/lib.rs +++ b/tools/cargo-hakari/src/lib.rs @@ -48,10 +48,10 @@ //! //! ### 1. Initialize the workspace-hack //! -//! Initialize a workspace-hack crate for a workspace at path `workspace-hack`: +//! Initialize a workspace-hack crate for a workspace at path `my-workspace-hack`: //! //! ```sh -//! cargo hakari init workspace-hack +//! cargo hakari init my-workspace-hack //! ``` //! //!