@@ -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
3656pub 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