fix(desktop): bind launcher rewrites to effective entry keys (#11701)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-18 23:51:26 -05:00
1 parent b0ef28a77f
commit d785d63267
4 files changed
+117 -40

No files matched your search

@@ -47,6 +47,42 @@ const fixture = async () => {
};
};
it.skipIf(process.platform === "win32")(
"binds inventory and argv to the effective desktop entry command",
async () => {
const { home, bin, environment } = await fixture();
await writeFile(join(bin, "current-app"), "#!/bin/sh\nexit 0\n", {
mode: 0o755,
});
const path = join(
environment.XDG_DATA_DIRS,
"applications/org.example.notes.desktop",
);
await writeFile(
path,
"[Desktop Entry]\nType=Application\nName=Earlier\nExec=notes-app --old\nExec=current-app --current %F\nName=Current\n",
);
const result = await inspectLinuxApplication({
desktopEntry: "org.example.notes",
executable: undefined,
arguments: ["/tmp/document.txt"],
environment,
home,
callerDirectory: home,
});
if (!result.ok) throw new Error(result.error.reason);
expect(result.value.inventory.argv).toEqual([
"current-app",
"--current",
"/tmp/document.txt",
]);
expect(result.value.inventory.executable.path).toBe(
join(bin, "current-app"),
);
expect(result.value.inventory.desktop_entry?.name).toBe("Current");
},
);
describe.skipIf(process.platform === "win32")(
"explicit executable traversal",
() => {
@@ -572,6 +572,20 @@ describe("electronLaunchArguments", () => {
});
describe("adjustInstalledDesktopText", () => {
it.each(["", "[Desktop Action New]\nExec=action-app\n[Desktop Entry]\n"])(
"rewrites the effective command rather than an overridden Exec (%j)",
(between) => {
const text = `[Desktop Entry]\nType=Application\nName=App\nExec=old-app %F\n${between}Exec=current-app %U\n`;
const rewritten = adjustInstalledDesktopText(text, true);
expect(rewritten).toEqual({
ok: true,
value: text.replace(
"Exec=current-app %U",
"Exec=current-app --no-sandbox %U",
),
});
},
);
const entry = (exec: string) =>
`[Desktop Entry]\nType=Application\nName=Typora\nExec=${exec}\n[Desktop Action New]\nExec=typora --new-window\n`;
+34
View File
@@ -12,6 +12,40 @@ import {
import { DESKTOP_LAUNCH_CASES } from "./desktopLaunchSyntax.fixture.js";
describe("parseDesktopEntry", () => {
it("uses the last main-group declarations, including repeated groups and empty values", () => {
const text = `[Desktop Entry]
Type=Link
Name=Earlier
Exec=old-app
Path=/old
Icon=old-icon
Categories=Old;
Terminal=false
[Desktop Action New]
Name=Action
Exec=action-app
[Desktop Entry]
Type=Application
Name=Current
Exec=current-app %F
Path=
Icon=current-icon
Categories=A\\;B;Current;
Terminal=true
`;
expect(parseDesktopEntry(text)).toMatchObject({
ok: true,
value: {
type: "Application",
name: "Current",
exec: "current-app %F",
path: "",
icon: "current-icon",
categories: ["A;B", "Current"],
terminal: true,
},
});
});
it("reads the main group and ignores localized keys and other groups", () => {
const parsed = parseDesktopEntry(
[
+33 -40
View File
@@ -99,14 +99,16 @@ const listValue = (value: string | undefined): readonly string[] => {
return entries;
};
/** Read the main group of a freedesktop desktop entry, ignoring localized keys. */
export const parseDesktopEntry = (
text: string,
): Result<DesktopEntry, "missing_desktop_entry_group"> => {
const values = new Map<string, string>();
/** GLib merges repeated groups and gives the last occurrence of a key precedence. */
const readDesktopEntryFields = (text: string) => {
const lines = text.split(/\r?\n/u);
const fields = new Map<
string,
{ readonly value: string; readonly line: number }
>();
let group: string | undefined;
let found = false;
for (const raw of text.split(/\r?\n/u)) {
for (const [index, raw] of lines.entries()) {
const line = raw.trim();
if (line.length === 0 || line.startsWith("#")) continue;
const header = GROUP.exec(line);
@@ -119,16 +121,25 @@ export const parseDesktopEntry = (
const entry = ENTRY.exec(raw.trimStart());
if (group !== "Desktop Entry" || entry?.groups === undefined) continue;
const { key, value } = entry.groups;
if (key !== undefined && value !== undefined && !values.has(key))
if (key !== undefined && value !== undefined)
// Keep the existing enum/boolean token treatment before value unescaping.
values.set(
key,
key === "Type" || key === "Terminal" ? value.trimEnd() : value,
);
fields.set(key, {
value: key === "Type" || key === "Terminal" ? value.trimEnd() : value,
line: index,
});
}
return { lines, fields, found };
};
/** Read the effective main-group keys, ignoring localized keys and action groups. */
export const parseDesktopEntry = (
text: string,
): Result<DesktopEntry, "missing_desktop_entry_group"> => {
const { fields, found } = readDesktopEntryFields(text);
if (!found) return err("missing_desktop_entry_group");
const rawField = (key: string) => fields.get(key)?.value;
const field = (key: string) => {
const value = values.get(key);
const value = rawField(key);
return value === undefined ? null : unescapeValue(value);
};
return ok({
@@ -139,8 +150,8 @@ export const parseDesktopEntry = (
path: field("Path"),
icon: field("Icon"),
startup_wm_class: field("StartupWMClass"),
categories: listValue(values.get("Categories")),
mime_types: listValue(values.get("MimeType")),
categories: listValue(rawField("Categories")),
mime_types: listValue(rawField("MimeType")),
terminal: field("Terminal") === "true",
});
};
@@ -188,39 +199,21 @@ export const formatDesktopExec = (argv: readonly string[]): string =>
)
.join(" ");
const EXEC_LINE = /^Exec\s*=/u;
/**
* Replace the first `Exec` line of the main group, leaving other groups
* alone. `exec` is the command `tokenizeDesktopExec` reads; it is escaped for
* the desktop-entry value layer before it is written.
* Replace the effective main-group `Exec`, using the same record owner as
* parsing. Overridden keys and action groups stay unchanged. `exec` is escaped
* for the desktop-entry value layer before it is written.
*/
export const replaceDesktopExec = (
text: string,
exec: string,
): Result<string, "missing_exec_line"> => {
const newline = text.includes("\r\n") ? "\r\n" : "\n";
let group: string | undefined;
let replaced = false;
const lines = text.split(/\r?\n/u).map((line) => {
const trimmed = line.trim();
const header = GROUP.exec(trimmed);
if (header?.groups?.name !== undefined) {
group = header.groups.name;
return line;
}
if (
replaced ||
group !== "Desktop Entry" ||
trimmed.length === 0 ||
trimmed.startsWith("#") ||
!EXEC_LINE.test(trimmed)
)
return line;
replaced = true;
return `Exec=${escapeDesktopEntryValue(exec)}`;
});
return replaced ? ok(lines.join(newline)) : err("missing_exec_line");
const { lines, fields } = readDesktopEntryFields(text);
const execField = fields.get("Exec");
if (execField === undefined) return err("missing_exec_line");
lines[execField.line] = `Exec=${escapeDesktopEntryValue(exec)}`;
return ok(lines.join(newline));
};
/** Facts substituted for the `%i`, `%c` and `%k` field codes. */