Skip to content

Commit 4acecfc

Browse files
authored
fix(sqlx-cli): do not clean sqlx during prepare (#2786)
* fix(cli): do not clean sqlx during prepare * feat(cli): only clean dependencies with new --all flag for prepare
1 parent ff0252d commit 4acecfc

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

sqlx-cli/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ pub async fn run(opt: Opt) -> Result<()> {
9090

9191
Command::Prepare {
9292
check,
93+
all,
9394
workspace,
9495
connect_opts,
9596
args,
96-
} => prepare::run(check, workspace, connect_opts, args).await?,
97+
} => prepare::run(check, all, workspace, connect_opts, args).await?,
9798

9899
#[cfg(feature = "completions")]
99100
Command::Completions { shell } => completions::run(shell),

sqlx-cli/src/opt.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ pub enum Command {
3131
#[clap(long)]
3232
check: bool,
3333

34+
/// Prepare query macros in dependencies that exist outside the current crate or workspace.
35+
#[clap(long)]
36+
all: bool,
37+
3438
/// Generate a single workspace-level `.sqlx` folder.
3539
///
3640
/// This option is intended for workspaces where multiple crates use SQLx. If there is only

sqlx-cli/src/prepare.rs

+26-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::opt::ConnectOpts;
1414

1515
pub struct PrepareCtx {
1616
pub workspace: bool,
17+
pub all: bool,
1718
pub cargo: OsString,
1819
pub cargo_args: Vec<String>,
1920
pub metadata: Metadata,
@@ -33,6 +34,7 @@ impl PrepareCtx {
3334

3435
pub async fn run(
3536
check: bool,
37+
all: bool,
3638
workspace: bool,
3739
connect_opts: ConnectOpts,
3840
cargo_args: Vec<String>,
@@ -49,6 +51,7 @@ hint: This command only works in the manifest directory of a Cargo package or wo
4951
let metadata: Metadata = Metadata::from_current_directory(&cargo)?;
5052
let ctx = PrepareCtx {
5153
workspace,
54+
all,
5255
cargo,
5356
cargo_args,
5457
metadata,
@@ -166,7 +169,7 @@ fn run_prepare_step(ctx: &PrepareCtx, cache_dir: &Path) -> anyhow::Result<()> {
166169

167170
// Try only triggering a recompile on crates that use `sqlx-macros` falling back to a full
168171
// clean on error
169-
setup_minimal_project_recompile(&ctx.cargo, &ctx.metadata, ctx.workspace)?;
172+
setup_minimal_project_recompile(&ctx.cargo, &ctx.metadata, ctx.all, ctx.workspace)?;
170173

171174
// Compile the queries.
172175
let check_status = {
@@ -216,10 +219,11 @@ struct ProjectRecompileAction {
216219
fn setup_minimal_project_recompile(
217220
cargo: impl AsRef<OsStr>,
218221
metadata: &Metadata,
222+
all: bool,
219223
workspace: bool,
220224
) -> anyhow::Result<()> {
221225
let recompile_action: ProjectRecompileAction = if workspace {
222-
minimal_project_recompile_action(metadata)
226+
minimal_project_recompile_action(metadata, all)
223227
} else {
224228
// Only touch the current crate.
225229
ProjectRecompileAction {
@@ -275,7 +279,7 @@ fn minimal_project_clean(
275279
Ok(())
276280
}
277281

278-
fn minimal_project_recompile_action(metadata: &Metadata) -> ProjectRecompileAction {
282+
fn minimal_project_recompile_action(metadata: &Metadata, all: bool) -> ProjectRecompileAction {
279283
// Get all the packages that depend on `sqlx-macros`
280284
let mut sqlx_macros_dependents = BTreeSet::new();
281285
let sqlx_macros_ids: BTreeSet<_> = metadata
@@ -300,8 +304,7 @@ fn minimal_project_recompile_action(metadata: &Metadata) -> ProjectRecompileActi
300304
}
301305
}
302306

303-
// In-workspace dependents have their source file's mtime updated. Out-of-workspace get
304-
// `cargo clean -p <PKGID>`ed
307+
// In-workspace dependents have their source file's mtime updated.
305308
let files_to_touch: Vec<_> = in_workspace_dependents
306309
.iter()
307310
.filter_map(|id| {
@@ -311,14 +314,22 @@ fn minimal_project_recompile_action(metadata: &Metadata) -> ProjectRecompileActi
311314
})
312315
.flatten()
313316
.collect();
314-
let packages_to_clean: Vec<_> = out_of_workspace_dependents
315-
.iter()
316-
.filter_map(|id| {
317-
metadata
318-
.package(id)
319-
.map(|package| package.name().to_owned())
320-
})
321-
.collect();
317+
318+
// Out-of-workspace get `cargo clean -p <PKGID>`ed, only if --all is set.
319+
let packages_to_clean: Vec<_> = if all {
320+
out_of_workspace_dependents
321+
.iter()
322+
.filter_map(|id| {
323+
metadata
324+
.package(id)
325+
.map(|package| package.name().to_owned())
326+
})
327+
// Do not clean sqlx, it depends on sqlx-macros but has no queries to prepare itself.
328+
.filter(|name| name != "sqlx")
329+
.collect()
330+
} else {
331+
Vec::new()
332+
};
322333

323334
ProjectRecompileAction {
324335
clean_packages: packages_to_clean,
@@ -366,11 +377,11 @@ mod tests {
366377
let sample_metadata = std::fs::read_to_string(sample_metadata_path)?;
367378
let metadata: Metadata = sample_metadata.parse()?;
368379

369-
let action = minimal_project_recompile_action(&metadata);
380+
let action = minimal_project_recompile_action(&metadata, false);
370381
assert_eq!(
371382
action,
372383
ProjectRecompileAction {
373-
clean_packages: vec!["sqlx".into()],
384+
clean_packages: vec![],
374385
touch_paths: vec![
375386
"/home/user/problematic/workspace/b_in_workspace_lib/src/lib.rs".into(),
376387
"/home/user/problematic/workspace/c_in_workspace_bin/src/main.rs".into(),

0 commit comments

Comments
 (0)