From 1b006ed2f70bba4a02b56cbbbc3eda6be3759b62 Mon Sep 17 00:00:00 2001 From: Mathieu Stanowski Date: Sat, 12 Sep 2026 09:19:44 +0200 Subject: [PATCH] fix(app): expand a leading ~ in a project pattern typed into the pane A pattern typed into the Projects pane never passes through a shell, so "~/work/app" reached the CLI as a literal tilde and matched nothing. It is expanded in the main process now, on the way into app-filter.json and on the way back out: the renderer has no home directory of its own, and a tilde it cannot resolve would make its switches disagree with the argv this process writes. The file can also be hand-edited, so the expansion runs on read as well as on write. projectMatch.test.ts covers the expanded shapes against the CLI's matchesProjectPattern, which is what keeps the switch and the argv on the same answer. --- app/electron/main.test.ts | 13 +++++++++++++ app/electron/main.ts | 12 +++++++++++- app/renderer/lib/projectMatch.test.ts | 6 ++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/electron/main.test.ts b/app/electron/main.test.ts index c62eabf3..cf5147b0 100644 --- a/app/electron/main.test.ts +++ b/app/electron/main.test.ts @@ -1,5 +1,6 @@ // @vitest-environment node import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { homedir } from 'node:os' import { tmpdir } from 'node:os' import { join } from 'node:path' import { describe, it, expect, vi } from 'vitest' @@ -864,6 +865,18 @@ describe('project filter', () => { }) }) + it('expands a leading ~ so the pane and the CLI resolve the same path', async () => { + await withFilterFile(filterPath => { + const home = homedir().replace(/\\/g, '/') + expect(writeProjectFilter({ project: ['~/work/app'], exclude: ['~'] })) + .toEqual({ project: [`${home}/work/app`], exclude: [home] }) + // Also on the way out: the file can be hand-edited with a tilde the + // renderer has no way to resolve. + writeFileSync(filterPath, JSON.stringify({ exclude: ['~/work/other'] })) + expect(readProjectFilter()).toEqual({ project: [], exclude: [`${home}/work/other`] }) + }) + }) + it('reads a hand-written bare string as one pattern instead of dropping it', async () => { await withFilterFile(filterPath => { writeFileSync(filterPath, JSON.stringify({ exclude: 'my-company' })) diff --git a/app/electron/main.ts b/app/electron/main.ts index bf829b83..20ab9c50 100644 --- a/app/electron/main.ts +++ b/app/electron/main.ts @@ -160,13 +160,23 @@ function normalizePatterns(value: unknown): string[] { const patterns = new Set() for (const entry of entries) { if (typeof entry !== 'string') continue - const pattern = entry.trim() + const pattern = expandTilde(entry.trim()) if (pattern === '') continue patterns.add(pattern) } return [...patterns] } +/// A pattern typed into the pane has no shell behind it, so "~/work/app" is +/// expanded here, on the way in and on the way out of the file. The renderer +/// has no home directory of its own, and a tilde it cannot resolve would make +/// its switches disagree with the argv this process writes. +function expandTilde(pattern: string): string { + const raw = pattern.replace(/\\/g, '/') + if (raw !== '~' && !raw.startsWith('~/')) return pattern + return os.homedir().replace(/\\/g, '/') + raw.slice(1) +} + function normalizeProjectFilter(value: unknown): ProjectFilter { const raw = (value ?? {}) as { project?: unknown; exclude?: unknown } return { project: normalizePatterns(raw.project), exclude: normalizePatterns(raw.exclude) } diff --git a/app/renderer/lib/projectMatch.test.ts b/app/renderer/lib/projectMatch.test.ts index 2d971dfd..463d094e 100644 --- a/app/renderer/lib/projectMatch.test.ts +++ b/app/renderer/lib/projectMatch.test.ts @@ -1,3 +1,5 @@ +import { homedir } from 'node:os' + import { describe, expect, it } from 'vitest' import { matchesProjectPattern } from '../../../src/parser.js' @@ -14,6 +16,7 @@ const PROJECTS = [ { name: 'unc', path: '\\\\Server\\Share\\Vault' }, { name: 'stripped', path: 'root/vault' }, { name: 'stripped-ui', path: 'root/vault-ui' }, + { name: 'home-proj', path: `${homedir().replace(/\\/g, '/')}/work/my-company` }, { name: 'encoded', path: '' }, { name: '-Users-me-Web-thing', path: '' }, ] @@ -24,6 +27,9 @@ const PATTERNS = [ 'root/vault', 'me/work', '/', '//', '', '-Users-me-Web', 'thing', '/nope', ] +// A tilde is absent on purpose: the main process expands it on the way in and +// out of the filter file (see normalizePatterns), so a pattern reaching the +// renderer is already a path it can resolve. describe('projectMatch parity with the CLI', () => { it('agrees with matchesProjectPattern on every pattern and project', () => { for (const project of PROJECTS) {