{"record":{"id":"e5038004613a0f2c","repo":"transact-rs/sqlx","slug":"could-not-fetch-metadata","errorCode":null,"errorMessage":"Could not fetch metadata","messagePattern":"Could not fetch metadata","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sqlx-macros-core/src/query/metadata.rs","lineNumber":45,"sourceCode":"        self.env.get_or_try_init(|builder| {\n            load_env(&self.manifest_dir, &workspace_root, &self.config, builder)\n        })\n    }\n\n    pub fn workspace_root(&self) -> PathBuf {\n        let mut root = self.workspace_root.lock().unwrap();\n        if root.is_none() {\n            use serde::Deserialize;\n            use std::process::Command;\n\n            let cargo = crate::env(\"CARGO\").unwrap();\n\n            let output = Command::new(cargo)\n                .args([\"metadata\", \"--format-version=1\", \"--no-deps\"])\n                .current_dir(&self.manifest_dir)\n                .env_remove(\"__CARGO_FIX_PLZ\")\n                .output()\n                .expect(\"Could not fetch metadata\");\n\n            #[derive(Deserialize)]\n            struct CargoMetadata {\n                workspace_root: PathBuf,\n            }\n\n            let metadata: CargoMetadata =\n                serde_json::from_slice(&output.stdout).expect(\"Invalid `cargo metadata` output\");\n\n            *root = Some(metadata.workspace_root);\n        }\n        root.clone().unwrap()\n    }\n}\n\npub fn try_for_crate() -> crate::Result<Arc<Metadata>> {\n    /// The `MtimeCache` in this type covers the config itself,\n    /// any changes to which will indirectly invalidate the loaded env vars as well.","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/transact-rs/sqlx/blob/03af8bcc5711a1935580a54bea249c219a0c217d/sqlx-macros-core/src/query/metadata.rs#L27-L63","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Pre-check before building with sqlx macros:\nuse std::process::Command;\nlet out = Command::new(\"cargo\").args([\"metadata\", \"--format-version=1\", \"--no-deps\"]).output().unwrap();\nassert!(out.status.success(), \"cargo metadata failed: {}\", String::from_utf8_lossy(&out.stderr));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["sqlx","macros","cargo-metadata","build","panic"],"backgroundTag":"cargo-metadata-failed","analyzedSha":"03af8bcc5711a1935580a54bea249c219a0c217d","analyzedAt":"2026-09-03T15:01:28.752Z","contentChangedAt":"2026-09-03T15:01:28.752Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}