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) {