fix(cli): anchor an absolute-path project pattern to a segment boundary

filterProjectsByName matched every pattern as a raw substring, so
--exclude /a/my-company also removed the sibling /a/my-company-kit, and
no pattern the caller could type expressed "this project and not its
neighbours".

A rooted pattern (POSIX, Windows drive, UNC) now keys through
normalizeAbsProjectPathKey, the helper #1271 added for project identity,
and matches the way isProxiedPath matches proxyPaths: equal to the
project path, or a prefix of it on a path-segment boundary. Going
through that helper keeps one identity rule in the parser: identified
Windows paths casefold, POSIX case stays identity, and a Codex-style
stripped absolute path ("root/vault") keys the same as the pattern
"/root/vault" the user types. A pattern that is not rooted stays the
loose substring people type on the command line, so --project
my-company keeps covering my-company-kit and worktrees.

The rule lives in one closure (makeProjectFilter) applied to the live
parse and to the carry-forward day cache alike. The cache path had its
own copy, lowercasing and substring-matching, so a filtered headline
anchored today's live days while matching every cached day loosely: on a
carried day split across /Users/gone/app ($30) and /Users/gone/app-kit
($70), --exclude /Users/gone/app returned 0 instead of 70, and --exclude
/users/gone/app excluded both instead of neither. Both shapes are in
tests/project-filter-durable-totals.test.ts, the suite that exists to
pin that a filter selects the same projects whether it is resolved
against a fresh parse or against the cache.

The README gains the rules that were not written down: an absolute POSIX
path is case-sensitive while a Windows drive or UNC path folds case, and
a leading ~ is the shell's to expand.
This commit is contained in:
Mathieu Stanowski 2026-09-09 17:02:57 +02:00
parent 29fcb38976
commit 172b8f4df6
4 changed files with 128 additions and 25 deletions

View file

@ -4413,6 +4413,26 @@ export function setCachePutMeta(meta: { startMs: number; endMs: number; sig: str
putMeta = meta
}
export function isRootedProjectPattern(pattern: string): boolean {
const raw = pattern.trim().replace(/\\/g, '/')
return raw.startsWith('/') || /^[a-zA-Z]:\//.test(raw)
}
/// An absolute path names ONE project, so it anchors on a segment boundary (the
/// isProxiedPath rule): "/a/proj" takes "/a/proj/sub" but not "/a/proj-ui-kit".
/// Both sides key through normalizeAbsProjectPathKey (Windows casefolds, POSIX
/// does not, #1260), and rootedness alone picks the branch, so "/" names none.
export function matchesProjectPattern(project: { project: string; projectPath?: string }, pattern: string): boolean {
const projectPath = project.projectPath ?? ''
if (isRootedProjectPattern(pattern)) {
const anchor = normalizeAbsProjectPathKey(pattern)
const target = normalizeAbsProjectPathKey(projectPath)
return anchor !== null && target !== null && (target === anchor || target.startsWith(anchor + '/'))
}
const needle = pattern.toLowerCase()
return project.project.toLowerCase().includes(needle) || projectPath.toLowerCase().includes(needle)
}
export function filterProjectsByName(
projects: ProjectSummary[],
include?: string[],
@ -4420,20 +4440,10 @@ export function filterProjectsByName(
): ProjectSummary[] {
let result = projects
if (include && include.length > 0) {
const patterns = include.map(s => s.toLowerCase())
result = result.filter(p => {
const name = p.project.toLowerCase()
const path = p.projectPath.toLowerCase()
return patterns.some(pat => name.includes(pat) || path.includes(pat))
})
result = result.filter(p => include.some(pattern => matchesProjectPattern(p, pattern)))
}
if (exclude && exclude.length > 0) {
const patterns = exclude.map(s => s.toLowerCase())
result = result.filter(p => {
const name = p.project.toLowerCase()
const path = p.projectPath.toLowerCase()
return !patterns.some(pat => name.includes(pat) || path.includes(pat))
})
result = result.filter(p => !exclude.some(pattern => matchesProjectPattern(p, pattern)))
}
return result
}