{"record":{"id":"5e698ba46e9b31b9","repo":"rust-lang/cargo","slug":"single-file-packages-cannot-be-used-as-dependencie","errorCode":null,"errorMessage":"single file packages cannot be used as dependencies","messagePattern":"single file packages cannot be used as dependencies","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/workspace/source_id.rs","lineNumber":403,"sourceCode":"\n    /// Returns `true` if this source from a Git repository.\n    pub fn is_git(self) -> bool {\n        matches!(self.inner.kind, SourceKind::Git(_))\n    }\n\n    /// Creates an implementation of `Source` corresponding to this ID.\n    pub fn load<'a>(self, gctx: &'a GlobalContext) -> CargoResult<Box<dyn Source + 'a>> {\n        trace!(\"loading SourceId; {}\", self);\n        match self.inner.kind {\n            SourceKind::Git(..) => Ok(Box::new(GitSource::new(self, gctx)?)),\n            SourceKind::Path => {\n                let path = self\n                    .inner\n                    .url\n                    .to_file_path()\n                    .expect(\"path sources cannot be remote\");\n                if crate::workspace::parser::is_embedded(&path) && path.is_file() {\n                    anyhow::bail!(\"single file packages cannot be used as dependencies\")\n                }\n                Ok(Box::new(PathSource::new(&path, self, gctx)))\n            }\n            SourceKind::Registry | SourceKind::SparseRegistry => {\n                Ok(Box::new(RegistrySource::remote(self, gctx)?))\n            }\n            SourceKind::LocalRegistry => {\n                let path = self\n                    .inner\n                    .url\n                    .to_file_path()\n                    .expect(\"path sources cannot be remote\");\n                Ok(Box::new(RegistrySource::local(self, &path, gctx)))\n            }\n            SourceKind::Directory => {\n                let path = self\n                    .inner\n                    .url","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/workspace/source_id.rs#L385-L421","documentation":"When Cargo loads a `Path` source via `SourceId::load`, it converts the source URL to a filesystem path and checks two conditions: whether the path is an embedded (single-file) package (`is_embedded(&path)`) and whether that path points at a file rather than a directory. Single-file packages (e.g. a single `main.rs`/`lib.rs`) are valid as a workspace root but are explicitly rejected when the path source is being loaded as a dependency of another package. The bail happens before a `PathSource` is constructed.","triggerScenarios":"Declaring a path dependency that points directly at a single `.rs` file identified as embedded, e.g. `my-dep = { path = \"./libs/single.rs\" }`, then resolving/loading that dependency's source. The check `crate::workspace::parser::is_embedded(&path) && path.is_file()` is true.","commonSituations":"Pointing a path dependency at a standalone `.rs` file instead of a directory containing a `Cargo.toml`; migrating a single-file experiment to a dependency without restructuring it into a proper package; tooling that auto-generates path deps from file paths.","solutions":["Convert the single-file package into a proper Cargo package: create a directory containing a `Cargo.toml` and the source file under `src/lib.rs` or `src/main.rs`, then point the dependency path at that directory.","Update the dependency declaration to reference the directory containing a valid `Cargo.toml` rather than the `.rs` file directly.","If you need the single file only as a build target and not a dependency, move it to a workspace member rather than a path dependency."],"exampleFix":"# before\n[dependencies]\nmy-lib = { path = \"./libs/single.rs\" }\n\n# after (create ./libs/my-lib/Cargo.toml + ./libs/my-lib/src/lib.rs)\n[dependencies]\nmy-lib = { path = \"./libs/my-lib\" }","handlingStrategy":"validation","validationCode":"// before adding a path dependency, ensure it points at a directory with a Cargo.toml\nuse std::path::Path;\nfn is_valid_path_dep(p: &Path) -> bool {\n    p.is_dir() && p.join(\"Cargo.toml\").exists()\n}\nassert!(is_valid_path_dep(Path::new(\"./libs/my-lib\")));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always point path dependencies at a directory containing a Cargo.toml, never at a raw .rs file.","Add a CI check that every path dependency resolves to a package directory.","Use `cargo metadata` to validate the manifest before committing path-dep changes."],"tags":["path-dependency","manifest","cargo","single-file-package"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}