slint-ui/slint · error

Failed to locate root directory, relative to

Error message

Failed to locate root directory, relative to {}

What it means

api/cpp/build.rs derives the repository root by walking two ancestors up from CARGO_MANIFEST_DIR (api/cpp -> api -> root). The panic fires when that ancestor does not exist, meaning the crate was built from an unexpected directory depth — e.g. the api/cpp sources were vendored or copied somewhere that breaks the assumed layout. It is a build-time sanity guard on directory structure, not a user-facing error.

Solutions

  1. Verify that CARGO_MANIFEST_DIR points at <root>/api/cpp; if the crate was moved, adjust the ancestors().nth(2) depth to match the new nesting level
  2. Run the build from a normal cargo invocation; overriding CARGO_MANIFEST_DIR to a non-standard path breaks the ancestor walk
  3. Replace the panic with an explicit Path::ancestors search for a directory containing workspace Cargo.toml to make the script location-independent
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at api/cpp/build.rs:13 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at api/cpp/build.rs:13

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

use crate::cbindgen::EnabledFeatures;
use std::path::Path;
mod cbindgen;

fn main() -> Result<(), anyhow::Error> {
    let manifest_dir = std::env::var_os("CARGO_MANIFEST_DIR").unwrap();

    // Go from $root/api/cpp down to $root
    let root_dir = Path::new(&manifest_dir).ancestors().nth(2).unwrap_or_else(|| {
        panic!("Failed to locate root directory, relative to {}", manifest_dir.to_string_lossy())
    });

    println!("cargo:rerun-if-env-changed=SLINT_GENERATED_INCLUDE_DIR");
    let output_dir = std::env::var_os("SLINT_GENERATED_INCLUDE_DIR").unwrap_or_else(|| {
        Path::new(&std::env::var_os("OUT_DIR").unwrap()).join("generated_include").into()
    });
    let output_dir = Path::new(&output_dir);

    println!("cargo:GENERATED_INCLUDE_DIR={}", output_dir.display());

    let enabled_features = EnabledFeatures::from_env();

    let dependencies = cbindgen::gen_all(root_dir, output_dir, enabled_features)?;
    for path in dependencies {
        println!("cargo:rerun-if-changed={}", path.display());
    }
    Ok(())
}

View on GitHub (pinned to bb937076de)