Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,54 @@ describe("projectIconResolver", () => {
const iconPath = writeFile(root, "public/brand/logo.svg", "<svg>brand</svg>");

expect(resolveProjectIconPath(root)).toBe(iconPath);

// `rel` is an unordered, whitespace-separated token list; any `icon` token
// marks an icon link, wherever it sits in the list.
const tokenListRoot = makeProjectRoot();
writeFile(tokenListRoot, "index.html", '<link rel="icon shortcut" href="/brand/logo.svg">');
const tokenListIconPath = writeFile(tokenListRoot, "public/brand/logo.svg", "<svg>brand</svg>");
expect(resolveProjectIconPath(tokenListRoot)).toBe(tokenListIconPath);

// The same token list in the `__root.tsx` head-config object form.
const objectRoot = makeProjectRoot();
writeFile(
objectRoot,
"app/routes/__root.tsx",
'export const Route = { head: () => ({ links: [{ rel: "icon shortcut", href: "/brand/logo.svg" }] }) };',
);
const objectIconPath = writeFile(objectRoot, "public/brand/logo.svg", "<svg>brand</svg>");
expect(resolveProjectIconPath(objectRoot)).toBe(objectIconPath);
Comment on lines +75 to +90

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Token-list cases lack coverage

Both new success cases put lowercase icon first and use a space. They do not exercise the preceding-token branch in ICON_REL_VALUE, tabs, mixed case, or single-quoted values. Those paths could break while these tests still pass.

Add table-driven cases such as rel="shortcut Icon", rel="Icon\tshortcut", and single-quoted values, including the object form, so the committed tests cover the behavior described by this fix.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop/src/main/services/projects/projectIconResolver.test.ts
Line: 75-90

Comment:
**Token-list cases lack coverage**

Both new success cases put lowercase `icon` first and use a space. They do not exercise the preceding-token branch in `ICON_REL_VALUE`, tabs, mixed case, or single-quoted values. Those paths could break while these tests still pass.

Add table-driven cases such as `rel="shortcut Icon"`, `rel="Icon\tshortcut"`, and single-quoted values, including the object form, so the committed tests cover the behavior described by this fix.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code


// The closing quote pairs with the opening one, so `icon's` has no `icon` token.
const possessiveRoot = makeProjectRoot();
writeFile(possessiveRoot, "index.html", '<link rel="icon\'s" href="/brand/logo.svg">');
writeFile(possessiveRoot, "public/brand/logo.svg", "<svg>brand</svg>");
expect(resolveProjectIconPath(possessiveRoot)).toBeNull();

// A matched link whose href cannot be used (missing file, remote URL) must
// not mask a later usable icon link in the same file.
const maskedRoot = makeProjectRoot();
writeFile(
maskedRoot,
"index.html",
[
'<link rel="icon shortcut" href="/brand/missing.png">',
'<link rel="icon" href="https://cdn.example.com/icon.png">',
'<link rel="shortcut icon" href="/brand/logo.svg">',
].join("\n"),
);
const maskedIconPath = writeFile(maskedRoot, "public/brand/logo.svg", "<svg>brand</svg>");
expect(resolveProjectIconPath(maskedRoot)).toBe(maskedIconPath);

// The same fallback across head-config link objects.
const maskedObjectRoot = makeProjectRoot();
writeFile(
maskedObjectRoot,
"app/routes/__root.tsx",
'export const Route = { head: () => ({ links: [{ rel: "icon shortcut", href: "/brand/missing.png" }, { rel: "icon", href: "/brand/logo.svg" }] }) };',
);
const maskedObjectIconPath = writeFile(maskedObjectRoot, "public/brand/logo.svg", "<svg>brand</svg>");
expect(resolveProjectIconPath(maskedObjectRoot)).toBe(maskedObjectIconPath);
});

