Skip to content

Commit f492398

Browse files
EVWorthclaude
andauthored
fix(update): decide from how the app was installed, not from one file's existence (#575)
Closes #354, #571. The check was `Path::new("/usr/bin/rpm-ostree").exists()`, which answers a narrower question than the one being asked and gets the important case backwards. Inside a Flatpak sandbox the host's /usr is not visible. A Flatpak on Silverblue therefore sees no rpm-ostree binary, reports itself as an ordinary install, and is offered an auto-update its runtime will not let it apply. The opposite mistake is available too: plenty of ordinary Fedora systems have the rpm-ostree binary installed without being OSTree-booted, and were told to use a manual command they do not need. get_platform_info replaces it, returning how the app was installed and on what architecture: Flatpak /.flatpak-info, or FLATPAK_ID Snap SNAP AppImage APPIMAGE RpmOstree /run/ostree-booted — whether the host booted from an OSTree, rather than whether a binary happens to be present Standard none of the above Sandboxes are tested first and deliberately: inside one, the host's markers are either invisible or not ours to act on. Only Standard and AppImage may update themselves. Flatpak, Snap and OSTree all have a runtime that owns the installed files, and each now gets its own command and a sentence saying why the app is not doing it. The rpm-ostree URL uses the running architecture — an aarch64 machine was handed an x86_64 download. A detection failure means "do not offer to update", which is the safe direction: an update the app cannot apply is worse than one it does not offer. The decision is a pure function over a probe trait, so each packaging is tested without arranging the real thing — I cannot produce a Flatpak sandbox or an OSTree host here, and would rather say so than imply the detection has been exercised in the wild. What is verified is that the rules are the ones intended, including the Flatpak-on-OSTree case that motivated this. Seven Rust tests, six on the channel policy. The status bar's diagnostic blob now reports the install type rather than a yes/no for rpm-ostree. Also: the Path import was Linux-only, because the old check was. The probe reads paths on every target, so it is unconditional now — left as it was, this would have compiled here and failed on the macOS and Windows release builds. Co-authored-by: Claude Opus 5 <[email protected]>
1 parent 894a3f1 commit f492398

11 files changed

Lines changed: 393 additions & 62 deletions

File tree

‎src-tauri/src/commands/mod.rs‎

