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.
This commit is contained in:
Mathieu Stanowski 2026-09-12 09:19:44 +02:00 • committed by iamtoruk
parent 6cac9d929c
commit 1b006ed2f7
3 changed files with 30 additions and 1 deletions

View file

@ -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' }))

View file

@ -160,13 +160,23 @@ function normalizePatterns(value: unknown): string[] {
const patterns = new Set<string>()
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) }

View file

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