astral-sh/ruff · error

Failed to convert path to URL

Error message

Failed to convert path to URL: {}

What it means

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.

Solutions

  1. Run Ruff with absolute paths (e.g. `ruff check --output-format sarif "$PWD/src"`) so `from_file_path` succeeds.
  2. Update to a recent Ruff version; newer versions handle wasm and relative-path cases differently.
  3. If piping stdin, provide a valid absolute virtual filename rather than a placeholder.
  4. Report the case if a legitimate absolute path still fails conversion (possible escaping bug).

Example fix

// before
$ ruff check --output-format sarif src/
// after
$ ruff check --output-format sarif "$PWD/src/"
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the path handed to ruff is absolute
const isAbs = (p) => p.startsWith('/');
if (!isAbs(target)) target = require('path').resolve(target);

Type guard

fn is_absolute_path(p: &str) -> bool { std::path::Path::new(p).is_absolute() }

Try / catch

match run_ruff_sarif(paths) {
    Err(e) if e.to_string().starts_with("Failed to convert path to URL") => {
        eprintln!("Re-run with absolute paths to generate valid SARIF URIs");
    }
    other => other?,
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/9bad84baacf52066. Report an issue: GitHub.

Appendix: source

Thrown at crates/ruff_linter/src/message/sarif.rs:390

            },
            replacements,
        }];

        Some(SarifFix {
            description: RuleDescription {
                text: fix_description,
            },
            artifact_changes,
        })
    }

    #[allow(clippy::unnecessary_wraps)]
    fn uri(diagnostic: &Diagnostic) -> Result<String> {
        let path = normalize_path(&*diagnostic.expect_ruff_filename());
        cfg_select! {
            target_arch = "wasm32" => Ok(format!("file://{}", path.display())),
            _ => url::Url::from_file_path(&path)
                .map_err(|()| anyhow::anyhow!("Failed to convert path to URL: {}", path.display()))
                .map(|url| url.to_string()),
        }
    }

    fn from_message(
        diagnostic: &'a Diagnostic,
        config: &'a DisplayDiagnosticConfig,
    ) -> Result<Self> {
        let start_location = diagnostic.ruff_start_location().unwrap_or_default();
        let end_location = diagnostic.ruff_end_location().unwrap_or_default();
        let region = SarifRegion {
            start_line: start_location.line,
            start_column: start_location.column,
            end_line: end_location.line,
            end_column: end_location.column,
        };

        let uri = Self::uri(diagnostic)?;

View on GitHub (pinned to 26f38c119c)