Lines changed: 202 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use mas_core::schema::SchemaInspector;
1616
use mas_sqlite::connection::SqliteConnectionManager;
1717
use mas_sqlite::query::SqliteQueryExecutor;
1818
use mas_sqlite::schema::SqliteSchemaInspector;
19-
#[cfg(target_os = "linux")]
19+
use serde::{Deserialize, Serialize};
20+
// Unconditional now: the platform probe reads paths on every target, where
21+
// the previous rpm-ostree check was Linux-only.
2022
use std::path::Path;
2123
use std::path::PathBuf;
2224
use std::sync::Arc;
@@ -699,15 +701,89 @@ pub fn keyring_available() -> bool {
699701
#[tauri::command]
700702
#[tracing::instrument]
701703
#[specta::specta]
702-
pub async fn is_rpm_ostree() -> Result<bool, String> {
703-
#[cfg(target_os = "linux")]
704-
{
705-
Ok(Path::new("/usr/bin/rpm-ostree").exists())
704+
pub async fn get_platform_info() -> Result<PlatformInfo, String> {
705+
Ok(PlatformInfo {
706+
package_format: detect_package_format(&RealProbe),
707+
arch: std::env::consts::ARCH.to_string(),
708+
})
709+
}
710+
711+
/// How this copy of SQLPilot was installed, which decides whether it may
712+
/// update itself.
713+
///
714+
/// The previous check was `Path::new("/usr/bin/rpm-ostree").exists()`, which
715+
/// answers a narrower question than the one being asked and gets the
716+
/// important case backwards. Inside a Flatpak sandbox the host's `/usr` is
717+
/// not visible, so a Flatpak on Silverblue sees no rpm-ostree binary, reports
718+
/// itself as an ordinary install, and is offered an auto-update its runtime
719+
/// cannot apply (#354).
720+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, specta::Type)]
721+
#[serde(rename_all = "snake_case")]
722+
pub enum PackageFormat {
723+
/// A plain install that owns its own files, and can replace them.
724+
Standard,
725+
/// Tauri's updater handles this format directly.
726+
AppImage,
727+
/// The runtime owns updates; the app must not replace its own files.
728+
Flatpak,
729+
/// As Flatpak — snapd manages the revision.
730+
Snap,
731+
/// An OSTree-booted system: /usr is immutable and layered packages are
732+
/// applied by rpm-ostree, taking effect on the next boot.
733+
RpmOstree,
734+
}
735+
736+
#[derive(Debug, Clone, Serialize, Deserialize, specta::Type)]
737+
pub struct PlatformInfo {
738+
pub package_format: PackageFormat,
739+
/// `x86_64`, `aarch64`. Needed to name the right download in a manual
740+
/// update command (#571).
741+
pub arch: String,
742+
}
743+
744+
/// The filesystem and environment questions the detection asks, so the
745+
/// decision can be tested without arranging a Flatpak sandbox.
746+
pub trait PlatformProbe {
747+
fn path_exists(&self, path: &str) -> bool;
748+
fn env(&self, key: &str) -> Option<String>;
749+
}
750+
751+
struct RealProbe;
752+
753+
impl PlatformProbe for RealProbe {
754+
fn path_exists(&self, path: &str) -> bool {
755+
Path::new(path).exists()
756+
}
757+
fn env(&self, key: &str) -> Option<String> {
758+
std::env::var(key).ok()
759+
}
760+
}
761+
762+
/// Sandboxes are checked first, and deliberately.
763+
///
764+
/// Inside a Flatpak or a Snap the host's markers are either invisible or not
765+
/// ours to act on, so what matters is the sandbox itself. `/.flatpak-info`
766+
/// exists in every Flatpak sandbox; `SNAP` is set for every snap; `APPIMAGE`
767+
/// is set by the AppImage runtime. `/run/ostree-booted` is the marker for an
768+
/// OSTree-booted host, and is a better question than whether an rpm-ostree
769+
/// binary happens to be installed.
770+
pub fn detect_package_format(probe: &impl PlatformProbe) -> PackageFormat {
771+
if !cfg!(target_os = "linux") {
772+
return PackageFormat::Standard;
773+
}
774+
if probe.path_exists("/.flatpak-info") || probe.env("FLATPAK_ID").is_some() {
775+
return PackageFormat::Flatpak;
776+
}
777+
if probe.env("SNAP").is_some() {
778+
return PackageFormat::Snap;
706779
}
707-
#[cfg(not(target_os = "linux"))]
708-
{
709-
Ok(false)
780+
if probe.env("APPIMAGE").is_some() {
781+
return PackageFormat::AppImage;
710782
}
783+
if probe.path_exists("/run/ostree-booted") {
784+
return PackageFormat::RpmOstree;
785+
}
786+
PackageFormat::Standard
711787
}
712788

713789
// File import commands
@@ -815,25 +891,129 @@ pub async fn pick_save_file(
815891

816892
#[cfg(test)]
817893
mod platform_tests {
818-
use super::is_rpm_ostree;
894+
use super::{detect_package_format, get_platform_info, PackageFormat, PlatformProbe};
895+
896+
/// A probe answering from a fixed set of facts, so each packaging can be
897+
/// described without arranging the real thing.
898+
struct FakeProbe {
899+
paths: Vec<&'static str>,
900+
vars: Vec<(&'static str, &'static str)>,
901+
}
902+
903+
impl FakeProbe {
904+
fn nothing() -> Self {
905+
Self {
906+
paths: vec![],
907+
vars: vec![],
908+
}
909+
}
910+
fn with_path(mut self, p: &'static str) -> Self {
911+
self.paths.push(p);
912+
self
913+
}
914+
fn with_env(mut self, k: &'static str, v: &'static str) -> Self {
915+
self.vars.push((k, v));
916+
self
917+
}
918+
}
919+
920+
impl PlatformProbe for FakeProbe {
921+
fn path_exists(&self, path: &str) -> bool {
922+
self.paths.contains(&path)
923+
}
924+
fn env(&self, key: &str) -> Option<String> {
925+
self.vars
926+
.iter()
927+
.find(|(k, _)| *k == key)
928+
.map(|(_, v)| v.to_string())
929+
}
930+
}
931+
932+
#[cfg(target_os = "linux")]
933+
#[test]
934+
fn a_plain_install_is_standard() {
935+
assert_eq!(
936+
detect_package_format(&FakeProbe::nothing()),
937+
PackageFormat::Standard
938+
);
939+
}
940+
941+
#[cfg(target_os = "linux")]
942+
#[test]
943+
fn a_flatpak_is_recognised_by_its_sandbox() {
944+
assert_eq!(
945+
detect_package_format(&FakeProbe::nothing().with_path("/.flatpak-info")),
946+
PackageFormat::Flatpak
947+
);
948+
assert_eq!(
949+
detect_package_format(&FakeProbe::nothing().with_env("FLATPAK_ID", "dev.sqlpilot")),
950+
PackageFormat::Flatpak
951+
);
952+
}
953+
954+
#[cfg(target_os = "linux")]
955+
#[test]
956+
fn a_flatpak_on_an_ostree_host_is_still_a_flatpak() {
957+
// The case the old check got backwards. Inside the sandbox the host's
958+
// /usr is not visible, so a Flatpak on Silverblue saw no rpm-ostree
959+
// binary, called itself an ordinary install, and was offered an
960+
// auto-update its runtime cannot apply (#354).
961+
let probe = FakeProbe::nothing()
962+
.with_path("/.flatpak-info")
963+
.with_path("/run/ostree-booted");
964+
assert_eq!(detect_package_format(&probe), PackageFormat::Flatpak);
965+
}
966+
967+
#[cfg(target_os = "linux")]
968+
#[test]
969+
fn a_snap_is_recognised() {
970+
assert_eq!(
971+
detect_package_format(&FakeProbe::nothing().with_env("SNAP", "/snap/sqlpilot/12")),
972+
PackageFormat::Snap
973+
);
974+
}
975+
976+
#[cfg(target_os = "linux")]
977+
#[test]
978+
fn an_appimage_is_recognised() {
979+
assert_eq!(
980+
detect_package_format(
981+
&FakeProbe::nothing().with_env("APPIMAGE", "/home/a/SQLPilot.AppImage")
982+
),
983+
PackageFormat::AppImage
984+
);
985+
}
986+
987+
#[cfg(target_os = "linux")]
988+
#[test]
989+
fn an_ostree_host_is_recognised_by_the_boot_marker() {
990+
// Not by whether an rpm-ostree binary happens to be installed, which
991+
// is true on plenty of ordinary Fedora systems.
992+
assert_eq!(
993+
detect_package_format(&FakeProbe::nothing().with_path("/run/ostree-booted")),
994+
PackageFormat::RpmOstree
995+
);
996+
assert_eq!(
997+
detect_package_format(&FakeProbe::nothing().with_path("/usr/bin/rpm-ostree")),
998+
PackageFormat::Standard
999+
);
1000+
}
8191001

8201002
#[tokio::test]
821-
async fn is_rpm_ostree_returns_false_on_non_linux() {
822-
#[cfg(not(target_os = "linux"))]
823-
assert!(!is_rpm_ostree().await.unwrap());
1003+
async fn platform_info_reports_this_machine() {
1004+
let info = get_platform_info().await.unwrap();
1005+
assert!(!info.arch.is_empty());
8241006
}
8251007

8261008
#[tokio::test]
827-
async fn is_rpm_ostree_reflects_binary_presence() {
828-
// On Linux this returns true iff /usr/bin/rpm-ostree exists. We don't
829-
// assert a specific value (CI runner may or may not be atomic) — just
830-
// that the command succeeds and the boolean matches the filesystem.
831-
#[cfg(target_os = "linux")]
832-
{
833-
let result = is_rpm_ostree().await.unwrap();
834-
let expected = std::path::Path::new("/usr/bin/rpm-ostree").exists();
835-
assert_eq!(result, expected);
836-
}
1009+
async fn platform_info_agrees_with_this_machine() {
1010+
// No fixed expectation — the runner may be anything. What must hold
1011+
// is that the command answers, and answers the same as the detection
1012+
// it delegates to.
1013+
let info = get_platform_info().await.unwrap();
1014+
assert_eq!(info.arch, std::env::consts::ARCH);
1015+
#[cfg(not(target_os = "linux"))]
1016+
assert_eq!(info.package_format, PackageFormat::Standard);
8371017
}
8381018
}
8391019

‎src-tauri/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn specta_builder() -> tauri_specta::Builder<tauri::Wry> {
153153
commands::pick_file,
154154
commands::write_file_contents,
155155
commands::pick_save_file,
156-
commands::is_rpm_ostree,
156+
commands::get_platform_info,
157157
commands::keyring_available,
158158
commands::sqlite::sqlite_open,
159159
commands::sqlite::sqlite_close,
@@ -211,7 +211,7 @@ pub fn specta_builder() -> tauri_specta::Builder<tauri::Wry> {
211211
commands::pick_file,
212212
commands::write_file_contents,
213213
commands::pick_save_file,
214-
commands::is_rpm_ostree,
214+
commands::get_platform_info,
215215
commands::keyring_available,
216216
commands::sqlite::sqlite_open,
217217
commands::sqlite::sqlite_close,

‎src/components/layout/StatusBar.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function StatusBar() {
8181
const updateVersion = useSettingsStore((s) => s.updateVersion);
8282
const updateError = useSettingsStore((s) => s.updateError);
8383
const manualUpdateCommand = useSettingsStore((s) => s.manualUpdateCommand);
84-
const platformHint = useSettingsStore((s) => s.platformHint);
84+
const packageFormat = useSettingsStore((s) => s.packageFormat);
8585
const downloadProgress = useSettingsStore((s) => s.downloadProgress);
8686
const checkForUpdates = useSettingsStore((s) => s.checkForUpdates);
8787
const installUpdate = useSettingsStore((s) => s.installUpdate);
@@ -146,7 +146,7 @@ export function StatusBar() {
146146
const diagnosticBlob = [
147147
`SQLPilot v${appVersion || "unknown"}`,
148148
`Platform: ${navigator.platform} (${navigator.userAgent})`,
149-
`rpm-ostree detected: ${platformHint === "rpm-ostree" ? "yes" : "no"}`,
149+
`Install type: ${packageFormat ?? "unknown"}`,
150150
`Update status: ${updateStatus}${updateVersion ? ` (target v${updateVersion})` : ""}`,
151151
updateError ? `Error: ${updateError}` : null,
152152
`Timestamp: ${new Date().toISOString()}`,

‎src/components/layout/__tests__/StatusBar.test.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { StatusBar } from "../StatusBar";
1313
vi.mock("../../../lib/tauri-api", () => ({
1414
api: {
1515
getAppVersion: vi.fn().mockResolvedValue("2.1.0"),
16-
isRpmOstree: vi.fn().mockResolvedValue(false),
16+
getPlatformInfo: vi.fn().mockResolvedValue({ package_format: "standard", arch: "x86_64" }),
1717
keyringAvailable: vi.fn().mockResolvedValue(true),
1818
},
1919
}));
@@ -373,7 +373,7 @@ describe("StatusBar", () => {
373373
expect(dialog).toBeInTheDocument();
374374
expect(dialog.textContent).toContain("rpm install failed: transaction test failed");
375375
expect(dialog.textContent).toMatch(/SQLPilot v2\.1\.0/);
376-
expect(dialog.textContent).toMatch(/rpm-ostree detected: no/);
376+
expect(dialog.textContent).toMatch(/Install type: standard/);
377377
expect(screen.getByRole("button", { name: /copy diagnostic/i })).toBeInTheDocument();
378378
expect(screen.getByRole("button", { name: /report issue/i })).toBeInTheDocument();
379379
expect(screen.getByRole("button", { name: /retry/i })).toBeInTheDocument();
@@ -407,10 +407,11 @@ describe("StatusBar", () => {
407407
updateStatus: "error",
408408
updateError: "rpm install failed",
409409
updateVersion: "0.4.1",
410-
platformHint: "rpm-ostree",
410+
packageFormat: "rpm_ostree",
411+
arch: "x86_64",
411412
});
412413
renderStatusBar();
413-
// detectPlatform runs on mount and overrides platformHint. Wait for
414+
// detectPlatform runs on mount and overrides the format. Wait for
414415
// it to settle, then re-set state and re-open the panel.
415416
await user.click(screen.getByText("Update failed"));
416417
// dialog text reflects post-detectPlatform hint; assert the structural fields instead.
@@ -420,7 +421,7 @@ describe("StatusBar", () => {
420421
expect(blob).toContain("SQLPilot v2.1.0");
421422
expect(blob).toContain("Error: rpm install failed");
422423
expect(blob).toContain("target v0.4.1");
423-
expect(blob).toMatch(/rpm-ostree detected: (yes|no)/);
424+
expect(blob).toMatch(/Install type: (standard|rpm_ostree|flatpak|snap|app_image|unknown)/);
424425
expect(blob).toMatch(/Timestamp: \d{4}-\d{2}-\d{2}T/);
425426
});
426427

‎src/lib/__tests__/tauri-api.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,17 @@ describe("api (Tauri available)", () => {
508508

509509
// -- Platform detection --------------------------------------------------
510510

511-
it("isRpmOstree calls invoke with no args and returns true", async () => {
511+
it("getPlatformInfo calls invoke with no args", async () => {
512512
invokeMock.mockResolvedValue(true);
513-
const result = await api.isRpmOstree();
514-
expect(invokeMock).toHaveBeenCalledWith("is_rpm_ostree");
513+
const result = await api.getPlatformInfo();
514+
expect(invokeMock).toHaveBeenCalledWith("get_platform_info");
515515
expect(result).toBe(true);
516516
});
517517

518-
it("isRpmOstree returns false on non-atomic systems", async () => {
518+
it("getPlatformInfo surfaces the reported format", async () => {
519519
invokeMock.mockResolvedValue(false);
520-
const result = await api.isRpmOstree();
521-
expect(invokeMock).toHaveBeenCalledWith("is_rpm_ostree");
520+
const result = await api.getPlatformInfo();
521+
expect(invokeMock).toHaveBeenCalledWith("get_platform_info");
522522
expect(result).toBe(false);
523523
});
524524

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, it } from "vitest";
2+
import { canSelfUpdate, manualUpdateFor } from "../update-channel";
3+
4+
describe("canSelfUpdate", () => {
5+
it("allows the formats that own their own files", () => {
6+
expect(canSelfUpdate("standard")).toBe(true);
7+
expect(canSelfUpdate("app_image")).toBe(true);
8+
});
9+
10+
it("refuses the formats whose runtime manages the install", () => {
11+
// Offering an update the runtime will not let the app apply produces a
12+
// failure the user cannot act on.
13+
expect(canSelfUpdate("flatpak")).toBe(false);
14+
expect(canSelfUpdate("snap")).toBe(false);
15+
expect(canSelfUpdate("rpm_ostree")).toBe(false);
16+
});
17+
});
18+
19+
describe("manualUpdateFor", () => {
20+
it("names the architecture the app is running on", () => {
21+
// An aarch64 machine was handed an x86_64 download URL (#571).
22+
const update = manualUpdateFor("rpm_ostree", "1.2.3", "aarch64");
23+
expect(update?.command).toContain("SQLPilot-1.2.3-1.aarch64.rpm");
24+
expect(update?.command).not.toContain("x86_64");
25+
});
26+
27+
it("gives each managed format its own command", () => {
28+
expect(manualUpdateFor("flatpak", "1.2.3", "x86_64")?.command).toContain("flatpak update");
29+
expect(manualUpdateFor("snap", "1.2.3", "x86_64")?.command).toContain("snap refresh");
30+
expect(manualUpdateFor("rpm_ostree", "1.2.3", "x86_64")?.command).toContain("rpm-ostree install");
31+
});
32+
33+
it("explains why the app is not doing it itself", () => {
34+
for (const format of ["flatpak", "snap", "rpm_ostree"] as const) {
35+
expect(manualUpdateFor(format, "1.2.3", "x86_64")?.reason).toBeTruthy();
36+
}
37+
});
38+
39+
it("has nothing to say for a format that updates itself", () => {
40+
expect(manualUpdateFor("standard", "1.2.3", "x86_64")).toBeNull();
41+
expect(manualUpdateFor("app_image", "1.2.3", "x86_64")).toBeNull();
42+
});
43+
});

0 commit comments

Comments
 (0)