Skip to content

Commit bec0ccf

Browse files
committed
refactor(manifest/bazel): address review feedback, fix lint
Walker: - Lower MAX_WORKSPACE_ROOTS from 256 to 16; well above realistic monorepo counts, tighter guard against pathological inputs. Orchestrator: - Inject the workspace-walker prune policy via ExtractBazelOptions (`ignoreDirNames`, `ignoreDirPrefixes`) instead of hardcoding it inside the orchestrator. The CLI command now owns the policy and supplies the codebase-wide IGNORED_DIRS plus the Bazel-specific bits (`.socket-auto-manifest`, VCS/IDE dirs, `bazel-*` prefix). - Drop the `dist*` prefix from the prune policy — it's a JS/web convention, not Bazel-specific, and shouldn't be hardcoded. - Drop the Bzlmod-mode "defensive fallback" probe over the conventional Maven hub names. `bazel mod show_extension` is the authoritative source under Bzlmod; customer-supplied extras (`--bazel-maven-repo=`) still get probed. WORKSPACE mode keeps the probe path unchanged. The fallback had no real-world justification. - Hoist the per-workspace `buildQueryOpts` helper to module scope (eslint consistent-function-scoping). - Verbose-mode logging in `reapBazelServer` and `removeTempdir` catch blocks so the operator can see why a cleanup step failed instead of having it swallowed silently. Lint: - bazel-query-runner.mts: add missing `await` on three `return runBazelOneShot(...)` sites (@typescript-eslint/return-await). - bazel-repo-discovery.test.mts: hoist five inline `probe` closures to module-scope named consts. - extract_bazel_to_maven.test.mts: hoist `readManifest` and an inline mock factory to module scope; reorder type imports. - extract_bazel_to_pypi.mts: merge duplicate imports of `bazel-pypi-discovery.mts`. - extract_bazel_to_maven.mts: reorder type imports.
1 parent 14f593d commit bec0ccf

9 files changed

Lines changed: 198 additions & 129 deletions

src/commands/manifest/bazel/bazel-cquery.test.mts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ describe('buildMetadataCqueryArgv', () => {
7575
})
7676
expect(argv).toContain('cquery')
7777
expect(argv).toContain('--output=jsonproto')
78-
expect(argv).toContain('--proto:output_rule_attrs=tags,maven_coordinates,maven_url')
78+
expect(argv).toContain(
79+
'--proto:output_rule_attrs=tags,maven_coordinates,maven_url',
80+
)
7981
expect(argv).toContain('--keep_going')
8082
expect(argv).toContain('--lockfile_mode=off')
8183
const expr = argv.find(a => a.includes('attr("tags"'))
@@ -164,7 +166,9 @@ describe('parseCqueryJsonproto', () => {
164166
{
165167
name: 'tags',
166168
type: 'STRING_LIST',
167-
string_list_value: ['maven_coordinates=com.example:snake:2.0'],
169+
string_list_value: [
170+
'maven_coordinates=com.example:snake:2.0',
171+
],
168172
},
169173
],
170174
},

