transact-rs/sqlx · error
Could not fetch metadata
Error message
Could not fetch metadata
What it means
sqlx's `query!` macros run `cargo metadata --format-version=1 --no-deps` in the crate's manifest directory to locate the workspace root (for resolving `.env` and config). This `.expect()` panics if the `cargo metadata` command fails to execute at all (non-zero exit / cannot spawn).
Source
Thrown at sqlx-macros-core/src/query/metadata.rs:45
self.env.get_or_try_init(|builder| {
load_env(&self.manifest_dir, &workspace_root, &self.config, builder)
})
}
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 = crate::env("CARGO").unwrap();
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");
#[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()
}
}
pub fn try_for_crate() -> crate::Result<Arc<Metadata>> {
/// The `MtimeCache` in this type covers the config itself,
/// any changes to which will indirectly invalidate the loaded env vars as well.View on GitHub (pinned to 03af8bcc57)
Solutions
- Run `cargo metadata --format-version=1 --no-deps` manually in the crate dir to see the real error; fix the reported Cargo.toml/workspace problem.
- Ensure the build is invoked through cargo so cargo is on PATH and CARGO_MANIFEST_DIR points at a valid crate.
- Fix or regenerate Cargo.lock / Cargo.toml if the manifest is corrupt.
- Clear stale target dir / `cargo clean` and rebuild.
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check before building with sqlx macros:
use std::process::Command;
let out = Command::new("cargo").args(["metadata", "--format-version=1", "--no-deps"]).output().unwrap();
assert!(out.status.success(), "cargo metadata failed: {}", String::from_utf8_lossy(&out.stderr)); Prevention
- Always build via cargo so cargo is on PATH and manifest dir is correct
- Keep Cargo.toml valid — run `cargo check` to catch manifest errors early
- Verify `cargo metadata --format-version=1 --no-deps` succeeds in CI before the build
When it happens
Trigger: Compiling a crate with `#[sqlx::test]`/`query!` macros while `cargo metadata` cannot run: cargo not on PATH (non-cargo build tool), corrupted workspace/manifest, or the command failing inside the manifest directory.
Common situations: Building with rust-analyzer or an IDE toolchain where cargo is unavailable or the working dir is wrong; broken Cargo.toml causing `cargo metadata` to exit non-zero; running builds from a directory where the manifest is missing; sandboxed CI denying process spawn.
Related errors
- Invalid `cargo metadata` output
- downcast to wrong DatabaseError type; original error: {self}
- downcast to wrong DatabaseError type; original error: {e}
- invalid column index: {}
- unimplemented!()
AI-assisted analysis of transact-rs/sqlx@03af8bcc57 (2026-09-03).
Data as JSON: /api/errors/e5038004613a0f2c.
Report an issue: GitHub.