mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-18 16:08:55 +00:00
fix(macos): run on Big Sur and Monterey with Safari's staged WebKit (#3534)
When the system WebKit predates Safari 15.4 (macOS 11, and 12 before 12.3) and Safari's update has staged a newer WebKit, main re-execs once with DYLD_FRAMEWORK_PATH pointing at the staged frameworks and clears it again in the second process, so child processes keep the system frameworks. macOS 12.3+ and 13+ keep the system engine without a restart; an unreadable system version also keeps it. - Entitlements: allow-dyld-environment-variables only; library validation stays on (the staged framework is Apple-signed). - Bundle and link minimum back to 11.0 so Launch Services opens the app on Big Sur; .cargo/config.toml sets MACOSX_DEPLOYMENT_TARGET as a default a developer can override. - The staged copy is checked with a stat before the system version is read, so current macOS never spawns defaults at startup. - Debug builds skip the bindings export when the build checkout is absent. - Boot error page, install and contributor docs describe the Big Sur case. Tested by the contributor on macOS 11.7.11 (Safari 16.6.1 staged) and 26.5; by us on macOS 15.7.8 (no re-exec). Co-authored-by: qq471754603 <23146592+qq471754603@users.noreply.github.com>
This commit is contained in:
co-authored by
qq471754603
parent
2b353dfe53
commit
6b2bb0bb6a
@@ -0,0 +1,7 @@
|
||||
# Link debug builds for the oldest system we support, so Launch Services
|
||||
# accepts the bundle on macOS 11. Without this a newer SDK leaves
|
||||
# LC_BUILD_VERSION.minos at 12.0 and the app is refused (-10825) before main
|
||||
# can load a newer WebKit. An explicitly exported MACOSX_DEPLOYMENT_TARGET
|
||||
# still wins: this is only the default for local builds and CI.
|
||||
[env]
|
||||
MACOSX_DEPLOYMENT_TARGET = "11.0"
|
||||
@@ -43,7 +43,7 @@ User-visible text is written in English at the call site and goes through the te
|
||||
|
||||
## WebKit compatibility
|
||||
|
||||
On macOS and Linux the app runs in WebKit, and the supported floor is Safari 15.4, the engine of macOS 12 (the app's minimum). `pnpm build` runs `scripts/check-webkit.ts` on the bundle, and `src/platform/webkitCompatibility.test.ts` checks application regexes. Avoid:
|
||||
On macOS and Linux the app runs in WebKit, and the supported floor is Safari 15.4, the engine of macOS 12 (the app's minimum). On macOS 11 and on macOS 12 before 12.3, `src-tauri/src/macos_webview.rs` loads the newer WebKit staged by a Safari update, provided that update is installed. `pnpm build` runs `scripts/check-webkit.ts` on the bundle, and `src/platform/webkitCompatibility.test.ts` checks application regexes. Avoid:
|
||||
|
||||
- regex lookbehind (`(?<=…)`, `(?<!…)`), which needs Safari 16.4, and the regex `v` flag;
|
||||
- import attributes;
|
||||
|
||||
@@ -44,6 +44,8 @@ You don't need a JDK to build: the Java extension bundles in `java/` are checked
|
||||
|
||||
`pnpm dev:app` is the same as `pnpm tauri dev`, plus the global Tauri API (`window.__TAURI__`, from `src-tauri/tauri.dev.conf.json`), which is useful when you drive the app from scripts.
|
||||
|
||||
On macOS, `.cargo/config.toml` defaults `MACOSX_DEPLOYMENT_TARGET` to 11.0 (export your own value to override), so debug builds launch on older systems too. When the system WebKit predates Safari 15.4, `src-tauri/src/macos_webview.rs` re-execs once into the newer WebKit staged by a Safari update; set `OPENIDEA_DISABLE_STAGED_WEBKIT=1` to keep the system engine while debugging.
|
||||
|
||||
Other everyday commands:
|
||||
|
||||
| Command | What it does |
|
||||
|
||||
@@ -9,7 +9,7 @@ In the file names below, `<version>` is the release version, for example `0.1.40
|
||||
|
||||
## macOS
|
||||
|
||||
macOS 12 Monterey or later.
|
||||
macOS 12 Monterey or later. macOS 11 Big Sur also works: install the latest Safari update in Software Update first, and OpenIDEA will load the newer WebKit Apple stages alongside it (the system engine predates Safari 15.4). On macOS 12, install that update too if its system WebKit is older than Safari 15.4; otherwise the built-in engine is used as is.
|
||||
|
||||
| Mac | File |
|
||||
| ------------- | -------------------------------- |
|
||||
|
||||
@@ -43,7 +43,7 @@ description: "前端 UI 代码的规则:设计 token、CSS Modules、图标、
|
||||
|
||||
## WebKit 兼容性
|
||||
|
||||
在 macOS 和 Linux 上应用运行于 WebKit,最低支持 Safari 15.4,即 macOS 12 的引擎(应用的最低系统版本)。`pnpm build` 会对产物运行 `scripts/check-webkit.ts`,`src/platform/webkitCompatibility.test.ts` 会检查应用中的正则。请避免:
|
||||
在 macOS 和 Linux 上应用运行于 WebKit,最低支持 Safari 15.4,即 macOS 12 的引擎(应用的最低系统版本)。在 macOS 11 以及 12.3 之前的 macOS 12 上,`src-tauri/src/macos_webview.rs` 会加载 Safari 更新暂存的新版 WebKit,前提是已安装该更新。`pnpm build` 会对产物运行 `scripts/check-webkit.ts`,`src/platform/webkitCompatibility.test.ts` 会检查应用中的正则。请避免:
|
||||
|
||||
- 正则后行断言(`(?<=…)`、`(?<!…)`,需要 Safari 16.4)和正则 `v` 标志;
|
||||
- import attributes;
|
||||
|
||||
@@ -44,6 +44,8 @@ sudo apt-get install -y libwebkit2gtk-4.1-dev libayatana-appindicator3-dev librs
|
||||
|
||||
`pnpm dev:app` 与 `pnpm tauri dev` 相同,只是额外开启全局 Tauri API(`window.__TAURI__`,来自 `src-tauri/tauri.dev.conf.json`),用脚本驱动应用时比较方便。
|
||||
|
||||
在 macOS 上,`.cargo/config.toml` 默认把 `MACOSX_DEPLOYMENT_TARGET` 设为 11.0(自己导出该变量即可覆盖),让 debug 构建也能在旧系统启动。当系统 WebKit 低于 Safari 15.4 时,`src-tauri/src/macos_webview.rs` 会重新 exec 到 Safari 更新暂存的新版 WebKit;调试时可设 `OPENIDEA_DISABLE_STAGED_WEBKIT=1` 继续用系统引擎。
|
||||
|
||||
其他常用命令:
|
||||
|
||||
| 命令 | 作用 |
|
||||
|
||||
@@ -9,7 +9,7 @@ description: "在 macOS、Windows 或 Linux 上下载并安装 OpenIDEA,以及
|
||||
|
||||
## macOS
|
||||
|
||||
需要 macOS 12 Monterey 或更高版本。
|
||||
需要 macOS 12 Monterey 或更高版本。macOS 11 Big Sur 也可运行:请先通过「软件更新」安装最新的 Safari 更新,OpenIDEA 会加载 Apple 随该更新暂存的新版 WebKit(系统自带的引擎低于 Safari 15.4)。macOS 12 若尚未更新到 Safari 15.4 及以上,同样需要先安装该更新;之后直接使用系统引擎。
|
||||
|
||||
| Mac | 文件 |
|
||||
| ---------- | -------------------------------- |
|
||||
|
||||
+2
-2
@@ -13,7 +13,7 @@
|
||||
details: "启动错误详情",
|
||||
timeout: "启动未在 60 秒内完成。",
|
||||
outdatedMac:
|
||||
"这台 Mac 的系统网页引擎过旧:OpenIDEA 的界面运行在 macOS 自带的 WebKit 引擎上,需要 macOS 12 Monterey 或更高版本(引擎为 Safari 15.4 或更新)。更旧的系统缺少应用所需的引擎,需要升级 macOS。",
|
||||
"这台 Mac 的系统网页引擎过旧:OpenIDEA 的界面运行在 macOS 自带的 WebKit 引擎上,需要 macOS 12 Monterey 或更高版本(引擎为 Safari 15.4 或更新)。macOS 11 Big Sur 上请先通过「软件更新」安装最新的 Safari 更新,OpenIDEA 会加载 Apple 随该更新暂存的新版 WebKit;若系统引擎仍低于 Safari 15.4,则无法启动。",
|
||||
outdated:
|
||||
"系统的 WebKitGTK 版本过旧:OpenIDEA 需要较新的 WebKitGTK(webkit2gtk-4.1)。请通过系统的包管理器更新后重新打开 OpenIDEA。",
|
||||
}
|
||||
@@ -27,7 +27,7 @@
|
||||
details: "Startup error details",
|
||||
timeout: "Startup did not finish within 60 seconds.",
|
||||
outdatedMac:
|
||||
"The system web engine on this Mac is too old: OpenIDEA's interface runs on macOS's built-in WebKit and needs macOS 12 Monterey or later (Safari 15.4 or newer). Older macOS releases lack the engine the app needs, so macOS itself needs upgrading.",
|
||||
"The system web engine on this Mac is too old: OpenIDEA's interface runs on macOS's built-in WebKit and needs macOS 12 Monterey or later (Safari 15.4 or newer). On macOS 11 Big Sur, install the latest Safari update in Software Update; OpenIDEA then loads the newer WebKit Apple stages alongside it. Without that update the system engine is too old to start.",
|
||||
outdated:
|
||||
"The system's WebKitGTK is too old: OpenIDEA needs a recent WebKitGTK (webkit2gtk-4.1). Update it with your package manager, then open OpenIDEA again.",
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ import { dirname, resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import ts from "typescript";
|
||||
|
||||
// Safari 15.4 is the bundle's floor, macOS 12's engine (the app's minimum). Keep this floor in sync with vite.config.ts.
|
||||
// Safari 15.4 is the bundle's floor, macOS 12's engine (the app's minimum). On macOS 11 the app loads the newer WebKit staged by a Safari update (src-tauri/src/macos_webview.rs). Keep this floor in sync with vite.config.ts.
|
||||
// Syntax lowering does not replace newer runtime APIs in dependencies.
|
||||
const newerStatics = new Set([
|
||||
"Promise.withResolvers",
|
||||
|
||||
@@ -7,5 +7,10 @@
|
||||
<true/>
|
||||
<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
|
||||
<true/>
|
||||
<!-- Let main honour DYLD_FRAMEWORK_PATH when it re-execs into the WebKit
|
||||
staged by a Safari update on macOS 11 / pre-15.4 12. The staged
|
||||
framework is Apple-signed, so library validation stays enabled. -->
|
||||
<key>com.apple.security.cs.allow-dyld-environment-variables</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -56,6 +56,12 @@ pub fn specta_builder() -> tauri_specta::Builder<tauri::Wry> {
|
||||
|
||||
pub fn export_bindings(builder: &tauri_specta::Builder<tauri::Wry>) {
|
||||
let target = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/bindings.ts"));
|
||||
// Debug builds can end up on machines other than the one that built them
|
||||
// (packaged test builds). Without the checkout there is nothing to sync,
|
||||
// and `CARGO_MANIFEST_DIR` points at the build machine anyway.
|
||||
if !target.parent().is_some_and(Path::is_dir) {
|
||||
return;
|
||||
}
|
||||
// Rewritten only when it changes: every debug launch exports, also a second instance that just
|
||||
// hands its path over, and a rewrite makes Vite reload the running app's windows.
|
||||
let fresh = std::env::temp_dir().join(format!("openidea-bindings-{}.ts", std::process::id()));
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
//! On macOS systems whose WebKit predates Safari 15.4, WKWebView keeps using
|
||||
//! that old engine even after Safari itself is updated. Apple stages the newer
|
||||
//! WebKit next to the Safari app; Safari.app points dyld at it, and we do the
|
||||
//! same before Tauri creates a webview.
|
||||
//!
|
||||
//! dyld reads framework search paths at process start, so switching engines
|
||||
//! means setting `DYLD_FRAMEWORK_PATH` and re-executing. The second process
|
||||
//! clears the variable again: the new engine stays loaded (verified on macOS
|
||||
//! 11.7.11 by dlopen'ing WebKit after unsetenv), while children such as
|
||||
//! language servers and git helpers keep using the system frameworks.
|
||||
//!
|
||||
//! Only a system WebKit older than Safari 15.4 with a staged copy present
|
||||
//! takes this path. macOS 12.3+ already ships Safari 15.4's engine, and
|
||||
//! macOS 13+ ships an even newer one; neither restarts the process.
|
||||
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
const STAGED_FRAMEWORKS: &str = "/Library/Apple/System/Library/StagedFrameworks/Safari";
|
||||
const STAGED_WEBKIT: &str = "/Library/Apple/System/Library/StagedFrameworks/Safari/WebKit.framework";
|
||||
const SYSTEM_WEBKIT_PLIST: &str = "/System/Library/Frameworks/WebKit.framework/Resources/Info.plist";
|
||||
const DYLD_FRAMEWORK_PATH: &str = "DYLD_FRAMEWORK_PATH";
|
||||
/// Set on the re-exec so we never loop, and so the second process knows to
|
||||
/// drop the dyld variable before spawning children.
|
||||
const REEXEC_MARKER: &str = "OPENIDEA_WEBKIT_REEXEC";
|
||||
const DISABLE: &str = "OPENIDEA_DISABLE_STAGED_WEBKIT";
|
||||
|
||||
/// WebKit's CFBundleVersion starts with `166NN`, where NN is the engine
|
||||
/// generation: 16611 = Safari 14.1 / WebKit 611, 16613 = Safari 15.4 / WebKit
|
||||
/// 613, 16615 = Safari 16 / WebKit 615. Anything below 16613 cannot parse the
|
||||
/// Safari 15.4 bundle (`vite.config.ts`).
|
||||
const MIN_SYSTEM_WEBKIT_BUNDLE: u32 = 16613;
|
||||
|
||||
/// Call at the very start of `main`, before Tauri creates a webview.
|
||||
pub fn ensure_modern_webkit() {
|
||||
if std::env::var_os(REEXEC_MARKER).is_some() {
|
||||
// dyld snapshots DYLD_* at process start, so removing it here keeps
|
||||
// the engine we loaded without passing the override to children.
|
||||
clear_dyld_framework_path();
|
||||
return;
|
||||
}
|
||||
// The `openidea` CLI wrapper runs this binary too, but never opens a
|
||||
// webview; don't pay for a re-exec on every git hook invocation.
|
||||
if std::env::args_os().any(|arg| arg.as_os_str() == std::ffi::OsStr::new("--cli")) {
|
||||
return;
|
||||
}
|
||||
if std::env::var_os(DISABLE).is_some() {
|
||||
return;
|
||||
}
|
||||
// Check for the staged copy first: it is a stat, while reading the system
|
||||
// version spawns `defaults`, which every launch on a current macOS would pay.
|
||||
if !Path::new(STAGED_WEBKIT).is_dir() || !needs_staged_webkit(system_webkit_bundle_version(), true) {
|
||||
return;
|
||||
}
|
||||
relaunch();
|
||||
}
|
||||
|
||||
fn needs_staged_webkit(system_bundle: Option<u32>, staged_present: bool) -> bool {
|
||||
// If we cannot read the system version, keep the system engine: an
|
||||
// unverifiable switch is worse than showing the startup failure page.
|
||||
staged_present && matches!(system_bundle, Some(version) if version < MIN_SYSTEM_WEBKIT_BUNDLE)
|
||||
}
|
||||
|
||||
fn relaunch() {
|
||||
let Ok(exe) = std::env::current_exe() else { return };
|
||||
let error = Command::new(exe)
|
||||
.args(std::env::args_os().skip(1))
|
||||
.env(DYLD_FRAMEWORK_PATH, STAGED_FRAMEWORKS)
|
||||
.env(REEXEC_MARKER, "1")
|
||||
.exec();
|
||||
// `exec` returns only on failure. Continue on the system engine so the
|
||||
// startup failure page can tell the user to install the Safari update.
|
||||
eprintln!("OpenIDEA: could not relaunch with Safari's WebKit: {error}");
|
||||
}
|
||||
|
||||
fn system_webkit_bundle_version() -> Option<u32> {
|
||||
let out =
|
||||
Command::new("/usr/bin/defaults").arg("read").arg(SYSTEM_WEBKIT_PLIST).arg("CFBundleVersion").output().ok()?;
|
||||
if !out.status.success() {
|
||||
return None;
|
||||
}
|
||||
bundle_prefix(&String::from_utf8_lossy(&out.stdout))
|
||||
}
|
||||
|
||||
fn bundle_prefix(version: &str) -> Option<u32> {
|
||||
let version = version.trim();
|
||||
let end = version.find(|c: char| !c.is_ascii_digit()).unwrap_or(version.len());
|
||||
version[..end].parse().ok()
|
||||
}
|
||||
|
||||
// `remove_var` is unsafe from edition 2024 on. This runs single-threaded at
|
||||
// the start of main, before Tauri starts any workers.
|
||||
#[allow(unused_unsafe)]
|
||||
fn clear_dyld_framework_path() {
|
||||
unsafe { std::env::remove_var(DYLD_FRAMEWORK_PATH) };
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_webkit_bundle_versions() {
|
||||
assert_eq!(bundle_prefix("16611.3.10.1.19\n"), Some(16611));
|
||||
assert_eq!(bundle_prefix("16613.4.9.1.12"), Some(16613));
|
||||
assert_eq!(bundle_prefix("16615"), Some(16615));
|
||||
assert_eq!(bundle_prefix(""), None);
|
||||
assert_eq!(bundle_prefix("unknown"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_switches_when_system_webkit_predates_safari_15_4() {
|
||||
// Big Sur and Monterey before 12.3: staged WebKit is needed.
|
||||
assert!(needs_staged_webkit(Some(16611), true));
|
||||
assert!(needs_staged_webkit(Some(16612), true));
|
||||
// Monterey 12.3+ and Ventura+: system WebKit is already new enough.
|
||||
assert!(!needs_staged_webkit(Some(16613), true));
|
||||
assert!(!needs_staged_webkit(Some(16615), true));
|
||||
// No staged copy, or an unreadable system version: leave it alone.
|
||||
assert!(!needs_staged_webkit(Some(16611), false));
|
||||
assert!(!needs_staged_webkit(None, true));
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos_webview;
|
||||
|
||||
fn main() {
|
||||
// Started by `ssh` / `git` as their askpass program: answer and exit, no UI.
|
||||
if let Some(code) = ide_ssh::askpass::run_helper() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
macos_webview::ensure_modern_webkit();
|
||||
openidea_lib::run()
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
"longDescription": "OpenIDEA is an open-source IDE inspired by IntelliJ IDEA. Not affiliated with JetBrains.",
|
||||
"macOS": {
|
||||
"entitlements": "./Entitlements.plist",
|
||||
"minimumSystemVersion": "12.0"
|
||||
"minimumSystemVersion": "11.0"
|
||||
}
|
||||
},
|
||||
"mainBinaryName": "openidea",
|
||||
|
||||
@@ -221,6 +221,8 @@ it("names an outdated system web engine as the cause", () => {
|
||||
window.dispatchEvent(new ErrorEvent("error", { error: new SyntaxError("Unexpected token '.'") }));
|
||||
const hint = document.querySelector("#boot-error p")?.textContent;
|
||||
expect(hint).toContain("needs macOS 12 Monterey or later");
|
||||
expect(hint).toContain("macOS 11 Big Sur");
|
||||
expect(hint).toContain("latest Safari update");
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user