src/commands/manifest/bazel/bazel-query-runner.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ async function runBazelOneShot(
309309
export async function runBazelModShowVisibleRepos(
310310
opts: BazelQueryOptions,
311311
): Promise<BazelQueryResult> {
312-
return runBazelOneShot(
312+
return await runBazelOneShot(
313313
buildBazelModShowVisibleReposArgv(opts),
314314
opts,
315315
'bazel mod dump_repo_mapping',
@@ -325,7 +325,7 @@ export async function runBazelModShowVisibleRepos(
325325
export async function runBazelModShowMavenExtension(
326326
opts: BazelQueryOptions,
327327
): Promise<BazelQueryResult> {
328-
return runBazelOneShot(
328+
return await runBazelOneShot(
329329
buildBazelModShowMavenExtensionArgv(opts),
330330
opts,
331331
'bazel mod show_extension rules_jvm_external maven',
@@ -340,7 +340,7 @@ export async function runBazelModShowMavenExtension(
340340
export async function runBazelModShowPipExtension(
341341
opts: BazelQueryOptions,
342342
): Promise<BazelQueryResult> {
343-
return runBazelOneShot(
343+
return await runBazelOneShot(
344344
buildBazelModShowPipExtensionArgv(opts),
345345
opts,
346346
'bazel mod show_extension rules_python pip',

src/commands/manifest/bazel/bazel-query-runner.test.mts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ describe('buildMavenProbeFor', () => {
373373
mocked.mockResolvedValueOnce({
374374
code: 1,
375375
stdout: '',
376-
stderr:
377-
"ERROR: No repository visible as '@nope' from main repository\n",
376+
stderr: "ERROR: No repository visible as '@nope' from main repository\n",
378377
})
379378
const probe = buildMavenProbeFor({
380379
bin: 'bazel',
@@ -385,8 +384,7 @@ describe('buildMavenProbeFor', () => {
385384
expect(result).toEqual({
386385
code: 1,
387386
stdout: '',
388-
stderr:
389-
"ERROR: No repository visible as '@nope' from main repository\n",
387+
stderr: "ERROR: No repository visible as '@nope' from main repository\n",
390388
})
391389
})
392390
})

src/commands/manifest/bazel/bazel-repo-discovery.test.mts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ const probeResult = (over: Partial<ProbeResult> = {}): ProbeResult => ({
4444
...over,
4545
})
4646

47+
const probePopulatedGuava: RepoProbe = async () => ({
48+
code: 0,
49+
stdout: '@maven//:guava\n',
50+
stderr: '',
51+
})
52+
53+
const probePopulatedX: RepoProbe = async () => ({
54+
code: 0,
55+
stdout: '@maven//:x\n',
56+
stderr: '',
57+
})
58+
59+
const probeThrows: RepoProbe = async () => {
60+
throw new Error('bazel exploded')
61+
}
62+
4763
describe('bazel-repo-discovery', () => {
4864
describe('parseShowExtensionOutput', () => {
4965
it('extracts hub repos with (imported by ...) annotations only', () => {
@@ -122,7 +138,7 @@ describe('bazel-repo-discovery', () => {
122138
probeResult({
123139
code: 1,
124140
stderr:
125-
"WARNING: Evaluation of query \"@maven_install//...\" failed: no targets found beneath ''\n",
141+
'WARNING: Evaluation of query "@maven_install//..." failed: no targets found beneath \'\'\n',
126142
}),
127143
),
128144
).toBe<ProbeStatus>('empty')
@@ -156,19 +172,13 @@ describe('bazel-repo-discovery', () => {
156172

157173
describe('probeCandidate', () => {
158174
it('returns the classified status from a probe', async () => {
159-
const probe: RepoProbe = async () => ({
160-
code: 0,
161-
stdout: '@maven//:guava\n',
162-
stderr: '',
163-
})
164-
expect(await probeCandidate('maven', probe)).toBe<ProbeStatus>('populated')
175+
expect(
176+
await probeCandidate('maven', probePopulatedGuava),
177+
).toBe<ProbeStatus>('populated')
165178
})
166179

167180
it('returns not-defined when the probe throws', async () => {
168-
const probe: RepoProbe = async () => {
169-
throw new Error('bazel exploded')
170-
}
171-
expect(await probeCandidate('crash', probe)).toBe<ProbeStatus>(
181+
expect(await probeCandidate('crash', probeThrows)).toBe<ProbeStatus>(
172182
'not-defined',
173183
)
174184
})
@@ -204,30 +214,17 @@ describe('bazel-repo-discovery', () => {
204214
}
205215

206216
it('probeCandidate stays silent without verbose', async () => {
207-
const probe: RepoProbe = async () => ({
208-
code: 0,
209-
stdout: '@maven//:x\n',
210-
stderr: '',
211-
})
212-
await probeCandidate('maven', probe)
217+
await probeCandidate('maven', probePopulatedX)
213218
expect(logSpy).not.toHaveBeenCalled()
214219
})
215220

216221
it('probeCandidate logs the status under verbose', async () => {
217-
const probe: RepoProbe = async () => ({
218-
code: 0,
219-
stdout: '@maven//:x\n',
220-
stderr: '',
221-
})
222-
await probeCandidate('maven', probe, true)
222+
await probeCandidate('maven', probePopulatedX, true)
223223
expect(loggedLines()).toMatch(/probe @maven:\s*populated/)
224224
})
225225

226226
it('probeCandidate logs the throw reason under verbose', async () => {
227-
const probe: RepoProbe = async () => {
228-
throw new Error('bazel exploded')
229-
}
230-
await probeCandidate('crash', probe, true)
227+
await probeCandidate('crash', probeThrows, true)
231228
expect(loggedLines()).toMatch(
232229
/probe @crash:\s*not-defined \(probe threw: bazel exploded\)/,
233230
)

src/commands/manifest/bazel/bazel-workspace-walk.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import path from 'node:path'
2020

2121
import { logger } from '@socketsecurity/registry/lib/logger'
2222

23-
// Hard ceiling on number of workspace roots we will surface. Real monorepos
24-
// have well under 50; this cap is a guard against pathological inputs.
25-
const MAX_WORKSPACE_ROOTS = 256
23+
// Hard ceiling on workspace roots; 16 sits well above realistic monorepo counts while tightening the guard against pathological inputs.
24+
const MAX_WORKSPACE_ROOTS = 16
2625
// Hard ceiling on directory walk depth. Deepest workspace marker observed
2726
// across the OSS corpus surveyed is 9 (bazel-self test fixtures); deepest
2827
// in realistic application code is 7 (checkmk's thirdparty layout). Cap

src/commands/manifest/bazel/cmd-manifest-bazel.mts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ import { commonFlags } from '../../../flags.mts'
1010
import { checkCommandInput } from '../../../utils/check-input.mts'
1111
import { InputError } from '../../../utils/errors.mts'
1212
import { getOutputKind } from '../../../utils/get-output-kind.mts'
13+
import { IGNORED_DIRS } from '../../../utils/glob.mts'
1314
import { meowOrExit } from '../../../utils/meow-with-subcommands.mts'
1415
import { getFlagListOutput } from '../../../utils/output-formatting.mts'
1516
import { readOrDefaultSocketJson } from '../../../utils/socket-json.mts'
1617

18+
// Walker prune policy for `socket manifest bazel`. Combines the
19+
// codebase-wide ignore list with VCS/IDE dirs and the walker's own
20+
// output dir, and Bazel's `bazel-*` output_base symlinks (prefix).
21+
const BAZEL_WALKER_IGNORE_DIR_NAMES: ReadonlySet<string> = new Set([
22+
...IGNORED_DIRS,
23+
'.hg',
24+
'.idea',
25+
'.pnpm-store',
26+
'.socket-auto-manifest',
27+
'.svn',
28+
'.vscode',
29+
])
30+
const BAZEL_WALKER_IGNORE_DIR_PREFIXES: readonly string[] = ['bazel-']
31+
1732
import type {
1833
CliCommandConfig,
1934
CliCommandContext,
@@ -303,6 +318,8 @@ async function run(
303318
bazelRc: bazelRc as string | undefined,
304319
bin: bazel as string | undefined,
305320
cwd,
321+
ignoreDirNames: BAZEL_WALKER_IGNORE_DIR_NAMES,
322+
ignoreDirPrefixes: BAZEL_WALKER_IGNORE_DIR_PREFIXES,
306323
out: out as string,
307324
verbose: Boolean(verbose),
308325
})

0 commit comments

Comments
 (0)