Skip to content

Commit 85b0fad

Browse files
committed
sqlx-cli, prepare: pass --workspace to cargo check when called for a workspace
1 parent 1d674f5 commit 85b0fad

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

sqlx-cli/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ to generate a single `.sqlx` directory at the root of the workspace.
133133
cargo sqlx prepare --workspace
134134
```
135135

136+
When `--workspace` is used, SQLx also checks every workspace member. Cargo options such as
137+
`--all-targets` and `--all-features` can still be forwarded after `--`:
138+
139+
```bash
140+
cargo sqlx prepare --workspace -- --all-targets --all-features
141+
```
142+
136143
Check this directory into version control and an active database connection will
137144
no longer be needed to build your project.
138145

sqlx-cli/src/prepare.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ impl PrepareCtx<'_> {
3131
Ok(manifest_dir(&self.cargo)?.join(".sqlx"))
3232
}
3333
}
34+
35+
/// Arguments passed to the `cargo check` invocation that prepares query metadata.
36+
///
37+
/// `--workspace` on `sqlx prepare` controls both the location of the generated `.sqlx`
38+
/// directory and which workspace packages are recompiled. It must also select all workspace
39+
/// packages for the Cargo invocation; otherwise a workspace root which is itself a package
40+
/// only checks that root package by default.
41+
fn cargo_check_args(&self) -> Vec<String> {
42+
cargo_check_args(self.workspace, &self.cargo_args)
43+
}
44+
}
45+
46+
fn cargo_check_args(workspace: bool, cargo_args: &[String]) -> Vec<String> {
47+
let mut args = cargo_args.to_vec();
48+
49+
if workspace && !args.iter().any(|arg| arg == "--workspace") {
50+
args.push("--workspace".to_owned());
51+
}
52+
53+
args
3454
}
3555

3656
pub async fn run(
@@ -179,7 +199,7 @@ fn run_prepare_step(ctx: &PrepareCtx, cache_dir: &Path) -> anyhow::Result<()> {
179199
let mut check_command = Command::new(&ctx.cargo);
180200
check_command
181201
.arg("check")
182-
.args(&ctx.cargo_args)
202+
.args(ctx.cargo_check_args())
183203
.env("SQLX_TMP", tmp_dir)
184204
.env("SQLX_OFFLINE", "false")
185205
.env("SQLX_OFFLINE_DIR", cache_dir);
@@ -372,6 +392,19 @@ mod tests {
372392
use super::*;
373393
use std::assert_eq;
374394

395+
#[test]
396+
fn workspace_prepare_selects_all_workspace_packages() {
397+
assert_eq!(
398+
cargo_check_args(true, &["--all-targets".into(), "--all-features".into()]),
399+
["--all-targets", "--all-features", "--workspace"]
400+
);
401+
}
402+
403+
#[test]
404+
fn workspace_prepare_does_not_duplicate_workspace_argument() {
405+
assert_eq!(cargo_check_args(true, &["--workspace".into()]), ["--workspace"]);
406+
}
407+
375408
#[test]
376409
fn minimal_project_recompile_action_works() -> anyhow::Result<()> {
377410
let sample_metadata_path = Path::new("tests")

0 commit comments

Comments
 (0)