{"record":{"id":"a848e905967ee656","repo":"rust-lang/cargo","slug":"can-only-edit-absolute-paths-got","errorCode":null,"errorMessage":"can only edit absolute paths, got {}","messagePattern":"can only edit absolute paths, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/workspace/editor/manifest.rs","lineNumber":268,"sourceCode":"impl Deref for LocalManifest {\n    type Target = Manifest;\n\n    fn deref(&self) -> &Manifest {\n        &self.manifest\n    }\n}\n\nimpl DerefMut for LocalManifest {\n    fn deref_mut(&mut self) -> &mut Manifest {\n        &mut self.manifest\n    }\n}\n\nimpl LocalManifest {\n    /// Construct the `LocalManifest` corresponding to the `Path` provided..\n    pub fn try_new(path: &Path) -> CargoResult<Self> {\n        if !path.is_absolute() {\n            anyhow::bail!(\"can only edit absolute paths, got {}\", path.display());\n        }\n        let raw = cargo_util::paths::read(&path)?;\n        let mut data = raw.clone();\n        let mut embedded = None;\n        if is_embedded(path) {\n            let source = ScriptSource::parse(&data)?;\n            if let Some(frontmatter) = source.frontmatter_span() {\n                embedded = Some(Embedded::exists(frontmatter));\n                data = source.frontmatter().unwrap().to_owned();\n            } else if let Some(shebang) = source.shebang_span() {\n                embedded = Some(Embedded::after(shebang));\n                data = String::new();\n            } else {\n                embedded = Some(Embedded::start());\n                data = String::new();\n            }\n        }\n        let manifest = data.parse().context(\"unable to parse Cargo.toml\")?;","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/workspace/editor/manifest.rs#L250-L286","documentation":"Thrown by LocalManifest::try_new when the provided path is not absolute. The editor needs an absolute path because it reads the file, may resolve includes/paths relative to it, and writes back to the same location; relative paths would make the operation dependent on the process's current directory, which is fragile. It uses Path::is_absolute to check.","triggerScenarios":"Calling LocalManifest::try_new(&Path::new(\"Cargo.toml\")) or any relative path. Common when a caller obtains a path from user input or from a config file without canonicalizing it first.","commonSituations":"A CLI tool passes a user-supplied relative path directly. A test uses a relative fixture path. Joining a relative base with a filename but forgetting to canonicalize. Paths read from environment variables that are relative.","solutions":["Canonicalize the path before calling try_new: std::fs::canonicalize(&path) or std::env::current_dir().join(path).","If the path comes from CLI args, use std::env::current_dir().join(arg) to absolutize it.","If the path may not exist yet (so canonicalize fails), join it with current_dir() manually."],"exampleFix":"// before\nlet manifest = LocalManifest::try_new(Path::new(\"Cargo.toml\"))?; // errors\n\n// after\nlet abs = std::env::current_dir()?.join(\"Cargo.toml\");\nlet manifest = LocalManifest::try_new(&abs)?;","handlingStrategy":"validation","validationCode":"fn ensure_absolute(path: &Path) -> CargoResult<PathBuf> {\n    if path.is_absolute() { Ok(path.to_path_buf()) }\n    else { Ok(std::env::current_dir()?.join(path)) }\n}\n// let abs = ensure_absolute(&path)?;\n// let manifest = LocalManifest::try_new(&abs)?;","typeGuard":"fn is_absolute_path(p: &Path) -> bool { p.is_absolute() }","tryCatchPattern":null,"preventionTips":["Canonicalize or absolutize all paths from user input before passing to LocalManifest::try_new.","In tests, use the absolute path of the temp directory fixture.","Join relative paths with current_dir() explicitly."],"tags":["cargo","manifest","path","editor","filesystem"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}