Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(desktop): require directory-capable Linux portals (MUL-7956) (#9187)

Multica Eve7e225943c8b6
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index 7a1c13372d..4e4d535588 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -7,6 +7,7 @@ import fixPath from "fix-path";
import { setupAutoUpdater } from "./updater";
import { setupDaemonManager } from "./daemon-manager";
import { setupLocalDirectory } from "./local-directory";
+import { configureLinuxFileDialogs } from "./linux-file-dialog";
import { openExternalSafely, downloadURLSafely } from "./external-url";
import { installContextMenu } from "./context-menu";
import { handleAppShortcut } from "./keyboard-shortcuts";
@@ -63,6 +64,8 @@ import {
parseNativeNotificationPayload,
} from "./notification-gate";
+configureLinuxFileDialogs(app.commandLine);
+
// Guards against registering the will-download handler more than once on the
// same session. window.webContents.session is shared, and createWindow() can
// be called again on macOS (app "activate" after all windows are closed).
diff --git a/apps/desktop/src/main/linux-file-dialog.test.ts b/apps/desktop/src/main/linux-file-dialog.test.ts
new file mode 100644
index 0000000000..bcc36e7411
--- /dev/null
+++ b/apps/desktop/src/main/linux-file-dialog.test.ts
@@ -0,0 +1,27 @@
+// @vitest-environment node
+import { describe, expect, it, vi } from "vitest";
+import { configureLinuxFileDialogs } from "./linux-file-dialog";
+
+describe("configureLinuxFileDialogs", () => {
+ it("requires a directory-capable portal on Linux", () => {
+ const commandLine = { hasSwitch: vi.fn(() => false), appendSwitch: vi.fn() };
+ configureLinuxFileDialogs(commandLine, "linux");
+ expect(commandLine.appendSwitch).toHaveBeenCalledExactlyOnceWith(
+ "xdg-portal-required-version", "3",
+ );
+ });
+
+ it("preserves an explicit portal override", () => {
+ const commandLine = { hasSwitch: vi.fn(() => true), appendSwitch: vi.fn() };
+ configureLinuxFileDialogs(commandLine, "linux");
+ expect(commandLine.hasSwitch).toHaveBeenCalledWith("xdg-portal-required-version");
+ expect(commandLine.appendSwitch).not.toHaveBeenCalled();
+ });
+
+ it.each(["darwin", "win32"] as const)("leaves %s dialogs unchanged", (platform) => {
+ const commandLine = { hasSwitch: vi.fn(), appendSwitch: vi.fn() };
+ configureLinuxFileDialogs(commandLine, platform);
+ expect(commandLine.hasSwitch).not.toHaveBeenCalled();
+ expect(commandLine.appendSwitch).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/main/linux-file-dialog.ts b/apps/desktop/src/main/linux-file-dialog.ts
new file mode 100644
index 0000000000..eff6211622
--- /dev/null
+++ b/apps/desktop/src/main/linux-file-dialog.ts
@@ -0,0 +1,49 @@
+import type { CommandLine } from "electron";
+
+/**
+ * Restores Electron's intended minimum xdg-desktop-portal FileChooser version
+ * on Linux, so `openDirectory` dialogs are not handed to a portal that can only
+ * pick files.
+ *
+ * Why this is needed: Chromium gates the portal correctly on its own
+ * (`kXdgPortalRequiredVersion = 3` in
+ * `ui/shell_dialogs/select_file_dialog_linux_portal.cc`), but Electron's
+ * `feat_add_support_for_missing_dialog_features_to_shell_dialogs.patch`
+ * disables that check and routes `IsPortalAvailable()` through its own copy in
+ * `shell/browser/ui/file_dialog_linux_portal.cc`. That copy declares
+ * `g_required_portal_version = 3` and then overwrites it:
+ *
+ * base::StringToUint(cmd->GetSwitchValueASCII("xdg-portal-required-version"),
+ * &g_required_portal_version);
+ *
+ * With the switch absent the value is the empty string, and Chromium's
+ * `StringToIntImpl` assigns the parse result (0) to the out-param even on
+ * failure. The minimum silently drops from 3 to 0, every portal version passes,
+ * and the `directory: true` option — only defined from FileChooser v3 — is
+ * ignored by v1/v2 portals, which answer with a file chooser instead.
+ *
+ * Setting the switch ourselves keeps the gate at 3, so hosts with an older
+ * portal fall back to Electron's native dialog, where folder selection works.
+ *
+ * Scope: reproduced in the Electron sources for v39 and still present in v42
+ * and v43, so an Electron upgrade alone will not retire this. Before removing
+ * it, confirm upstream actually fixed the overwrite.
+ * https://github.com/electron/electron/issues/49116
+ *
+ * Must run at main-script top level: the probe happens later, when
+ * `ToolkitInitialized()` reaches `shell_dialog_linux::Initialize()`, and the
+ * result is cached for the process lifetime.
+ */
+export function configureLinuxFileDialogs(
+ commandLine: Pick<CommandLine, "hasSwitch" | "appendSwitch">,
+ platform: NodeJS.Platform = process.platform,
+): void {
+ if (platform !== "linux") return;
+
+ // An operator who passes the switch explicitly — including to opt into an
+ // older portal — keeps their value.
+ const switchName = "xdg-portal-required-version";
+ if (!commandLine.hasSwitch(switchName)) {
+ commandLine.appendSwitch(switchName, "3");
+ }
+}
diff --git a/apps/desktop/src/main/local-directory.test.ts b/apps/desktop/src/main/local-directory.test.ts
new file mode 100644
index 0000000000..f9c2393038
--- /dev/null
+++ b/apps/desktop/src/main/local-directory.test.ts
@@ -0,0 +1,89 @@
+// @vitest-environment node
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { IpcMainInvokeEvent } from "electron";
+import { constants } from "fs";
+import { setupLocalDirectory } from "./local-directory";
+
+const mocks = vi.hoisted(() => ({
+ handle: vi.fn(),
+ showOpenDialog: vi.fn(),
+ fromWebContents: vi.fn(),
+ stat: vi.fn(),
+ access: vi.fn(),
+}));
+vi.mock("electron", () => ({
+ ipcMain: { handle: mocks.handle },
+ dialog: { showOpenDialog: mocks.showOpenDialog },
+ BrowserWindow: { fromWebContents: mocks.fromWebContents },
+}));
+vi.mock("fs/promises", () => ({ stat: mocks.stat, access: mocks.access }));
+
+const event = { sender: {} } as IpcMainInvokeEvent;
+function invoke(channel: string, path?: string) {
+ const handler = mocks.handle.mock.calls.find(([name]) => name === channel)?.[1];
+ if (!handler) throw new Error(`Missing handler: ${channel}`);
+ return handler(event, path);
+}
+
+beforeEach(() => {
+ vi.resetAllMocks();
+ mocks.fromWebContents.mockReturnValue({});
+ setupLocalDirectory(() => null);
+});
+
+describe("local directory picker IPC", () => {
+ it("returns the selected folder and requests one directory with a starting path", async () => {
+ mocks.showOpenDialog.mockResolvedValue({ canceled: false, filePaths: ["/projects/demo"] });
+ await expect(invoke("local-directory:pick", "/projects")).resolves.toEqual({
+ ok: true, path: "/projects/demo", basename: "demo",
+ });
+ expect(mocks.showOpenDialog).toHaveBeenCalledWith({}, {
+ properties: ["openDirectory", "createDirectory"], defaultPath: "/projects",
+ });
+ });
+
+ it.each([
+ { canceled: true, filePaths: ["/projects/demo"] },
+ { canceled: false, filePaths: [] },
+ ])("preserves cancellation for %j", async (result) => {
+ mocks.showOpenDialog.mockResolvedValue(result);
+ await expect(invoke("local-directory:pick")).resolves.toEqual({ ok: false, reason: "cancelled" });
+ });
+
+ it("reports native dialog errors", async () => {
+ mocks.showOpenDialog.mockRejectedValue(new Error("Dialog unavailable"));
+ await expect(invoke("local-directory:pick")).resolves.toEqual({
+ ok: false, reason: "error", error: "Dialog unavailable",
+ });
+ });
+});
+
+describe("local directory validation IPC", () => {
+ it("accepts a readable and writable directory", async () => {
+ mocks.stat.mockResolvedValueOnce({ isDirectory: () => true }).mockRejectedValue(new Error("No git entry"));
+ mocks.access.mockResolvedValue(undefined);
+ await expect(invoke("local-directory:validate", "/projects/demo")).resolves.toEqual({
+ ok: true, is_git_repo: false,
+ });
+ expect(mocks.access).toHaveBeenCalledWith("/projects/demo", constants.R_OK);
+ expect(mocks.access).toHaveBeenCalledWith("/projects/demo", constants.W_OK);
+ });
+
+ it("rejects files even if returned by a broken native picker", async () => {
+ mocks.stat.mockResolvedValue({ isDirectory: () => false });
+ await expect(invoke("local-directory:validate", "/projects/file.txt")).resolves.toEqual({
+ ok: false, reason: "not_a_directory",
+ });
+ });
+
+ it.each([
+ [constants.R_OK, "not_readable"],
+ [constants.W_OK, "not_writable"],
+ ])("preserves permission failure %s", async (mode, reason) => {
+ mocks.stat.mockResolvedValue({ isDirectory: () => true });
+ mocks.access.mockImplementation(async (_path, requestedMode) => {
+ if (requestedMode === mode) throw new Error("Permission denied");
+ });
+ await expect(invoke("local-directory:validate", "/projects/demo")).resolves.toEqual({ ok: false, reason });
+ });
+});