Skip to content

fix(macros-core): optional dependency on cargo #2978

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 8 additions & 8 deletions sqlx-macros-core/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ struct Metadata {
}

impl Metadata {
pub fn workspace_root(&self) -> PathBuf {
pub fn workspace_root(&self) -> Option<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 cargo = env("CARGO").ok()?;

let output = Command::new(&cargo)
.args(&["metadata", "--format-version=1", "--no-deps"])
let output = Command::new(cargo)
.args(["metadata", "--format-version=1", "--no-deps"])
.current_dir(&self.manifest_dir)
.env_remove("__CARGO_FIX_PLZ")
.output()
.expect("Could not fetch metadata");
.ok()?;

#[derive(Deserialize)]
struct CargoMetadata {
Expand All @@ -102,7 +102,7 @@ impl Metadata {

*root = Some(metadata.workspace_root);
}
root.clone().unwrap()
root.clone()
}
}

Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn expand_input<'a>(
let dirs = [
env("SQLX_OFFLINE_DIR").ok().map(PathBuf::from),
Some(METADATA.manifest_dir.join(".sqlx")),
Some(METADATA.workspace_root().join(".sqlx")),
METADATA.workspace_root().map(|root| root.join(".sqlx")),
];
let Some(data_file_path) = dirs
.iter()
Expand Down Expand Up @@ -200,7 +200,7 @@ pub fn expand_input<'a>(
database_url_parsed,
..
} => Err(format!(
"no database driver found matching URL scheme {:?}; the corresponding Cargo feature may need to be enabled",
"no database driver found matching URL scheme {:?}; the corresponding Cargo feature may need to be enabled",
database_url_parsed.scheme()
).into()),
QueryDataSource::Cached(data) => {
Expand Down