{"record":{"id":"9bad84baacf52066","repo":"astral-sh/ruff","slug":"failed-to-convert-path-to-url","errorCode":null,"errorMessage":"Failed to convert path to URL: {}","messagePattern":"Failed to convert path to URL: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/message/sarif.rs","lineNumber":390,"sourceCode":"            },\n            replacements,\n        }];\n\n        Some(SarifFix {\n            description: RuleDescription {\n                text: fix_description,\n            },\n            artifact_changes,\n        })\n    }\n\n    #[allow(clippy::unnecessary_wraps)]\n    fn uri(diagnostic: &Diagnostic) -> Result<String> {\n        let path = normalize_path(&*diagnostic.expect_ruff_filename());\n        cfg_select! {\n            target_arch = \"wasm32\" => Ok(format!(\"file://{}\", path.display())),\n            _ => url::Url::from_file_path(&path)\n                .map_err(|()| anyhow::anyhow!(\"Failed to convert path to URL: {}\", path.display()))\n                .map(|url| url.to_string()),\n        }\n    }\n\n    fn from_message(\n        diagnostic: &'a Diagnostic,\n        config: &'a DisplayDiagnosticConfig,\n    ) -> Result<Self> {\n        let start_location = diagnostic.ruff_start_location().unwrap_or_default();\n        let end_location = diagnostic.ruff_end_location().unwrap_or_default();\n        let region = SarifRegion {\n            start_line: start_location.line,\n            start_column: start_location.column,\n            end_line: end_location.line,\n            end_column: end_location.column,\n        };\n\n        let uri = Self::uri(diagnostic)?;","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/message/sarif.rs#L372-L408","documentation":"When serializing diagnostics to SARIF, each result needs a `uri` for the file location. Ruff normalizes the diagnostic filename and converts it to a `file://` URL with `url::Url::from_file_path`; if the path is not absolute or cannot be represented as a URL, this error is returned.","triggerScenarios":"Emitting diagnostics in `sarif` output format for a diagnostic whose filename, after `normalize_path`, is relative (not absolute) or otherwise invalid as a file-path URL (e.g. `--output-format sarif` with relative paths or odd stdin naming).","commonSituations":"Running `ruff check --output-format sarif .` from a working directory where paths stay relative, feeding stdin diagnostics with a placeholder filename, or paths containing characters the URL parser rejects.","solutions":["Run Ruff with absolute paths (e.g. `ruff check --output-format sarif \"$PWD/src\"`) so `from_file_path` succeeds.","Update to a recent Ruff version; newer versions handle wasm and relative-path cases differently.","If piping stdin, provide a valid absolute virtual filename rather than a placeholder.","Report the case if a legitimate absolute path still fails conversion (possible escaping bug)."],"exampleFix":"// before\n$ ruff check --output-format sarif src/\n// after\n$ ruff check --output-format sarif \"$PWD/src/\"","handlingStrategy":"try-catch","validationCode":"// ensure the path handed to ruff is absolute\nconst isAbs = (p) => p.startsWith('/');\nif (!isAbs(target)) target = require('path').resolve(target);","typeGuard":"fn is_absolute_path(p: &str) -> bool { std::path::Path::new(p).is_absolute() }","tryCatchPattern":"match run_ruff_sarif(paths) {\n    Err(e) if e.to_string().starts_with(\"Failed to convert path to URL\") => {\n        eprintln!(\"Re-run with absolute paths to generate valid SARIF URIs\");\n    }\n    other => other?,\n}","preventionTips":["Always pass absolute paths when using `--output-format sarif`.","Avoid stdin-based runs when SARIF output is required, or use a valid absolute placeholder filename.","Keep Ruff up to date for path/URL handling fixes."],"tags":["sarif","output-format","paths","ruff"],"backgroundTag":"url-to-path-conversion-failed","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}