libnyanpasu/clash-nyanpasu · error

failed to get file stem

Error message

failed to get file stem

What it means

init_launch resolves the current executable path (dunce::canonicalize) and extracts its file_stem to register auto-launch. If the canonicalized path has no file stem or the stem is not valid UTF-8, the error is thrown. On normal installs this is impossible; it indicates a corrupted or exotic executable path.

Source

Thrown at backend/tauri/src/core/sysopt.rs:222

        Ok(())
    }

    /// init the auto launch
    pub fn init_launch(&self) -> Result<()> {
        let enable = { Config::verge().latest().enable_auto_launch };
        let enable = enable.unwrap_or(false);

        log::info!(target: "app", "Initializing auto-launch with enable={}", enable);

        let app_exe = current_exe()?;
        let app_exe = dunce::canonicalize(app_exe)?;
        log::debug!(target: "app", "Resolved app executable path: {:?}", app_exe);

        let app_name = app_exe
            .file_stem()
            .and_then(|f| f.to_str())
            .ok_or(anyhow!("failed to get file stem"))?;

        let app_path = app_exe
            .as_os_str()
            .to_str()
            .ok_or(anyhow!("failed to get app_path"))?
            .to_string();

        log::debug!(target: "app", "Initial app path: {}", app_path);

        // fix issue #26
        #[cfg(target_os = "windows")]
        let app_path = format!("\"{app_path}\"");
        #[cfg(target_os = "windows")]
        log::debug!(target: "app", "Windows formatted app path: {}", app_path);

        // use the /Applications/Clash Nyanpasu.app path
        #[cfg(target_os = "macos")]
        let app_path = (|| -> Option<String> {

View on GitHub (pinned to f7dbce2997)

Solutions

  1. Move/install the app to a plain ASCII path without special characters
  2. Log the canonicalized app_exe path to diagnose the malformed path
  3. Fall back to the full file_name() string when file_stem() is unavailable

Example fix

// before
let app_name = app_exe.file_stem().and_then(|f| f.to_str()).ok_or(anyhow!("failed to get file stem"))?;
// after
let app_name = app_exe.file_stem().or_else(|| app_exe.file_name()).and_then(|f| f.to_str()).ok_or_else(|| anyhow!("failed to get file stem from {:?}", app_exe))?;
Defensive patterns

Strategy: validation

Validate before calling

let exe = std::env::current_exe()?;
if exe.file_stem().and_then(|s| s.to_str()).is_none() {
    eprintln!("executable path {:?} has no valid UTF-8 file stem; auto-launch will fail", exe);
}

Type guard

fn has_valid_stem(p: &std::path::Path) -> bool {
    p.file_stem().and_then(|s| s.to_str()).is_some()
}

Try / catch

match init_launch(app_exe) {
    Ok(launch) => launch,
    Err(e) if e.to_string().contains("file stem") => /* log path, degrade: skip auto-launch setup */,
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling update_launch -> init_launch when current_exe() yields a path whose file_name is missing (e.g. "/" or a drive root) or whose stem contains invalid UTF-8 (non-Unicode path components).

Common situations: App installed in a directory with non-UTF-8 (e.g. GBK-encoded) characters on Windows, running from a temp/deleted path, or unusual portable deployments where the exe path degenerates.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of libnyanpasu/clash-nyanpasu@f7dbce2997 (2026-09-08). Data as JSON: /api/errors/1fbab6442a069b25. Report an issue: GitHub.