diff --git a/sqlx-cli/src/prepare.rs b/sqlx-cli/src/prepare.rs index f3688add2a..b8dfe139b4 100644 --- a/sqlx-cli/src/prepare.rs +++ b/sqlx-cli/src/prepare.rs @@ -41,8 +41,7 @@ pub async fn run( connect_opts: ConnectOpts, cargo_args: Vec, ) -> anyhow::Result<()> { - let cargo = env::var_os("CARGO") - .context("failed to get value of `CARGO`; `prepare` subcommand may only be invoked as `cargo sqlx prepare`")?; + let cargo = cargo_command(env::var_os("CARGO"))?; anyhow::ensure!( Path::new("Cargo.toml").exists(), @@ -68,6 +67,13 @@ hint: This command only works in the manifest directory of a Cargo package or wo } } +fn cargo_command(cargo: Option) -> anyhow::Result { + cargo.context( + "the `prepare` subcommand must be invoked as `cargo sqlx prepare`; \ + running `sqlx prepare` directly is not supported", + ) +} + async fn prepare(ctx: &PrepareCtx<'_>) -> anyhow::Result<()> { if ctx.connect_opts.database_url.is_some() { check_backend(ctx.config, &ctx.connect_opts).await?; @@ -372,6 +378,16 @@ mod tests { use super::*; use std::assert_eq; + #[test] + fn missing_cargo_environment_explains_prepare_invocation() { + let error = cargo_command(None).expect_err("missing CARGO should fail"); + + assert_eq!( + error.to_string(), + "the `prepare` subcommand must be invoked as `cargo sqlx prepare`; running `sqlx prepare` directly is not supported" + ); + } + #[test] fn minimal_project_recompile_action_works() -> anyhow::Result<()> { let sample_metadata_path = Path::new("tests")