it("detects nested app icons in monorepos", () => {
Expand Down
42 changes: 27 additions & 15 deletions apps/desktop/src/main/services/projects/projectIconResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,30 @@ const IOS_ASSET_CATALOG_CANDIDATE_DIRS = [

const IOS_ASSET_ICONSET_EXTENSIONS = new Set([".appiconset", ".imageset"]);

const LINK_ICON_HTML_RE =
/<link\b(?=[^>]*\brel=["'](?:icon|shortcut icon)["'])(?=[^>]*\bhref=["']([^"'?]+))[^>]*>/i;
const LINK_ICON_OBJ_RE =
/(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i;
// `rel` is an unordered, whitespace-separated token list; any `icon` token marks an icon link.
const ICON_REL_VALUE = String.raw`(?:"(?:[^"]*[\t\n\f\r ])?icon(?=[\t\n\f\r "])|'(?:[^']*[\t\n\f\r ])?icon(?=[\t\n\f\r ']))`;
const LINK_ICON_HTML_RE = new RegExp(
String.raw`<link\b(?=[^>]*\brel=${ICON_REL_VALUE})(?=[^>]*\bhref=["']([^"'?]+))[^>]*>`,
"gi",
);
const LINK_ICON_OBJ_RE = new RegExp(
String.raw`(?=[^}]*\brel\s*:\s*${ICON_REL_VALUE})(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*`,
"gi",
);

type ProjectIconOverride = string | null | undefined;

function extractIconHref(source: string): string | null {
const htmlMatch = source.match(LINK_ICON_HTML_RE);
if (htmlMatch?.[1]) return htmlMatch[1];
const objMatch = source.match(LINK_ICON_OBJ_RE);
if (objMatch?.[1]) return objMatch[1];
return null;
function extractIconHrefs(source: string): string[] {
// Every icon link in order, so one unusable href (a missing file, a CDN URL,
// a `data:` URL) does not mask a later usable icon in the same file.
const hrefs: string[] = [];
for (const match of source.matchAll(LINK_ICON_HTML_RE)) {
if (match[1]) hrefs.push(match[1]);
}
for (const match of source.matchAll(LINK_ICON_OBJ_RE)) {
if (match[1]) hrefs.push(match[1]);
}
Comment on lines +99 to +101

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Icon lookup freezes ADE

extractIconHrefs now runs LINK_ICON_OBJ_RE over the whole file before the caller can use a valid HTML link. When the file has no } or object-style rel: field, the regex scans the remaining text again at each position.

An 80 KB HTML input with a valid first icon link took about 2.24 seconds, versus under 1 ms for the previous extraction path. This runs synchronously in Electron's main process, so opening such a project stalls other app requests. Yield hrefs lazily so the caller can stop after a usable HTML link, or limit the object search to individual objects.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop/src/main/services/projects/projectIconResolver.ts
Line: 99-101

Comment:
**Icon lookup freezes ADE**

`extractIconHrefs` now runs `LINK_ICON_OBJ_RE` over the whole file before the caller can use a valid HTML link. When the file has no `}` or object-style `rel:` field, the regex scans the remaining text again at each position.

An 80 KB HTML input with a valid first icon link took about 2.24 seconds, versus under 1 ms for the previous extraction path. This runs synchronously in Electron's main process, so opening such a project stalls other app requests. Yield hrefs lazily so the caller can stop after a usable HTML link, or limit the object search to individual objects.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code

return hrefs;
}

function isLocalIconHref(href: string): boolean {
Expand Down Expand Up @@ -810,11 +821,12 @@ export function resolveProjectIconPath(
} catch {
continue;
}
const href = extractIconHref(source);
if (!href || !isLocalIconHref(href)) continue;
const existing = findExistingFile(root, resolveIconHref(root, href));
if (existing && isSupportedIconPath(existing) && isInlineableIconFile(existing)) {
return cacheValue(existing);
for (const href of extractIconHrefs(source)) {
if (!isLocalIconHref(href)) continue;
const existing = findExistingFile(root, resolveIconHref(root, href));
if (existing && isSupportedIconPath(existing) && isInlineableIconFile(existing)) {
return cacheValue(existing);
}
}
}

Expand Down
Loading