Don't require cargo to build offline queries (#1415)

In particular building with bazel and cargo-raze doesn't imply having
cargo at all, and deferring the lookup allows same-crate builds to
succeed with no change to semantics.

Fixes #1414

Signed-off-by: Robert Collins <robert.collins@cognite.com>
This commit is contained in:
Robert Collins 2021-09-01 02:35:50 +02:00 committed by GitHub
parent d94c081468
commit 89ee690550
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,6 @@
use std::path::PathBuf;
#[cfg(feature = "offline")]
use std::sync::{Arc, Mutex};
use once_cell::sync::Lazy;
use proc_macro2::TokenStream;
@ -30,9 +32,40 @@ struct Metadata {
#[cfg(feature = "offline")]
target_dir: PathBuf,
#[cfg(feature = "offline")]
workspace_root: Arc<Mutex<Option<PathBuf>>>,
}
#[cfg(feature = "offline")]
impl Metadata {
pub fn workspace_root(&self) -> PathBuf {
let mut root = self.workspace_root.lock().unwrap();
if root.is_none() {
use serde::Deserialize;
use std::process::Command;
let cargo = env("CARGO").expect("`CARGO` must be set");
let output = Command::new(&cargo)
.args(&["metadata", "--format-version=1"])
.current_dir(&self.manifest_dir)
.env_remove("__CARGO_FIX_PLZ")
.output()
.expect("Could not fetch metadata");
#[derive(Deserialize)]
struct CargoMetadata {
workspace_root: PathBuf,
}
let metadata: CargoMetadata =
serde_json::from_slice(&output.stdout).expect("Invalid `cargo metadata` output");
*root = Some(metadata.workspace_root);
}
root.clone().unwrap()
}
}
// If we are in a workspace, lookup `workspace_root` since `CARGO_MANIFEST_DIR` won't
// reflect the workspace dir: https://github.com/rust-lang/cargo/issues/3946
static METADATA: Lazy<Metadata> = Lazy::new(|| {
@ -71,31 +104,6 @@ static METADATA: Lazy<Metadata> = Lazy::new(|| {
let database_url = env("DATABASE_URL").ok();
#[cfg(feature = "offline")]
let workspace_root = {
use serde::Deserialize;
use std::process::Command;
let cargo = env("CARGO").expect("`CARGO` must be set");
let output = Command::new(&cargo)
.args(&["metadata", "--format-version=1"])
.current_dir(&manifest_dir)
.env_remove("__CARGO_FIX_PLZ")
.output()
.expect("Could not fetch metadata");
#[derive(Deserialize)]
struct CargoMetadata {
workspace_root: PathBuf,
}
let metadata: CargoMetadata =
serde_json::from_slice(&output.stdout).expect("Invalid `cargo metadata` output");
metadata.workspace_root
};
Metadata {
manifest_dir,
offline,
@ -103,7 +111,7 @@ static METADATA: Lazy<Metadata> = Lazy::new(|| {
#[cfg(feature = "offline")]
target_dir,
#[cfg(feature = "offline")]
workspace_root,
workspace_root: Arc::new(Mutex::new(None)),
}
});
@ -118,11 +126,12 @@ pub fn expand_input(input: QueryMacroInput) -> crate::Result<TokenStream> {
#[cfg(feature = "offline")]
_ => {
let data_file_path = METADATA.manifest_dir.join("sqlx-data.json");
let workspace_data_file_path = METADATA.workspace_root.join("sqlx-data.json");
if data_file_path.exists() {
expand_from_file(input, data_file_path)
} else if workspace_data_file_path.exists() {
} else {
let workspace_data_file_path = METADATA.workspace_root().join("sqlx-data.json");
if workspace_data_file_path.exists() {
expand_from_file(input, workspace_data_file_path)
} else {
Err(
@ -132,6 +141,7 @@ pub fn expand_input(input: QueryMacroInput) -> crate::Result<TokenStream> {
)
}
}
}
#[cfg(not(feature = "offline"))]
Metadata { offline: true, .. } => {