Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqlx-cli): do not clean sqlx during prepare #2786

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ pub async fn run(opt: Opt) -> Result<()> {

Command::Prepare {
check,
all,
workspace,
connect_opts,
args,
} => prepare::run(check, workspace, connect_opts, args).await?,
} => prepare::run(check, all, workspace, connect_opts, args).await?,

#[cfg(feature = "completions")]
Command::Completions { shell } => completions::run(shell),
Expand Down
4 changes: 4 additions & 0 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub enum Command {
#[clap(long)]
check: bool,

/// Prepare query macros in dependencies that exist outside the current crate or workspace.
#[clap(long)]
all: bool,

/// Generate a single workspace-level `.sqlx` folder.
///
/// This option is intended for workspaces where multiple crates use SQLx. If there is only
Expand Down
41 changes: 26 additions & 15 deletions sqlx-cli/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::opt::ConnectOpts;

pub struct PrepareCtx {
pub workspace: bool,
pub all: bool,
pub cargo: OsString,
pub cargo_args: Vec<String>,
pub metadata: Metadata,
Expand All @@ -33,6 +34,7 @@ impl PrepareCtx {

pub async fn run(
check: bool,
all: bool,
workspace: bool,
connect_opts: ConnectOpts,
cargo_args: Vec<String>,
Expand All @@ -49,6 +51,7 @@ hint: This command only works in the manifest directory of a Cargo package or wo
let metadata: Metadata = Metadata::from_current_directory(&cargo)?;
let ctx = PrepareCtx {
workspace,
all,
cargo,
cargo_args,
metadata,
Expand Down Expand Up @@ -166,7 +169,7 @@ fn run_prepare_step(ctx: &PrepareCtx, cache_dir: &Path) -> anyhow::Result<()> {

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

// Compile the queries.
let check_status = {
Expand Down Expand Up @@ -216,10 +219,11 @@ struct ProjectRecompileAction {
fn setup_minimal_project_recompile(
cargo: impl AsRef<OsStr>,
metadata: &Metadata,
all: bool,
workspace: bool,
) -> anyhow::Result<()> {
let recompile_action: ProjectRecompileAction = if workspace {
minimal_project_recompile_action(metadata)
minimal_project_recompile_action(metadata, all)
} else {
// Only touch the current crate.
ProjectRecompileAction {
Expand Down Expand Up @@ -275,7 +279,7 @@ fn minimal_project_clean(
Ok(())
}

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

// In-workspace dependents have their source file's mtime updated. Out-of-workspace get
// `cargo clean -p <PKGID>`ed
// In-workspace dependents have their source file's mtime updated.
let files_to_touch: Vec<_> = in_workspace_dependents
.iter()
.filter_map(|id| {
Expand All @@ -311,14 +314,22 @@ fn minimal_project_recompile_action(metadata: &Metadata) -> ProjectRecompileActi
})
.flatten()
.collect();
let packages_to_clean: Vec<_> = out_of_workspace_dependents
.iter()
.filter_map(|id| {
metadata
.package(id)
.map(|package| package.name().to_owned())
})
.collect();

// Out-of-workspace get `cargo clean -p <PKGID>`ed, only if --all is set.
let packages_to_clean: Vec<_> = if all {
out_of_workspace_dependents
.iter()
.filter_map(|id| {
metadata
.package(id)
.map(|package| package.name().to_owned())
})
// Do not clean sqlx, it depends on sqlx-macros but has no queries to prepare itself.
.filter(|name| name != "sqlx")
.collect()
} else {
Vec::new()
};

ProjectRecompileAction {
clean_packages: packages_to_clean,
Expand Down Expand Up @@ -366,11 +377,11 @@ mod tests {
let sample_metadata = std::fs::read_to_string(sample_metadata_path)?;
let metadata: Metadata = sample_metadata.parse()?;

let action = minimal_project_recompile_action(&metadata);
let action = minimal_project_recompile_action(&metadata, false);
assert_eq!(
action,
ProjectRecompileAction {
clean_packages: vec!["sqlx".into()],
clean_packages: vec![],
touch_paths: vec![
"/home/user/problematic/workspace/b_in_workspace_lib/src/lib.rs".into(),
"/home/user/problematic/workspace/c_in_workspace_bin/src/main.rs".into(),
Expand Down
Loading