fix(desktop): repair a stale Linux .desktop entry instead of preserving it (#1751)

This commit is contained in:
Hampus authored and GitHub committed 2026-08-19 14:56:29 +02:00
1 parent d9ed256a4b
commit 5aa283e8e0
1 file changed
+44
@@ -148,6 +148,31 @@ function buildDesktopFileContents(execPath: string, hidden: boolean): string {
].join('\n'); ].join('\n');
} }
function readDesktopEntryValue(contents: string, key: string): string | null {
for (const line of contents.split('\n')) {
const trimmed = line.trim();
if (trimmed.startsWith('[Desktop Action ')) break;
if (trimmed.startsWith(`${key}=`)) return trimmed.slice(key.length + 1).trim();
}
return null;
}
function isExecutableFile(candidate: string): boolean {
try {
if (!fs.statSync(candidate).isFile()) return false;
fs.accessSync(candidate, fs.constants.X_OK);
return true;
} catch {
return false;
}
}
function isStaleDesktopFile(contents: string): boolean {
const tryExec = readDesktopEntryValue(contents, 'TryExec');
if (tryExec === null || !tryExec.startsWith('/')) return false;
return !isExecutableFile(tryExec);
}
function readExistingDesktopFile(filePath: string): string | null { function readExistingDesktopFile(filePath: string): string | null {
try { try {
return fs.readFileSync(filePath, 'utf8'); return fs.readFileSync(filePath, 'utf8');
@@ -213,11 +238,30 @@ export function ensureLinuxProtocolDesktopEntry(): void {
} }
if (existing !== null) { if (existing !== null) {
if (!existing.includes(GENERATED_MARKER)) { if (!existing.includes(GENERATED_MARKER)) {
if (!isStaleDesktopFile(existing)) {
logger.debug('Linux .desktop entry was hand-edited; leaving untouched', {filePath}); logger.debug('Linux .desktop entry was hand-edited; leaving untouched', {filePath});
return; return;
} }
const systemEntry = findSystemDesktopEntry();
if (systemEntry) {
try {
fs.unlinkSync(filePath);
logger.info('Removed stale .desktop entry shadowing the system entry', {filePath, systemEntry});
} catch (error) {
logger.warn('Failed to remove stale .desktop entry', {filePath, error});
}
try {
app.setAsDefaultProtocolClient(APP_PROTOCOL);
} catch (error) {
logger.warn('Failed to register protocol client', {error});
}
return;
}
logger.info('Rewriting stale .desktop entry whose TryExec no longer resolves', {filePath});
} else {
needsWrite = existing !== desired; needsWrite = existing !== desired;
} }
}
if (!needsWrite) { if (!needsWrite) {
logger.debug('Linux .desktop entry already up to date', {filePath}); logger.debug('Linux .desktop entry already up to date', {filePath});
} else { } else {