mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-12 23:37:13 +00:00
Use cargo check consistently in prepare
(#2071)
This commit is contained in:
parent
0823e1139c
commit
20af5cd9c3
2 changed files with 39 additions and 42 deletions
|
@ -56,8 +56,8 @@ pub struct Metadata {
|
||||||
///
|
///
|
||||||
/// Typically `target` at the workspace root, but can be overridden
|
/// Typically `target` at the workspace root, but can be overridden
|
||||||
target_directory: PathBuf,
|
target_directory: PathBuf,
|
||||||
/// Package metadata for the crate in the current working directory, None if run from
|
/// Crate in the current working directory, empty if run from a
|
||||||
/// a workspace with the `merged` flag.
|
/// virtual workspace root.
|
||||||
current_package: Option<Package>,
|
current_package: Option<Package>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,8 +110,8 @@ impl FromStr for Metadata {
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let cargo_metadata: CargoMetadata = serde_json::from_str(s)?;
|
let cargo_metadata: CargoMetadata = serde_json::from_str(s)?;
|
||||||
|
|
||||||
// Extract the package for the current working directory, will be empty if running
|
// Extract the package in the current working directory, empty if run from a
|
||||||
// from a workspace root.
|
// virtual workspace root.
|
||||||
let current_package: Option<Package> = cargo_metadata.root_package().map(Package::from);
|
let current_package: Option<Package> = cargo_metadata.root_package().map(Package::from);
|
||||||
|
|
||||||
let CargoMetadata {
|
let CargoMetadata {
|
||||||
|
|
|
@ -10,7 +10,6 @@ use std::io::{BufReader, BufWriter};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::SystemTime;
|
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
|
||||||
use crate::metadata::Metadata;
|
use crate::metadata::Metadata;
|
||||||
|
@ -125,10 +124,9 @@ hint: This command only works in the manifest directory of a Cargo package."#
|
||||||
// have repeatedly caused issues in the past.
|
// have repeatedly caused issues in the past.
|
||||||
let _ = remove_dir_all(metadata.target_directory().join("sqlx"));
|
let _ = remove_dir_all(metadata.target_directory().join("sqlx"));
|
||||||
|
|
||||||
let check_status = if merge {
|
// Try only triggering a recompile on crates that use `sqlx-macros`, falling back to a full
|
||||||
// Try only triggering a recompile on crates that use `sqlx-macros` falling back to a full
|
// clean on error.
|
||||||
// clean on error
|
match setup_minimal_project_recompile(&cargo, &metadata, merge) {
|
||||||
match setup_minimal_project_recompile(&cargo, &metadata) {
|
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!(
|
println!(
|
||||||
|
@ -142,6 +140,8 @@ hint: This command only works in the manifest directory of a Cargo package."#
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Compile the queries.
|
||||||
|
let check_status = {
|
||||||
let mut check_command = Command::new(&cargo);
|
let mut check_command = Command::new(&cargo);
|
||||||
check_command
|
check_command
|
||||||
.arg("check")
|
.arg("check")
|
||||||
|
@ -151,35 +151,18 @@ hint: This command only works in the manifest directory of a Cargo package."#
|
||||||
|
|
||||||
// `cargo check` recompiles on changed rust flags which can be set either via the env var
|
// `cargo check` recompiles on changed rust flags which can be set either via the env var
|
||||||
// or through the `rustflags` field in `$CARGO_HOME/config` when the env var isn't set.
|
// or through the `rustflags` field in `$CARGO_HOME/config` when the env var isn't set.
|
||||||
// Because of this we only pass in `$RUSTFLAGS` when present
|
// Because of this we only pass in `$RUSTFLAGS` when present.
|
||||||
if let Ok(rustflags) = env::var("RUSTFLAGS") {
|
if let Ok(rustflags) = env::var("RUSTFLAGS") {
|
||||||
check_command.env("RUSTFLAGS", rustflags);
|
check_command.env("RUSTFLAGS", rustflags);
|
||||||
}
|
}
|
||||||
|
|
||||||
check_command.status()?
|
check_command.status()?
|
||||||
} else {
|
|
||||||
Command::new(&cargo)
|
|
||||||
.arg("rustc")
|
|
||||||
.args(cargo_args)
|
|
||||||
.arg("--")
|
|
||||||
.arg("--emit")
|
|
||||||
.arg("dep-info,metadata")
|
|
||||||
// set an always-changing cfg so we can consistently trigger recompile
|
|
||||||
.arg("--cfg")
|
|
||||||
.arg(format!(
|
|
||||||
"__sqlx_recompile_trigger=\"{}\"",
|
|
||||||
SystemTime::UNIX_EPOCH.elapsed()?.as_millis()
|
|
||||||
))
|
|
||||||
.env("SQLX_OFFLINE", "false")
|
|
||||||
.env("DATABASE_URL", url)
|
|
||||||
.env("CARGO_TARGET_DIR", metadata.target_directory().clone())
|
|
||||||
.status()?
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if !check_status.success() {
|
if !check_status.success() {
|
||||||
bail!("`cargo check` failed with status: {}", check_status);
|
bail!("`cargo check` failed with status: {}", check_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Combine the queries into one file.
|
||||||
let package_dir = if merge {
|
let package_dir = if merge {
|
||||||
// Merge queries from all workspace crates.
|
// Merge queries from all workspace crates.
|
||||||
"**"
|
"**"
|
||||||
|
@ -243,12 +226,26 @@ struct ProjectRecompileAction {
|
||||||
/// This gets a listing of all crates that depend on `sqlx-macros` (direct and transitive). The
|
/// This gets a listing of all crates that depend on `sqlx-macros` (direct and transitive). The
|
||||||
/// crates within the current workspace have their source file's mtimes updated while crates
|
/// crates within the current workspace have their source file's mtimes updated while crates
|
||||||
/// outside the workspace are selectively `cargo clean -p`ed. In this way we can trigger a
|
/// outside the workspace are selectively `cargo clean -p`ed. In this way we can trigger a
|
||||||
/// recompile of crates that may be using compile-time macros without forcing a full recompile
|
/// recompile of crates that may be using compile-time macros without forcing a full recompile.
|
||||||
fn setup_minimal_project_recompile(cargo: &str, metadata: &Metadata) -> anyhow::Result<()> {
|
///
|
||||||
|
/// If `workspace` is false, only the current package will have its files' mtimes updated.
|
||||||
|
fn setup_minimal_project_recompile(
|
||||||
|
cargo: &str,
|
||||||
|
metadata: &Metadata,
|
||||||
|
workspace: bool,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
let ProjectRecompileAction {
|
let ProjectRecompileAction {
|
||||||
clean_packages,
|
clean_packages,
|
||||||
touch_paths,
|
touch_paths,
|
||||||
} = minimal_project_recompile_action(metadata)?;
|
} = if workspace {
|
||||||
|
minimal_project_recompile_action(metadata)?
|
||||||
|
} else {
|
||||||
|
// Only touch the current crate.
|
||||||
|
ProjectRecompileAction {
|
||||||
|
clean_packages: Vec::new(),
|
||||||
|
touch_paths: metadata.current_package().context("Failed to get package in current working directory, pass `--merged` if running from a workspace root")?.src_paths().to_vec(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for file in touch_paths {
|
for file in touch_paths {
|
||||||
let now = filetime::FileTime::now();
|
let now = filetime::FileTime::now();
|
||||||
|
|
Loading…
Reference in a new issue