slint-ui/slint · error · std::io::Error

Failed to convert path to URL

Error message

Failed to convert path to URL

What it means

In tools/lsp/preview.rs, get_path_from_cache converts a filesystem path to a file:// URL with Url::from_file_path before consulting the preview cache. If the conversion fails (path is relative, or not an absolute path), it returns a NotFound io::Error "Failed to convert path to URL". from_file_path only accepts absolute paths, so any relative or malformed path input triggers this.

Solutions

  1. Make the path absolute before calling: use std::fs::canonicalize or join it with the workspace root.
  2. Verify the path exists and is absolute (path.is_absolute()) before lookup.
  3. If the source is configuration, store absolute paths (or resolve them against the workspace root at ingestion).

Example fix

// before
let (version, code) = get_path_from_cache(Path::new("ui/app.slint"))?;
// after
let abs = std::fs::canonicalize("ui/app.slint")?;
let (version, code) = get_path_from_cache(&abs)?;
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_absolute(path: &Path) -> std::io::Result<PathBuf> {
    if path.is_absolute() { Ok(path.to_path_buf()) }
    else { std::fs::canonicalize(path) }
}

Type guard

fn is_abs_path(p: &Path) -> bool { p.is_absolute() && p.to_str().map_or(false, |s| !s.is_empty()) }

Try / catch

let path = ensure_absolute(path)?;
let (version, code) = get_path_from_cache(&path)
    .map_err(|e| e.context(format!("preview lookup failed for {}", path.display())))?;

Prevention

When it happens

Trigger: Calling get_path_from_cache with a relative Path (e.g. "src/main.slint") or a path that cannot be represented as a file URL (empty path, non-absolute), which makes Url::from_file_path return Err(()).

Common situations: An editor or tool passes a workspace-relative path from configuration or a diagnostic instead of an absolute path; or a path built by string concatenation without canonicalization is handed to the preview lookup.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@3a7e700487 (2026-09-16). Data as JSON: /api/errors/c97cc5ec4386a294. Report an issue: GitHub.

Appendix: source

Thrown at tools/lsp/preview.rs:1406

/// Otherwise the file is indeed missing.
///
/// In any way, register it as a dependency
fn get_url_from_cache(url: &Url) -> std::io::Result<(SourceFileVersion, String)> {
    PREVIEW_STATE.with_borrow_mut(|preview_state| {
        preview_state.dependencies.insert(url.to_owned());

        preview_state.source_code.get(url).map(|r| (r.version, r.code.clone())).ok_or(
            std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "File not registered in Live-Preview!",
            ),
        )
    })
}

fn get_path_from_cache(path: &Path) -> std::io::Result<(SourceFileVersion, String)> {
    let url = Url::from_file_path(path).map_err(|()| {
        std::io::Error::new(std::io::ErrorKind::NotFound, "Failed to convert path to URL")
    })?;
    get_url_from_cache(&url)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LoadBehavior {
    /// We reload the preview, most likely because a file has changed
    Reload,
    /// Load the preview and make the window visible if it wasn't already.
    Load,
    /// Load the preview and make the window visible if it wasn't already.
    LoadWithoutLiveData,
    /// We show the preview because the user asked for it. The UI should become visible and focused if it wasn't already
    BringWindowToFront,
}

pub fn reload_preview() {
    let pc = PREVIEW_STATE

View on GitHub (pinned to 3a7e700487)