Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: key runtime-template app data by identifier and set its Windows AUMID

Tw9398714d46e05f
diff --git a/AGENTS.md b/AGENTS.md
index 2c9d8131..3efea904 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -107,7 +107,7 @@ Oracles per row live under `tests/unit/` (for example `event-link-guard.test.js`
- CLI options are user-facing and must stay synchronized across `bin/helpers/cli-program.ts`, `bin/types.ts`, `bin/defaults.ts`, `bin/helpers/merge.ts`, generated `dist/cli.js`, `schema/pake.schema.json`, and `docs/cli-usage*.md`. Schema-to-CLI sync is enforced by `tests/unit/config-file.test.ts`; the rest is manual discipline.
- The `--json` machine contract is public API for agents: stdout carries exactly one JSON result (nothing else may write to stdout in machine mode), and the exit codes (0/2/3/4/1) plus error codes (`INVALID_INPUT`, `ENV_MISSING`, `BUILD_FAILED`, `UNEXPECTED`, plus `NETWORK` which is reserved and currently unused, since network failures report under the phase code) must stay stable. `logger.warn` feeds the JSON `warnings` array, so warn is for real warnings only, not status lines. Owners: `bin/utils/output.ts`, `bin/cli.ts`, `bin/utils/shell.ts`.
- Packaging CLI builds use a private temporary workspace for configs, injected code, icons, entitlements, and local content. The installed package is the template, not a writable staging area. Complete installed Tauri dependencies are reused without reinstalling; incomplete dependencies are installed only in the private workspace. Owners: `bin/utils/build-workspace.ts`, `bin/utils/dir.ts`, and `bin/helpers/merge.ts`. `restoreLocalTree()` at CLI startup remains only to recover a `dist_bak` left by the published 3.15.7 implementation. The repository's `bin/dev.ts` intentionally retains source-based Tauri development/watch behavior.
-- The `runtime-template` Cargo feature builds a prebuilt runtime that a separate builder customizes without a compiler: at launch it reads `pake-runtime/{pake.json, app.json, custom.js?, tray.png?}` from `Contents/Resources` on macOS or next to the executable elsewhere, and applies app.json's `identifier` and `productName` to the Tauri context before build. The CLI never enables the feature, so CLI-built apps keep their compiled configuration even if such a folder appears. A present but invalid override exits with a `[Pake]` message instead of falling back to the template's identity. Logic and tests live in `src-tauri/src/util.rs` (`apply_runtime_override`); keep `cargo clippy` and `cargo test --lib` green both with and without `--features runtime-template`.
+- The `runtime-template` Cargo feature builds a prebuilt runtime that a separate builder customizes without a compiler: at launch it reads `pake-runtime/{pake.json, app.json, custom.js?, tray.png?}` from `Contents/Resources` on macOS or next to the executable elsewhere, and applies app.json's `identifier` and `productName` to the Tauri context before build. The CLI never enables the feature, so CLI-built apps keep their compiled configuration even if such a folder appears. A present but invalid override exits with a `[Pake]` message instead of falling back to the template's identity. A runtime app keys its WebView profile folder under the config dir (`%APPDATA%\<identifier>\EBWebView` on Windows) by identifier instead of product name (`data_dir_name`), so a rename keeps logins and a generated name cannot share another program's folder; CLI apps keep the product-name folder. On Windows a runtime app also sets the process AppUserModelID to its identifier before any window exists, so shortcuts carrying that ID group with the running window and toasts are attributed to it. Logic and tests live in `src-tauri/src/util.rs` (`apply_runtime_override`, `data_dir_name`, `app_user_model_id`); keep `cargo clippy` and `cargo test --lib` green both with and without `--features runtime-template`.
- Linux multi-format builds compile once with `--no-bundle`, then use `tauri bundle` for each format with the same config, features, target, and debug profile. Keep per-format failure isolation and AppImage `NO_STRIP` retries on bundling only. Single-format builds retain the combined build path.
- Build preparation uses private workspaces before acquiring the shared compilation cache lock. Cancellation while waiting must clean only that workspace and leave the active owner's lock intact.
- Native build gates use a fresh output directory, require exit code zero and the CLI JSON's exact output path, and verify macOS executable architectures from `CFBundleExecutable`. Compare existing artifact paths with `realpath`, since macOS `/var` and `/private/var` can identify the same file; retain a negative test for genuinely different files. Old bundles, empty directories, or timeout artifacts never prove a successful build.
diff --git a/src-tauri/src/app/config.rs b/src-tauri/src/app/config.rs
index 38e8f848..3577321a 100644
--- a/src-tauri/src/app/config.rs
+++ b/src-tauri/src/app/config.rs
@@ -110,6 +110,10 @@ pub struct PakeConfig {
/// Replaces the compiled custom.js when a runtime override supplies one.
#[serde(skip)]
pub runtime_custom_js: Option<String>,
+ /// Set when a runtime override supplied the app identity, so per-app
+ /// directories follow the identifier rather than the product name.
+ #[serde(skip)]
+ pub runtime_app: bool,
}
impl PakeConfig {
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs
index af698c05..0bb72162 100644
--- a/src-tauri/src/app/window.rs
+++ b/src-tauri/src/app/window.rs
@@ -2,8 +2,8 @@ use crate::app::config::PakeConfig;
#[cfg(target_os = "windows")]
use crate::app::navigation::set_password_autosave;
use crate::util::{
- check_file_or_append, get_data_dir, get_download_dir, get_download_message_with_lang,
- sanitize_download_filename, show_toast, MessageType,
+ check_file_or_append, data_dir_name, get_data_dir, get_download_dir,
+ get_download_message_with_lang, sanitize_download_filename, show_toast, MessageType,
};
#[cfg(target_os = "macos")]
use dispatch::Queue;
@@ -421,11 +421,8 @@ fn build_window(
#[cfg(target_os = "macos")]
let prefer_native_window_tabbing = use_native_window_tabbing && label != "pake";
- let package_name = tauri_config
- .product_name
- .clone()
- .unwrap_or_else(|| "pake".to_string());
- let _data_dir = get_data_dir(app, package_name).map_err(tauri::Error::Io)?;
+ let _data_dir =
+ get_data_dir(app, data_dir_name(config, tauri_config)).map_err(tauri::Error::Io)?;
let window_config = config.windows.first().ok_or_else(|| {
tauri::Error::Io(std::io::Error::new(
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 8efd86cd..0e9d2a38 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -290,6 +290,10 @@ pub fn run_app() {
}
let (pake_config, tauri_config, runtime_app) = get_pake_config();
+ #[cfg(windows)]
+ if let Some(id) = util::app_user_model_id(runtime_app.as_ref()) {
+ util::set_app_user_model_id(id);
+ }
let mut context = tauri::generate_context!();
// Paths, app data and the macOS menu derive from the context, so a runtime
// app must not share the template's identity.
diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs
index 83d235f5..4191a63b 100644
--- a/src-tauri/src/util.rs
+++ b/src-tauri/src/util.rs
@@ -99,6 +99,14 @@ fn apply_runtime_override(
app_path.display()
));
}
+ // The identifier names per-app folders and the Windows AppUserModelID.
+ if !is_valid_identifier(&app.identifier) {
+ return Err(format!(
+ "{} has an invalid identifier {:?}",
+ app_path.display(),
+ app.identifier
+ ));
+ }
pake_config.runtime_custom_js = read_optional(&dir.join("custom.js"))?;
if !pake_config.system_tray_path.is_empty()
@@ -109,11 +117,60 @@ fn apply_runtime_override(
.to_string_lossy()
.into_owned();
}
+ pake_config.runtime_app = true;
tauri_config.identifier = app.identifier.clone();
tauri_config.product_name = Some(app.product_name.clone());
Ok((pake_config, tauri_config, Some(app)))
}
+/// Same rule as the CLI's --identifier check, capped at the 128 characters an
+/// AppUserModelID allows.
+fn is_valid_identifier(id: &str) -> bool {
+ let bytes = id.as_bytes();
+ id.len() <= 128
+ && bytes.first().is_some_and(u8::is_ascii_alphabetic)
+ && bytes.last().is_some_and(u8::is_ascii_alphanumeric)
+ && bytes
+ .iter()
+ .all(|b| b.is_ascii_alphanumeric() || *b == b'.' || *b == b'-')
+}
+
+/// Folder under the config dir that holds the WebView profile (WebView2's
+/// EBWebView on Windows). CLI apps keep their product-name folder; a runtime
+/// app uses its identifier so a rename keeps its logins and a generated name
+/// cannot land in another program's folder.
+pub fn data_dir_name(pake_config: &PakeConfig, tauri_config: &Config) -> String {
+ if pake_config.runtime_app {
+ tauri_config.identifier.clone()
+ } else {
+ tauri_config
+ .product_name
+ .clone()
+ .unwrap_or_else(|| "pake".to_string())
+ }
+}
+
+/// The Windows AppUserModelID a runtime app sets before creating windows, so
+/// a shortcut carrying the identifier groups with the running window and
+/// toasts (sent under the identifier) are attributed to it. CLI apps keep the
+/// shell's default.
+#[cfg_attr(not(windows), allow(dead_code))]
+pub fn app_user_model_id(runtime_app: Option<&RuntimeApp>) -> Option<&str> {
+ runtime_app.map(|app| app.identifier.as_str())
+}
+
+/// Must run before any window exists; the shell reads the ID at window creation.
+#[cfg(windows)]
+pub fn set_app_user_model_id(id: &str) {
+ use windows_sys::Win32::UI::Shell::SetCurrentProcessExplicitAppUserModelID;
+ let wide: Vec<u16> = id.encode_utf16().chain(std::iter::once(0)).collect();
+ // SAFETY: `wide` is a NUL-terminated UTF-16 buffer that outlives the call.
+ let result = unsafe { SetCurrentProcessExplicitAppUserModelID(wide.as_ptr()) };
+ if result < 0 {
+ eprintln!("[Pake] Could not set the AppUserModelID {id}: HRESULT {result:#010x}");
+ }
+}
+
pub fn get_data_dir(app: &AppHandle, package_name: String) -> std::io::Result<PathBuf> {
let data_dir = app
.path()
@@ -698,6 +755,20 @@ mod tests {
("pake.json", pake_json),
("app.json", r#"{"identifier":" ","productName":"Probe"}"#),
],
+ vec![
+ ("pake.json", pake_json),
+ (
+ "app.json",
+ r#"{"identifier":"com.pake/../x","productName":"Probe"}"#,
+ ),
+ ],
+ vec![
+ ("pake.json", pake_json),
+ (
+ "app.json",
+ r#"{"identifier":"com pake","productName":"Probe"}"#,
+ ),
+ ],
] {
let dir = runtime_dir(&files);
let (pake, tauri) = compiled_configs();
@@ -708,4 +779,53 @@ mod tests {
fs::remove_dir_all(dir.parent().unwrap()).unwrap();
}
}
+
+ #[test]
+ fn data_dir_follows_product_name_for_cli_and_identifier_for_runtime() {
+ let (pake, tauri) = compiled_configs();
+ let cli_name = tauri.product_name.clone().unwrap();
+ let (pake, tauri, _) = apply_runtime_override(pake, tauri, None).unwrap();
+ assert_eq!(data_dir_name(&pake, &tauri), cli_name);
+
+ let mut nameless = tauri.clone();
+ nameless.product_name = None;
+ assert_eq!(data_dir_name(&pake, &nameless), "pake");
+
+ let dir = runtime_dir(&[
+ ("pake.json", include_str!("../pake.json")),
+ ("app.json", RUNTIME_APP),
+ ]);
+ let (pake, tauri, _) = apply_runtime_override(pake, tauri, Some(&dir)).unwrap();
+ assert_eq!(data_dir_name(&pake, &tauri), "com.pake.a1b2c3");
+ fs::remove_dir_all(dir.parent().unwrap()).unwrap();
+ }
+
+ #[test]
+ fn identifiers_follow_the_cli_rule() {
+ for id in ["com.pake.a1b2c3", "com.example.app-2", "a1"] {
+ assert!(is_valid_identifier(id), "{id}");
+ }
+ let too_long = format!("a{}", "b".repeat(128));
+ for id in [
+ "",
+ "1abc",
+ "com.pake.",
+ "com pake",
+ "com/pake",
+ "..x",
+ too_long.as_str(),
+ ] {
+ assert!(!is_valid_identifier(id), "{id}");
+ }
+ }
+
+ #[test]
+ fn app_user_model_id_is_set_only_for_runtime_apps() {
+ assert_eq!(app_user_model_id(None), None);
+ let app = RuntimeApp {
+ identifier: "com.pake.a1b2c3".into(),
+ product_name: "Runtime Probe".into(),
+ };
+ assert_eq!(app_user_model_id(Some(&app)), Some("com.pake.a1b2c3"));
+ }
}