astral-sh/ruff · error

Failed to convert path to URL: {}

Error message

Failed to convert path to URL: {}

What it means

When emitting SARIF, each diagnostic's file path is converted to a `file://` URL with url::Url::from_file_path, which only accepts absolute paths; a relative path produces this error (wasm32 builds format the path directly and never hit it).

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 672bb4edf0)

Solutions

  1. Give stdin an absolute name: `ruff check --stdin-filename "$PWD/stdin.py" - --output-format sarif`
  2. Pass absolute file paths instead of relative ones
  3. If the path cannot be made absolute, use a format that does not build file:// URLs (full, concise, json, json-lines)

Example fix

# before
ruff check --output-format sarif - < input.py

# after
ruff check --stdin-filename "$PWD/input.py" --output-format sarif - < input.py
Defensive patterns

Strategy: validation

Validate before calling

# absolutize inputs before invoking ruff
f=$(realpath -- "$f")
ruff check --output-format sarif "$f"
# for stdin, always pass an absolute --stdin-filename
ruff check --stdin-filename "$PWD/stdin.py" --output-format sarif -

Prevention

When it happens

Trigger: `ruff check --output-format sarif` on a run where a diagnostic's filename is not absolute — notably linting stdin (`ruff check -` names the file `-`) or a relative `--stdin-filename` value.

Common situations: Editor integrations and scripts that pipe source via stdin and request SARIF; tools passing relative paths in unusual working directories.

Related errors


AI-assisted analysis of astral-sh/ruff@672bb4edf0 (2026-08-16). Data as JSON: /api/errors/9bad84baacf52066. Report an issue: GitHub.