From f2315ecc6e73548d79eef822a4a8092af2a60675 Mon Sep 17 00:00:00 2001 From: Pyramid Date: Tue, 10 Nov 2026 22:09:55 +0000 Subject: [PATCH] feat: skip gitignored paths and build output directories in discovery --- .changeset/skip-ignored-discovery.md | 5 + site/src/content/docs/api/unplugin.mdx | 2 +- site/src/content/docs/api/vite.mdx | 3 +- src/node/internal/Ignores.ts | 256 +++++++++++++++++++++++++ src/unplugin/index.ts | 11 ++ src/vite/index.test.ts | 48 +++++ src/vite/index.ts | 45 ++++- 7 files changed, 363 insertions(+), 7 deletions(-) create mode 100644 .changeset/skip-ignored-discovery.md create mode 100644 src/node/internal/Ignores.ts diff --git a/.changeset/skip-ignored-discovery.md b/.changeset/skip-ignored-discovery.md new file mode 100644 index 000000000..af98b18c0 --- /dev/null +++ b/.changeset/skip-ignored-discovery.md @@ -0,0 +1,5 @@ +--- +'zyzz': patch +--- + +Discovery and the portable adapters' source scan now skip paths that `.gitignore` files ignore and the bundler's output directories, so stale bundles beside the sources are never parsed. diff --git a/site/src/content/docs/api/unplugin.mdx b/site/src/content/docs/api/unplugin.mdx index d2fdb26be..5b34ba03c 100644 --- a/site/src/content/docs/api/unplugin.mdx +++ b/site/src/content/docs/api/unplugin.mdx @@ -67,7 +67,7 @@ const plugin = zyzz.rollup({ reset: true }) - **Type:** `string{:ts}` - **Default:** `process.cwd(){:ts}` -The source directory to compile. Every module under it contributes CSS, including unimported global styles. Hidden directories, `node_modules`, `dist`, `build`, `coverage`, tests, fixtures, and declaration, test, and benchmark modules are skipped. +The source directory to compile. Every module under it contributes CSS, including unimported global styles. Hidden directories, `node_modules`, `dist`, `build`, `coverage`, the bundler's output directory, paths that `.gitignore` files ignore, tests, fixtures, and declaration, test, and benchmark modules are skipped. ```ts title="build.ts" import { zyzz } from 'zyzz/unplugin' diff --git a/site/src/content/docs/api/vite.mdx b/site/src/content/docs/api/vite.mdx index b6d37d31b..f15117a11 100644 --- a/site/src/content/docs/api/vite.mdx +++ b/site/src/content/docs/api/vite.mdx @@ -171,11 +171,12 @@ Discovery covers the first of these that applies: 2. The directories that hold a `zyzz.config.*` module, found by name below the Vite root. Place the configuration at the root of the application's source, such as `src/zyzz.config.ts`, so tool configuration and scripts beside `src/` stay out. 3. The Vite root. -The `zyzz.config.*` search and a root scan skip nested packages: directories below the Vite root that hold their own `package.json`, such as a workspace's examples. [`exclude`](#optionsexclude) removes modules in every case. Adding or moving a `zyzz.config.*` module or a nested `package.json` takes full effect when the server restarts. +The `zyzz.config.*` search and a root scan skip nested packages: directories below the Vite root that hold their own `package.json`, such as a workspace's examples. Every scan also skips each environment's `build.outDir` and the paths that `.gitignore` files ignore, read from the repository root down, or from the scanned directory outside a repository, so stale bundles beside the sources are never parsed. [`exclude`](#optionsexclude) removes modules in every case. Adding or moving a `zyzz.config.*` module or a nested `package.json` takes full effect when the server restarts. ```txt app/ ├── examples/web/ # skipped: nested package +├── dist-preview/ # skipped: listed in .gitignore ├── playwright.config.ts # skipped: outside src/ ├── src/ │ ├── global.ts # discovered diff --git a/src/node/internal/Ignores.ts b/src/node/internal/Ignores.ts new file mode 100644 index 000000000..2120842fb --- /dev/null +++ b/src/node/internal/Ignores.ts @@ -0,0 +1,256 @@ +/** Matches paths against `.gitignore` rules, so project discovery skips what Git ignores. @module */ +import * as Fs from 'node:fs' +import * as Path from 'node:path' + +/** One `.gitignore` pattern, anchored at the directory holding its file. */ +export type Rule = { + /** Directory holding the `.gitignore` file, with platform separators. */ + readonly base: string + /** Matches directories only, as a trailing slash requires. */ + readonly directory: boolean + /** Re-includes a path that an earlier rule ignored. */ + readonly negated: boolean + /** Compiled pattern over the `/`-separated path relative to `base`. */ + readonly pattern: RegExp +} + +/** + * Creates a reader of the `.gitignore` files that apply below source directories. Rules apply from + * the enclosing repository root down, or from the source directory outside a repository. Files are + * read synchronously once per directory, as watcher events need an immediate answer. + * @param options - The source directories discovery walks. + */ +export function create(options: create.Options) { + const directories = new Map() + const repositories = new Map() + const rules = new Map() + + function repository(directory: string): boolean { + let value = repositories.get(directory) + + if (value === undefined) { + const parent = Path.dirname(directory) + + value = + Fs.existsSync(Path.join(directory, '.git')) || + (parent !== directory && repository(parent)) + repositories.set(directory, value) + } + + return value + } + + function read(directory: string): readonly Rule[] { + let value = rules.get(directory) + + if (value === undefined) { + const parent = Path.dirname(directory) + const inherited = + parent === directory || + Fs.existsSync(Path.join(directory, '.git')) || + (options.roots.includes(directory) && !repository(parent)) + ? [] + : read(parent) + const source = (() => { + try { + return Fs.readFileSync(Path.join(directory, '.gitignore'), 'utf8') + } catch (error) { + const code = (error as NodeJS.ErrnoException).code + + if (code === 'ENOENT' || code === 'ENOTDIR' || code === 'EISDIR') + return '' + throw error + } + })() + + value = [...inherited, ...parse({ base: directory, source })] + rules.set(directory, value) + } + + return value + } + + return { + /** + * Whether the rules of a path's directory ignore it. Callers check each ancestor below a + * source directory, since Git never re-includes a path below an ignored directory. + */ + ignored(path: ignored.Options): boolean { + if (!path.directory) return ignored(read(Path.dirname(path.path)), path) + + let value = directories.get(path.path) + + if (value === undefined) { + value = ignored(read(Path.dirname(path.path)), path) + directories.set(path.path, value) + } + + return value + }, + } +} + +/** Ignore reader contracts. */ +export declare namespace create { + /** Directories whose own `.gitignore` starts the rules outside a repository. */ + type Options = { + /** Absolute source directories. */ + readonly roots: readonly string[] + } +} + +/** + * Whether the last rule matching a path ignores it, as Git decides within one directory. A path + * below an ignored directory is the caller's concern: Git never re-includes it. + * @param rules - Rules of every `.gitignore` file that applies, outermost first. + * @param options - Absolute path and whether it names a directory. + */ +export function ignored( + rules: readonly Rule[], + options: ignored.Options, +): boolean { + let value = false + + for (const rule of rules) { + if (rule.directory && !options.directory) continue + + const relative = relativePath(rule.base, options.path) + + if (relative !== undefined && rule.pattern.test(relative)) + value = !rule.negated + } + + return value +} + +/** Path matching contracts. */ +export declare namespace ignored { + /** The path to test. */ + type Options = { + /** Whether the path is a directory. */ + readonly directory: boolean + /** Absolute path with platform separators. */ + readonly path: string + } +} + +/** + * Parses a `.gitignore` file into rules in file order. Blank lines, comments, and patterns Git + * treats as invalid produce no rule. + * @param options - File text and the directory holding it. + */ +export function parse(options: parse.Options): readonly Rule[] { + const rules: Rule[] = [] + + for (const line of options.source.split(/\r?\n/)) { + // Trailing spaces are insignificant unless escaped. + let value = line.replace(/(?( let assets = new Map() let css = '' let cssMap = '' + // The bundler's output directory, which the scan skips like ignored files. + let outdir: string | undefined let script = '' function identity(file: string) { @@ -113,6 +116,8 @@ const portable = createUnplugin( > = Object.create(null) const contracts: Record = Object.create(null) + const ignores = Ignores.create({ roots: [root] }) + async function scan(directory: string): Promise { directories.add(directory) const entries = await Fs.readdir(directory, { withFileTypes: true }) @@ -137,6 +142,11 @@ const portable = createUnplugin( continue const file = Path.join(directory, entry.name) + if ( + (entry.isDirectory() && file === outdir) || + ignores.ignored({ directory: entry.isDirectory(), path: file }) + ) + continue if (entry.isDirectory()) await scan(file) else if ( entry.isFile() && @@ -551,6 +561,7 @@ const portable = createUnplugin( options.absWorkingDir ?? process.cwd(), options.outdir, ) + outdir = options.outdir }, setup(build) { // Unplugin forwards watchFiles but not watchDirs from its transform hook. diff --git a/src/vite/index.test.ts b/src/vite/index.test.ts index 51fcec9a0..5055fe990 100644 --- a/src/vite/index.test.ts +++ b/src/vite/index.test.ts @@ -2691,6 +2691,54 @@ export function unrelated(props: { className?: string | undefined } = {}) { } }) + test('skips ignored files and build output in discovery', async () => { + const { config, root } = await create() + const global = (color: string) => + `import { global } from 'zyzz/web'; global({ body: { color: '${color}' } });` + + // Stale bundles from earlier builds sit beside the sources. + await Fs.mkdir(Path.join(root, 'bundles')) + await Fs.mkdir(Path.join(root, 'out')) + await Fs.writeFile( + Path.join(root, '.gitignore'), + 'bundles/\n*.bundle.js\n!kept.bundle.js\n', + ) + await Fs.writeFile(Path.join(root, 'bundles/stale.js'), global('#123456')) + await Fs.writeFile(Path.join(root, 'out/index.js'), global('#234567')) + await Fs.writeFile(Path.join(root, 'stale.bundle.js'), global('#345678')) + await Fs.writeFile(Path.join(root, 'kept.bundle.js'), global('#456789')) + + try { + const result = await Vite.build({ + ...config, + build: { outDir: 'out', write: false }, + resolve: { + alias: { + ...(config.resolve!.alias as Record), + 'zyzz/web': Path.resolve('src/web/index.ts'), + }, + }, + }) + const output = Array.isArray(result) ? result[0]! : result + if (!('output' in output)) throw new Error('Unexpected watch build') + const css = output.output + .flatMap((file) => + file.type === 'asset' && file.fileName.endsWith('.css') + ? [String(file.source)] + : [], + ) + .join('\n') + + expect(css.includes('#06c')).toMatchInlineSnapshot(`true`) + expect(css.includes('#123456')).toMatchInlineSnapshot(`false`) + expect(css.includes('#234567')).toMatchInlineSnapshot(`false`) + expect(css.includes('#345678')).toMatchInlineSnapshot(`false`) + expect(css.includes('#456789')).toMatchInlineSnapshot(`true`) + } finally { + await Fs.rm(root, { recursive: true, force: true }) + } + }) + test('skips nested packages in discovery unless included', async () => { const { config, root } = await create() const example = Path.join(root, 'examples/web') diff --git a/src/vite/index.ts b/src/vite/index.ts index b8c7ba21c..aa2fdd81c 100644 --- a/src/vite/index.ts +++ b/src/vite/index.ts @@ -7,6 +7,7 @@ import * as AtRules from '../compiler/internal/AtRules.js' import * as Catalogs from '../compiler/internal/Catalogs.js' import * as Results from '../compiler/internal/Results.js' import * as Contracts from '../node/internal/Contracts.js' +import * as Ignores from '../node/internal/Ignores.js' import * as Mapping from '@jridgewell/gen-mapping' import * as Lightning from 'lightningcss' import * as Crypto from 'node:crypto' @@ -293,6 +294,9 @@ export function zyzz(options: zyzz.Options = {}): Plugin { const roots = new Map() const lazyImports = new WeakMap() const nestedPackages = new Map() + // Discovery skips what Git ignores and what a build writes, such as stale bundles beside sources. + let ignores = Ignores.create({ roots: [] }) + let outDirs: readonly string[] = [] const contributions = new Map() let directories: readonly string[] // The directories discovery walks, and the include patterns that narrow it. @@ -449,7 +453,8 @@ export function zyzz(options: zyzz.Options = {}): Plugin { const file = Path.join(directory, item.name) - if (item.isDirectory()) return nested(file) ? [] : collect(file) + if (item.isDirectory()) + return nested(file) || hidden(file, true) ? [] : collect(file) if (!item.isFile() || !eager(file, environment)) return [] host.watch(file) @@ -516,6 +521,17 @@ export function zyzz(options: zyzz.Options = {}): Plugin { return value } + /** + * Whether discovery skips a path below a source directory: a build output directory, or a path + * the applicable `.gitignore` files ignore. + */ + function hidden(path: string, directory: boolean) { + return ( + (directory && outDirs.includes(path)) || + ignores.ignored({ directory, path }) + ) + } + function eager(file: string, environment: Environment) { // Watcher events reach files outside the scopes that discovery walked. if (!scopes.some((scope) => contains(scope, file))) return false @@ -529,10 +545,11 @@ export function zyzz(options: zyzz.Options = {}): Plugin { directory !== undefined && parent !== directory; parent = Path.dirname(parent) ) - if (nested(parent)) return false + if (nested(parent) || hidden(parent, true)) return false return ( eligible(file) && + !hidden(file, false) && file !== environment.config.configFile && !environment.config.configFileDependencies.includes(file) && // Server entrypoints must not contribute to the client stylesheet scan. @@ -2157,12 +2174,28 @@ export function zyzz(options: zyzz.Options = {}): Plugin { // The root keeps the `app` namespace, so only bases outside it add source directories. directories = [root, ...bases.filter((base) => !contains(root, base))] + // A build output directory holding a source directory, such as `outDir: '..'`, stays walked. + outDirs = [ + ...new Set( + [ + config.build.outDir, + ...Object.values(config.environments).map( + (environment) => environment.build.outDir, + ), + ].map((directory) => Path.resolve(root, directory)), + ), + ].filter( + (output) => + !directories.some((directory) => contains(output, directory)), + ) + // Portable build adapters share this module and do not require Vite. const { createFilter } = await import('vite') const patterns = [...configFiles, ...(options.exclude ?? [])] filter = createFilter(null, patterns, { resolve: root }) exclusions.clear() + ignores = Ignores.create({ roots: directories }) nestedPackages.clear() roots.clear() @@ -2178,7 +2211,7 @@ export function zyzz(options: zyzz.Options = {}): Plugin { scoped = undefined // A `zyzz.config.*` module marks the application's source directory. - const configured = await configDirectories({ nested, root }) + const configured = await configDirectories({ hidden, nested, root }) if (configured.length) scopes = configured } @@ -2854,7 +2887,7 @@ async function pattern(options: pattern.Options) { * The walk skips the directories discovery skips, including nested packages. */ async function configDirectories(options: configDirectories.Options) { - const { nested, root } = options + const { hidden, nested, root } = options const found: string[] = [] async function walk(directory: string): Promise { @@ -2864,7 +2897,7 @@ async function configDirectories(options: configDirectories.Options) { const file = Path.join(directory, item.name) if (item.isDirectory()) { - if (!nested(file)) await walk(file) + if (!nested(file) && !hidden(file, true)) await walk(file) } else if ( item.isFile() && /^zyzz\.config\.[cm]?[jt]sx?$/.test(item.name) && @@ -2885,6 +2918,8 @@ async function configDirectories(options: configDirectories.Options) { declare namespace configDirectories { type Options = { + /** Whether discovery skips a directory as ignored or build output. */ + readonly hidden: (path: string, directory: boolean) => boolean /** Whether discovery skips a directory as a nested package. */ readonly nested: (directory: string) => boolean /** The physical Vite root. */