tauri-apps/tauri · error

failed to canonicalize global API script path

Error message

failed to canonicalize global API script path

What it means

`define_global_api_script_path` is a build-script helper that canonicalizes the given path (resolving symlinks and making it absolute) before emitting it to Cargo. It panics with this message when `Path::canonicalize` fails — the path does not exist or cannot be resolved (permissions, broken symlink, non-UTF8/OS error).

Solutions

  1. Ensure the global API script file exists (generate/write it) before calling `define_global_api_script_path`
  2. Verify the path is correct relative to the build script's working directory (`CARGO_MANIFEST_DIR`)
  3. Check symlink targets exist and the file is readable
  4. Join the path onto `PathBuf::from(env::var("CARGO_MANIFEST_DIR"))` to make it unambiguous

Example fix

// before
define_global_api_script_path(Path::new("scripts/api.js"));
// after
let p = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("scripts/api.js");
assert!(p.exists(), "api script missing: {}", p.display());
define_global_api_script_path(&p);
Defensive patterns

Strategy: validation

Validate before calling

let p = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join(rel);
assert!(p.is_file(), "global API script not found: {}", p.display());

Prevention

When it happens

Trigger: Calling `tauri_utils::plugin::define_global_api_script_path(path)` in a build script with a path that does not exist yet (file not generated before this call), a wrong relative path from the crate root, or a broken symlink.

Common situations: Build scripts calling the helper before the global API script is generated; typos in the script path; running the build from a different working directory than expected; symlink issues on CI.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/8d651e79d483f3b6. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri-utils/src/plugin.rs:27

#[cfg(any(feature = "build", feature = "build-2"))]
mod build {
  use std::{
    env::vars_os,
    fs,
    path::{Path, PathBuf},
  };

  const GLOBAL_API_SCRIPT_PATH_KEY: &str = "GLOBAL_API_SCRIPT_PATH";
  /// Known file name of the file that contains an array with the path of all API scripts defined with [`define_global_api_script_path`].
  pub const GLOBAL_API_SCRIPT_FILE_LIST_PATH: &str = "__global-api-script.js";

  /// Defines the path to the global API script using Cargo instructions.
  pub fn define_global_api_script_path(path: &Path) {
    println!(
      "cargo:{GLOBAL_API_SCRIPT_PATH_KEY}={}",
      path
        .canonicalize()
        .expect("failed to canonicalize global API script path")
        .display()
    )
  }

  /// Collects the path of all the global API scripts defined with [`define_global_api_script_path`]
  /// and saves them to the out dir with filename [`GLOBAL_API_SCRIPT_FILE_LIST_PATH`].
  ///
  /// `tauri_global_scripts` is only used in Tauri's monorepo for the examples to work
  /// since they don't have a build script to run `tauri-build` and pull in the deps env vars
  pub fn save_global_api_scripts_paths(out_dir: &Path, mut tauri_global_scripts: Option<PathBuf>) {
    let mut scripts = Vec::new();

    for (key, value) in vars_os() {
      let key = key.to_string_lossy();

      if key == format!("DEP_TAURI_{GLOBAL_API_SCRIPT_PATH_KEY}") {
        tauri_global_scripts = Some(PathBuf::from(value));
      } else if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {

View on GitHub (pinned to 460ec35447)