tauri-apps/tauri · error

failed to write global API script

Error message

failed to write global API script

What it means

In the same function, `fs::write(out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH), ...)` is wrapped in `expect("failed to write global API script")`. It panics when the file list cannot be written — typically a bad `out_dir`, missing parent directory, or permission problem.

Solutions

  1. Verify `out_dir` is Cargo's real `OUT_DIR` (from `env::var("OUT_DIR")`) and exists before writing
  2. Create the directory (`fs::create_dir_all(out_dir)`) if it is missing
  3. Check write permissions on the build output directory / CI workspace
  4. Ensure the target path is not itself a directory
Defensive patterns

Strategy: validation

Validate before calling

let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR not set"));
assert!(out_dir.is_dir(), "OUT_DIR missing: {}", out_dir.display());

Prevention

When it happens

Trigger: Calling `save_global_api_scripts_paths` with an `out_dir` that does not exist, is read-only, or where `GLOBAL_API_SCRIPT_FILE_LIST_PATH` cannot be created (permissions, disk full, path is a directory).

Common situations: Passing a wrong/hardcoded out dir instead of Cargo's `OUT_DIR`; sandboxed CI runners with read-only build dirs; antivirus or ACLs blocking writes on Windows.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


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

Appendix: source

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

      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) {
        let script_path = PathBuf::from(value);
        scripts.push(script_path);
      }
    }

    if let Some(tauri_global_scripts) = tauri_global_scripts {
      scripts.insert(0, tauri_global_scripts);
    }

    fs::write(
      out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH),
      serde_json::to_string(&scripts).expect("failed to serialize global API script paths"),
    )
    .expect("failed to write global API script");
  }

  /// Read global api scripts from [`GLOBAL_API_SCRIPT_FILE_LIST_PATH`]
  pub fn read_global_api_scripts(out_dir: &Path) -> Option<Vec<String>> {
    let global_scripts_path = out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH);
    if !global_scripts_path.exists() {
      return None;
    }

    let global_scripts_str = fs::read_to_string(global_scripts_path)
      .expect("failed to read plugin global API script paths");
    let global_scripts = serde_json::from_str::<Vec<PathBuf>>(&global_scripts_str)
      .expect("failed to parse plugin global API script paths");

    Some(
      global_scripts
        .into_iter()
        .map(|p| {

View on GitHub (pinned to 460ec35447)