From 17b4fd5b09c78a0b05fb8b5471221f026f5188c8 Mon Sep 17 00:00:00 2001 From: jdalton Date: Mon, 23 Nov 2026 13:14:30 -0500 Subject: [PATCH] fix(scan): reduce memory use during directory scans --- scripts/test/native.mts | 7 ++-- .../manifest/cmd-manifest-cdxgen.test.mts | 1 + src/utils/glob.mts | 9 +++-- src/utils/glob.test.mts | 40 ++++++++++++++++++- test/release-workflow.test.mts | 8 +++- 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/scripts/test/native.mts b/scripts/test/native.mts index 27cc304be..238349790 100644 --- a/scripts/test/native.mts +++ b/scripts/test/native.mts @@ -1,3 +1,5 @@ +import { SEA_VERSION } from '../../src/sea/version.mts' + import type { SpawnSyncOptions } from 'node:child_process' import { spawnSync } from 'node:child_process' import { createHash } from 'node:crypto' @@ -8,7 +10,6 @@ import process from 'node:process' import { isMainModule } from '../lib/is-main-module.mts' import type { ScriptMeta } from '../lib/run-main.mts' import { runMain } from '../lib/run-main.mts' -import { SEA_VERSION } from '../../src/sea/version.mts' import { assertSnapshotDependenciesCurrent } from '../refresh/snapshots/dependencies.mts' import { persistNativeSigningReceipt, @@ -73,10 +74,10 @@ async function verifyArtifact( const manifest = JSON.parse(manifestText) as NativeManifest if ( manifest.application?.name !== packageInfo.name || - manifest.application?.version !== packageInfo.version + manifest.application?.version !== SEA_VERSION ) { throw new Error( - `Native manifest is for ${manifest.application?.name}@${manifest.application?.version}; expected ${packageInfo.name}@${packageInfo.version}`, + `Native manifest is for ${manifest.application?.name}@${manifest.application?.version}; expected ${packageInfo.name}@${SEA_VERSION}`, ) } if (manifest.target?.triple !== TRIPLE) { diff --git a/src/commands/manifest/cmd-manifest-cdxgen.test.mts b/src/commands/manifest/cmd-manifest-cdxgen.test.mts index c01f736f8..27feeba7c 100644 --- a/src/commands/manifest/cmd-manifest-cdxgen.test.mts +++ b/src/commands/manifest/cmd-manifest-cdxgen.test.mts @@ -117,6 +117,7 @@ describe('socket manifest cdxgen', async () => { '`socket manifest cdxgen`', ) }, + { timeout: 60_000 }, ) it.skipIf(process.platform === 'win32')( diff --git a/src/utils/glob.mts b/src/utils/glob.mts index c01dd31c0..1e4529467 100644 --- a/src/utils/glob.mts +++ b/src/utils/glob.mts @@ -436,6 +436,9 @@ export async function globWithGitIgnore( ] : [...ignores, ...cliMinimatchIgnores].map(stripTrailingSlash), ...additionalOptions, + // fast-glob's uniqueness set retains every match before the streaming + // filter runs. Filtered scans deduplicate only supported paths below. + unique: filter ? false : additionalOptions.unique, // Skip dirs the running user cannot read instead of aborting on the first // `EACCES`. Pinned after `...additionalOptions` so a caller's options bag // cannot flip it off; it is a safety invariant, not a tunable. @@ -483,7 +486,7 @@ export async function globWithGitIgnore( // optional caller filter drops non-matches before they accumulate. On the slow // path each surviving entry is also re-checked against its gitignore ancestor // chain, which carries the full negation support fast-glob lacks. - const results: string[] = [] + const results = new Set() const stream = fastGlob.globStream( patterns as string[], globOptions, @@ -502,9 +505,9 @@ export async function globWithGitIgnore( if (filter && !filter(p)) { continue } - results.push(p) + results.add(p) } - return results + return [...results] } export async function globWorkspace( diff --git a/src/utils/glob.test.mts b/src/utils/glob.test.mts index e86abcece..42e8a0e04 100644 --- a/src/utils/glob.test.mts +++ b/src/utils/glob.test.mts @@ -8,7 +8,8 @@ import { import os from 'node:os' import path from 'node:path' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import fastGlob from 'fast-glob' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { normalizePath } from '@socketsecurity/registry/lib/path' @@ -170,6 +171,43 @@ describe('glob utilities', () => { ]) }) + it('disables fast-glob uniqueness when filtering', async () => { + mockTestFs({ [`${mockFixturePath}/package.json`]: '{}' }) + const globStreamSpy = vi.spyOn(fastGlob, 'globStream') + + try { + await globWithGitIgnore(['**/*'], { + cwd: mockFixturePath, + filter: filterJsonFiles, + }) + + expect(globStreamSpy).toHaveBeenLastCalledWith( + ['**/*'], + expect.objectContaining({ unique: false }), + ) + } finally { + globStreamSpy.mockRestore() + } + }) + + it('deduplicates overlapping matches after filtering', async () => { + mockTestFs({ + [`${mockFixturePath}/package.json`]: '{}', + [`${mockFixturePath}/src/package.json`]: '{}', + [`${mockFixturePath}/src/readme.md`]: '', + }) + + const results = await globWithGitIgnore(['**/*.json', 'src/**/*'], { + cwd: mockFixturePath, + filter: filterJsonFiles, + }) + + expect(results.map(normalizePath).sort()).toEqual([ + `${mockFixturePath}/package.json`, + `${mockFixturePath}/src/package.json`, + ]) + }) + it('should skip directories ignored via trailing-slash patterns', async () => { // fast-glob silently drops `ignore` entries that end in `/`, so // the pattern produced for `dist/` (`**/dist/`) used to do diff --git a/test/release-workflow.test.mts b/test/release-workflow.test.mts index 85ed6d3b5..d9fcde454 100644 --- a/test/release-workflow.test.mts +++ b/test/release-workflow.test.mts @@ -257,9 +257,13 @@ describe('v1 release workflow contract', () => { const manifest = JSON.parse( readFileSync(new URL('../package.json', import.meta.url), 'utf8'), ) as { publishConfig?: { tag?: string }; version?: string } - // The stable v1 lane and the 2.1 prerelease lane both write this version. + const releasePolicy = JSON.parse( + readFileSync(new URL('../.config/release.json', import.meta.url), 'utf8'), + ) as { identifier: string; versionHint: string } expect(manifest.version).toMatch(/^(?:1\.\d+\.\d+|2\.1\.0-rc\.\d+)$/u) - expect(manifest.publishConfig?.tag).toBe('rc-2.1.0') + expect(manifest.publishConfig?.tag).toBe( + `${releasePolicy.identifier}-${releasePolicy.versionHint}`, + ) expect(workflow.on.workflow_dispatch.inputs['release-major']?.default).toBe( '1', )