diff --git a/.gitignore b/.gitignore
index 24b7ae1f1..93d4b0b00 100644
--- a/.gitignore
+++ b/.gitignore
@@ -30,6 +30,7 @@
!/assets/fleet/
!/bin/
!/docs/
+!/docs/dynamic-sbom-inference-settings.md
!/patches/
!/scripts/
!/scripts/build-package-matrix.mts
@@ -556,6 +557,8 @@
!/src/commands/manifest/bazel/extract_bazel_to_pypi.constructed.test.mts
!/src/commands/manifest/bazel/extract_bazel_to_pypi.mts
!/src/commands/manifest/bazel/extract_bazel_to_pypi.test.mts
+!/src/commands/manifest/build-root-config.mts
+!/src/commands/manifest/build-root-config.test.mts
!/src/commands/manifest/cmd-manifest-auto.mts
!/src/commands/manifest/cmd-manifest-auto.test.mts
!/src/commands/manifest/cmd-manifest-cdxgen.mts
@@ -603,6 +606,7 @@
!/src/commands/manifest/handle-manifest-dynamic-sbom-inference.test.mts
!/src/commands/manifest/handle-manifest-setup.mts
!/src/commands/manifest/init.gradle
+!/src/commands/manifest/load-build.mts
!/src/commands/manifest/manifest-flags.mts
!/src/commands/manifest/output-manifest-dynamic-sbom-inference.mts
!/src/commands/manifest/output-manifest-setup.mts
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 079119e4b..07b54ebbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,7 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
## [Unreleased]
+### Added
+- `socket manifest setup --dynamic-sbom-inference` can skip any build: one Socket can load is skipped together with its modules, leaving the builds beside and beneath it alone. The setup shows the files it will write before writing them.
+- Configure or skip one build of a directory on its own, with `socket.json` settings under `defaults.manifest.builds` keyed by its build file name.
+
+### Changed
+- `socket.json` settings apply from every directory above a scan's starting point, not only from that directory down.
+- `--dynamic-sbom-inference` builds a module that lives outside its reactor's directory (such as Maven's `../core`) only inside that reactor, instead of also building it on its own.
+- A build that fails to generate Socket facts now stops only the builds in and beneath its directory, and its failure is ignored when another build that includes it succeeds.
+
### Fixed
+- `--dynamic-sbom-inference` builds a Gradle or sbt build nested in another build on its own as well, so the projects only its own settings include are no longer missed.
- Maven Socket facts honor artifact types that build extensions define, so they no longer list dependencies Maven doesn't resolve, or fetch files under the wrong extension.
- Maven Socket facts no longer drop dependencies whose scope Maven 3 doesn't define, such as Maven 4's `compile-only` or `none`.
diff --git a/docs/dynamic-sbom-inference-settings.md b/docs/dynamic-sbom-inference-settings.md
new file mode 100644
index 000000000..4d28e1441
--- /dev/null
+++ b/docs/dynamic-sbom-inference-settings.md
@@ -0,0 +1,147 @@
+# Build settings for `--dynamic-sbom-inference`
+
+How to write the `socket.json` settings that `--dynamic-sbom-inference` reads,
+by hand or with an agent. `socket manifest setup --dynamic-sbom-inference`
+asks the same questions interactively and writes the same files.
+
+## What the scan does
+
+1. Finds every file that looks like a build: `pom.xml` and its variants
+ (`*-*.pom.xml`, `pom`), `.sln`, `.slnx`, `.csproj`, `.fsproj`, `.vbproj`,
+ `build.gradle(.kts)`, `settings.gradle(.kts)`, `build.sbt`. It honours
+ `.gitignore`, `socket.yml` ignores and `--exclude-paths`.
+2. Runs each build's own tool (Maven, Gradle, sbt, the .NET SDK) to resolve
+ its dependencies, unless another build already loads it. A Maven reactor
+ builds its modules, including ones outside its directory such as
+ `../core`; a Gradle or sbt build builds its subprojects.
+3. Fails if any build it runs fails, naming the build.
+
+Settings tell the build tools how to run each build, and which builds not to
+run.
+
+## Prefer builds Socket can load
+
+Socket learns what a build includes only by letting its tool load it: Maven
+profiles, Gradle settings scripts and sbt definitions are code. When a build
+loads:
+
+- each module is built once, inside the build that owns it;
+- skipping the build skips exactly it and its modules, wherever they are, and
+ leaves the builds next to and beneath it alone.
+
+So fix a build that fails to load (usually a JDK, build tool or option) before
+reaching for a skip.
+
+## Where settings live
+
+- Any directory may hold a `socket.json`. Its settings apply to that directory
+ and everything beneath it.
+- Every `socket.json` from the filesystem root down to a build's directory
+ applies; for each field the nearest one wins. `null` clears an inherited
+ value.
+- `defaults.manifest.builds` in a directory's `socket.json` holds settings for
+ one build of that directory, keyed by its build file name. They apply after
+ all directory settings, to that build alone.
+
+```json
+{
+ "version": 1,
+ "defaults": {
+ "manifest": {
+ "maven": { "javaHome": "$JAVA17_HOME" },
+ "builds": {
+ "pom.xml": { "disabled": true },
+ "tools-cli.pom.xml": { "mavenOpts": "-Pcli" }
+ }
+ }
+ }
+}
+```
+
+A Gradle or sbt build is keyed by any one of its build files that exists
+(`settings.gradle`, `build.gradle.kts`, `build.sbt`, ...); two entries for the
+same build are an error.
+
+## Fields
+
+Under `defaults.manifest.maven`, `.gradle`, `.sbt` or `.dotnet`, or inside a
+`builds` entry:
+
+| Field | Meaning |
+|---|---|
+| `bin` | Build tool command. Relative paths are relative to the build's directory. Defaults to the project's wrapper (`mvnw`, `gradlew`) if present, else `mvn`, `gradle`, `sbt`, `dotnet`. |
+| `javaHome` | JDK for Maven, Gradle and sbt. `$VAR` and `${VAR}` expand from the environment. |
+| `mavenOpts`, `gradleOpts`, `sbtOpts`, `dotnetOpts` | Arguments passed to the build tool, for example profiles or `-p:Configuration=Release`. Maven's `-f`/`--file` is ignored: each POM is built by its own file. |
+| `includeConfigs`, `excludeConfigs` | Comma-separated globs over Maven scopes, Gradle and sbt configurations, or .NET target frameworks. |
+| `ignoreUnresolved` | `true` warns about dependencies that don't resolve instead of failing. |
+| `disabled` | `true` skips builds; see below. |
+| `facts` | Gradle and sbt: `false` skips the build. |
+| `verbose` | `true` streams the build tool's output. |
+
+## Skipping builds
+
+| To skip | Set | Effect |
+|---|---|---|
+| One build and what it loads | `builds[""].disabled: true` in its directory | Socket still loads the build, resolving nothing, to find its modules. If it can't load it, the scan fails. |
+| A directory and everything beneath it | `defaults.manifest..disabled: true` in that directory | Nothing is loaded. A nearer `socket.json` with `disabled: false` turns builds beneath it back on. |
+
+Don't use `--exclude-paths` to skip builds: it also removes those files from
+the scan and from reachability analysis.
+
+Socket can't skip a module of a build that runs: the build resolves its
+modules itself. Settings for such a module are ignored, with a warning.
+
+## Procedure for an agent
+
+1. Run `socket manifest dynamic-sbom-inference --json` from the
+ repository root. It runs every build and lists each with a status:
+
+ | Status | Meaning |
+ |---|---|
+ | `generated` | Built. |
+ | `empty` | Built; nothing to resolve. |
+ | `skippedCovered` | Built as part of another build. |
+ | `skippedDisabled` | Skipped by a setting, or loaded by a skipped build. |
+ | `failed` | Its build tool failed; the output above it says why. |
+ | `aborted` | Not run because a build in or above its directory failed. |
+
+2. For each `failed` build, read the build tool's error and fix the cause in
+ the nearest fitting `socket.json`: `javaHome` for a wrong JDK, `bin` for a
+ missing tool, `Opts` for missing profiles or properties.
+3. Skip a build only when it is not meant to be built (a test fixture, a
+ template, an abandoned project), using the table above. Prefer
+ `builds[""].disabled` when the build loads; use the directory form
+ only when it doesn't.
+4. Repeat until no build is `failed` or `aborted`.
+5. Commit the `socket.json` files.
+
+## Example
+
+```
+repo/
+ pom.xml reactor with modules api, web
+ codegen/
+ pom.xml not shipped: skip it
+ codegen-cli.pom.xml shipped
+ legacy/
+ pom.xml doesn't load any more, with modules old-core, old-ui
+ tool/pom.xml separate build, shipped
+```
+
+`codegen/socket.json` skips one of two builds in the directory:
+
+```json
+{ "version": 1, "defaults": { "manifest": { "builds": { "pom.xml": { "disabled": true } } } } }
+```
+
+`legacy/pom.xml` doesn't load, so `legacy/socket.json` skips the directory:
+
+```json
+{ "version": 1, "defaults": { "manifest": { "maven": { "disabled": true } } } }
+```
+
+and `legacy/tool/socket.json` turns the build beneath it back on:
+
+```json
+{ "version": 1, "defaults": { "manifest": { "maven": { "disabled": false } } } }
+```
diff --git a/src/commands/fix/coana-fix.mts b/src/commands/fix/coana-fix.mts
index 570b63302..df4472978 100644
--- a/src/commands/fix/coana-fix.mts
+++ b/src/commands/fix/coana-fix.mts
@@ -56,6 +56,7 @@ import { getPackageFilesForScan } from '../../utils/path-resolve.mts'
import { setupSdk } from '../../utils/sdk.mts'
import { excludePathToScanIgnores } from '../scan/exclude-paths.mts'
import { fetchSupportedScanFileNames } from '../scan/fetch-supported-scan-file-names.mts'
+import { withoutDiscardedFacts } from '../scan/run-dynamic-sbom-inference.mts'
import type { GeneratedSocketFacts } from './generated-socket-facts.mts'
import type { FixConfig } from './types.mts'
@@ -390,7 +391,7 @@ async function coanaFixWithFacts(
outputPath: DOT_SOCKET_DOT_FACTS_JSON,
}),
)
- const scanFilepaths = await findScanFilepaths()
+ let scanFilepaths = await findScanFilepaths()
if (factsSlot) {
if (!silence) {
spinner?.stop()
@@ -404,6 +405,10 @@ async function coanaFixWithFacts(
(await findScanFilepaths()).filter(isSocketFactsFile),
tmpDir: factsSlot.tmpDir,
})
+ scanFilepaths = await withoutDiscardedFacts(
+ scanFilepaths,
+ factsSlot.generated.discardedPaths,
+ )
const known = new Set(scanFilepaths)
scanFilepaths.push(...factsSlot.generated.paths.filter(p => !known.has(p)))
if (!silence) {
diff --git a/src/commands/fix/generated-socket-facts.mts b/src/commands/fix/generated-socket-facts.mts
index 80d98a858..a6654b968 100644
--- a/src/commands/fix/generated-socket-facts.mts
+++ b/src/commands/fix/generated-socket-facts.mts
@@ -9,6 +9,8 @@ import type { PURL_Type } from '../../utils/ecosystem.mts'
export type GeneratedSocketFacts = {
// Coana's flags selecting what the facts files resolve.
artifactsFlags: string[]
+ // Facts files of dropped runs, which Coana must not read.
+ discardedPaths: string[]
paths: string[]
// The facts files' per-project classpaths, written outside the repository.
sidecarFile: string
@@ -60,18 +62,24 @@ export async function generateSocketFactsForFix({
await restoreAll(originals)
}
let artifactsFlags: string[]
+ let discardedPaths: string[]
let paths: string[]
const sidecarFile = path.join(tmpDir, 'sidecar.json')
try {
- const { factsOnlyTypes, factsPaths, resolvedPathsSidecar } =
- await runDynamicSbomInference({
- consumer: 'fix',
- cwd,
- excludePaths,
- purlTypes,
- sbtTmpDir: undefined,
- })
+ const {
+ discardedFactsPaths,
+ factsOnlyTypes,
+ factsPaths,
+ resolvedPathsSidecar,
+ } = await runDynamicSbomInference({
+ consumer: 'fix',
+ cwd,
+ excludePaths,
+ purlTypes,
+ sbtTmpDir: undefined,
+ })
artifactsFlags = useOnlySocketFactsArtifactsFlags(factsOnlyTypes)
+ discardedPaths = discardedFactsPaths
paths = factsPaths.map(p => path.resolve(cwd, p))
// Written even when empty, as for reach; applying fixes looks each facts
// file up in it, so an empty one is the same as none.
@@ -88,9 +96,11 @@ export async function generateSocketFactsForFix({
]
return {
artifactsFlags,
+ discardedPaths,
paths,
sidecarFile,
- remove: () => reset(paths),
+ // A discarded file a run left as written is removed too.
+ remove: () => reset([...paths, ...discardedPaths]),
restore: () => restoreAll(backups),
}
}
diff --git a/src/commands/fix/generated-socket-facts.test.mts b/src/commands/fix/generated-socket-facts.test.mts
index 02c4e7b3a..f395c62ae 100644
--- a/src/commands/fix/generated-socket-facts.test.mts
+++ b/src/commands/fix/generated-socket-facts.test.mts
@@ -46,7 +46,11 @@ describe('generateSocketFactsForFix', () => {
mockRunDynamicSbomInference.mockImplementation(async () => {
await fs.writeFile(existing, 'regenerated')
await fs.writeFile(fresh, 'generated')
- return { factsOnlyTypes: ['maven'], factsPaths: [existing, fresh] }
+ return {
+ discardedFactsPaths: [],
+ factsOnlyTypes: ['maven'],
+ factsPaths: [existing, fresh],
+ }
})
const generated = await generate()
@@ -63,10 +67,29 @@ describe('generateSocketFactsForFix', () => {
expect(existsSync(fresh)).toBe(false)
})
+ it('removes a discarded facts file a dropped run left behind', async () => {
+ const discarded = path.join(cwd, 'sub', 'pom.xml.socket.facts.json')
+ mockRunDynamicSbomInference.mockImplementation(async () => {
+ await fs.writeFile(discarded, 'dropped')
+ return {
+ discardedFactsPaths: [discarded, existing],
+ factsOnlyTypes: ['maven'],
+ factsPaths: [],
+ }
+ })
+
+ const generated = await generate()
+ await generated.remove()
+
+ expect(existsSync(discarded)).toBe(false)
+ expect(await fs.readFile(existing, 'utf8')).toBe('original')
+ })
+
it('writes the sidecar outside the repository', async () => {
mockRunDynamicSbomInference.mockImplementation(async () => {
await fs.writeFile(fresh, 'generated')
return {
+ discardedFactsPaths: [],
factsOnlyTypes: ['maven', 'nuget'],
factsPaths: [fresh],
resolvedPathsSidecar: { [fresh]: { components: [], projects: [] } },
@@ -90,6 +113,7 @@ describe('generateSocketFactsForFix', () => {
it('writes an empty sidecar when no build root resolved any paths', async () => {
mockRunDynamicSbomInference.mockResolvedValue({
+ discardedFactsPaths: [],
factsOnlyTypes: ['maven'],
factsPaths: [],
})
@@ -103,7 +127,11 @@ describe('generateSocketFactsForFix', () => {
it('restores an existing facts file that generation does not rewrite', async () => {
mockRunDynamicSbomInference.mockImplementation(async () => {
await fs.writeFile(fresh, 'generated')
- return { factsOnlyTypes: ['maven'], factsPaths: [fresh] }
+ return {
+ discardedFactsPaths: [],
+ factsOnlyTypes: ['maven'],
+ factsPaths: [fresh],
+ }
})
const generated = await generate()
diff --git a/src/commands/manifest/README.md b/src/commands/manifest/README.md
index 63527aa03..9080a9752 100644
--- a/src/commands/manifest/README.md
+++ b/src/commands/manifest/README.md
@@ -167,6 +167,11 @@ Generates a manifest file (`pom.xml`) from Scala's `build.sbt` file.
Starts an interactive configurator that writes default flag values for
`socket manifest` into a `socket.json` in the current directory.
+With `--dynamic-sbom-inference`, it walks every build beneath the current
+directory and writes the settings for each. The settings themselves are
+documented in [`docs/dynamic-sbom-inference-settings.md`](../../../docs/dynamic-sbom-inference-settings.md),
+for anyone, or any agent, writing them without the configurator.
+
## Dev
Run it like these examples:
diff --git a/src/commands/manifest/build-root-config.mts b/src/commands/manifest/build-root-config.mts
new file mode 100644
index 000000000..d8d3ec301
--- /dev/null
+++ b/src/commands/manifest/build-root-config.mts
@@ -0,0 +1,232 @@
+import { existsSync } from 'node:fs'
+import path from 'node:path'
+
+import { logger } from '@socketsecurity/registry/lib/logger'
+import { isObjectObject } from '@socketsecurity/registry/lib/objects'
+
+import { buildRootPath, isBuildFile } from './discover-manifest-roots.mts'
+import { BUILD_FILES, BUILD_TOOLS } from './scripts/build-tool.mts'
+import { SOCKET_JSON } from '../../constants.mts'
+import { InputError } from '../../utils/errors.mts'
+import { getOrInsert } from '../../utils/get-or-insert.mts'
+import {
+ mergeSocketJsonChain,
+ readSocketJsonChain,
+} from '../../utils/socket-json.mts'
+
+import type { BuildRoot } from './discover-manifest-roots.mts'
+import type { BuildTool } from './scripts/build-tool.mts'
+import type {
+ SocketJson,
+ SocketJsonBuildEntry,
+ SocketJsonInDir,
+} from '../../utils/socket-json.mts'
+
+// The setting that makes the effective `disabled` true.
+export type DisabledBy = {
+ // The directory of the socket.json that sets it.
+ dir: string
+ // The `defaults.manifest.builds` key, when it is the build's own setting.
+ key?: string | undefined
+}
+
+export type ResolvedBuildRootSettings = {
+ disabledBy: DisabledBy | undefined
+ // The key of the build's own `defaults.manifest.builds` entry.
+ entryKey: string | undefined
+ // The cascaded settings, the build's own entries applied last.
+ sockJson: SocketJson
+}
+
+// Gradle and sbt name a build by any build file of its directory.
+function namesBuildFile(key: string): boolean {
+ return BUILD_TOOLS.some(tool => {
+ const files = BUILD_FILES[tool]
+ return files.byFile ? isBuildFile(tool, key) : files.names.includes(key)
+ })
+}
+
+// The `builds` keys naming an existing build file of `tool`'s build in `dir`:
+// its entry file, or any of a Gradle or sbt directory's build files.
+function ownEntryKeys(
+ tool: BuildTool,
+ dir: string,
+ builds: object,
+ entryFile?: string | undefined,
+): string[] {
+ const files = BUILD_FILES[tool]
+ if (files.byFile) {
+ return entryFile && Object.hasOwn(builds, entryFile) ? [entryFile] : []
+ }
+ return files.names.filter(
+ name => Object.hasOwn(builds, name) && existsSync(path.join(dir, name)),
+ )
+}
+
+function validateBuilds({ dir, sockJson }: SocketJsonInDir): void {
+ const builds: unknown = sockJson.defaults?.manifest?.builds
+ if (builds === undefined) {
+ return
+ }
+ const file = socketJsonPath(dir)
+ if (!isObjectObject(builds)) {
+ throw new InputError(
+ `defaults.manifest.builds in ${file} must be an object keyed by build file name.`,
+ )
+ }
+ for (const { 0: key, 1: entry } of Object.entries(builds)) {
+ if (key.includes('/') || key.includes('\\') || !namesBuildFile(key)) {
+ throw new InputError(
+ `${buildsSetting(key)} in ${file} must be keyed by the name of a build file in that directory, such as pom.xml or build.gradle.`,
+ )
+ }
+ if (!isObjectObject(entry)) {
+ throw new InputError(
+ `${buildsSetting(key)} in ${file} must be an object.`,
+ )
+ }
+ if (
+ entry['disabled'] !== undefined &&
+ typeof entry['disabled'] !== 'boolean'
+ ) {
+ throw new InputError(
+ `${buildsSetting(key)}.disabled in ${file} must be true or false.`,
+ )
+ }
+ }
+ for (const tool of BUILD_TOOLS) {
+ const keys = ownEntryKeys(tool, dir, builds)
+ if (keys.length > 1) {
+ throw new InputError(
+ `${keys.map(buildsSetting).join(' and ')} in ${file} configure the same ${tool} build. Keep one of them.`,
+ )
+ }
+ }
+}
+
+function warnMissingFiles(
+ { dir, sockJson }: SocketJsonInDir,
+ warned: Set,
+): void {
+ for (const key of Object.keys(sockJson.defaults?.manifest?.builds ?? {})) {
+ const file = path.join(dir, key)
+ if (!warned.has(file) && !existsSync(file)) {
+ warned.add(file)
+ logger.warn(
+ `${buildsSetting(key)} in ${socketJsonPath(dir)} names a file that is not there, so it applies to no build.`,
+ )
+ }
+ }
+}
+
+export function buildsSetting(key: string): string {
+ return `defaults.manifest.builds[${JSON.stringify(key)}]`
+}
+
+// Resolves the socket.json settings of build roots once, reading and
+// validating each socket.json once. `readChain` lists a directory's socket.json files,
+// farthest first; resolvers sharing `warned` warn about a file once.
+export function createBuildRootSettingsResolver(
+ readChain: (dir: string) => SocketJsonInDir[] = readSocketJsonChain,
+ warned: Set = new Set(),
+): (tool: BuildTool, root: BuildRoot) => ResolvedBuildRootSettings {
+ const chains = new Map()
+ const resolved = new Map()
+ const validated = new Set()
+
+ const chainFor = (dir: string): SocketJsonInDir[] => {
+ let chain = chains.get(dir)
+ if (!chain) {
+ chain = readChain(dir)
+ for (const file of chain) {
+ if (!validated.has(file.dir)) {
+ validated.add(file.dir)
+ validateBuilds(file)
+ warnMissingFiles(file, warned)
+ }
+ }
+ chains.set(dir, chain)
+ }
+ return chain
+ }
+
+ const resolve = (
+ tool: BuildTool,
+ root: BuildRoot,
+ ): ResolvedBuildRootSettings => {
+ const chain = chainFor(root.dir)
+ const merged = mergeSocketJsonChain(chain)
+ const section: Record = {
+ ...merged.defaults?.manifest?.[tool],
+ }
+ let sectionDisabledBy: DisabledBy | undefined
+ for (const { dir, sockJson } of chain) {
+ if (sockJson.defaults?.manifest?.[tool]?.disabled !== undefined) {
+ sectionDisabledBy = { dir }
+ }
+ }
+ const directorySkip = section['disabled'] === true
+
+ const own = chain.at(-1)
+ const builds =
+ own?.dir === root.dir
+ ? own.sockJson.defaults?.manifest?.builds
+ : undefined
+ // Validation leaves at most one.
+ const entryKey = builds
+ ? ownEntryKeys(tool, root.dir, builds, root.entryFile)[0]
+ : undefined
+ let entryDisabledBy: DisabledBy | undefined
+ if (entryKey) {
+ const entry = builds![entryKey] as SocketJsonBuildEntry
+ Object.assign(section, entry)
+ if (entry.disabled !== undefined) {
+ entryDisabledBy = { dir: root.dir, key: entryKey }
+ }
+ }
+
+ // An entry can turn a build in a skipped directory back on; skipping it
+ // there too changes nothing, as nothing in the directory is loaded.
+ let disabledBy: DisabledBy | undefined
+ if (section['disabled']) {
+ disabledBy = directorySkip ? sectionDisabledBy : entryDisabledBy
+ }
+
+ return {
+ disabledBy,
+ entryKey,
+ sockJson: {
+ ...merged,
+ defaults: {
+ ...merged.defaults,
+ manifest: { ...merged.defaults?.manifest, [tool]: section },
+ },
+ } as SocketJson,
+ }
+ }
+ return (tool, root) =>
+ getOrInsert(resolved, `${tool}\0${buildRootPath(root)}`, () =>
+ resolve(tool, root),
+ )
+}
+
+// The setting that skips builds, as ` sets `.
+export function describeDisabledBy(
+ tool: BuildTool,
+ { dir, key }: DisabledBy,
+): string {
+ const setting = key
+ ? `${buildsSetting(key)}.disabled`
+ : `defaults.manifest.${tool}.disabled`
+ return `${socketJsonPath(dir)} sets ${setting}`
+}
+
+// Relative to the user's shell when inside it, else absolute.
+export function displayPath(file: string): string {
+ const rel = path.relative(process.cwd(), file)
+ return rel.startsWith('..') || path.isAbsolute(rel) ? file : rel || '.'
+}
+
+export function socketJsonPath(dir: string): string {
+ return displayPath(path.join(dir, SOCKET_JSON))
+}
diff --git a/src/commands/manifest/build-root-config.test.mts b/src/commands/manifest/build-root-config.test.mts
new file mode 100644
index 000000000..2126a8075
--- /dev/null
+++ b/src/commands/manifest/build-root-config.test.mts
@@ -0,0 +1,244 @@
+import { promises as fs } from 'node:fs'
+import { tmpdir } from 'node:os'
+import path from 'node:path'
+
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { logger } from '@socketsecurity/registry/lib/logger'
+
+import {
+ createBuildRootSettingsResolver,
+ describeDisabledBy,
+ displayPath,
+} from './build-root-config.mts'
+import { InputError } from '../../utils/errors.mts'
+
+describe('createBuildRootSettingsResolver', () => {
+ let scanRoot = ''
+
+ const write = async (
+ rel: string,
+ manifest: unknown,
+ files: string[] = [],
+ ) => {
+ const dir = path.join(scanRoot, rel)
+ await fs.mkdir(dir, { recursive: true })
+ await fs.writeFile(
+ path.join(dir, 'socket.json'),
+ JSON.stringify({ version: 1, defaults: { manifest } }),
+ )
+ for (const file of files) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(path.join(dir, file), '')
+ }
+ }
+ const maven = (dir: string, entryFile = 'pom.xml') => ({
+ dir: path.join(scanRoot, dir),
+ entryFile,
+ })
+
+ beforeEach(async () => {
+ scanRoot = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'build-root-config-')),
+ )
+ })
+ afterEach(async () => {
+ await fs.rm(scanRoot, { recursive: true, force: true })
+ })
+
+ it('resolves the cascaded sections alone when the directory has no entry for the build', async () => {
+ await write('.', { maven: { mavenOpts: '-Proot', javaHome: '/jdk' } })
+ await write('svc', { maven: { mavenOpts: '-Psvc' } })
+
+ const resolved = createBuildRootSettingsResolver()('maven', maven('svc'))
+
+ expect(resolved.entryKey).toBeUndefined()
+ expect(resolved.sockJson.defaults?.manifest?.maven).toEqual({
+ javaHome: '/jdk',
+ mavenOpts: '-Psvc',
+ })
+ })
+
+ it("applies a directory's entry to the build of that file only, after every section", async () => {
+ await write('.', { maven: { javaHome: '/jdk-17' } })
+ await write(
+ 'svc',
+ {
+ maven: { mavenOpts: '-B' },
+ builds: { 'svc-alt.pom.xml': { mavenOpts: '-Palt' } },
+ },
+ ['pom.xml', 'svc-alt.pom.xml'],
+ )
+ const resolve = createBuildRootSettingsResolver()
+
+ expect(
+ resolve('maven', maven('svc')).sockJson.defaults?.manifest?.maven,
+ ).toEqual({ javaHome: '/jdk-17', mavenOpts: '-B' })
+ const alt = resolve('maven', maven('svc', 'svc-alt.pom.xml'))
+ expect(alt.entryKey).toBe('svc-alt.pom.xml')
+ expect(alt.sockJson.defaults?.manifest?.maven).toEqual({
+ javaHome: '/jdk-17',
+ mavenOpts: '-Palt',
+ })
+ })
+
+ it('names a Gradle or sbt build by any build file of its directory', async () => {
+ await write(
+ 'lib',
+ { builds: { 'settings.gradle.kts': { disabled: true } } },
+ ['settings.gradle.kts'],
+ )
+
+ const resolved = createBuildRootSettingsResolver()('gradle', {
+ dir: path.join(scanRoot, 'lib'),
+ })
+
+ expect(resolved.disabledBy).toEqual({
+ dir: path.join(scanRoot, 'lib'),
+ key: 'settings.gradle.kts',
+ })
+ })
+
+ it('applies no entry that names a missing build file of a Gradle build', async () => {
+ await write(
+ 'lib',
+ {
+ gradle: { javaHome: '/jdk-21' },
+ builds: { 'build.gradle': { disabled: true, javaHome: '/jdk-8' } },
+ },
+ ['build.gradle.kts'],
+ )
+
+ const resolved = createBuildRootSettingsResolver()('gradle', {
+ dir: path.join(scanRoot, 'lib'),
+ })
+
+ expect(resolved.disabledBy).toBeUndefined()
+ expect(resolved.entryKey).toBeUndefined()
+ expect(resolved.sockJson.defaults?.manifest?.gradle).toEqual({
+ javaHome: '/jdk-21',
+ })
+ })
+
+ it('rejects two entries that name the same Gradle build', async () => {
+ await write(
+ 'lib',
+ {
+ builds: {
+ 'build.gradle': { disabled: true },
+ 'settings.gradle': { disabled: false },
+ },
+ },
+ ['build.gradle', 'settings.gradle'],
+ )
+
+ expect(() =>
+ createBuildRootSettingsResolver()('gradle', {
+ dir: path.join(scanRoot, 'lib'),
+ }),
+ ).toThrow(/configure the same gradle build/)
+ })
+
+ it('ignores the entries of other directories, above or below', async () => {
+ await write('.', { builds: { 'pom.xml': { disabled: true } } }, ['pom.xml'])
+ await write('svc', {}, ['pom.xml'])
+
+ expect(
+ createBuildRootSettingsResolver()('maven', maven('svc')).disabledBy,
+ ).toBeUndefined()
+ })
+
+ it('tells a skip of the build itself from a skip of its directory', async () => {
+ await write('legacy', { maven: { disabled: true } }, ['pom.xml'])
+ await write('legacy/tool', {}, ['pom.xml'])
+ await write('app', { builds: { 'pom.xml': { disabled: true } } }, [
+ 'pom.xml',
+ ])
+ const resolve = createBuildRootSettingsResolver()
+
+ const tool = resolve('maven', maven('legacy/tool')).disabledBy!
+ expect(tool).toEqual({ dir: path.join(scanRoot, 'legacy') })
+ expect(describeDisabledBy('maven', tool)).toBe(
+ `${displayPath(path.join(scanRoot, 'legacy/socket.json'))} sets defaults.manifest.maven.disabled`,
+ )
+ const app = resolve('maven', maven('app')).disabledBy!
+ expect(describeDisabledBy('maven', app)).toBe(
+ `${displayPath(path.join(scanRoot, 'app/socket.json'))} sets defaults.manifest.builds["pom.xml"].disabled`,
+ )
+ })
+
+ it('turns one build back on under a skipped directory through its entry', async () => {
+ await write(
+ 'legacy',
+ {
+ maven: { disabled: true },
+ builds: { 'legacy-keep.pom.xml': { disabled: false } },
+ },
+ ['pom.xml', 'legacy-keep.pom.xml'],
+ )
+ const resolve = createBuildRootSettingsResolver()
+
+ expect(resolve('maven', maven('legacy')).disabledBy).toBeDefined()
+ expect(
+ resolve('maven', maven('legacy', 'legacy-keep.pom.xml')).disabledBy,
+ ).toBeUndefined()
+ })
+
+ it.for([
+ ['svc/pom.xml', 'must be keyed by the name of a build file'],
+ ['README.md', 'must be keyed by the name of a build file'],
+ ['..', 'must be keyed by the name of a build file'],
+ ] as const)('rejects the key %j', async ({ 0: key, 1: reason }) => {
+ await write('.', { builds: { [key]: { disabled: true } } })
+ const root = maven('.')
+
+ expect(() => createBuildRootSettingsResolver()('maven', root)).toThrow(
+ InputError,
+ )
+ expect(() => createBuildRootSettingsResolver()('maven', root)).toThrow(
+ expect.objectContaining({ message: expect.stringContaining(reason) }),
+ )
+ })
+
+ it.for([
+ [{ 'pom.xml': true }, 'must be an object'],
+ [{ 'pom.xml': { disabled: 'yes' } }, 'must be true or false'],
+ ] as const)('rejects the entries %j', async ({ 0: builds, 1: reason }) => {
+ await write('.', { builds }, ['pom.xml'])
+
+ expect(() =>
+ createBuildRootSettingsResolver()('maven', maven('.')),
+ ).toThrow(
+ expect.objectContaining({ message: expect.stringContaining(reason) }),
+ )
+ })
+
+ it('warns once about an entry for a file that is not there', async () => {
+ await write('.', { builds: { 'old-gone.pom.xml': { disabled: true } } })
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+ try {
+ const resolve = createBuildRootSettingsResolver()
+ resolve('maven', maven('.'))
+ resolve('maven', maven('.', 'app-other.pom.xml'))
+
+ expect(warnSpy).toHaveBeenCalledTimes(1)
+ expect(String(warnSpy.mock.calls[0]?.[0])).toContain(
+ 'defaults.manifest.builds["old-gone.pom.xml"]',
+ )
+ } finally {
+ warnSpy.mockRestore()
+ }
+ })
+
+ it('keeps a directory skip a directory skip when a build there is also skipped by name', async () => {
+ await write(
+ 'legacy',
+ { maven: { disabled: true }, builds: { 'pom.xml': { disabled: true } } },
+ ['pom.xml'],
+ )
+
+ expect(
+ createBuildRootSettingsResolver()('maven', maven('legacy')).disabledBy,
+ ).toEqual({ dir: path.join(scanRoot, 'legacy') })
+ })
+})
diff --git a/src/commands/manifest/convert-dotnet-to-facts.mts b/src/commands/manifest/convert-dotnet-to-facts.mts
index 79150ba4b..d4fcd5a77 100644
--- a/src/commands/manifest/convert-dotnet-to-facts.mts
+++ b/src/commands/manifest/convert-dotnet-to-facts.mts
@@ -1,7 +1,10 @@
import { logger } from '@socketsecurity/registry/lib/logger'
import { findDotnetEntries } from './detect-manifest-actions.mts'
-import { realpathOrResolved } from './discover-manifest-roots.mts'
+import {
+ emptyMembership,
+ realpathOrResolved,
+} from './discover-manifest-roots.mts'
import { buildUncoveredRoot } from './generate-recursive-manifests.mts'
import type { EcosystemBuildConfig } from './generate-recursive-manifests.mts'
@@ -39,7 +42,7 @@ export async function convertDotnetToFacts({
return null
}
const dir = await realpathOrResolved(cwd)
- const covered = new Set()
+ const covered = emptyMembership()
let last: RunManifestFactsOutcome
for (const entry of entries) {
// eslint-disable-next-line no-await-in-loop
diff --git a/src/commands/manifest/discover-manifest-roots.mts b/src/commands/manifest/discover-manifest-roots.mts
index 607c33e42..73636c0fc 100644
--- a/src/commands/manifest/discover-manifest-roots.mts
+++ b/src/commands/manifest/discover-manifest-roots.mts
@@ -14,6 +14,7 @@ import {
} from './scripts/build-tool.mts'
import { listDotnetEntryProjects } from './scripts/run.mts'
import { realpathOrResolved } from '../../utils/fs.mts'
+import { getOrInsert } from '../../utils/get-or-insert.mts'
import { globWithGitIgnore } from '../../utils/glob.mts'
import { excludePathToScanIgnores } from '../scan/exclude-paths.mts'
@@ -98,51 +99,108 @@ async function realFilePath(file: string): Promise {
)
}
-// Gradle reports a project's build file even when absent, which covers
-// nothing. A sibling outside the build root is independently locatable,
-// unless its projects resolve alone.
-export async function addCoveredBuildFiles(
- covered: Set,
- tool: BuildTool,
+// What a build loaded: its projects' directories and build files,
+// realpath-resolved. Gradle reports a project's build file even when absent,
+// which names nothing.
+export type Membership = { dirs: Set; files: Set }
+
+export function emptyMembership(): Membership {
+ return { dirs: new Set(), files: new Set() }
+}
+
+export function addMembership(into: Membership, from: Membership): void {
+ for (const dir of from.dirs) {
+ into.dirs.add(dir)
+ }
+ for (const file of from.files) {
+ into.files.add(file)
+ }
+}
+
+export function unionMemberships(
+ memberships: Iterable,
+): Membership {
+ const union = emptyMembership()
+ for (const membership of memberships) {
+ addMembership(union, membership)
+ }
+ return union
+}
+
+export function isPathInside(dir: string, file: string): boolean {
+ return file === dir || file.startsWith(`${dir}${path.sep}`)
+}
+
+// Paths are relative to `buildRoot`. Only what lies inside `scanRoot` (when
+// given) counts, as only that can be a candidate.
+export async function membershipOf(
buildRoot: string,
projects: readonly SocketFactsSbomProject[],
-): Promise {
- const buildFiles = BUILD_FILES[tool]
- const anywhere = buildFiles.byFile && !!buildFiles.projectsResolveAlone
- const realBuildRoot = await realpathOrResolved(buildRoot)
- const files = await Promise.all(
- projects
- .flatMap(project => project.manifestFiles ?? [])
- .map(({ file }) => realFilePath(path.resolve(buildRoot, file))),
- )
+ scanRoot?: string | undefined,
+): Promise {
+ const realScanRoot = scanRoot ? await realpathOrResolved(scanRoot) : undefined
+ const inScan = (p: string) => !realScanRoot || isPathInside(realScanRoot, p)
+ const membership = emptyMembership()
+ const [dirs, files] = await Promise.all([
+ Promise.all(
+ projects.map(({ subprojectDir }) =>
+ realpathOrResolved(path.resolve(buildRoot, subprojectDir)),
+ ),
+ ),
+ Promise.all(
+ projects
+ .flatMap(project => project.manifestFiles ?? [])
+ .map(({ file }) => realFilePath(path.resolve(buildRoot, file))),
+ ),
+ ])
+ for (const dir of dirs) {
+ if (inScan(dir)) {
+ membership.dirs.add(dir)
+ }
+ }
for (const file of files) {
- if (
- (anywhere || file.startsWith(`${realBuildRoot}${path.sep}`)) &&
- existsSync(file)
- ) {
- covered.add(file)
+ if (inScan(file) && existsSync(file)) {
+ membership.files.add(file)
}
}
+ return membership
}
-// `root.dir` must be realpath-resolved, as findBuildToolCandidates returns it.
+const rootMarkerByDir = new Map()
+
+export function hasRootMarker(tool: BuildTool, root: BuildRoot): boolean {
+ const files = BUILD_FILES[tool]
+ if (files.byFile) {
+ return false
+ }
+ return getOrInsert(rootMarkerByDir, `${tool}\0${root.dir}`, () =>
+ files.rootMarkers.some(marker => existsSync(path.join(root.dir, marker))),
+ )
+}
+
+// Whether a build loaded `root`'s project; a build of its own (see
+// hasRootMarker) is more than that project. `root.dir` must be
+// realpath-resolved, as findBuildToolCandidates returns it.
export function isBuildRootCovered(
- covered: ReadonlySet,
+ covered: Membership,
tool: BuildTool,
root: BuildRoot,
): boolean {
const files = BUILD_FILES[tool]
if (files.byFile) {
- return !!root.entryFile && covered.has(buildRootPath(root))
+ return !!root.entryFile && covered.files.has(buildRootPath(root))
}
- return files.names.some(name => covered.has(path.join(root.dir, name)))
+ return (
+ covered.dirs.has(root.dir) ||
+ files.names.some(name => covered.files.has(path.join(root.dir, name)))
+ )
}
// A dotnet solution is covered once every project it builds is, or, when the
// SDK cannot read it, by a same-named solution in the other format beside it
// (`dotnet sln migrate` writes App.slnx next to App.sln) that it can read.
async function isSolutionCovered(
- covered: ReadonlySet,
+ covered: Membership,
root: BuildRoot,
opts: Pick<
ManifestScriptOptions,
@@ -158,7 +216,7 @@ async function isSolutionCovered(
ext.toLowerCase() === '.sln' ? '.slnx' : '.sln',
)
const hasTwin = existsSync(path.join(root.dir, twin))
- if (!covered.size && !hasTwin) {
+ if (!covered.files.size && !hasTwin) {
return false
}
const list = (file: string) =>
@@ -174,14 +232,26 @@ async function isSolutionCovered(
)
return true
}
- const members = await Promise.all(listed.map(realFilePath))
- return !!members.length && members.every(member => covered.has(member))
+ const members = await realMembers(listed)
+ return !!members.length && members.every(member => covered.files.has(member))
+}
+
+// The SDK's member listings are memoized, so their resolved paths are too.
+const realMembersByListing = new WeakMap>()
+
+function realMembers(listed: readonly string[]): Promise {
+ let members = realMembersByListing.get(listed)
+ if (!members) {
+ members = Promise.all(listed.map(realFilePath))
+ realMembersByListing.set(listed, members)
+ }
+ return members
}
// Whether earlier builds cover `root`: its own build files, or every project
// a solution builds. isBuildRootCovered alone is the cheap first half.
export async function isRootCovered(
- covered: ReadonlySet,
+ covered: Membership,
tool: BuildTool,
root: BuildRoot,
opts: Pick<
@@ -197,6 +267,25 @@ export async function isRootCovered(
)
}
+// A build of its own (see hasRootMarker) is covered only by builds that load
+// all of `own`, what it loads itself.
+export async function isBuildCovered(
+ covered: Membership,
+ tool: BuildTool,
+ root: BuildRoot,
+ opts: Parameters[3],
+ own?: Membership | undefined,
+): Promise {
+ if (!hasRootMarker(tool, root)) {
+ return await isRootCovered(covered, tool, root, opts)
+ }
+ const pick = (membership: Membership) =>
+ BUILD_FILES[tool].byFile ? membership.files : membership.dirs
+ const members = own ? pick(own) : new Set()
+ const loaded = pick(covered)
+ return !!members.size && [...members].every(member => loaded.has(member))
+}
+
function compareStrings(a: string, b: string): number {
return a < b ? -1 : a > b ? 1 : 0
}
diff --git a/src/commands/manifest/discover-manifest-roots.test.mts b/src/commands/manifest/discover-manifest-roots.test.mts
index 81bc64af9..231085c52 100644
--- a/src/commands/manifest/discover-manifest-roots.test.mts
+++ b/src/commands/manifest/discover-manifest-roots.test.mts
@@ -7,14 +7,16 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import {
buildRootPath,
findBuildToolCandidates,
+ hasRootMarker,
isBuildRootCovered,
isRootCovered,
+ membershipOf,
sortByDepthThenPath,
} from './discover-manifest-roots.mts'
import { testPath } from '../../../test/utils.mts'
import { loadSocketConfig } from '../../utils/config.mts'
-import type { BuildRoot } from './discover-manifest-roots.mts'
+import type { BuildRoot, Membership } from './discover-manifest-roots.mts'
import type { BuildTool } from './scripts/build-tool.mts'
import type { PURL_Type } from '../../utils/ecosystem.mts'
import type { SocketJson } from '../../utils/socket-json.mts'
@@ -249,6 +251,10 @@ describe('findBuildToolCandidates', () => {
})
})
+function filesCovered(files: string[]): Membership {
+ return { dirs: new Set(), files: new Set(files) }
+}
+
describe('isBuildRootCovered', () => {
it.for([
[
@@ -280,9 +286,113 @@ describe('isBuildRootCovered', () => {
] as const)(
'%s: %s covers %o: %s',
({ 0: tool, 1: file, 2: root, 3: expected }) => {
- expect(isBuildRootCovered(new Set([file]), tool, root)).toBe(expected)
+ expect(isBuildRootCovered(filesCovered([file]), tool, root)).toBe(
+ expected,
+ )
},
)
+
+ it.for([
+ ['gradle', '/repo/lib', true],
+ ['sbt', '/repo/lib', true],
+ ['gradle', '/repo/lib/sub', false],
+ // A Maven build is addressed by its file.
+ ['maven', '/repo/lib', false],
+ ] as const)(
+ '%s: a project in %s covers /repo/lib: %s',
+ ({ 0: tool, 1: dir, 2: expected }) => {
+ const covered = { dirs: new Set([dir]), files: new Set() }
+ const root =
+ tool === 'maven'
+ ? { dir: '/repo/lib', entryFile: 'pom.xml' }
+ : { dir: '/repo/lib' }
+ expect(isBuildRootCovered(covered, tool, root)).toBe(expected)
+ },
+ )
+})
+
+function mavenProject(subprojectDir: string, file: string) {
+ return {
+ type: 'maven' as const,
+ name: subprojectDir,
+ id: subprojectDir,
+ subprojectDir,
+ manifestFiles: [{ file }],
+ children: [],
+ }
+}
+
+describe('membershipOf', () => {
+ let scanRoot = ''
+
+ beforeAll(async () => {
+ scanRoot = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'membership-')),
+ )
+ for (const dir of ['build/member', 'sibling']) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(path.join(scanRoot, dir), { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(path.join(scanRoot, dir, 'pom.xml'), '')
+ }
+ })
+
+ afterAll(async () => {
+ await fs.rm(scanRoot, { recursive: true, force: true })
+ })
+
+ it('takes project dirs and existing build files anywhere in the scan root, relative to the build root', async () => {
+ const membership = await membershipOf(
+ path.join(scanRoot, 'build'),
+ [
+ mavenProject('member', 'member/pom.xml'),
+ mavenProject('../sibling', '../sibling/pom.xml'),
+ mavenProject('absent', 'absent/build.gradle'),
+ mavenProject('../../outside', '../../outside/pom.xml'),
+ ],
+ scanRoot,
+ )
+ expect([...membership.files].sort()).toEqual([
+ path.join(scanRoot, 'build/member/pom.xml'),
+ path.join(scanRoot, 'sibling/pom.xml'),
+ ])
+ expect([...membership.dirs].sort()).toEqual([
+ path.join(scanRoot, 'build/absent'),
+ path.join(scanRoot, 'build/member'),
+ path.join(scanRoot, 'sibling'),
+ ])
+ })
+})
+
+describe('hasRootMarker', () => {
+ let dir = ''
+
+ beforeAll(async () => {
+ dir = await fs.mkdtemp(path.join(tmpdir(), 'root-marker-'))
+ await fs.mkdir(path.join(dir, 'gradle-build'))
+ await fs.writeFile(
+ path.join(dir, 'gradle-build', 'settings.gradle.kts'),
+ '',
+ )
+ await fs.mkdir(path.join(dir, 'gradle-project'))
+ await fs.writeFile(path.join(dir, 'gradle-project', 'build.gradle'), '')
+ await fs.mkdir(path.join(dir, 'sbt-build', 'project'), { recursive: true })
+ await fs.mkdir(path.join(dir, 'sbt-project'))
+ })
+
+ afterAll(async () => {
+ await fs.rm(dir, { recursive: true, force: true })
+ })
+
+ it.for([
+ ['gradle', 'gradle-build', true],
+ ['gradle', 'gradle-project', false],
+ ['sbt', 'sbt-build', true],
+ ['sbt', 'sbt-project', false],
+ ['maven', 'gradle-build', false],
+ ] as const)('%s at %s: %s', ({ 0: tool, 1: sub, 2: expected }) => {
+ expect(hasRootMarker(tool, { dir: path.join(dir, sub) })).toBe(expected)
+ })
})
describe('sortByDepthThenPath', () => {
@@ -352,7 +462,7 @@ describe('isRootCovered', () => {
covered: string[],
list: Parameters[4],
r: BuildRoot = root,
- ) => isRootCovered(new Set(covered), 'dotnet', r, {}, list)
+ ) => isRootCovered(filesCovered(covered), 'dotnet', r, {}, list)
it('covers a solution once every project it builds is covered', async () => {
const covered = ['/repo/src/A/A.csproj', '/repo/src/B/B.csproj']
@@ -372,7 +482,7 @@ describe('isRootCovered', () => {
).toBe(false)
expect(
await isRootCovered(
- new Set(['/x']),
+ filesCovered(['/x']),
'maven',
{ dir: '/repo', entryFile: 'pom.xml' },
{},
diff --git a/src/commands/manifest/enumerate-workspaces.mts b/src/commands/manifest/enumerate-workspaces.mts
index 04c79a982..87f838bef 100644
--- a/src/commands/manifest/enumerate-workspaces.mts
+++ b/src/commands/manifest/enumerate-workspaces.mts
@@ -4,6 +4,7 @@ import {
expandEnvVarRefs,
formatMissingEnvVarRefs,
} from './expand-env-var-refs.mts'
+import { tailBuildOutput } from './run-manifest-facts.mts'
import { enumerateWorkspaces as enumerateWorkspacesScript } from './scripts/run.mts'
import { getErrorMessageOr } from '../../utils/errors.mts'
@@ -15,10 +16,9 @@ export type EnumerateWorkspacesResult = {
projects: SocketFactsSbomProject[]
}
-// Cheap subproject discovery (no dependency resolution) for
-// `socket manifest setup --dynamic-sbom-inference`; dynamic-sbom-inference
-// itself instead gets this list for free as a side effect of its own full
-// facts run.
+// Cheap subproject discovery (no dependency resolution), for builds that are
+// not resolved: the setup wizard's, and skipped ones. Callers decide whether
+// a failure fails the command.
export async function enumerateWorkspaces({
bin,
buildOpts,
@@ -46,7 +46,6 @@ export async function enumerateWorkspaces({
if (javaHome) {
const expanded = expandEnvVarRefs(javaHome)
if (expanded.missing) {
- process.exitCode = 1
logger.fail(
`javaHome (\`${javaHome}\`) ${formatMissingEnvVarRefs(expanded.missing)}.`,
)
@@ -73,7 +72,6 @@ export async function enumerateWorkspaces({
try {
result = await enumerateWorkspacesScript(ecosystem, scriptOpts)
} catch (e) {
- process.exitCode = 1
logger.fail(
`Could not run the ${ecosystem} build tool: ${getErrorMessageOr(e, String(e))}`,
)
@@ -81,15 +79,19 @@ export async function enumerateWorkspaces({
}
if (!result.projects.length) {
- process.exitCode = 1
logger.fail(
`The ${ecosystem} build produced no workspace records (exit code ${result.code}); every build reports at least its own root project, so the enumeration task did not run.`,
)
+ const tail = verbose ? '' : tailBuildOutput(result.stdout, result.stderr)
+ if (tail) {
+ logger.group('Build output:')
+ logger.error(tail)
+ logger.groupEnd()
+ }
return
}
if (!result.buildRoot) {
- process.exitCode = 1
logger.fail(
`The ${ecosystem} build did not report its build root, so its projects' paths cannot be located.`,
)
diff --git a/src/commands/manifest/enumerate-workspaces.test.mts b/src/commands/manifest/enumerate-workspaces.test.mts
index 207da368f..28209f538 100644
--- a/src/commands/manifest/enumerate-workspaces.test.mts
+++ b/src/commands/manifest/enumerate-workspaces.test.mts
@@ -74,7 +74,7 @@ describe('enumerateWorkspaces', () => {
})
expect(result).toBeUndefined()
expect(enumerateWorkspacesScript).not.toHaveBeenCalled()
- expect(process.exitCode).toBe(1)
+ expect(process.exitCode).toBeUndefined()
})
it('leaves the environment untouched when javaHome is unset', async () => {
@@ -94,7 +94,7 @@ describe('enumerateWorkspaces', () => {
})
const result = await enumerateWorkspaces(baseArgs)
expect(result).toBeUndefined()
- expect(process.exitCode).toBe(1)
+ expect(process.exitCode).toBeUndefined()
})
it('fails even on a clean exit if no workspace records were produced - every real run reports at least its own root project', async () => {
@@ -107,7 +107,7 @@ describe('enumerateWorkspaces', () => {
})
const result = await enumerateWorkspaces(baseArgs)
expect(result).toBeUndefined()
- expect(process.exitCode).toBe(1)
+ expect(process.exitCode).toBeUndefined()
})
it('fails when the build did not report its build root', async () => {
@@ -117,6 +117,6 @@ describe('enumerateWorkspaces', () => {
})
const result = await enumerateWorkspaces(baseArgs)
expect(result).toBeUndefined()
- expect(process.exitCode).toBe(1)
+ expect(process.exitCode).toBeUndefined()
})
})
diff --git a/src/commands/manifest/generate-recursive-manifests.mts b/src/commands/manifest/generate-recursive-manifests.mts
index c9a815034..6bebf01b0 100644
--- a/src/commands/manifest/generate-recursive-manifests.mts
+++ b/src/commands/manifest/generate-recursive-manifests.mts
@@ -1,33 +1,55 @@
+import { existsSync, promises as fs } from 'node:fs'
+import { tmpdir } from 'node:os'
import path from 'node:path'
import { logger } from '@socketsecurity/registry/lib/logger'
import {
- addCoveredBuildFiles,
+ buildsSetting,
+ createBuildRootSettingsResolver,
+ describeDisabledBy,
+ displayPath,
+ socketJsonPath,
+} from './build-root-config.mts'
+import {
+ addMembership,
buildRootPath,
+ emptyMembership,
findBuildToolCandidates,
+ hasRootMarker,
+ isBuildCovered,
isBuildRootCovered,
+ isPathInside,
isRootCovered,
+ membershipOf,
realpathOrResolved,
+ unionMemberships,
withoutDisabledFlags,
} from './discover-manifest-roots.mts'
+import { loadBuild, withModules } from './load-build.mts'
import { parseBuildToolOpts } from './parse-build-tool-opts.mts'
import { runManifestFacts } from './run-manifest-facts.mts'
-import { resolveBuildToolBin } from './scripts/build-tool.mts'
+import {
+ BUILD_FILES,
+ resolveBuildToolBin,
+ socketFactsFileName,
+} from './scripts/build-tool.mts'
import {
DSI_SETUP_COMMAND,
REACH_CONTINUE_ON_INSTALL_ERRORS_FLAG,
} from './scripts/resolution-report-render.mts'
-import { SOCKET_JSON } from '../../constants.mts'
import { loadSocketConfig } from '../../utils/config.mts'
import { withTmpDir } from '../../utils/fs.mts'
-import {
- readOrDefaultSocketJson,
- readSocketJsonCascade,
-} from '../../utils/socket-json.mts'
+import { getOrInsert } from '../../utils/get-or-insert.mts'
+import { readOrDefaultSocketJson } from '../../utils/socket-json.mts'
-import type { BuildRoot } from './discover-manifest-roots.mts'
-import type { RunManifestFactsOutcome } from './run-manifest-facts.mts'
+import type { ResolvedBuildRootSettings } from './build-root-config.mts'
+import type { BuildRoot, Membership } from './discover-manifest-roots.mts'
+import type { LoadedBuild } from './load-build.mts'
+import type {
+ RunManifestFactsOutcome,
+ RunManifestFactsResult,
+} from './run-manifest-facts.mts'
import type { BuildTool } from './scripts/build-tool.mts'
import type {
BuildOption,
@@ -46,6 +68,9 @@ export type RecursiveManifestOutcomeStatus =
| 'skippedDisabled'
export type RecursiveManifestOutcome = BuildRoot & {
+ // The facts file of a run that was dropped, which no kept run wrote: it holds
+ // no kept build's facts, so it must not be collected.
+ discardedFactsPath?: string | undefined
ecosystem: BuildTool
factsPath?: string | undefined
status: RecursiveManifestOutcomeStatus
@@ -63,12 +88,6 @@ export type EcosystemBuildConfig = {
verbose: boolean
}
-// Relative to the user's shell when inside it, else absolute.
-function displayPath(file: string): string {
- const rel = path.relative(process.cwd(), file)
- return rel.startsWith('..') || path.isAbsolute(rel) ? file : rel
-}
-
// A cascaded (not just root-level) disabled additionally skips this one build
// root here, on top of its existing root-only ecosystem-wide meaning for
// auto/gradle/etc. facts:false has no pom-mode equivalent here (facts-only),
@@ -87,8 +106,6 @@ function getSkipReason(
return undefined
}
-type DisabledRoot = { dir: string; sockJson: SocketJson }
-
// Maven 3's options starting with `f`; its GnuParser takes a long name after
// one dash too, and splits `-fX` and `-f=X` into `-f X`.
const MAVEN_FILE_OPTS = new Set(['f', 'file'])
@@ -126,152 +143,637 @@ function withoutMavenFileOpts(dir: string, opts: string[]): string[] {
return kept
}
-// Nearest already-confirmed-disabled `dir` or ancestor of it, if any - lets the
-// caller shorten `readSocketJsonCascade`'s walk to start there instead of
-// all the way back at `cwd`, without skipping any nested override.
-function nearestDisabledRoot(
- dir: string,
- disabledRoots: readonly DisabledRoot[],
-): DisabledRoot | undefined {
- let nearest: DisabledRoot | undefined
- for (const root of disabledRoots) {
- if (
- (dir === root.dir || dir.startsWith(`${root.dir}${path.sep}`)) &&
- (!nearest || root.dir.length > nearest.dir.length)
- ) {
- nearest = root
- }
- }
- return nearest
+type Candidate = {
+ index: number
+ // A build of its own (see hasRootMarker): covered only by the builds that
+ // load everything it loads, which only running it tells.
+ marker: boolean
+ root: BuildRoot
}
-async function runEcosystemCandidates({
- candidatesByTool,
- excludePaths,
- ignoreUnresolved: ignoreUnresolvedOverride,
- optionFlags,
- realCwd,
- rootSockJson,
- sbtTmpDir,
- sidecarAcc,
- verbose,
- withFiles,
-}: {
- candidatesByTool: Map
+type SidecarEntry = NonNullable>
+
+// A copy of a facts file, if there was one, and its sidecar entry.
+type FactsState = {
+ facts: string | undefined
+ sidecar: SidecarEntry | undefined
+}
+
+type Run = Candidate & {
+ // The facts file's real path.
+ factsKey: string | undefined
+ membership: Membership
+ // What the run wrote, written back once the walk keeps it.
+ output: FactsState | undefined
+ // Undefined when the build had nothing to resolve.
+ result: RunManifestFactsResult | undefined
+}
+
+// A build skipped by its own `builds` entry, loaded to skip what it loads.
+type Load = Candidate & LoadedBuild
+
+type WalkContext = {
+ backupDir: () => Promise
excludePaths: string[] | undefined
+ // What each facts file held before the walk, by real path; undefined when a
+ // run wrote it away from its own directory (Gradle's `-p`) before it was
+ // saved.
+ originals: Map
ignoreUnresolved: boolean | undefined
optionFlags: ReadonlySet
realCwd: string
- rootSockJson: SocketJson
- // sbt only: a shared global base reused across every sbt root in this run,
- // so sbt's own Scala-toolchain cache under /boot survives between
- // invocations instead of being reprovisioned per root. Undefined when no
- // sbt root was discovered, matching runManifestFacts' own ephemeral default.
+ resolveSettings: (
+ tool: BuildTool,
+ root: BuildRoot,
+ ) => ResolvedBuildRootSettings
sbtTmpDir: string | undefined
sidecarAcc: SidecarAccumulator | undefined
verbose: boolean
withFiles: boolean | undefined
-}): Promise {
- const outcomes: RecursiveManifestOutcome[] = []
- for (const [ecosystem, roots] of candidatesByTool) {
- // Per tool: a solution counts as covered only by .NET projects.
- const covered = new Set()
- const disabledRoots: DisabledRoot[] = []
- const configByDir = new Map()
- for (const { 0: rootIndex, 1: root } of roots.entries()) {
- const { dir } = root
- if (isBuildRootCovered(covered, ecosystem, root)) {
- outcomes.push({ ...root, ecosystem, status: 'skippedCovered' })
+}
+
+type Loader = { path: string; skipped: boolean }
+
+// What loads a build another build covers, for messages.
+function describeLoader(loader: Loader | undefined): string {
+ return loader?.skipped
+ ? `it is part of ${loader.path}, which is skipped`
+ : `${loader?.path ?? 'another build'} builds it`
+}
+
+function statusOf(loader: Loader | undefined): RecursiveManifestOutcomeStatus {
+ return loader?.skipped ? 'skippedDisabled' : 'skippedCovered'
+}
+
+// The options the build-tool scripts take besides the build itself.
+export function buildToolScriptOptions(
+ config: Pick,
+ excludeBase: string | undefined,
+ excludePaths: string[] | undefined,
+) {
+ return {
+ bin: config.bin,
+ excludeBase,
+ excludePaths,
+ toolOpts: config.buildOpts,
+ }
+}
+
+async function everyAsync(
+ items: readonly T[],
+ test: (item: T) => Promise,
+): Promise {
+ for (const item of items) {
+ // eslint-disable-next-line no-await-in-loop
+ if (!(await test(item))) {
+ return false
+ }
+ }
+ return true
+}
+
+// Order-independent: a run that a later build turns out to load is dropped.
+async function walkTool(
+ ctx: WalkContext,
+ ecosystem: BuildTool,
+ roots: readonly BuildRoot[],
+): Promise {
+ const buildFiles = BUILD_FILES[ecosystem]
+ const candidates: Candidate[] = roots.map((root, index) => ({
+ index,
+ marker: hasRootMarker(ecosystem, root),
+ root,
+ }))
+ // The tool run in a directory without a root marker loads the build of the
+ // nearest ancestor that has one, so that build's skip applies to it.
+ const ancestorBuild = candidates.map(candidate => {
+ if (
+ buildFiles.byFile ||
+ !buildFiles.loadsAncestorBuild ||
+ buildFiles.loadsAncestorBuild.except.includes(
+ path.basename(candidate.root.dir),
+ ) ||
+ candidate.marker
+ ) {
+ return undefined
+ }
+ let nearest: Candidate | undefined
+ for (const other of candidates) {
+ if (
+ other.marker &&
+ other.root.dir !== candidate.root.dir &&
+ isPathInside(other.root.dir, candidate.root.dir) &&
+ (!nearest || other.root.dir.length > nearest.root.dir.length)
+ ) {
+ nearest = other
+ }
+ }
+ return nearest
+ })
+
+ const resolvedFor = ({ root }: Candidate) =>
+ ctx.resolveSettings(ecosystem, root)
+ const configs = new Map()
+ const configFor = (candidate: Candidate): EcosystemBuildConfig => {
+ const { entryKey, sockJson } = resolvedFor(candidate)
+ const { dir } = candidate.root
+ // Builds sharing a directory and its settings resolve them once.
+ return getOrInsert(configs, `${dir}\0${entryKey ?? ''}`, () =>
+ resolveEcosystemConfig(ecosystem, dir, sockJson),
+ )
+ }
+
+ // 'load' when the build's own entry skips it, 'skip' when a directory
+ // setting or the build it belongs to does.
+ const skips = new Map()
+ const warnedSkips = new Set()
+ const warnOnce = (message: string) => {
+ if (!warnedSkips.has(message)) {
+ warnedSkips.add(message)
+ logger.warn(message)
+ }
+ }
+ const skipOf = (candidate: Candidate): 'load' | 'skip' | undefined =>
+ getOrInsert(skips, candidate.index, () => {
+ const { disabledBy } = resolvedFor(candidate)
+ const { skipReason } = configFor(candidate)
+ const ancestor = ancestorBuild[candidate.index]
+ if (disabledBy?.key) {
+ return 'load'
+ }
+ if (skipReason) {
+ // One line per setting, not per build root it skips.
+ warnOnce(
+ disabledBy
+ ? `Skipping ${ecosystem} builds in and beneath ${displayPath(disabledBy.dir)}: ${describeDisabledBy(ecosystem, disabledBy)}.`
+ : `Skipping ${buildRootPath(candidate.root)} (${ecosystem}): ${skipReason}.`,
+ )
+ return 'skip'
+ }
+ return ancestor && skipOf(ancestor) ? 'skip' : undefined
+ })
+
+ const scriptOpts = (candidate: Candidate) =>
+ buildToolScriptOptions(configFor(candidate), ctx.realCwd, ctx.excludePaths)
+
+ // A solution the SDK cannot read, built through its twin instead.
+ const superseded = new Map>()
+ const isSuperseded = (candidate: Candidate): Promise =>
+ getOrInsert(superseded, candidate.index, async () =>
+ ecosystem === 'dotnet'
+ ? await isRootCovered(
+ emptyMembership(),
+ ecosystem,
+ candidate.root,
+ scriptOpts(candidate),
+ )
+ : false,
+ )
+
+ const isCovered = (covered: Membership, candidate: Candidate) =>
+ isBuildCovered(covered, ecosystem, candidate.root, scriptOpts(candidate))
+ const isRunCovered = (covered: Membership, run: Run) =>
+ isBuildCovered(
+ covered,
+ ecosystem,
+ run.root,
+ scriptOpts(run),
+ run.membership,
+ )
+ // Whether a build loads `candidate`'s project, even one that is a build of
+ // its own: what excuses its failure.
+ const isLoadedBy = (covered: Membership, candidate: Candidate) =>
+ isRootCovered(covered, ecosystem, candidate.root, scriptOpts(candidate))
+ const isCheaplyCovered = (covered: Membership, candidate: Candidate) =>
+ !candidate.marker && isBuildRootCovered(covered, ecosystem, candidate.root)
+
+ const runs = new Map()
+ const loads = new Map()
+ const failures: Candidate[] = []
+
+ // Drops, latest first, each run the others or the skipped builds cover
+ // while every run dropped before stays covered.
+ const keptRuns = async (): Promise => {
+ const ordered = [...runs.values()].sort((a, b) => a.index - b.index)
+ const kept = new Set(ordered)
+ const dropped: Run[] = []
+ const skipped = [...loads.values()].map(load => load.membership)
+ // How many kept runs and skipped builds load each path: only a run whose
+ // own paths another loads too can be covered, so most skip the union.
+ const loaders = new Map()
+ const count = (membership: Membership, by: 1 | -1) => {
+ for (const key of [...membership.dirs, ...membership.files]) {
+ loaders.set(key, (loaders.get(key) ?? 0) + by)
+ }
+ }
+ const loadedByOthers = (run: Run, key: string) =>
+ (loaders.get(key) ?? 0) -
+ (run.membership.dirs.has(key) || run.membership.files.has(key)
+ ? 1
+ : 0) >
+ 0
+ // Covering a run takes another loading its root or something it loads.
+ const mayBeCovered = (run: Run): boolean =>
+ [
+ buildRootPath(run.root),
+ ...(buildFiles.byFile
+ ? []
+ : buildFiles.names.map(name => path.join(run.root.dir, name))),
+ run.root.dir,
+ ...run.membership.dirs,
+ ...run.membership.files,
+ ].some(key => loadedByOthers(run, key))
+ for (const membership of [...skipped, ...ordered.map(r => r.membership)]) {
+ count(membership, 1)
+ }
+ const latestFirst = [...ordered].sort((a, b) => b.index - a.index)
+ for (const run of latestFirst) {
+ if (!mayBeCovered(run)) {
continue
}
+ const others = unionMemberships([
+ ...skipped,
+ ...[...kept]
+ .filter(other => other !== run)
+ .map(other => other.membership),
+ ])
+ if (
+ // eslint-disable-next-line no-await-in-loop
+ (await isRunCovered(others, run)) &&
+ // eslint-disable-next-line no-await-in-loop
+ (await everyAsync(dropped, done => isRunCovered(others, done)))
+ ) {
+ kept.delete(run)
+ dropped.push(run)
+ count(run.membership, -1)
+ }
+ }
+ return ordered.filter(run => kept.has(run))
+ }
- let dirConfig = configByDir.get(dir)
- if (!dirConfig) {
- const nearestRoot = nearestDisabledRoot(dir, disabledRoots)
- const sockJson = nearestRoot
- ? readSocketJsonCascade(dir, nearestRoot.dir, nearestRoot.sockJson)
- : readSocketJsonCascade(dir, realCwd, rootSockJson)
- dirConfig = resolveEcosystemConfig(ecosystem, dir, sockJson)
- configByDir.set(dir, dirConfig)
- if (dirConfig.skipReason) {
- // Only warn for a genuinely new disabled root, not one already
- // covered by an ancestor's warning above - otherwise a big disabled
- // reactor with hundreds of nested poms would spam one warning line
- // per pom for what's really a single root cause. The aggregate
- // count still shows up in the final summary either way.
- if (!nearestRoot) {
- logger.warn(
- `Skipping ${dir} (${ecosystem}): ${dirConfig.skipReason}.`,
- )
- }
- disabledRoots.push({ dir, sockJson })
- }
+ let kept: Run[] = []
+ let coverage = emptyMembership()
+ let skippedCoverage = emptyMembership()
+ let uncoveredFailures: Candidate[] = []
+ // Set when a run or load is added: only then can coverage change.
+ let unsettled = false
+ const settle = async () => {
+ if (unsettled) {
+ unsettled = false
+ kept = await keptRuns()
+ coverage = unionMemberships(kept.map(run => run.membership))
+ skippedCoverage = unionMemberships(
+ [...loads.values()].map(load => load.membership),
+ )
+ }
+ const stillFailed: Candidate[] = []
+ for (const failure of failures) {
+ if (
+ // eslint-disable-next-line no-await-in-loop
+ !(await isLoadedBy(coverage, failure)) &&
+ // eslint-disable-next-line no-await-in-loop
+ !(await isLoadedBy(skippedCoverage, failure))
+ ) {
+ stillFailed.push(failure)
}
- if (dirConfig.skipReason) {
- outcomes.push({ ...root, ecosystem, status: 'skippedDisabled' })
+ }
+ uncoveredFailures = stillFailed
+ }
+ const isAborted = (candidate: Candidate) =>
+ uncoveredFailures.some(
+ failure =>
+ failure !== candidate &&
+ isPathInside(failure.root.dir, candidate.root.dir),
+ )
+
+ for (;;) {
+ // eslint-disable-next-line no-await-in-loop
+ await settle()
+ let next: Candidate | undefined
+ for (const candidate of candidates) {
+ const { index } = candidate
+ if (
+ runs.has(index) ||
+ loads.has(index) ||
+ failures.includes(candidate) ||
+ isAborted(candidate) ||
+ isCheaplyCovered(coverage, candidate) ||
+ isCheaplyCovered(skippedCoverage, candidate) ||
+ skipOf(candidate) === 'skip' ||
+ // eslint-disable-next-line no-await-in-loop
+ (await isSuperseded(candidate)) ||
+ // eslint-disable-next-line no-await-in-loop
+ (await isCovered(coverage, candidate)) ||
+ // eslint-disable-next-line no-await-in-loop
+ (await isCovered(skippedCoverage, candidate))
+ ) {
continue
}
+ next = candidate
+ break
+ }
+ if (!next) {
+ break
+ }
+ if (skipOf(next) === 'load') {
// eslint-disable-next-line no-await-in-loop
- const result = await buildUncoveredRoot({
- config: {
- ...dirConfig,
- ignoreUnresolved:
- ignoreUnresolvedOverride ?? dirConfig.ignoreUnresolved,
- verbose: verbose || dirConfig.verbose,
- },
- covered,
+ const loaded = await loadBuild({
+ config: configFor(next),
ecosystem,
- // Relative to the scan root: a build can reach modules outside its
- // own directory (Maven `../x` modules, relocated Gradle projects).
- excludeBase: realCwd,
- excludePaths,
- hints: {
- continueOnInstallErrorsFlag: withFiles
- ? REACH_CONTINUE_ON_INSTALL_ERRORS_FLAG
- : undefined,
- flags: optionFlags,
- setupCommand: DSI_SETUP_COMMAND,
- socketJson: displayPath(path.join(dir, SOCKET_JSON)),
- },
- root,
- sidecarAcc,
- tmpDir: ecosystem === 'sbt' ? sbtTmpDir : undefined,
- withFiles,
+ excludePaths: ctx.excludePaths,
+ root: next.root,
+ scanRoot: ctx.realCwd,
+ verbose: ctx.verbose,
})
- if (result === 'covered') {
- outcomes.push({ ...root, ecosystem, status: 'skippedCovered' })
- continue
+ const why = describeDisabledBy(ecosystem, resolvedFor(next).disabledBy!)
+ if (loaded) {
+ loads.set(next.index, { ...next, ...loaded })
+ unsettled = true
+ } else {
+ failures.push(next)
+ logger.fail(
+ `Could not load ${buildRootPath(next.root)} to tell which builds to skip with it (${why}). Fix its settings, or skip its whole directory instead, with \`${DSI_SETUP_COMMAND}\`.`,
+ )
}
- if (result === null) {
- outcomes.push({ ...root, ecosystem, status: 'failed' })
- const abortedRoots = roots.slice(rootIndex + 1)
- if (abortedRoots.length) {
- logger.warn(
- `Aborting ${ecosystem} discovery: ${buildRootPath(root)} failed to generate Socket facts, and without them the remaining build roots cannot be safely classified as covered or independent.`,
- )
- }
- for (const aborted of abortedRoots) {
- outcomes.push({ ...aborted, ecosystem, status: 'aborted' })
- }
- break
+ continue
+ }
+ // eslint-disable-next-line no-await-in-loop
+ const run = await runCandidate(ctx, ecosystem, next, configFor(next))
+ if (run) {
+ runs.set(next.index, run)
+ unsettled = true
+ } else {
+ failures.push(next)
+ }
+ }
+
+ // The first kept run, or else skipped build, that loads `candidate`.
+ const loaderOf = async (
+ candidate: Candidate,
+ ): Promise => {
+ const ownRun = runs.get(candidate.index)
+ const loaders: Array<{
+ membership: Membership
+ root: BuildRoot
+ skipped: boolean
+ }> = [
+ ...kept
+ .filter(run => run !== ownRun)
+ .map(run => ({ ...run, skipped: false })),
+ ...[...loads.values()].map(load => ({ ...load, skipped: true })),
+ ]
+ for (const loader of loaders) {
+ if (
+ // eslint-disable-next-line no-await-in-loop
+ await (ownRun
+ ? isRunCovered(loader.membership, ownRun)
+ : isLoadedBy(loader.membership, candidate))
+ ) {
+ return { path: buildRootPath(loader.root), skipped: loader.skipped }
}
- if (!result) {
- outcomes.push({ ...root, ecosystem, status: 'empty' })
- continue
+ }
+ return undefined
+ }
+
+ const keptSet = new Set(kept)
+ const keptKeys = new Set(kept.map(run => run.factsKey))
+ const outcomes: RecursiveManifestOutcome[] = []
+ for (const candidate of candidates) {
+ const { index, root } = candidate
+ const run = runs.get(index)
+ if (run && keptSet.has(run)) {
+ if (run.result) {
+ // eslint-disable-next-line no-await-in-loop
+ await putFacts(ctx, run.result.factsPath, run.factsKey!, run.output)
}
outcomes.push({
...root,
ecosystem,
- factsPath: result.factsPath,
- status: 'generated',
+ ...(run.result ? { factsPath: run.result.factsPath } : {}),
+ status: run.result ? 'generated' : 'empty',
})
+ continue
+ }
+ let status: RecursiveManifestOutcomeStatus
+ let discardedFactsPath: string | undefined
+ if (run) {
+ // eslint-disable-next-line no-await-in-loop
+ const loader = await loaderOf(candidate)
+ if (run.result) {
+ logger.info(
+ `Dropping the Socket facts of ${buildRootPath(root)}: ${describeLoader(loader)}.`,
+ )
+ if (!keptKeys.has(run.factsKey)) {
+ discardedFactsPath = run.factsKey
+ }
+ }
+ status = statusOf(loader)
+ } else if (loads.has(index)) {
+ // eslint-disable-next-line no-await-in-loop
+ if (await isCovered(coverage, candidate)) {
+ status = 'skippedCovered'
+ } else {
+ status = 'skippedDisabled'
+ logger.warn(
+ `Skipping ${withModules(buildRootPath(root), loads.get(index)!.modules)}: ${describeDisabledBy(ecosystem, resolvedFor(candidate).disabledBy!)}.`,
+ )
+ }
+ } else if (failures.includes(candidate)) {
+ if (uncoveredFailures.includes(candidate)) {
+ status = 'failed'
+ } else {
+ // eslint-disable-next-line no-await-in-loop
+ const loader = await loaderOf(candidate)
+ if (skipOf(candidate) !== 'load') {
+ logger.info(
+ `${buildRootPath(root)} failed to generate Socket facts on its own, but ${describeLoader(loader)}, so its failure is ignored.`,
+ )
+ }
+ status = statusOf(loader)
+ }
+ // eslint-disable-next-line no-await-in-loop
+ } else if (await isCovered(coverage, candidate)) {
+ status = 'skippedCovered'
+ // eslint-disable-next-line no-await-in-loop
+ } else if (await isCovered(skippedCoverage, candidate)) {
+ status = 'skippedDisabled'
+ } else if (isAborted(candidate)) {
+ status = 'aborted'
+ } else if (skipOf(candidate)) {
+ status = 'skippedDisabled'
+ } else {
+ // A solution the SDK cannot read, built through its twin.
+ status = 'skippedCovered'
+ }
+ if (status === 'skippedCovered') {
+ // eslint-disable-next-line no-await-in-loop
+ await warnUnappliedEntry(candidate, resolvedFor(candidate).entryKey, () =>
+ loaderOf(candidate),
+ )
+ }
+ outcomes.push({
+ ...root,
+ ...(discardedFactsPath ? { discardedFactsPath } : {}),
+ ecosystem,
+ status,
+ })
+ }
+
+ for (const failure of uncoveredFailures) {
+ const aborted = candidates.filter(
+ candidate =>
+ isAborted(candidate) &&
+ !failures.includes(candidate) &&
+ isPathInside(failure.root.dir, candidate.root.dir),
+ )
+ if (aborted.length) {
+ logger.warn(
+ `Not building ${aborted.length} ${ecosystem} build root(s) in or under ${displayPath(failure.root.dir)}: ${buildRootPath(failure.root)} failed, and without it they cannot be told apart from its modules.`,
+ )
}
}
return outcomes
}
-// Builds `root` unless earlier builds cover it, then adds the build files it
-// covers.
+// A build root's own entry applies to nothing when another build loads it:
+// that build resolves it with its own settings.
+async function warnUnappliedEntry(
+ candidate: Candidate,
+ entryKey: string | undefined,
+ loaderOf: () => Promise<{ path: string } | undefined>,
+): Promise {
+ if (!entryKey) {
+ return
+ }
+ const loader = await loaderOf()
+ logger.warn(
+ `${buildsSetting(entryKey)} in ${socketJsonPath(candidate.root.dir)} does not apply: ${loader?.path ?? 'another build'} builds ${buildRootPath(candidate.root)} with its own settings.`,
+ )
+}
+
+async function runCandidate(
+ ctx: WalkContext,
+ ecosystem: BuildTool,
+ candidate: Candidate,
+ dirConfig: EcosystemBuildConfig,
+): Promise {
+ const { root } = candidate
+ const expectedFactsPath = path.join(
+ root.dir,
+ socketFactsFileName(ecosystem, root.entryFile) ?? '',
+ )
+ const expectedKey = await realpathOrResolved(expectedFactsPath)
+ if (!ctx.originals.has(expectedKey)) {
+ ctx.originals.set(
+ expectedKey,
+ await saveFacts(
+ ctx,
+ expectedFactsPath,
+ expectedKey,
+ `${ecosystem}-${candidate.index}`,
+ ),
+ )
+ }
+ const result = await runManifestFacts({
+ bin: dirConfig.bin,
+ buildOpts: dirConfig.buildOpts,
+ cwd: root.dir,
+ ecosystem,
+ entryFile: root.entryFile,
+ // Relative to the scan root: a build can reach modules outside its own
+ // directory (Maven `../x` modules, relocated Gradle projects).
+ excludeBase: ctx.realCwd,
+ excludeConfigs: dirConfig.excludeConfigs,
+ excludePaths: ctx.excludePaths,
+ hints: {
+ continueOnInstallErrorsFlag: ctx.withFiles
+ ? REACH_CONTINUE_ON_INSTALL_ERRORS_FLAG
+ : undefined,
+ flags: ctx.optionFlags,
+ setupCommand: DSI_SETUP_COMMAND,
+ socketJson: socketJsonPath(root.dir),
+ },
+ ignoreUnresolved: ctx.ignoreUnresolved ?? dirConfig.ignoreUnresolved,
+ includeConfigs: dirConfig.includeConfigs,
+ javaHome: dirConfig.javaHome,
+ sidecarAcc: ctx.sidecarAcc,
+ tmpDir: ecosystem === 'sbt' ? ctx.sbtTmpDir : undefined,
+ verbose: ctx.verbose || dirConfig.verbose,
+ withFiles: ctx.withFiles,
+ })
+ if (result === null) {
+ return undefined
+ }
+ let factsKey: string | undefined
+ let output: FactsState | undefined
+ if (result) {
+ factsKey = await realpathOrResolved(result.factsPath)
+ output = await saveFacts(
+ ctx,
+ result.factsPath,
+ factsKey,
+ `${ecosystem}-${candidate.index}.out`,
+ )
+ if (!ctx.originals.has(factsKey)) {
+ ctx.originals.set(factsKey, undefined)
+ }
+ await putFacts(ctx, result.factsPath, factsKey, ctx.originals.get(factsKey))
+ }
+ return {
+ ...candidate,
+ factsKey,
+ // Gradle's `-p` can root the build away from `root.dir`.
+ membership: result
+ ? await membershipOf(
+ path.dirname(result.factsPath),
+ result.projects,
+ ctx.realCwd,
+ )
+ : emptyMembership(),
+ output,
+ result: result ?? undefined,
+ }
+}
+
+// Puts `state` in place of a facts file and its sidecar entry; an unknown state
+// leaves the file and drops the entry.
+async function putFacts(
+ ctx: WalkContext,
+ file: string,
+ key: string,
+ state: FactsState | undefined,
+): Promise {
+ if (state?.sidecar) {
+ ctx.sidecarAcc?.set(key, state.sidecar)
+ } else {
+ ctx.sidecarAcc?.delete(key)
+ }
+ if (!state) {
+ return
+ }
+ if (state.facts) {
+ await fs.copyFile(state.facts, file)
+ } else {
+ await fs.rm(file, { force: true })
+ }
+}
+
+async function saveFacts(
+ ctx: WalkContext,
+ file: string,
+ key: string,
+ name: string,
+): Promise {
+ let facts: string | undefined
+ if (existsSync(file)) {
+ facts = path.join(await ctx.backupDir(), name)
+ await fs.copyFile(file, facts)
+ }
+ return { facts, sidecar: ctx.sidecarAcc?.get(key) }
+}
+
+// Builds `root` unless earlier builds cover it, then adds what it loaded to
+// `covered`.
export async function buildUncoveredRoot({
config,
covered,
@@ -286,7 +788,7 @@ export async function buildUncoveredRoot({
withFiles,
}: {
config: EcosystemBuildConfig
- covered: Set
+ covered: Membership
// Where the build runs, as the user named it; `root.dir` by default.
cwd?: string | undefined
ecosystem: BuildTool
@@ -300,12 +802,12 @@ export async function buildUncoveredRoot({
}): Promise {
const { bin, buildOpts } = config
if (
- await isRootCovered(covered, ecosystem, root, {
- bin,
- excludeBase,
- excludePaths,
- toolOpts: buildOpts,
- })
+ await isRootCovered(
+ covered,
+ ecosystem,
+ root,
+ buildToolScriptOptions(config, excludeBase, excludePaths),
+ )
) {
return 'covered'
}
@@ -328,19 +830,15 @@ export async function buildUncoveredRoot({
withFiles,
})
if (result) {
- // Gradle's `-p` can root the build away from `root.dir`.
- await addCoveredBuildFiles(
+ addMembership(
covered,
- ecosystem,
- path.dirname(result.factsPath),
- result.projects,
+ await membershipOf(path.dirname(result.factsPath), result.projects),
)
}
return result
}
-// A failed root aborts only its own ecosystem's remaining walk: the build
-// files it would have covered are all its own tool's.
+// A failure fails the walk only when no kept or skipped build loads it.
export async function generateRecursiveManifests({
cwd,
excludePaths,
@@ -381,9 +879,8 @@ export async function generateRecursiveManifests({
// must match or every boundary/relative-path comparison below breaks as
// soon as cwd contains a symlink (macOS /tmp -> /private/tmp, etc.).
const realCwd = await realpathOrResolved(cwd)
- // A root-disabled ecosystem must still be scanned for - a nested socket.json
- // may re-enable it - so the per-directory cascade below, not this scan, is
- // what actually decides skip vs. include.
+ // A nested socket.json may re-enable an ecosystem the root disables, so the
+ // per-root settings below decide skips, not this scan.
const candidatesByTool = await findBuildToolCandidates({
cwd,
excludePaths,
@@ -391,20 +888,43 @@ export async function generateRecursiveManifests({
socketConfig: loadSocketConfig(cwd),
sockJson: withoutDisabledFlags(rootSockJson),
})
+ const resolveSettings = createBuildRootSettingsResolver()
+ // Invalid settings fail the scan before any build runs.
+ for (const { 0: tool, 1: roots } of candidatesByTool) {
+ for (const root of roots) {
+ resolveSettings(tool, root)
+ }
+ }
+ // A failure another build excuses leaves the exit code it set.
+ const exitCodeBefore = process.exitCode
- const runAll = (resolvedSbtTmpDir: string | undefined) =>
- runEcosystemCandidates({
- candidatesByTool,
+ let backupDir: string | undefined
+ const runAll = async (resolvedSbtTmpDir: string | undefined) => {
+ const ctx: WalkContext = {
+ backupDir: async () => {
+ backupDir ??= await fs.mkdtemp(
+ path.join(tmpdir(), 'socket-facts-backup-'),
+ )
+ return backupDir
+ },
excludePaths,
ignoreUnresolved,
optionFlags,
+ originals: new Map(),
realCwd,
- rootSockJson,
+ resolveSettings,
sbtTmpDir: resolvedSbtTmpDir,
sidecarAcc,
verbose,
withFiles,
- })
+ }
+ const outcomes: RecursiveManifestOutcome[] = []
+ for (const { 0: ecosystem, 1: roots } of candidatesByTool) {
+ // eslint-disable-next-line no-await-in-loop
+ outcomes.push(...(await walkTool(ctx, ecosystem, roots)))
+ }
+ return outcomes
+ }
// A shared global base across every sbt root in this run lets sbt's own
// Scala-toolchain cache under /boot survive between invocations
@@ -413,11 +933,22 @@ export async function generateRecursiveManifests({
// caller-supplied sbtTmpDir is reused as-is (the caller owns its lifetime);
// otherwise one is allocated and cleaned up here, but only when there's an
// sbt root to benefit from sharing it at all.
- const outcomes = sbtTmpDir
- ? await runAll(sbtTmpDir)
- : candidatesByTool.get('sbt')?.length
- ? await withTmpDir('socket-sbt-facts-shared-', runAll)
- : await runAll(undefined)
+ let outcomes: RecursiveManifestOutcome[]
+ try {
+ outcomes = sbtTmpDir
+ ? await runAll(sbtTmpDir)
+ : candidatesByTool.get('sbt')?.length
+ ? await withTmpDir('socket-sbt-facts-shared-', runAll)
+ : await runAll(undefined)
+ } finally {
+ if (backupDir) {
+ await fs.rm(backupDir, { recursive: true, force: true })
+ }
+ }
+
+ if (!outcomes.some(o => o.status === 'failed')) {
+ process.exitCode = exitCodeBefore
+ }
if (verbose) {
logger.info(`Discovered ${outcomes.length} build-tool candidate(s).`)
diff --git a/src/commands/manifest/generate-recursive-manifests.test.mts b/src/commands/manifest/generate-recursive-manifests.test.mts
index f84dc555e..3bb4031be 100644
--- a/src/commands/manifest/generate-recursive-manifests.test.mts
+++ b/src/commands/manifest/generate-recursive-manifests.test.mts
@@ -1,4 +1,4 @@
-import { promises as fs } from 'node:fs'
+import { existsSync, promises as fs } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
@@ -6,32 +6,51 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { logger } from '@socketsecurity/registry/lib/logger'
-vi.mock('../../utils/socket-json.mts', () => ({
+vi.mock('../../utils/socket-json.mts', async importOriginal => ({
+ ...(await importOriginal()),
readOrDefaultSocketJson: vi.fn(() => ({})),
- // Default: no per-root override found, fall back to the root config - matches
- // there being no nested socket.json anywhere in the fixture tree.
- readSocketJsonCascade: vi.fn((_dir, _boundaryDir, fallback) => fallback),
+ // Default: no nested socket.json anywhere in the fixture tree.
+ readSocketJsonChain: vi.fn(() => []),
}))
vi.mock('./run-manifest-facts.mts', () => ({
runManifestFacts: vi.fn(),
}))
+vi.mock('./enumerate-workspaces.mts', () => ({
+ enumerateWorkspaces: vi.fn(),
+}))
vi.mock('./scripts/run.mts', async importOriginal => ({
...(await importOriginal()),
listDotnetEntryProjects: vi.fn(async () => undefined),
}))
import { buildRootPath } from './discover-manifest-roots.mts'
+import { enumerateWorkspaces } from './enumerate-workspaces.mts'
import { generateRecursiveManifests } from './generate-recursive-manifests.mts'
import { runManifestFacts } from './run-manifest-facts.mts'
import { listDotnetEntryProjects } from './scripts/run.mts'
import { testPath } from '../../../test/utils.mts'
import {
readOrDefaultSocketJson,
- readSocketJsonCascade,
+ readSocketJsonChain,
} from '../../utils/socket-json.mts'
import type { SocketJson } from '../../utils/socket-json.mts'
+// The settings each build root's directory resolves to, as one socket.json at
+// the filesystem root.
+function mockCascade(cascade: (dir: string) => unknown): void {
+ vi.mocked(readSocketJsonChain).mockImplementation(dir => [
+ { dir: path.parse(dir).root, sockJson: cascade(dir) as SocketJson },
+ ])
+}
+
+// `dir`'s own socket.json, with `builds` entries for the builds there.
+function mockOwnSettings(own: Record): void {
+ vi.mocked(readSocketJsonChain).mockImplementation(dir =>
+ own[dir] ? [{ dir, sockJson: own[dir] as SocketJson }] : [],
+ )
+}
+
const monorepo = path.join(
testPath,
'fixtures/commands/manifest/dynamic-sbom-inference/monorepo',
@@ -47,13 +66,22 @@ function relOf(dir: string): string {
return path.relative(monorepo, dir).replaceAll('\\', '/')
}
+function mavenProject(subprojectDir: string) {
+ return {
+ type: 'maven' as const,
+ name: subprojectDir,
+ id: subprojectDir,
+ subprojectDir,
+ manifestFiles: [{ file: `${subprojectDir}/pom.xml` }],
+ children: [],
+ }
+}
+
describe('generateRecursiveManifests', () => {
beforeEach(() => {
vi.mocked(runManifestFacts).mockReset()
vi.mocked(readOrDefaultSocketJson).mockReturnValue({} as SocketJson)
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (_dir, _boundaryDir, fallback) => fallback,
- )
+ mockCascade(() => ({}))
})
afterEach(() => {
process.exitCode = undefined
@@ -117,66 +145,182 @@ describe('generateRecursiveManifests', () => {
})
it.each([
- // Escaping references must get their own independent invocation
- // regardless of where they happen to sort alphabetically relative to the
- // reactor that declares them - before ('aaa-shared-lib') and after
- // ('zzz-shared-lib') both have to behave identically.
- ['aaa-shared-lib'],
- ['zzz-shared-lib'],
+ // Sorting before the reactor ('aaa-shared-lib'), the member is built on
+ // its own first and dropped once the reactor turns out to load it; after
+ // it ('zzz-shared-lib'), it is never built on its own.
+ ['aaa-shared-lib', true],
+ ['zzz-shared-lib', false],
])(
- 'never suppresses a sibling subprojectDir that escapes its declaring reactor (name: %s)',
- async sharedLibName => {
+ 'builds a member outside its reactor directory only inside the reactor (name: %s)',
+ async (sharedLibName, builtAlone) => {
const outer = await fs.realpath(
await fs.mkdtemp(path.join(tmpdir(), 'escaping-subproject-')),
)
const reactorA = path.join(outer, 'reactor-a')
const sharedLib = path.join(outer, sharedLibName)
+ const sharedFacts = path.join(sharedLib, 'pom.xml.socket.facts.json')
+ const sidecarAcc = new Map()
try {
await fs.mkdir(reactorA, { recursive: true })
await fs.mkdir(sharedLib, { recursive: true })
await fs.writeFile(path.join(reactorA, 'pom.xml'), '')
await fs.writeFile(path.join(sharedLib, 'pom.xml'), '')
+ await fs.writeFile(sharedFacts, 'committed')
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => {
- if (cwd === reactorA) {
- return {
- factsPath: path.join(cwd, '.socket.facts.json'),
- projects: [
- {
- type: 'maven',
- name: 'shared-lib',
- subprojectDir: `../${sharedLibName}`,
- manifestFiles: [{ file: `../${sharedLibName}/pom.xml` }],
- children: [],
- },
- ],
- }
- }
+ const factsPath = path.join(cwd, 'pom.xml.socket.facts.json')
+ await fs.writeFile(factsPath, 'generated')
+ sidecarAcc.set(factsPath, { components: [], projects: [] })
return {
- factsPath: path.join(cwd, '.socket.facts.json'),
- projects: [],
+ factsPath,
+ projects:
+ cwd === reactorA
+ ? [
+ {
+ type: 'maven',
+ name: 'shared-lib',
+ id: 'shared-lib',
+ subprojectDir: `../${sharedLibName}`,
+ manifestFiles: [{ file: `../${sharedLibName}/pom.xml` }],
+ children: [],
+ },
+ ]
+ : [],
}
})
const outcomes = await generateRecursiveManifests({
cwd: outer,
+ sidecarAcc,
verbose: false,
})
const byDir = new Map(outcomes.map(o => [o.dir, o.status]))
expect(byDir.get(reactorA)).toBe('generated')
- expect(byDir.get(sharedLib)).toBe('generated')
+ expect(byDir.get(sharedLib)).toBe('skippedCovered')
expect(
vi
.mocked(runManifestFacts)
.mock.calls.some(([opts]) => opts.cwd === sharedLib),
- ).toBe(true)
+ ).toBe(builtAlone)
+ expect(await fs.readFile(sharedFacts, 'utf8')).toBe('committed')
+ expect([...sidecarAcc.keys()]).toEqual([
+ path.join(reactorA, 'pom.xml.socket.facts.json'),
+ ])
+ // The restored file is not this scan's, so it is not to be collected.
+ expect(
+ outcomes.find(o => o.dir === sharedLib)?.discardedFactsPath,
+ ).toBe(builtAlone ? sharedFacts : undefined)
+ } finally {
+ await fs.rm(outer, { recursive: true, force: true })
+ }
+ },
+ )
+
+ it.for([
+ // The build that redirects Gradle with `-p` runs second, then first.
+ ['a-main', 'z-alias'],
+ ['z-main', 'a-alias'],
+ ])(
+ 'keeps the facts and sidecar entry of the kept run together when two runs write one facts file (%s, %s)',
+ async ([mainName, aliasName]) => {
+ const outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'shared-facts-')),
+ )
+ const main = path.join(outer, mainName!)
+ const alias = path.join(outer, aliasName!)
+ const sidecarAcc = new Map()
+ try {
+ for (const dir of [main, alias]) {
+ for (const file of ['settings.gradle', 'build.gradle']) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(dir, { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(path.join(dir, file), '')
+ }
+ }
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => {
+ const factsPath = path.join(main, 'gradle.socket.facts.json')
+ await fs.writeFile(factsPath, cwd)
+ sidecarAcc.set(factsPath, { components: [], projects: [], cwd })
+ return {
+ factsPath,
+ projects: [
+ {
+ type: 'maven' as const,
+ name: 'root',
+ id: 'root',
+ subprojectDir: '.',
+ children: [],
+ },
+ ],
+ }
+ })
+
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ sidecarAcc,
+ verbose: false,
+ })
+
+ const generated = outcomes.filter(o => o.status === 'generated')
+ expect(generated).toHaveLength(1)
+ const { factsPath } = generated[0]!
+ expect(generated[0]!.discardedFactsPath).toBeUndefined()
+ // The kept build's own facts and sidecar entry, whichever ran last.
+ expect(await fs.readFile(factsPath!, 'utf8')).toBe(generated[0]!.dir)
+ expect(sidecarAcc.get(factsPath!)?.cwd).toBe(generated[0]!.dir)
} finally {
await fs.rm(outer, { recursive: true, force: true })
}
},
)
+ it('removes the facts file of a dropped run when none existed before it', async () => {
+ const outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'dropped-run-')),
+ )
+ const lib = path.join(outer, 'aaa-lib')
+ const app = path.join(outer, 'app')
+ try {
+ for (const dir of [lib, app]) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(dir, { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(path.join(dir, 'pom.xml'), '')
+ }
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => {
+ const factsPath = path.join(cwd, 'pom.xml.socket.facts.json')
+ await fs.writeFile(factsPath, 'generated')
+ return {
+ factsPath,
+ projects:
+ cwd === app
+ ? [
+ {
+ type: 'maven',
+ name: 'lib',
+ id: 'lib',
+ subprojectDir: '../aaa-lib',
+ manifestFiles: [{ file: '../aaa-lib/pom.xml' }],
+ children: [],
+ },
+ ]
+ : [],
+ }
+ })
+
+ await generateRecursiveManifests({ cwd: outer, verbose: false })
+
+ expect(existsSync(path.join(lib, 'pom.xml.socket.facts.json'))).toBe(
+ false,
+ )
+ expect(existsSync(path.join(app, 'pom.xml.socket.facts.json'))).toBe(true)
+ } finally {
+ await fs.rm(outer, { recursive: true, force: true })
+ }
+ })
+
it('covers only the build files a build reports, so an independent build sharing a member directory still runs', async () => {
const outer = await fs.realpath(
await fs.mkdtemp(path.join(tmpdir(), 'co-located-builds-')),
@@ -327,7 +471,7 @@ describe('generateRecursiveManifests', () => {
}
})
- it('judges coverage against the reported build root, not the discovery directory', async () => {
+ it('judges coverage against the reported build root, not the discovery directory, anywhere in the scan root', async () => {
const outer = await fs.realpath(
await fs.mkdtemp(path.join(tmpdir(), 'relocated-build-root-')),
)
@@ -376,7 +520,8 @@ describe('generateRecursiveManifests', () => {
const byDir = new Map(outcomes.map(o => [o.dir, o.status]))
expect(byDir.get(member)).toBe('skippedCovered')
- expect(byDir.get(escaped)).toBe('generated')
+ // Inside the scan root, though outside its build root.
+ expect(byDir.get(escaped)).toBe('skippedCovered')
} finally {
await fs.rm(outer, { recursive: true, force: true })
}
@@ -401,10 +546,10 @@ describe('generateRecursiveManifests', () => {
expect(atDualMarkerDir.every(o => o.status === 'generated')).toBe(true)
})
- it('aborts only the failing ecosystem (fail-closed), marking its remaining candidates aborted, while an unrelated ecosystem proceeds normally', async () => {
+ it('aborts only the build roots in and under a failed one, which may be its members, while siblings and other ecosystems proceed', async () => {
vi.mocked(runManifestFacts).mockImplementation(
async ({ cwd, ecosystem }) => {
- if (ecosystem === 'maven' && cwd === dualMarkerDir) {
+ if (ecosystem === 'maven' && cwd === reactor) {
process.exitCode = 1
return null
}
@@ -422,35 +567,87 @@ describe('generateRecursiveManifests', () => {
const byKey = new Map(
outcomes.map(o => [`${o.ecosystem}:${relOf(o.dir)}`, o.status]),
)
- expect(byKey.get('maven:dual-marker-dir')).toBe('failed')
- // Without dual-marker-dir's own projects[], reactor's still-undiscovered
- // members can't be safely told apart from independent projects - so
- // nothing else in the maven ecosystem gets attempted, but that's now
- // reported explicitly rather than silently omitted.
- expect(byKey.get('maven:reactor')).toBe('aborted')
- expect(byKey.get('maven:reactor/moduleB/independent-submodule')).toBe(
- 'aborted',
- )
+ expect(byKey.get('maven:reactor')).toBe('failed')
+ // Without reactor's projects[], its members can't be told apart from
+ // independent projects beneath it.
+ for (const member of [
+ 'maven:reactor/moduleA',
+ 'maven:reactor/moduleB',
+ 'maven:reactor/moduleB/independent-submodule',
+ ]) {
+ expect(byKey.get(member)).toBe('aborted')
+ }
expect(
vi
.mocked(runManifestFacts)
- .mock.calls.some(
- ([opts]) => opts.ecosystem === 'maven' && opts.cwd === reactor,
- ),
- ).toBe(false)
- // Coverage is tracked per ecosystem, so maven's failure has nothing to
- // do with gradle's - it still runs to completion at the same directory.
- expect(byKey.get('gradle:dual-marker-dir')).toBe('generated')
+ .mock.calls.filter(([opts]) => opts.ecosystem === 'maven')
+ .map(([opts]) => relOf(opts.cwd))
+ .sort(),
+ ).toEqual(['dual-marker-dir', 'reactor'])
+ expect(byKey.get('maven:dual-marker-dir')).toBe('generated')
+ expect(byKey.get('gradle:reactor/moduleA/nested-gradle')).toBe(
+ 'generated',
+ )
expect(
warnSpy.mock.calls.some(c =>
- /Aborting maven discovery/.test(String(c[0])),
+ /Not building 3 maven build root\(s\) in or under \S*reactor:/.test(
+ String(c[0]),
+ ),
),
).toBe(true)
+ expect(process.exitCode).toBe(1)
} finally {
warnSpy.mockRestore()
}
})
+ it('excuses the failure of a member built on its own before the build that loads it', async () => {
+ const outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'misordered-member-')),
+ )
+ const core = path.join(outer, 'core')
+ const parent = path.join(outer, 'parent')
+ try {
+ for (const dir of [core, parent]) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(dir, { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(path.join(dir, 'pom.xml'), '')
+ }
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => {
+ if (cwd === core) {
+ process.exitCode = 1
+ return null
+ }
+ return {
+ factsPath: path.join(cwd, 'pom.xml.socket.facts.json'),
+ projects: [
+ {
+ type: 'maven',
+ name: 'core',
+ id: 'core',
+ subprojectDir: '../core',
+ manifestFiles: [{ file: '../core/pom.xml' }],
+ children: [],
+ },
+ ],
+ }
+ })
+
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ verbose: false,
+ })
+
+ const byDir = new Map(outcomes.map(o => [o.dir, o.status]))
+ expect(byDir.get(core)).toBe('skippedCovered')
+ expect(byDir.get(parent)).toBe('generated')
+ expect(process.exitCode).toBeUndefined()
+ } finally {
+ await fs.rm(outer, { recursive: true, force: true })
+ }
+ })
+
it('still classifies a failure as failed (not empty) when process.exitCode was already non-zero beforehand', async () => {
process.exitCode = 1
vi.mocked(runManifestFacts).mockImplementation(
@@ -558,11 +755,10 @@ describe('generateRecursiveManifests', () => {
})
it('resolves each build root its own nearest socket.json instead of only the root config', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === dualMarkerDir
- ? { defaults: { manifest: { maven: { javaHome: '/opt/jdk-11' } } } }
- : fallback,
+ mockCascade(dir =>
+ dir === dualMarkerDir
+ ? { defaults: { manifest: { maven: { javaHome: '/opt/jdk-11' } } } }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -588,15 +784,14 @@ describe('generateRecursiveManifests', () => {
})
it('resolves an explicit null override back to the no-restriction default, not a literal null', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === dualMarkerDir
- ? {
- defaults: {
- manifest: { maven: { excludeConfigs: null, javaHome: null } },
- },
- }
- : fallback,
+ mockCascade(dir =>
+ dir === dualMarkerDir
+ ? {
+ defaults: {
+ manifest: { maven: { excludeConfigs: null, javaHome: null } },
+ },
+ }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -618,11 +813,10 @@ describe('generateRecursiveManifests', () => {
})
it("honors a build root's own socket.json verbose and points hints at it", async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === dualMarkerDir
- ? { defaults: { manifest: { maven: { verbose: true } } } }
- : fallback,
+ mockCascade(dir =>
+ dir === dualMarkerDir
+ ? { defaults: { manifest: { maven: { verbose: true } } } }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -650,15 +844,14 @@ describe('generateRecursiveManifests', () => {
it.each([true, false])(
'lets an explicit ignoreUnresolved (%s) override every build root socket.json',
async ignoreUnresolved => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === dualMarkerDir
- ? {
- defaults: {
- manifest: { maven: { ignoreUnresolved: !ignoreUnresolved } },
- },
- }
- : fallback,
+ mockCascade(dir =>
+ dir === dualMarkerDir
+ ? {
+ defaults: {
+ manifest: { maven: { ignoreUnresolved: !ignoreUnresolved } },
+ },
+ }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -680,11 +873,10 @@ describe('generateRecursiveManifests', () => {
)
it("falls back to each build root's socket.json ignoreUnresolved when not overridden", async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === dualMarkerDir
- ? { defaults: { manifest: { maven: { ignoreUnresolved: true } } } }
- : fallback,
+ mockCascade(dir =>
+ dir === dualMarkerDir
+ ? { defaults: { manifest: { maven: { ignoreUnresolved: true } } } }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -709,11 +901,10 @@ describe('generateRecursiveManifests', () => {
})
it('skips (with a warning) a resolved config that sets facts: false, never invoking the build tool', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === dualMarkerDir
- ? { defaults: { manifest: { gradle: { facts: false } } } }
- : fallback,
+ mockCascade(dir =>
+ dir === dualMarkerDir
+ ? { defaults: { manifest: { gradle: { facts: false } } } }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -747,7 +938,7 @@ describe('generateRecursiveManifests', () => {
}
})
- it('shortens the cascade walk for a nested candidate under an already-disabled root, instead of re-walking from cwd', async () => {
+ it('warns once per setting that disables build roots, not once per build root it skips', async () => {
const independentSubmodule = path.join(
reactor,
'moduleB',
@@ -761,21 +952,10 @@ describe('generateRecursiveManifests', () => {
path.join(reactor, 'moduleB'),
independentSubmodule,
])
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, boundaryDir, fallback) => {
- if (dir === reactor && boundaryDir === monorepo) {
- // The one full walk: reactor's own socket.json disables maven.
- return { defaults: { manifest: { maven: { disabled: true } } } }
- }
- // Every other maven candidate nested under reactor must use a
- // boundary nearer than the overall recursion root - never re-walk
- // all the way back to monorepo/rootSockJson once an ancestor is
- // already confirmed disabled.
- if (nestedMavenDirs.has(dir)) {
- expect(boundaryDir).not.toBe(monorepo)
- }
- return fallback
- },
+ mockCascade(dir =>
+ dir === reactor || nestedMavenDirs.has(dir)
+ ? { defaults: { manifest: { maven: { disabled: true } } } }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -810,7 +990,9 @@ describe('generateRecursiveManifests', () => {
// moduleB + independent-submodule), but only the root cause should
// warn - otherwise a big disabled reactor spams one line per pom.
expect(warnSpy).toHaveBeenCalledTimes(1)
- expect(String(warnSpy.mock.calls[0]?.[0])).toMatch(/disabled is true/)
+ expect(String(warnSpy.mock.calls[0]?.[0])).toMatch(
+ /sets defaults\.manifest\.maven\.disabled/,
+ )
} finally {
warnSpy.mockRestore()
}
@@ -822,18 +1004,16 @@ describe('generateRecursiveManifests', () => {
'moduleB',
'independent-submodule',
)
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) => {
- if (dir === reactor) {
- return { defaults: { manifest: { maven: { disabled: true } } } }
- }
- if (dir === independentSubmodule) {
- // Its own socket.json explicitly clears the inherited disable.
- return { defaults: { manifest: { maven: { disabled: false } } } }
- }
- return fallback
- },
- )
+ mockCascade(dir => {
+ if (dir === reactor) {
+ return { defaults: { manifest: { maven: { disabled: true } } } }
+ }
+ if (dir === independentSubmodule) {
+ // Its own socket.json explicitly clears the inherited disable.
+ return { defaults: { manifest: { maven: { disabled: false } } } }
+ }
+ return {}
+ })
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
projects: [],
@@ -862,12 +1042,9 @@ describe('generateRecursiveManifests', () => {
vi.mocked(readOrDefaultSocketJson).mockReturnValue({
defaults: { manifest: { maven: { disabled: true } } },
} as SocketJson)
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === reactor
- ? { defaults: { manifest: { maven: { disabled: false } } } }
- : fallback,
- )
+ mockCascade(dir => ({
+ defaults: { manifest: { maven: { disabled: dir !== reactor } } },
+ }))
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
projects: [],
@@ -893,11 +1070,10 @@ describe('generateRecursiveManifests', () => {
})
it('skips (with a warning) a resolved config that sets a cascaded disabled: true', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(
- (dir, _boundaryDir, fallback) =>
- dir === reactor
- ? { defaults: { manifest: { maven: { disabled: true } } } }
- : fallback,
+ mockCascade(dir =>
+ dir === reactor
+ ? { defaults: { manifest: { maven: { disabled: true } } } }
+ : {},
)
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
factsPath: path.join(cwd, '.socket.facts.json'),
@@ -912,7 +1088,7 @@ describe('generateRecursiveManifests', () => {
})
const warned = warnSpy.mock.calls.map(c => String(c[0])).join('\n')
- expect(warned).toMatch(/disabled is true/)
+ expect(warned).toMatch(/sets defaults\.manifest\.maven\.disabled/)
const byKey = new Map(
outcomes.map(o => [`${o.ecosystem}:${relOf(o.dir)}`, o.status]),
@@ -923,49 +1099,44 @@ describe('generateRecursiveManifests', () => {
}
})
- it('bounds the socket.json cascade at a symlinked cwd instead of walking past it to the real filesystem root', async () => {
+ it('reads the socket.json files above a symlinked scan directory, through its real path', async () => {
const actual = await vi.importActual<
typeof import('../../utils/socket-json.mts')
>('../../utils/socket-json.mts')
vi.mocked(readOrDefaultSocketJson).mockImplementation(
actual.readOrDefaultSocketJson,
)
- vi.mocked(readSocketJsonCascade).mockImplementation(
- actual.readSocketJsonCascade,
+ vi.mocked(readSocketJsonChain).mockImplementation(
+ actual.readSocketJsonChain,
)
- const outer = await fs.mkdtemp(path.join(tmpdir(), 'symlink-cascade-'))
+ const outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'symlink-cascade-')),
+ )
const realCwd = path.join(outer, 'real-cwd')
const cwdLink = path.join(outer, 'cwd-link')
const project = path.join(realCwd, 'project')
try {
await fs.mkdir(project, { recursive: true })
await fs.writeFile(path.join(project, 'pom.xml'), '')
- // Sits strictly above the intended recursion root - must never be read.
await fs.writeFile(
path.join(outer, 'socket.json'),
JSON.stringify({
version: 1,
- defaults: { manifest: { maven: { bin: 'LEAKED-BIN' } } },
+ defaults: { manifest: { maven: { bin: 'ABOVE-BIN' } } },
}),
)
await fs.symlink(realCwd, cwdLink)
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
+ factsPath: path.join(cwd, '.socket.facts.json'),
+ projects: [],
+ }))
- vi.mocked(runManifestFacts).mockImplementation(async ({ bin, cwd }) => {
- expect(bin).not.toBe('LEAKED-BIN')
- return {
- factsPath: path.join(cwd, '.socket.facts.json'),
- projects: [],
- }
- })
-
- const outcomes = await generateRecursiveManifests({
- cwd: cwdLink,
- verbose: false,
- })
+ await generateRecursiveManifests({ cwd: cwdLink, verbose: false })
- expect(outcomes.some(o => o.status === 'generated')).toBe(true)
- expect(runManifestFacts).toHaveBeenCalled()
+ expect(
+ vi.mocked(runManifestFacts).mock.calls.map(([opts]) => opts.bin),
+ ).toEqual(['ABOVE-BIN'])
} finally {
await fs.rm(outer, { recursive: true, force: true })
}
@@ -1156,8 +1327,7 @@ describe('generateRecursiveManifests', () => {
})
it('resolves settings once per directory and drops a --file in mavenOpts with a warning', async () => {
- vi.mocked(readSocketJsonCascade).mockClear()
- vi.mocked(readOrDefaultSocketJson).mockReturnValue({
+ mockCascade(() => ({
defaults: {
manifest: {
maven: {
@@ -1166,7 +1336,8 @@ describe('generateRecursiveManifests', () => {
},
},
},
- } as SocketJson)
+ }))
+ vi.mocked(readSocketJsonChain).mockClear()
vi.mocked(runManifestFacts).mockImplementation(
async ({ cwd, entryFile }) => ({
factsPath: path.join(cwd, `${entryFile}.socket.facts.json`),
@@ -1179,7 +1350,7 @@ describe('generateRecursiveManifests', () => {
await generateRecursiveManifests({ cwd: pomNames, verbose: false })
const cascadeDirs = vi
- .mocked(readSocketJsonCascade)
+ .mocked(readSocketJsonChain)
.mock.calls.map(([dir]) => dir)
expect(cascadeDirs.length).toBeGreaterThan(0)
expect(cascadeDirs).toEqual([...new Set(cascadeDirs)])
@@ -1197,4 +1368,511 @@ describe('generateRecursiveManifests', () => {
warnSpy.mockRestore()
}
})
+
+ describe('with a Gradle build nested in another that includes its directory', () => {
+ let outer = ''
+ const gradleFacts = (cwd: string, dirs: string[]) => ({
+ factsPath: path.join(cwd, 'gradle.socket.facts.json'),
+ projects: dirs.map(subprojectDir => ({
+ type: 'maven' as const,
+ name: subprojectDir,
+ id: subprojectDir,
+ subprojectDir,
+ children: [],
+ })),
+ })
+
+ beforeEach(async () => {
+ outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'nested-gradle-build-')),
+ )
+ for (const file of [
+ 'settings.gradle',
+ 'build.gradle',
+ 'sub/settings.gradle',
+ 'sub/build.gradle',
+ 'sub/a/build.gradle',
+ ]) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(path.dirname(path.join(outer, file)), {
+ recursive: true,
+ })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(path.join(outer, file), '')
+ }
+ })
+ afterEach(async () => {
+ await fs.rm(outer, { recursive: true, force: true })
+ })
+
+ it.for([
+ // sub's own settings include `a`, which the outer build does not load.
+ [['.', 'a'], 'generated'],
+ // Everything sub loads, the outer build loads too.
+ [['.'], 'skippedCovered'],
+ ] as const)(
+ 'runs the nested build and keeps it only when it loads more (%j)',
+ async ({ 0: subProjects, 1: subStatus }) => {
+ const sub = path.join(outer, 'sub')
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) =>
+ cwd === outer
+ ? gradleFacts(cwd, [
+ '.',
+ 'sub',
+ ...(subProjects.length > 1 ? [] : ['sub/a']),
+ ])
+ : gradleFacts(cwd, [...subProjects]),
+ )
+
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ purlTypes: ['maven'],
+ verbose: false,
+ })
+
+ const byDir = new Map(
+ outcomes
+ .filter(o => o.ecosystem === 'gradle')
+ .map(o => [path.relative(outer, o.dir) || '.', o.status]),
+ )
+ expect(byDir.get('.')).toBe('generated')
+ expect(byDir.get('sub')).toBe(subStatus)
+ expect(byDir.get('sub/a')).toBe('skippedCovered')
+ expect(
+ vi
+ .mocked(runManifestFacts)
+ .mock.calls.map(([o]) => path.relative(outer, o.cwd) || '.'),
+ ).toEqual(['.', 'sub'])
+ },
+ )
+
+ it('ignores the failure of a nested build of its own when the build that includes its directory succeeds', async () => {
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => {
+ if (cwd === path.join(outer, 'sub')) {
+ process.exitCode = 1
+ return null
+ }
+ return gradleFacts(cwd, ['.', 'sub', 'sub/a'])
+ })
+ vi.spyOn(logger, 'info').mockImplementation(() => logger)
+
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ purlTypes: ['maven'],
+ verbose: false,
+ })
+
+ expect(
+ outcomes.find(o => o.dir === path.join(outer, 'sub'))?.status,
+ ).toBe('skippedCovered')
+ expect(outcomes.some(o => o.status === 'failed')).toBe(false)
+ expect(process.exitCode).toBeUndefined()
+ })
+
+ it.for([
+ // `a` is a project of sub: skipped with what sub loads.
+ [['.', 'a'], 'and its 1 module'],
+ // `a` isn't: Gradle can only run it through sub, which is skipped.
+ [['.'], ''],
+ ] as const)(
+ 'skips a skipped settings build and the Gradle directories beneath it that belong to it (%j)',
+ async ({ 0: subProjects, 1: modules }) => {
+ const sub = path.join(outer, 'sub')
+ mockOwnSettings({
+ [sub]: {
+ defaults: {
+ manifest: { builds: { 'settings.gradle': { disabled: true } } },
+ },
+ },
+ })
+ vi.mocked(enumerateWorkspaces).mockResolvedValue({
+ buildRoot: sub,
+ projects: gradleFacts(sub, [...subProjects]).projects,
+ })
+ vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) =>
+ gradleFacts(cwd, ['.']),
+ )
+ const warnSpy = vi
+ .spyOn(logger, 'warn')
+ .mockImplementation(() => logger)
+
+ try {
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ purlTypes: ['maven'],
+ verbose: false,
+ })
+
+ const byDir = new Map(
+ outcomes.map(o => [path.relative(outer, o.dir) || '.', o.status]),
+ )
+ expect(byDir.get('sub')).toBe('skippedDisabled')
+ expect(byDir.get('sub/a')).toBe('skippedDisabled')
+ expect(
+ vi
+ .mocked(runManifestFacts)
+ .mock.calls.map(([o]) => path.relative(outer, o.cwd) || '.'),
+ ).toEqual(['.'])
+ expect(warnSpy.mock.calls.map(c => String(c[0]))).toEqual([
+ expect.stringMatching(
+ new RegExp(
+ `^Skipping \\S*sub${modules ? ` ${modules}` : ''}: \\S*sub/socket\\.json sets defaults\\.manifest\\.builds\\["settings\\.gradle"\\]\\.disabled\\.$`,
+ ),
+ ),
+ ])
+ } finally {
+ warnSpy.mockRestore()
+ }
+ },
+ )
+ })
+
+ it('configures each POM of a directory through its own builds entry', async () => {
+ mockOwnSettings({
+ [pomNames]: {
+ defaults: {
+ manifest: {
+ maven: { mavenOpts: '-B' },
+ builds: { 'app-dev.pom.xml': { mavenOpts: '-Pdev' } },
+ },
+ },
+ },
+ })
+ vi.mocked(runManifestFacts).mockImplementation(
+ async ({ cwd, entryFile }) => ({
+ factsPath: path.join(cwd, `${entryFile}.socket.facts.json`),
+ projects: [],
+ }),
+ )
+
+ await generateRecursiveManifests({ cwd: pomNames, verbose: false })
+
+ const optsByRoot = new Map(
+ vi
+ .mocked(runManifestFacts)
+ .mock.calls.map(([o]) => [
+ path.relative(pomNames, path.join(o.cwd, o.entryFile ?? '')),
+ o.buildOpts,
+ ]),
+ )
+ expect(optsByRoot.get('app-dev.pom.xml')).toEqual(['-Pdev'])
+ expect(optsByRoot.get('pom.xml')).toEqual(['-B'])
+ })
+
+ it('warns that a builds entry naming a member of another build does not apply', async () => {
+ mockOwnSettings({
+ [path.join(reactor, 'moduleA')]: {
+ defaults: {
+ manifest: { builds: { 'pom.xml': { mavenOpts: '-Pa' } } },
+ },
+ },
+ })
+ vi.mocked(runManifestFacts).mockImplementation(
+ async ({ cwd, ecosystem }) => ({
+ factsPath: path.join(cwd, '.socket.facts.json'),
+ projects:
+ cwd === reactor && ecosystem === 'maven'
+ ? [
+ {
+ type: 'maven',
+ name: 'moduleA',
+ id: 'moduleA',
+ subprojectDir: 'moduleA',
+ manifestFiles: [{ file: 'moduleA/pom.xml' }],
+ children: [],
+ },
+ ]
+ : [],
+ }),
+ )
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+
+ try {
+ const outcomes = await generateRecursiveManifests({
+ cwd: monorepo,
+ verbose: false,
+ })
+
+ expect(
+ outcomes.find(
+ o => o.ecosystem === 'maven' && relOf(o.dir) === 'reactor/moduleA',
+ )?.status,
+ ).toBe('skippedCovered')
+ const warned = warnSpy.mock.calls.map(c => String(c[0]))
+ expect(
+ warned.filter(w => w.includes('defaults.manifest.builds["pom.xml"]')),
+ ).toEqual([
+ expect.stringMatching(
+ /in \S*reactor\/moduleA\/socket\.json does not apply: \S*reactor\/pom\.xml builds \S*reactor\/moduleA\/pom\.xml with its own settings\.$/,
+ ),
+ ])
+ } finally {
+ warnSpy.mockRestore()
+ }
+ })
+
+ describe('with a Maven build skipped by its own entry', () => {
+ let outer = ''
+ const pom = (rel: string) => path.join(outer, rel, 'pom.xml')
+
+ beforeEach(async () => {
+ outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'skipped-reactor-')),
+ )
+ for (const rel of ['aaa-module', 'legacy', 'legacy/old', 'legacy/tool']) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(path.join(outer, rel), { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(pom(rel), '')
+ }
+ await fs.writeFile(
+ path.join(outer, 'legacy/legacy-keep.pom.xml'),
+ '',
+ )
+ mockOwnSettings({
+ [path.join(outer, 'legacy')]: {
+ defaults: {
+ manifest: { builds: { 'pom.xml': { disabled: true } } },
+ },
+ },
+ })
+ vi.mocked(runManifestFacts).mockImplementation(
+ async ({ cwd, entryFile }) => {
+ const factsPath = path.join(cwd, `${entryFile}.socket.facts.json`)
+ await fs.writeFile(factsPath, 'generated')
+ return {
+ factsPath,
+ projects: [
+ {
+ ...mavenProject('.'),
+ manifestFiles: [{ file: entryFile ?? 'pom.xml' }],
+ },
+ ],
+ }
+ },
+ )
+ })
+ afterEach(async () => {
+ await fs.rm(outer, { recursive: true, force: true })
+ })
+
+ it('loads it without resolving, and skips what it loads with it, wherever that is', async () => {
+ vi.mocked(enumerateWorkspaces).mockResolvedValue({
+ buildRoot: path.join(outer, 'legacy'),
+ projects: [
+ mavenProject('.'),
+ mavenProject('old'),
+ mavenProject('../aaa-module'),
+ ],
+ })
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+
+ try {
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ verbose: false,
+ })
+
+ const byRoot = new Map(
+ outcomes.map(o => [path.relative(outer, buildRootPath(o)), o.status]),
+ )
+ expect(Object.fromEntries(byRoot)).toEqual({
+ 'aaa-module/pom.xml': 'skippedDisabled',
+ 'legacy/legacy-keep.pom.xml': 'generated',
+ 'legacy/pom.xml': 'skippedDisabled',
+ 'legacy/old/pom.xml': 'skippedDisabled',
+ 'legacy/tool/pom.xml': 'generated',
+ })
+ // aaa-module sorts first, so it ran before legacy was loaded.
+ expect(
+ existsSync(path.join(outer, 'aaa-module/pom.xml.socket.facts.json')),
+ ).toBe(false)
+ expect(enumerateWorkspaces).toHaveBeenCalledWith(
+ expect.objectContaining({
+ cwd: path.join(outer, 'legacy'),
+ entryFile: 'pom.xml',
+ }),
+ )
+ expect(warnSpy.mock.calls.map(c => String(c[0]))).toEqual([
+ expect.stringMatching(
+ /^Skipping \S*legacy\/pom\.xml and its 2 modules: \S*legacy\/socket\.json sets defaults\.manifest\.builds\["pom\.xml"\]\.disabled\.$/,
+ ),
+ ])
+ } finally {
+ warnSpy.mockRestore()
+ }
+ })
+
+ it('reports a skipped build that another build includes as part of that build, warning that the skip does not apply', async () => {
+ await fs.mkdir(path.join(outer, 'zzz-reactor'), { recursive: true })
+ await fs.writeFile(pom('zzz-reactor'), '')
+ vi.mocked(enumerateWorkspaces).mockResolvedValue({
+ buildRoot: path.join(outer, 'legacy'),
+ projects: [mavenProject('.')],
+ })
+ vi.mocked(runManifestFacts).mockImplementation(
+ async ({ cwd, entryFile }) => ({
+ factsPath: path.join(cwd, `${entryFile}.socket.facts.json`),
+ projects:
+ cwd === path.join(outer, 'zzz-reactor')
+ ? [mavenProject('.'), mavenProject('../legacy')]
+ : [
+ {
+ ...mavenProject('.'),
+ manifestFiles: [{ file: entryFile ?? 'pom.xml' }],
+ },
+ ],
+ }),
+ )
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+
+ try {
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ verbose: false,
+ })
+
+ expect(
+ outcomes.find(
+ o =>
+ o.dir === path.join(outer, 'legacy') && o.entryFile === 'pom.xml',
+ )?.status,
+ ).toBe('skippedCovered')
+ const warned = warnSpy.mock.calls.map(c => String(c[0]))
+ expect(warned.some(w => w.startsWith('Skipping'))).toBe(false)
+ expect(warned).toContainEqual(
+ expect.stringMatching(
+ /does not apply: \S*zzz-reactor\/pom\.xml builds \S*legacy\/pom\.xml/,
+ ),
+ )
+ } finally {
+ warnSpy.mockRestore()
+ }
+ })
+
+ it('fails, without building what lies beneath it, when it cannot be loaded', async () => {
+ vi.mocked(enumerateWorkspaces).mockImplementation(async () => {
+ process.exitCode = 1
+ return undefined
+ })
+ const failSpy = vi.spyOn(logger, 'fail').mockImplementation(() => logger)
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+
+ try {
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ verbose: false,
+ })
+
+ const byRoot = new Map(
+ outcomes.map(o => [path.relative(outer, buildRootPath(o)), o.status]),
+ )
+ expect(byRoot.get('legacy/pom.xml')).toBe('failed')
+ expect(byRoot.get('legacy/old/pom.xml')).toBe('aborted')
+ expect(byRoot.get('aaa-module/pom.xml')).toBe('generated')
+ expect(String(failSpy.mock.calls.at(-1)?.[0])).toMatch(
+ /^Could not load \S*legacy\/pom\.xml .* with `socket manifest setup --dynamic-sbom-inference`\.$/,
+ )
+ expect(process.exitCode).toBe(1)
+ } finally {
+ failSpy.mockRestore()
+ warnSpy.mockRestore()
+ }
+ })
+ })
+
+ it('reports a build a kept build covers as part of it, even when a skipped build loads it too or its own entry skips it', async () => {
+ const outer = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'covered-solution-')),
+ )
+ const at = (rel: string) => path.join(outer, rel)
+ const projectRecord = (file: string) => ({
+ type: 'nuget' as const,
+ name: file,
+ id: file,
+ subprojectDir: path.dirname(file),
+ manifestFiles: [{ file }],
+ children: [],
+ })
+ const members: Record = {
+ 'App.sln': [at('p/a/A.csproj')],
+ 'R.sln': [at('p/a/A.csproj'), at('p/c/C.csproj')],
+ 'Skip.sln': [at('p/a/A.csproj'), at('gone/Gone.csproj')],
+ 'Y.sln': [at('p/a/A.csproj')],
+ }
+ try {
+ for (const rel of [
+ 'p/a/A.csproj',
+ 'p/c/C.csproj',
+ 'r/R.sln',
+ 'skip/Skip.sln',
+ 'y/Y.sln',
+ 'z/App.sln',
+ ]) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(path.dirname(at(rel)), { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(at(rel), '')
+ }
+ mockOwnSettings({
+ [at('skip')]: {
+ defaults: {
+ manifest: { builds: { 'Skip.sln': { disabled: true } } },
+ },
+ },
+ [at('y')]: {
+ defaults: { manifest: { builds: { 'Y.sln': { disabled: true } } } },
+ },
+ })
+ vi.mocked(listDotnetEntryProjects).mockImplementation(
+ async ({ entryFile }) => members[entryFile ?? ''],
+ )
+ vi.mocked(enumerateWorkspaces).mockResolvedValue({
+ buildRoot: at('skip'),
+ projects: [projectRecord('../p/a/A.csproj')],
+ })
+ vi.mocked(runManifestFacts).mockImplementation(
+ async ({ cwd, entryFile }) => ({
+ factsPath: path.join(cwd, `${entryFile}.socket.facts.json`),
+ projects:
+ entryFile === 'R.sln'
+ ? [
+ projectRecord('../p/a/A.csproj'),
+ projectRecord('../p/c/C.csproj'),
+ ]
+ : [projectRecord(entryFile ?? '')],
+ }),
+ )
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+
+ const outcomes = await generateRecursiveManifests({
+ cwd: outer,
+ purlTypes: ['nuget'],
+ verbose: false,
+ })
+
+ expect(
+ Object.fromEntries(
+ outcomes.map(o => [path.relative(outer, buildRootPath(o)), o.status]),
+ ),
+ ).toEqual({
+ 'p/a/A.csproj': 'skippedCovered',
+ 'p/c/C.csproj': 'skippedCovered',
+ 'r/R.sln': 'generated',
+ 'skip/Skip.sln': 'skippedDisabled',
+ 'y/Y.sln': 'skippedCovered',
+ 'z/App.sln': 'skippedCovered',
+ })
+ expect(warnSpy.mock.calls.map(c => String(c[0]))).toContainEqual(
+ expect.stringMatching(
+ /^defaults\.manifest\.builds\["Y\.sln"\] in \S*y\/socket\.json does not apply: \S*r\/R\.sln builds \S*y\/Y\.sln with its own settings\.$/,
+ ),
+ )
+ } finally {
+ vi.mocked(listDotnetEntryProjects).mockImplementation(
+ async () => undefined,
+ )
+ await fs.rm(outer, { recursive: true, force: true })
+ }
+ })
})
diff --git a/src/commands/manifest/load-build.mts b/src/commands/manifest/load-build.mts
new file mode 100644
index 000000000..c65d48ee7
--- /dev/null
+++ b/src/commands/manifest/load-build.mts
@@ -0,0 +1,57 @@
+import { pluralize } from '@socketsecurity/registry/lib/words'
+
+import { membershipOf } from './discover-manifest-roots.mts'
+import { enumerateWorkspaces } from './enumerate-workspaces.mts'
+
+import type { BuildRoot, Membership } from './discover-manifest-roots.mts'
+import type { EcosystemBuildConfig } from './generate-recursive-manifests.mts'
+import type { BuildTool } from './scripts/build-tool.mts'
+
+export type LoadedBuild = {
+ membership: Membership
+ // The projects it loads besides its own.
+ modules: number
+}
+
+// Lets the build tool load a build, resolving nothing, for what it includes.
+export async function loadBuild({
+ config,
+ ecosystem,
+ excludePaths,
+ root,
+ scanRoot,
+ verbose,
+}: {
+ config: EcosystemBuildConfig
+ ecosystem: BuildTool
+ excludePaths: string[] | undefined
+ root: BuildRoot
+ scanRoot: string
+ verbose: boolean
+}): Promise {
+ const result = await enumerateWorkspaces({
+ bin: config.bin,
+ buildOpts: config.buildOpts,
+ cwd: root.dir,
+ ecosystem,
+ entryFile: root.entryFile,
+ excludeBase: scanRoot,
+ excludePaths,
+ javaHome: config.javaHome,
+ verbose: verbose || config.verbose,
+ })
+ if (!result) {
+ return undefined
+ }
+ return {
+ membership: await membershipOf(result.buildRoot, result.projects, scanRoot),
+ modules: Math.max(0, result.projects.length - 1),
+ }
+}
+
+// `label`, followed by how many modules it has, when any.
+export function withModules(label: string, modules: number): string {
+ return modules
+ ? `${label} and its ${modules} ${pluralize('module', modules)}`
+ : label
+}
diff --git a/src/commands/manifest/run-manifest-facts.mts b/src/commands/manifest/run-manifest-facts.mts
index 3470dcae7..a0e789f9b 100644
--- a/src/commands/manifest/run-manifest-facts.mts
+++ b/src/commands/manifest/run-manifest-facts.mts
@@ -35,7 +35,7 @@ export type RunManifestFactsResult = {
// Last N non-empty lines of the captured build output, for diagnosing a crash
// without forcing a --verbose rebuild.
-function tailBuildOutput(stdout: string, stderr: string): string {
+export function tailBuildOutput(stdout: string, stderr: string): string {
const combined = [stdout, stderr]
.map(s => s.trimEnd())
.filter(Boolean)
diff --git a/src/commands/manifest/scripts/build-tool.mts b/src/commands/manifest/scripts/build-tool.mts
index 67f9b7f94..20c4a5651 100644
--- a/src/commands/manifest/scripts/build-tool.mts
+++ b/src/commands/manifest/scripts/build-tool.mts
@@ -109,14 +109,20 @@ const BUILD_TOOL_WRAPPER = {
// a directory can hold several builds, matched case-insensitively. `walkGlob`
// is a case-sensitive superset of `nocaseGlob`, for the walk.
export type BuildFiles =
- | { byFile: false; names: readonly string[] }
+ | {
+ byFile: false
+ names: readonly string[]
+ // Entries that make a directory a build of its own: a build that
+ // includes the directory as a project does not load what it defines.
+ rootMarkers: readonly string[]
+ // The tool run in a directory without a root marker loads the build of
+ // the nearest ancestor that has one, except in these directories.
+ loadsAncestorBuild?: { except: readonly string[] }
+ }
| {
byFile: true
nocaseGlob: string
walkGlob: string
- // Each project restores on its own whichever build reaches it, so a build
- // covers its projects wherever they live; solution properties aside.
- projectsResolveAlone?: true
// Within one directory, lower runs first; by name otherwise.
entryRank?: (file: string) => number
}
@@ -132,8 +138,6 @@ export const BUILD_FILES: Record = {
nocaseGlob: '*.{sln,slnx,csproj,fsproj,vbproj}',
walkGlob:
'*.{[sS][lL][nN],[sS][lL][nN][xX],[cCfFvV][sSbB][pP][rR][oO][jJ]}',
- // NuGet restores each project into its own project.assets.json.
- projectsResolveAlone: true,
// A solution covers the projects it lists.
entryRank: (file: string) => (isDotnetSolution(file) ? 0 : 1),
},
@@ -145,13 +149,17 @@ export const BUILD_FILES: Record = {
'settings.gradle',
'settings.gradle.kts',
],
+ rootMarkers: ['settings.gradle', 'settings.gradle.kts'],
+ // Gradle builds `buildSrc` on its own.
+ loadsAncestorBuild: { except: ['buildSrc'] },
},
maven: {
byFile: true,
nocaseGlob: '{*-*.,}pom{.xml,}',
walkGlob: '*[pP][oO][mM]*',
},
- sbt: { byFile: false, names: ['build.sbt'] },
+ // A subproject's own `project/` is ignored by the build that includes it.
+ sbt: { byFile: false, names: ['build.sbt'], rootMarkers: ['project'] },
} as unknown as Record
// sbt happily runs in any directory, synthesizing a default project from its
diff --git a/src/commands/manifest/scripts/sidecar.mts b/src/commands/manifest/scripts/sidecar.mts
index bce3714e8..679b1dd42 100644
--- a/src/commands/manifest/scripts/sidecar.mts
+++ b/src/commands/manifest/scripts/sidecar.mts
@@ -89,8 +89,7 @@ function sortByPurl(entries: T[]): T[] {
// Emit an entry for every SBOM component AND every first-party project: a
// top-level module is a project, not a dependency component, yet its source
// roots are where reachability starts, so the sidecar must carry them.
-// Every build writes its own facts file, so a key is accumulated once; a
-// repeated call for the same factsFile (the same build run again) overwrites.
+// A run writing a facts file another run wrote replaces its entry.
export function accumulateSidecar(
acc: SidecarAccumulator,
facts: SocketFactsSbom,
diff --git a/src/commands/manifest/setup-recursive-manifest-config.mts b/src/commands/manifest/setup-recursive-manifest-config.mts
index dee44c18f..c2759f1f0 100644
--- a/src/commands/manifest/setup-recursive-manifest-config.mts
+++ b/src/commands/manifest/setup-recursive-manifest-config.mts
@@ -3,22 +3,34 @@ import path from 'node:path'
import micromatch from 'micromatch'
+import { joinAnd } from '@socketsecurity/registry/lib/arrays'
import { logger } from '@socketsecurity/registry/lib/logger'
import { select } from '@socketsecurity/registry/lib/prompts'
+import { pluralize } from '@socketsecurity/registry/lib/words'
import {
- addCoveredBuildFiles,
+ createBuildRootSettingsResolver,
+ displayPath,
+ socketJsonPath,
+} from './build-root-config.mts'
+import {
+ addMembership,
buildRootPath,
+ emptyMembership,
findBuildToolCandidates,
- isBuildRootCovered,
- isRootCovered,
+ isBuildCovered,
+ isPathInside,
realpathOrResolved,
sortByDepthThenPath,
withoutDisabledFlags,
} from './discover-manifest-roots.mts'
-import { enumerateWorkspaces } from './enumerate-workspaces.mts'
-import { resolveEcosystemConfig } from './generate-recursive-manifests.mts'
-import { ECOSYSTEM_LABELS } from './scripts/build-tool.mts'
+import {
+ buildToolScriptOptions,
+ resolveEcosystemConfig,
+} from './generate-recursive-manifests.mts'
+import { loadBuild, withModules } from './load-build.mts'
+import { BUILD_FILES, ECOSYSTEM_LABELS } from './scripts/build-tool.mts'
+import { DSI_SETUP_COMMAND } from './scripts/resolution-report-render.mts'
import {
setupDotnet,
setupGradle,
@@ -29,20 +41,29 @@ import { SOCKET_JSON } from '../../constants.mts'
import { loadSocketConfig } from '../../utils/config.mts'
import { getOrInsert } from '../../utils/get-or-insert.mts'
import {
+ mergeSocketJsonChain,
readOrDefaultSocketJson,
- readSocketJsonCascade,
+ readSocketJsonChain,
readSocketJsonSync,
writeSocketJson,
} from '../../utils/socket-json.mts'
import { excludePathToScanIgnores } from '../scan/exclude-paths.mts'
-import type { BuildRoot } from './discover-manifest-roots.mts'
+import type {
+ DisabledBy,
+ ResolvedBuildRootSettings,
+} from './build-root-config.mts'
+import type { BuildRoot, Membership } from './discover-manifest-roots.mts'
+import type { LoadedBuild } from './load-build.mts'
import type { BuildTool } from './scripts/build-tool.mts'
import type { CResult } from '../../types.mts'
-import type { ManifestDefaults, SocketJson } from '../../utils/socket-json.mts'
+import type {
+ ManifestDefaults,
+ SocketJson,
+ SocketJsonInDir,
+} from '../../utils/socket-json.mts'
-// One prompt per directory, as settings are per directory; Maven may have
-// several POMs, so several roots, there.
+// The builds of one tool in one directory.
type Candidate = { dir: string; ecosystem: BuildTool; roots: BuildRoot[] }
const ROOT_ECOSYSTEMS: BuildTool[] = ['maven', 'gradle', 'sbt', 'dotnet']
@@ -94,7 +115,7 @@ function findExclusionRoot(
return relDir
}
-export type ScannedBuildRoots = {
+type ScannedBuildRoots = {
fullByTool: Map
includedByTool: Map
}
@@ -119,8 +140,11 @@ export async function scanBuildRoots({
return { fullByTool, includedByTool }
}
-export type DiscoveredBuildRoots = {
+type DiscoveredBuildRoots = {
excluded: ExclusionRoot[]
+ // Build files --exclude-paths matches in a directory whose other build files
+ // it does not match.
+ excludedBuilds: Array<{ ecosystem: BuildTool; root: BuildRoot }>
// Every candidate --exclude-paths didn't rule out - these get the
// interactive configure-or-inherit walk instead of a bulk write.
included: Candidate[]
@@ -144,6 +168,7 @@ export async function discoverBuildRoots({
}): Promise {
const realCwd = await realpathOrResolved(cwd)
const included: Candidate[] = []
+ const excludedBuilds: DiscoveredBuildRoots['excludedBuilds'] = []
const ignorePatterns = (excludePaths ?? []).flatMap(excludePathToScanIgnores)
const ecosystemsByRoot = new Map>()
for (const [ecosystem, fullRoots] of fullByTool) {
@@ -175,12 +200,9 @@ export async function discoverBuildRoots({
}
if (includedRoots.length) {
included.push({ dir, ecosystem, roots: includedRoots })
- // `disabled` is per directory, so it cannot exclude one build file
- // without its included siblings.
+ // A section's `disabled` would also skip the included siblings.
for (const root of excludedRoots) {
- logger.warn(
- `Not disabling ${ecosystem} in ${relDir}: --exclude-paths matches ${buildRootPath(root)} but not the other build files there. Pass the same --exclude-paths to the scan instead.`,
- )
+ excludedBuilds.push({ ecosystem, root })
}
continue
}
@@ -197,384 +219,610 @@ export async function discoverBuildRoots({
dir,
ecosystems: [...ecosystems].sort(),
})),
+ excludedBuilds,
included,
}
}
-// Fails closed: without the members' build files, covered and independent
-// candidates can't be told apart. In order, as a root's enumeration can cover
-// the roots after it.
-export async function markWorkspaceCoverage({
- covered,
- cwd,
- dir,
- ecosystem,
- excludePaths,
- rootSockJson,
- roots,
-}: {
- covered: Set
- cwd: string
- dir: string
- ecosystem: BuildTool
- excludePaths?: string[] | undefined
- rootSockJson: SocketJson
- roots: readonly BuildRoot[]
-}): Promise> {
- if (roots.every(root => isBuildRootCovered(covered, ecosystem, root))) {
- return { ok: true, data: undefined }
- }
- const cascade = readSocketJsonCascade(dir, cwd, rootSockJson)
- const { bin, buildOpts, javaHome, skipReason } = resolveEcosystemConfig(
- ecosystem,
- dir,
- cascade,
- )
- if (skipReason) {
- return { ok: true, data: undefined }
- }
+// Changes to socket.json files, kept until the user confirms them.
+type SettingsDraft = {
+ files: Map
+ // What changed in each file, for the confirmation.
+ notes: Map
+ // Resolves against the files as they are now; dropped on every change.
+ resolver: ResolveSettings | undefined
+ warned: Set
+}
- for (const root of roots) {
- if (
- // eslint-disable-next-line no-await-in-loop
- await isRootCovered(covered, ecosystem, root, {
- bin,
- excludeBase: cwd,
- excludePaths,
- toolOpts: buildOpts,
- })
- ) {
- continue
- }
- // eslint-disable-next-line no-await-in-loop
- const enumResult = await enumerateWorkspaces({
- bin,
- buildOpts,
- cwd: dir,
- ecosystem,
- entryFile: root.entryFile,
- excludeBase: cwd,
- excludePaths,
- javaHome,
- verbose: false,
- })
- if (!enumResult) {
- const relRoot = path.relative(cwd, buildRootPath(root)) || '.'
- return {
- ok: false,
- message: `Could not determine ${ecosystem} workspace layout for ${relRoot}; aborting rather than risk misclassifying its members.`,
- }
- }
- // eslint-disable-next-line no-await-in-loop
- await addCoveredBuildFiles(
- covered,
- ecosystem,
- enumResult.buildRoot,
- enumResult.projects,
- )
+type ResolveSettings = (
+ tool: BuildTool,
+ root: BuildRoot,
+) => ResolvedBuildRootSettings
+
+function createSettingsDraft(): SettingsDraft {
+ return {
+ files: new Map(),
+ notes: new Map(),
+ resolver: undefined,
+ warned: new Set(),
}
- return { ok: true, data: undefined }
}
-// Marks one exclusion root's own socket.json `disabled: true` for whichever
-// of its ecosystems aren't already disabled via cascade (an already-disabled
-// ancestor from a prior run) - a no-op write is skipped entirely so re-running
-// the wizard doesn't keep rewriting already-disabled roots.
-export async function disableExclusionRoot({
- cwd,
- dir,
- ecosystems,
- rootSockJson,
-}: {
- cwd: string
- dir: string
- ecosystems: readonly BuildTool[]
- rootSockJson: SocketJson
-}): Promise> {
- const relDir = path.relative(cwd, dir) || '.'
- const cascade = readSocketJsonCascade(dir, cwd, rootSockJson)
- const needsWrite = ecosystems.filter(
- ecosystem => getEcosystemSection(cascade, ecosystem)['disabled'] !== true,
- )
- if (!needsWrite.length) {
- return notCanceled()
+function setDraftFile(
+ draft: SettingsDraft,
+ dir: string,
+ sockJson: SocketJson,
+): void {
+ draft.files.set(dir, sockJson)
+ draft.resolver = undefined
+}
+
+function cloneSocketJson(sockJson: SocketJson): SocketJson {
+ return JSON.parse(JSON.stringify(sockJson)) as SocketJson
+}
+
+// `dir`'s own socket.json as it will be written.
+function draftSettings(draft: SettingsDraft, dir: string): SocketJson {
+ return cloneSocketJson(draft.files.get(dir) ?? readOrDefaultSocketJson(dir))
+}
+
+function stageSettings(
+ draft: SettingsDraft,
+ dir: string,
+ sockJson: SocketJson,
+ note: string,
+): void {
+ setDraftFile(draft, dir, sockJson)
+ const notes = getOrInsert(draft.notes, dir, () => [])
+ if (!notes.includes(note)) {
+ notes.push(note)
}
+}
- const ownSockJson = readOrDefaultSocketJson(dir)
- if (!ownSockJson.defaults) {
- ownSockJson.defaults = {}
+function manifestOf(sockJson: SocketJson): Record {
+ if (!sockJson.defaults) {
+ sockJson.defaults = {}
}
- if (!ownSockJson.defaults.manifest) {
- ownSockJson.defaults.manifest = {}
+ if (!sockJson.defaults.manifest) {
+ sockJson.defaults.manifest = {}
}
- const manifest = ownSockJson.defaults.manifest as Record
- for (const ecosystem of needsWrite) {
- manifest[ecosystem] = {
- ...getEcosystemSection(ownSockJson, ecosystem),
- disabled: true,
+ return sockJson.defaults.manifest as Record
+}
+
+// The socket.json files from the filesystem root down to `dir`, as they will
+// be written.
+function draftChain(draft: SettingsDraft, dir: string): SocketJsonInDir[] {
+ const byDir = new Map(
+ readSocketJsonChain(dir).map(file => [file.dir, file.sockJson]),
+ )
+ for (const { 0: fileDir, 1: sockJson } of draft.files) {
+ if (isPathInside(fileDir, dir)) {
+ byDir.set(fileDir, sockJson)
}
}
+ return [...byDir]
+ .sort((a, b) => a[0].length - b[0].length)
+ .map(({ 0: fileDir, 1: sockJson }) => ({ dir: fileDir, sockJson }))
+}
- const writeResult = await writeSocketJson(dir, ownSockJson)
- if (!writeResult.ok) {
- return writeResult
- }
- logger.success(`Disabled ${relDir} (${needsWrite.join(', ')})`)
- return notCanceled()
+function draftCascade(draft: SettingsDraft, dir: string): SocketJson {
+ return mergeSocketJsonChain(draftChain(draft, dir))
}
-// Dispatches to the right ecosystem-specific wizard. `factsOnly` skips the
-// facts/pom question - dynamic-sbom-inference never generates a pom.xml, it
-// just skips a project resolved to `facts: false` (see
-// generateRecursiveManifests' skipReason), so offering that choice here
-// would be misleading.
-async function runEcosystemWizard(
+function resolveDraftSettings(
+ draft: SettingsDraft,
ecosystem: BuildTool,
- config: Record,
-): Promise> {
- if (ecosystem === 'dotnet') {
- return await setupDotnet(config as ManifestDefaults<'dotnet'>)
- }
- if (ecosystem === 'gradle') {
- return await setupGradle(config as ManifestDefaults<'gradle'>, {
- factsOnly: true,
- })
+ root: BuildRoot,
+): ResolvedBuildRootSettings {
+ draft.resolver ??= createBuildRootSettingsResolver(
+ dir => draftChain(draft, dir),
+ draft.warned,
+ )
+ return draft.resolver(ecosystem, root)
+}
+
+// "other build", or "3 other builds".
+function otherBuilds(count: number): string {
+ return count === 1 ? 'other build' : `${count} other builds`
+}
+
+function relPath(cwd: string, file: string): string {
+ return path.relative(cwd, file) || '.'
+}
+
+// One build of the walk; a directory holding several builds of one tool has
+// a prompt, and settings, per build file.
+type WalkBuild = { ecosystem: BuildTool; root: BuildRoot; sharesDir: boolean }
+
+type Walk = {
+ builds: WalkBuild[]
+ counts: {
+ configured: number
+ covered: number
+ inherited: number
+ skipped: number
}
- if (ecosystem === 'maven') {
- return await setupMaven(config as ManifestDefaults<'maven'>)
+ // What the builds left in, and the skipped builds, load: neither is asked
+ // about again.
+ covered: Map
+ cwd: string
+ decided: Set
+ draft: SettingsDraft
+ excludePaths: string[] | undefined
+ skipped: Map
+}
+
+function buildKey({ ecosystem, root }: WalkBuild): string {
+ return `${ecosystem}\0${buildRootPath(root)}`
+}
+
+function buildLabel(cwd: string, { ecosystem, root, sharesDir }: WalkBuild) {
+ const where = sharesDir
+ ? relPath(cwd, buildRootPath(root))
+ : root.dir === cwd
+ ? 'This project'
+ : relPath(cwd, root.dir)
+ return `${where} (${ecosystem})`
+}
+
+// The build file a build's own `builds` entry is keyed by: the one its entry
+// names, else the first one in its directory.
+function buildsKeyOf(draft: SettingsDraft, { ecosystem, root }: WalkBuild) {
+ const files = BUILD_FILES[ecosystem]
+ if (files.byFile) {
+ return root.entryFile ?? ''
}
- return await setupSbt(config as ManifestDefaults<'sbt'>, { factsOnly: true })
+ return (
+ resolveDraftSettings(draft, ecosystem, root).entryKey ??
+ files.names.find(name => existsSync(path.join(root.dir, name))) ??
+ files.names[0]!
+ )
}
-// Seeds the wizard with this candidate's effective (cascaded) value for any
-// field its own file doesn't already set, so accepting every prompt
-// unchanged preserves what it currently inherits, while an actual change
-// writes an explicit override.
-export async function configureCandidate({
- cwd,
- dir,
- ecosystem,
- rootSockJson,
-}: {
- cwd: string
- dir: string
- ecosystem: BuildTool
- rootSockJson: SocketJson
-}): Promise> {
- const relDir = path.relative(cwd, dir) || '.'
- const ownSockJson = readOrDefaultSocketJson(dir)
- if (!ownSockJson.defaults) {
- ownSockJson.defaults = {}
- }
- if (!ownSockJson.defaults.manifest) {
- ownSockJson.defaults.manifest = {}
- }
-
- // Ancestor-only baseline (excludes dir's own file, unlike the cascade below)
- // so a field left exactly as shown can be told apart from one the user
- // actually set - otherwise every accepted default gets written verbatim,
- // permanently pinning it against future changes to an ancestor's value.
+type BuildsEntries = Record>
+
+// Whether a build's settings go in its own entry, rather than in its
+// directory's section, which the builds beneath inherit.
+function writesOwnEntry(draft: SettingsDraft, build: WalkBuild): boolean {
+ return (
+ build.sharesDir ||
+ !!resolveDraftSettings(draft, build.ecosystem, build.root).entryKey
+ )
+}
+
+function membershipFor(
+ map: Map,
+ ecosystem: BuildTool,
+): Membership {
+ return getOrInsert(map, ecosystem, emptyMembership)
+}
+
+function buildConfig(walk: Walk, { ecosystem, root }: WalkBuild) {
+ return resolveEcosystemConfig(
+ ecosystem,
+ root.dir,
+ resolveDraftSettings(walk.draft, ecosystem, root).sockJson,
+ )
+}
+
+// Whether a build `covered` stands for already loads `build`.
+async function isLoadedBy(
+ walk: Walk,
+ covered: Membership,
+ build: WalkBuild,
+): Promise {
+ return await isBuildCovered(
+ covered,
+ build.ecosystem,
+ build.root,
+ buildToolScriptOptions(
+ buildConfig(walk, build),
+ walk.cwd,
+ walk.excludePaths,
+ ),
+ )
+}
+
+// Keeps only answers that differ from what the build would inherit; a
+// directory section also sets defaults for the builds beneath it.
+async function configureBuild(
+ draft: SettingsDraft,
+ cwd: string,
+ build: WalkBuild,
+): Promise> {
+ const { ecosystem, root } = build
+ const key = buildsKeyOf(draft, build)
+ const own = draftSettings(draft, root.dir)
+ const manifest = manifestOf(own)
+ const builds = { ...(manifest['builds'] as BuildsEntries | undefined) }
+ const intoEntry = writesOwnEntry(draft, build)
+ const target: Record = intoEntry
+ ? { ...builds[key] }
+ : { ...getEcosystemSection(own, ecosystem) }
const inherited = getEcosystemSection(
- readSocketJsonCascade(path.dirname(dir), cwd, rootSockJson),
+ draftCascade(draft, intoEntry ? root.dir : path.dirname(root.dir)),
ecosystem,
)
- const cascade = readSocketJsonCascade(dir, cwd, rootSockJson)
const seed: Record = {
- ...getEcosystemSection(cascade, ecosystem),
- ...getEcosystemSection(ownSockJson, ecosystem),
+ ...getEcosystemSection(
+ resolveDraftSettings(draft, ecosystem, root).sockJson,
+ ecosystem,
+ ),
}
+ const seeded = Object.keys(seed)
const result = await runEcosystemWizard(ecosystem, seed)
if (!result.ok || result.data.canceled) {
return result
}
- const toWrite: Record = {}
- for (const key of Object.keys(seed)) {
- if (seed[key] !== inherited[key]) {
- toWrite[key] = seed[key]
+ const before = JSON.stringify(target)
+ // An answer the wizard removed leaves the field to inheritance.
+ for (const field of new Set([...seeded, ...Object.keys(seed)])) {
+ if (!Object.hasOwn(seed, field) || seed[field] === inherited[field]) {
+ delete target[field]
+ } else {
+ target[field] = seed[field]
}
}
- // Every field equals what dir would already inherit - writing it would just
- // be noise (own file unaffected, dir keeps inheriting exactly as before).
- if (!Object.keys(toWrite).length) {
- logger.log(`No changes for ${relDir} (${ecosystem}); nothing written.`)
+ const label = buildLabel(cwd, build)
+ if (JSON.stringify(target) === before) {
+ logger.log(`No changes for ${label}.`)
return notCanceled()
}
+ if (intoEntry) {
+ manifest['builds'] = { ...builds, [key]: target }
+ stageSettings(draft, root.dir, own, `configure ${key}`)
+ } else {
+ manifest[ecosystem] = target
+ stageSettings(draft, root.dir, own, `configure ${ecosystem} builds`)
+ }
+ logger.success(`Configured ${label}`)
+ return notCanceled()
+}
- const manifest = ownSockJson.defaults.manifest as Record
- manifest[ecosystem] = toWrite
+// Sets `disabled` for every `ecosystems` in `dir`'s section, so their builds
+// in and beneath it are skipped, unless the settings above already do.
+function disableDirectory(
+ draft: SettingsDraft,
+ dir: string,
+ ecosystems: readonly BuildTool[],
+): BuildTool[] {
+ const cascade = draftCascade(draft, dir)
+ const needed = ecosystems.filter(
+ ecosystem => getEcosystemSection(cascade, ecosystem)['disabled'] !== true,
+ )
+ if (needed.length) {
+ const own = draftSettings(draft, dir)
+ const manifest = manifestOf(own)
+ for (const ecosystem of needed) {
+ manifest[ecosystem] = {
+ ...getEcosystemSection(own, ecosystem),
+ disabled: true,
+ }
+ }
+ stageSettings(
+ draft,
+ dir,
+ own,
+ `skip ${needed.join(', ')} builds in and beneath it`,
+ )
+ }
+ return needed
+}
- const writeResult = await writeSocketJson(dir, ownSockJson)
- if (!writeResult.ok) {
- return writeResult
+// Skips just this build and what it loads, under its own build file.
+function disableBuild(draft: SettingsDraft, build: WalkBuild): void {
+ const { root } = build
+ const key = buildsKeyOf(draft, build)
+ const own = draftSettings(draft, root.dir)
+ const manifest = manifestOf(own)
+ const builds = (manifest['builds'] ?? {}) as Record<
+ string,
+ Record
+ >
+ if (builds[key]?.['disabled'] === true) {
+ return
}
- logger.success(`Configured ${relDir} (${ecosystem})`)
- return notCanceled()
+ manifest['builds'] = { ...builds, [key]: { ...builds[key], disabled: true } }
+ stageSettings(draft, root.dir, own, `skip ${key}`)
}
-type CandidateAction = 'configure' | 'inherit'
+// Turns a skipped build back on at the setting that skipped it: dropping that
+// setting where nothing above skips the build too, else overriding it.
+function enableBuild(
+ draft: SettingsDraft,
+ build: WalkBuild,
+ disabledBy: DisabledBy,
+): void {
+ const { ecosystem, root } = build
+ const own = draftSettings(draft, disabledBy.dir)
+ const manifest = manifestOf(own)
+ const target = disabledBy.key
+ ? { ...(manifest['builds'] as BuildsEntries | undefined)?.[disabledBy.key] }
+ : getEcosystemSection(own, ecosystem)
+ delete target['disabled']
+ const place = (value: Record) => {
+ if (disabledBy.key) {
+ manifest['builds'] = {
+ ...(manifest['builds'] as object),
+ [disabledBy.key]: value,
+ }
+ } else {
+ manifest[ecosystem] = value
+ }
+ }
+ place(target)
+ setDraftFile(draft, disabledBy.dir, own)
+ if (resolveDraftSettings(draft, ecosystem, root).disabledBy) {
+ place({ ...target, disabled: false })
+ }
+ stageSettings(
+ draft,
+ disabledBy.dir,
+ own,
+ disabledBy.key
+ ? `build ${disabledBy.key} again`
+ : `build ${ecosystem} builds here again`,
+ )
+}
-// No disable choice here - that's --exclude-paths' job (see
-// findExclusionRoot), so a whole subtree still collapses to one write
-// instead of one per candidate.
-async function askCandidateAction(
- relDir: string,
- ecosystem: BuildTool,
-): Promise {
+type CantLoadAction = 'configure' | 'directory' | 'stop'
+
+// The build tool could not load `build`. Fixing its settings comes first: a
+// build Socket can load is built once with its modules, and skips exactly.
+async function askCantLoad(
+ walk: Walk,
+ build: WalkBuild,
+ allowDirectory: boolean,
+): Promise {
+ const label = buildLabel(walk.cwd, build)
+ const relDir = relPath(walk.cwd, build.root.dir)
+ const beneath = walk.builds.filter(
+ other =>
+ other !== build &&
+ isPathInside(build.root.dir, other.root.dir) &&
+ !walk.decided.has(buildKey(other)),
+ ).length
+ const skipNotes = [
+ ...(beneath
+ ? [
+ `Also skips the ${otherBuilds(beneath)} in and beneath ${relDir}/. To build one of them, run \`${DSI_SETUP_COMMAND}\` in its directory.`,
+ ]
+ : []),
+ `Modules of ${relPath(walk.cwd, buildRootPath(build.root))} outside ${relDir}/ can't be found without loading it, so they are asked about separately.`,
+ ]
return (await select({
- message: `${relDir} (${ecosystem})`,
+ message: `Socket could not load ${label} (see above). Builds Socket can load are built once with all their modules, and can be skipped without skipping the builds next to them. What should happen with it?`,
choices: [
{
- name: 'Leave as-is',
- value: 'inherit',
- description:
- "Make no change - keep this project's current effective configuration",
- },
- {
- name: 'Configure',
+ name: 'Configure it and try again',
value: 'configure',
- description: 'Set bin/JDK/opts/etc. for this project specifically',
+ description: 'Set the JDK, build tool or options, then load it again',
},
+ allowDirectory
+ ? {
+ name: `Skip ${relDir}/ and everything beneath it, without loading it`,
+ value: 'directory',
+ description: skipNotes.join(' '),
+ }
+ : {
+ name: 'Go on without loading it',
+ value: 'directory',
+ description:
+ 'The builds it would include are asked about separately',
+ },
+ { name: 'Stop', value: 'stop' },
],
- default: 'inherit',
- })) as CandidateAction | null
+ default: 'configure',
+ })) as CantLoadAction | null
}
-type CandidateOutcome = 'configured' | 'inherited' | 'skipped'
+type LoadOutcome = LoadedBuild | 'directory'
-// Asks the standard configure-or-leave-as-is question and applies the
-// answer. Shared by the normal path and the post-re-enable path below, so
-// re-enabling a candidate isn't a dead end that skips straight past its own
-// bin/JDK/opts configuration.
-async function askAndApplyAction({
- cwd,
- dir,
- ecosystem,
- relDir,
- rootSockJson,
-}: {
- cwd: string
- dir: string
- ecosystem: BuildTool
- relDir: string
- rootSockJson: SocketJson
-}): Promise<
- CResult<{ canceled: boolean; outcome: 'configured' | 'inherited' }>
-> {
- const action = await askCandidateAction(relDir, ecosystem)
- if (action === undefined || action === null) {
- canceledByUser()
- return { ok: true, data: { canceled: true, outcome: 'inherited' } }
- }
- if (action === 'configure') {
- const result = await configureCandidate({
- cwd,
- dir,
- ecosystem,
- rootSockJson,
+// Loads `build`, and while that fails, asks how to go on.
+async function loadOrAsk(
+ walk: Walk,
+ build: WalkBuild,
+ allowDirectory = true,
+): Promise> {
+ for (;;) {
+ // eslint-disable-next-line no-await-in-loop
+ const loaded = await loadBuild({
+ config: buildConfig(walk, build),
+ ecosystem: build.ecosystem,
+ excludePaths: walk.excludePaths,
+ root: build.root,
+ scanRoot: walk.cwd,
+ verbose: false,
})
- if (!result.ok) {
- return result
+ if (loaded) {
+ return { ok: true, data: { canceled: false, loaded } }
+ }
+ // eslint-disable-next-line no-await-in-loop
+ const action = await askCantLoad(walk, build, allowDirectory)
+ if (action === null || action === undefined) {
+ return canceledByUser()
+ }
+ if (action === 'stop') {
+ return {
+ ok: false,
+ message: `Stopped: Socket could not load ${buildLabel(walk.cwd, build)}.`,
+ }
+ }
+ if (action === 'directory') {
+ return { ok: true, data: { canceled: false, loaded: 'directory' } }
+ }
+ // eslint-disable-next-line no-await-in-loop
+ const configured = await configureBuild(walk.draft, walk.cwd, build)
+ if (!configured.ok || configured.data.canceled) {
+ return configured
}
- return { ok: true, data: { ...result.data, outcome: 'configured' } }
}
- // 'inherit', or any unexpected value - the safe no-op default.
- return { ok: true, data: { canceled: false, outcome: 'inherited' } }
}
-// Prompts for configure/inherit, unless the cascade shows the candidate
-// disabled: offer to re-enable if that's its own file, then continue into
-// the normal prompt; stay silent if it's only inherited from an ancestor's
-// bulk write (that's noise for a large excluded subtree).
-export async function processCandidate({
- cwd,
- dir,
- ecosystem,
- rootSockJson,
-}: {
- cwd: string
- dir: string
- ecosystem: BuildTool
- rootSockJson: SocketJson
-}): Promise<
- CResult<{ canceled: boolean; outcome: CandidateOutcome; reenabled: boolean }>
-> {
- const relDir = path.relative(cwd, dir) || '.'
- const cascade = readSocketJsonCascade(dir, cwd, rootSockJson)
- if (getEcosystemSection(cascade, ecosystem)['disabled'] === true) {
- const ownSockJson = readOrDefaultSocketJson(dir)
- if (getEcosystemSection(ownSockJson, ecosystem)['disabled'] !== true) {
- // Inherited from an ancestor, not this candidate's own file - stay silent.
- return {
- ok: true,
- data: { canceled: false, outcome: 'skipped', reenabled: false },
- }
+// Skips `build`'s directory and everything beneath it, unasked.
+function skipDirectory(walk: Walk, build: WalkBuild): void {
+ const beneath = walk.builds.filter(
+ other => other !== build && isPathInside(build.root.dir, other.root.dir),
+ )
+ disableDirectory(walk.draft, build.root.dir, [
+ ...new Set([build.ecosystem, ...beneath.map(other => other.ecosystem)]),
+ ])
+ for (const other of [build, ...beneath]) {
+ if (!walk.decided.has(buildKey(other))) {
+ walk.decided.add(buildKey(other))
+ walk.counts.skipped += 1
}
+ }
+ logger.success(
+ `Skipped ${relPath(walk.cwd, build.root.dir)}/ and everything beneath it.`,
+ )
+}
- const wantsReenable = await askYesNo(
- `${relDir} (${ecosystem}) is disabled - re-enable it?`,
- )
- if (wantsReenable === undefined || wantsReenable === null) {
- canceledByUser()
- return {
- ok: true,
- data: { canceled: true, outcome: 'skipped', reenabled: false },
- }
+// A scan skips the modules of a build skipped under its own name, so the
+// wizard doesn't ask about them either.
+async function loadInto(
+ walk: Walk,
+ build: WalkBuild,
+ into: 'covered' | 'skipped',
+ { allowDirectory = true, announce = true } = {},
+): Promise> {
+ const result = await loadOrAsk(walk, build, allowDirectory)
+ if (!result.ok || result.data.canceled) {
+ return result
+ }
+ const { loaded } = result.data
+ if (loaded === 'directory') {
+ if (allowDirectory) {
+ skipDirectory(walk, build)
}
- if (!wantsReenable) {
- return {
- ok: true,
- data: { canceled: false, outcome: 'skipped', reenabled: false },
+ } else if (loaded) {
+ addMembership(membershipFor(walk[into], build.ecosystem), loaded.membership)
+ if (into === 'skipped') {
+ disableBuild(walk.draft, build)
+ walk.counts.skipped += 1
+ if (announce) {
+ logger.success(
+ `Skipped ${withModules(relPath(walk.cwd, buildRootPath(build.root)), loaded.modules)}.`,
+ )
}
}
- if (!ownSockJson.defaults) {
- ownSockJson.defaults = {}
- }
- if (!ownSockJson.defaults.manifest) {
- ownSockJson.defaults.manifest = {}
+ }
+ return notCanceled()
+}
+
+type BuildAction = 'configure' | 'inherit' | 'skip'
+
+async function askBuildAction(label: string): Promise {
+ return (await select({
+ message: label,
+ choices: [
+ {
+ name: 'Leave as-is',
+ value: 'inherit',
+ description: 'Build it with its current settings',
+ },
+ {
+ name: 'Configure',
+ value: 'configure',
+ description: 'Set the JDK, build tool or options for it',
+ },
+ {
+ name: 'Skip',
+ value: 'skip',
+ description: "Don't build it or its modules",
+ },
+ ],
+ default: 'inherit',
+ })) as BuildAction | null
+}
+
+type LoadDecision = { announce: boolean; into: 'covered' | 'skipped' }
+
+function decided(
+ load: LoadDecision,
+): CResult<{ canceled: boolean; load: LoadDecision }> {
+ return { ok: true, data: { canceled: false, load } }
+}
+
+// What to load `build` into, as decided; none for a build skipped unloaded.
+async function reviewBuild(
+ walk: Walk,
+ build: WalkBuild,
+): Promise> {
+ walk.decided.add(buildKey(build))
+ const label = buildLabel(walk.cwd, build)
+ const { disabledBy } = resolveDraftSettings(
+ walk.draft,
+ build.ecosystem,
+ build.root,
+ )
+ if (disabledBy) {
+ // A directory skip from above covers this build silently; the setting
+ // that names this build or its directory is offered back.
+ if (!disabledBy.key && disabledBy.dir !== build.root.dir) {
+ walk.counts.skipped += 1
+ return notCanceled()
}
- const manifest = ownSockJson.defaults.manifest as Record
- manifest[ecosystem] = {
- ...getEcosystemSection(ownSockJson, ecosystem),
- disabled: false,
+ const again = await askYesNo(`${label} is skipped. Build it again?`)
+ if (again === null || again === undefined) {
+ return canceledByUser()
}
- const writeResult = await writeSocketJson(dir, ownSockJson)
- if (!writeResult.ok) {
- return writeResult
+ if (!again) {
+ if (!disabledBy.key) {
+ walk.counts.skipped += 1
+ return notCanceled()
+ }
+ return decided({ announce: false, into: 'skipped' })
}
- logger.success(`Re-enabled ${relDir} (${ecosystem})`)
+ enableBuild(walk.draft, build, disabledBy)
+ }
- const result = await askAndApplyAction({
- cwd,
- dir,
- ecosystem,
- relDir,
- rootSockJson,
- })
- if (!result.ok) {
- return result
+ const action = await askBuildAction(label)
+ if (action === null || action === undefined) {
+ return canceledByUser()
+ }
+ if (action === 'skip') {
+ return decided({ announce: true, into: 'skipped' })
+ }
+ if (action === 'configure') {
+ const configured = await configureBuild(walk.draft, walk.cwd, build)
+ if (!configured.ok || configured.data.canceled) {
+ return configured
}
- return { ok: true, data: { ...result.data, reenabled: true } }
+ walk.counts.configured += 1
+ } else {
+ walk.counts.inherited += 1
}
+ return decided({ announce: true, into: 'covered' })
+}
- const result = await askAndApplyAction({
- cwd,
- dir,
- ecosystem,
- relDir,
- rootSockJson,
- })
- if (!result.ok) {
- return result
+// Dispatches to the right ecosystem-specific wizard. `factsOnly` skips the
+// facts/pom question - dynamic-sbom-inference never generates a pom.xml, it
+// just skips a project resolved to `facts: false` (see
+// generateRecursiveManifests' skipReason), so offering that choice here
+// would be misleading.
+async function runEcosystemWizard(
+ ecosystem: BuildTool,
+ config: Record,
+): Promise> {
+ if (ecosystem === 'dotnet') {
+ return await setupDotnet(config as ManifestDefaults<'dotnet'>)
}
- return { ok: true, data: { ...result.data, reenabled: false } }
+ if (ecosystem === 'gradle') {
+ return await setupGradle(config as ManifestDefaults<'gradle'>, {
+ factsOnly: true,
+ })
+ }
+ if (ecosystem === 'maven') {
+ return await setupMaven(config as ManifestDefaults<'maven'>)
+ }
+ return await setupSbt(config as ManifestDefaults<'sbt'>, { factsOnly: true })
}
-// Accepting every prompt's default leaves an ecosystem's section genuinely
-// empty (see askForBin in setup-manifest-config.mts) - drop it so
-// `configuredAny` reflects what was actually configured, not just which
-// ecosystems the user said "yes" to.
+// Accepting every prompt's default leaves an ecosystem's section empty (see
+// askForBin in setup-manifest-config.mts); an empty one changes nothing.
function dropIfEmpty(
manifest: Record,
ecosystem: BuildTool,
@@ -597,74 +845,49 @@ async function askYesNo(message: string): Promise {
})) as boolean | null
}
-// Unlike the plain single-project wizard, cwd here is often just a common
-// ancestor - so `ecosystemsAtCwd` (a subset of `ecosystems`) picks the
-// wording: cwd's own build root gets "configure this project's settings",
-// everything else gets "configure defaults for nested projects". A
-// pre-existing `disabled: true` gets a re-enable question first, otherwise
-// there'd be no way back from a prior disable. Declining everything is
-// normal, not an abort.
-async function setupRecursiveRootDefaults(
+// Defaults for the builds in and beneath cwd. A skip that applies here, from
+// this directory's socket.json or one above it, is offered back first. A tool's
+// only build at cwd, without an entry of its own, sets them from its prompt.
+async function setupRootDefaults(
+ draft: SettingsDraft,
cwd: string,
- defaultOnReadError: boolean,
ecosystems: readonly BuildTool[],
- ecosystemsAtCwd: readonly BuildTool[],
+ buildsAtCwd: (ecosystem: BuildTool) => BuildRoot[],
): Promise> {
- const jsonPath = path.join(cwd, SOCKET_JSON)
- if (existsSync(jsonPath)) {
- logger.info(`Found ${SOCKET_JSON} at ${jsonPath}`)
- } else {
- logger.info(`No ${SOCKET_JSON} found at ${cwd}, will generate a new one`)
- }
-
- logger.log('')
- logger.log(
- 'Note: This tool will set up flag and argument defaults for certain',
- )
- logger.log(' CLI commands. You can still override them by explicitly')
- logger.log(' setting the flag.')
- logger.log('')
- logger.log(`This command will generate a ${SOCKET_JSON} file in ${cwd}.`)
- logger.log('socket.json properties are inherited by nested paths.')
- logger.log('')
-
- const sockJsonCResult = readSocketJsonSync(cwd, defaultOnReadError)
- if (!sockJsonCResult.ok) {
- return sockJsonCResult
- }
- const sockJson = sockJsonCResult.data
- if (!sockJson.defaults) {
- sockJson.defaults = {}
- }
- if (!sockJson.defaults.manifest) {
- sockJson.defaults.manifest = {}
- }
- const manifest = sockJson.defaults.manifest as Record
-
- let configuredAny = false
-
for (const ecosystem of ecosystems) {
const label = ECOSYSTEM_LABELS[ecosystem]
- const existingSection = manifest[ecosystem] as
- | Record
- | undefined
- if (existingSection?.['disabled'] === true) {
+ const { disabledBy } = resolveDraftSettings(draft, ecosystem, { dir: cwd })
+ if (disabledBy && !disabledBy.key) {
+ const where =
+ disabledBy.dir === cwd
+ ? `${label} builds here are skipped.`
+ : `${socketJsonPath(disabledBy.dir)} skips ${label} builds in and beneath ${displayPath(disabledBy.dir)}, including this directory.`
// eslint-disable-next-line no-await-in-loop
- const wantsReenable = await askYesNo(
- `${label} is currently disabled here - re-enable it?`,
- )
- if (wantsReenable === undefined || wantsReenable === null) {
+ const again = await askYesNo(`${where} Build the ${label} projects here?`)
+ if (again === null || again === undefined) {
return canceledByUser()
}
- if (!wantsReenable) {
+ if (!again) {
continue
}
- existingSection['disabled'] = false
- configuredAny = true
+ const own = draftSettings(draft, cwd)
+ const manifest = manifestOf(own)
+ manifest[ecosystem] = {
+ ...getEcosystemSection(own, ecosystem),
+ disabled: false,
+ }
+ stageSettings(draft, cwd, own, `build ${label} projects here again`)
}
- const message = ecosystemsAtCwd.includes(ecosystem)
- ? `Configure ${label} settings for this project?`
+ const atCwd = buildsAtCwd(ecosystem)
+ if (
+ atCwd.length === 1 &&
+ !writesOwnEntry(draft, { ecosystem, root: atCwd[0]!, sharesDir: false })
+ ) {
+ continue
+ }
+ const message = atCwd.length
+ ? `Configure ${label} defaults for the projects here and beneath?`
: `Configure ${label} defaults for any nested projects?`
// eslint-disable-next-line no-await-in-loop
const wants = await askYesNo(message)
@@ -674,48 +897,59 @@ async function setupRecursiveRootDefaults(
if (!wants) {
continue
}
- if (!manifest[ecosystem]) {
- manifest[ecosystem] = {}
- }
+ const own = draftSettings(draft, cwd)
+ const manifest = manifestOf(own)
+ const section = { ...getEcosystemSection(own, ecosystem) }
+ manifest[ecosystem] = section
// eslint-disable-next-line no-await-in-loop
- const result = await runEcosystemWizard(
- ecosystem,
- manifest[ecosystem] as Record,
- )
+ const result = await runEcosystemWizard(ecosystem, section)
if (!result.ok || result.data.canceled) {
return result
}
if (dropIfEmpty(manifest, ecosystem)) {
- configuredAny = true
+ stageSettings(draft, cwd, own, `configure ${label} defaults`)
}
}
+ return notCanceled()
+}
- if (!configuredAny) {
- logger.log('')
- logger.log('No root-level defaults configured.')
+async function confirmAndWrite(
+ draft: SettingsDraft,
+ cwd: string,
+): Promise> {
+ if (!draft.files.size) {
+ logger.log('Nothing to write: no settings changed.')
return notCanceled()
}
-
- logger.log('')
- logger.log(`Writing ${SOCKET_JSON} to ${jsonPath}`)
- logger.log('')
-
- const writeResult = await writeSocketJson(cwd, sockJson)
- if (!writeResult.ok) {
- return writeResult
+ const dirs = [...draft.files.keys()].sort()
+ const files = dirs.map(dir => relPath(cwd, path.join(dir, SOCKET_JSON)))
+ const width = Math.max(...files.map(file => file.length))
+ logger.log(`Ready to write ${dirs.length} ${pluralize('file', dirs.length)}:`)
+ for (const { 0: index, 1: dir } of dirs.entries()) {
+ logger.log(
+ ` ${files[index]!.padEnd(width)} ${(draft.notes.get(dir) ?? []).join('; ')}`,
+ )
}
+ const write = await askYesNo(dirs.length === 1 ? 'Write it?' : 'Write them?')
+ if (write === null || write === undefined) {
+ return canceledByUser()
+ }
+ if (!write) {
+ logger.log('Nothing written.')
+ return notCanceled()
+ }
+ for (const dir of dirs) {
+ // eslint-disable-next-line no-await-in-loop
+ const result = await writeSocketJson(dir, draft.files.get(dir)!)
+ if (!result.ok) {
+ return result
+ }
+ }
+ logger.success(`Wrote ${joinAnd(files)}`)
return notCanceled()
}
-// `socket manifest setup --dynamic-sbom-inference`: the up-front scan (see
-// scanBuildRoots) only ever answers "which build tools exist", never "which
-// projects to configure". `--exclude-paths` matches are bulk-disabled
-// unconditionally, independent of the interactive walk below, so re-running
-// later with one more entry (or none) never re-answers settled decisions.
-// Everything else gets a configure-or-inherit prompt (see processCandidate),
-// except a workspace member a parent build already declares - learned via
-// markWorkspaceCoverage right after the parent's own prompt, not a separate
-// bulk pass, so a reactor member is silently skipped instead of prompted.
+// Nothing is written until the user confirms the draft.
export async function setupRecursiveManifestConfig(
cwd: string,
defaultOnReadError: boolean,
@@ -724,10 +958,11 @@ export async function setupRecursiveManifestConfig(
logger.log('')
logger.log(`Configuring ${cwd} ...`)
- const initialSockJson = readOrDefaultSocketJson(cwd)
- // Resolved once here for sortByDepthThenPath/disableExclusionRoot's
- // relative-path math, consistent with discoverBuildRoots' own internal
- // resolution (see its comment for why this matters).
+ const ownRead = readSocketJsonSync(cwd, defaultOnReadError)
+ if (!ownRead.ok) {
+ return ownRead
+ }
+ // Candidate dirs come back realpath-resolved; cwd must match.
const realCwd = await realpathOrResolved(cwd)
logger.log('')
@@ -735,11 +970,10 @@ export async function setupRecursiveManifestConfig(
const { fullByTool, includedByTool } = await scanBuildRoots({
cwd,
excludePaths,
- sockJson: withoutDisabledFlags(initialSockJson),
+ sockJson: withoutDisabledFlags(ownRead.data),
})
- // includedByTool (not the unfiltered fullByTool), so an ecosystem whose
- // only candidate is about to be excluded this same run isn't offered
- // either - there'd be nothing left for its root defaults to apply to.
+ // includedByTool, so an ecosystem whose only builds --exclude-paths
+ // excludes isn't offered.
const detectedEcosystems = ROOT_ECOSYSTEMS.filter(
ecosystem => (includedByTool.get(ecosystem)?.length ?? 0) > 0,
)
@@ -751,10 +985,7 @@ export async function setupRecursiveManifestConfig(
logger.log(
`No Gradle, Maven, sbt or .NET build roots found beneath ${cwd}.`,
)
- // Distinct from "found some, but --exclude-paths excluded them all" -
- // that's a deliberate choice and setup still proceeds normally below.
- // Zero build roots anywhere means --dynamic-sbom-inference doesn't apply
- // to this target at all, so fail rather than silently do nothing.
+ // Builds that --exclude-paths excludes are a choice, not an absence.
const hasAnyBuildRoot = ROOT_ECOSYSTEMS.some(
ecosystem => (fullByTool.get(ecosystem)?.length ?? 0) > 0,
)
@@ -768,172 +999,158 @@ export async function setupRecursiveManifestConfig(
}
logger.log('')
- // Which detected ecosystems cwd is itself a build root for - see
- // setupRecursiveRootDefaults' wording split.
- const ecosystemsAtCwd = ROOT_ECOSYSTEMS.filter(ecosystem =>
- (fullByTool.get(ecosystem) ?? []).some(root => root.dir === realCwd),
- )
-
- const rootResult = await setupRecursiveRootDefaults(
- cwd,
- defaultOnReadError,
+ const draft = createSettingsDraft()
+ const atCwd = (ecosystem: BuildTool) =>
+ (includedByTool.get(ecosystem) ?? []).filter(root => root.dir === realCwd)
+ const rootResult = await setupRootDefaults(
+ draft,
+ realCwd,
detectedEcosystems,
- ecosystemsAtCwd,
+ atCwd,
)
- if (!rootResult.ok) {
+ if (!rootResult.ok || rootResult.data.canceled) {
return rootResult
}
- if (rootResult.data.canceled) {
- return canceledByUser()
- }
- // Re-read: the root wizard may have just written a new socket.json.
- const rootSockJson = readOrDefaultSocketJson(cwd)
- const { excluded, included } = await discoverBuildRoots({
+ const { excluded, excludedBuilds, included } = await discoverBuildRoots({
cwd,
excludePaths,
fullByTool,
includedByTool,
})
-
- // Applies regardless of the interactive walk below - see the module doc
- // comment on why --exclude-paths is unconditional.
- if (excludePaths?.length) {
- if (!excluded.length) {
- logger.log('None matched --exclude-paths; nothing disabled.')
- } else {
- const orderedExcluded = sortByDepthThenPath(excluded, realCwd)
- for (const candidate of orderedExcluded) {
- // eslint-disable-next-line no-await-in-loop
- const result = await disableExclusionRoot({
- cwd: realCwd,
- dir: candidate.dir,
- ecosystems: candidate.ecosystems,
- rootSockJson,
- })
- if (!result.ok) {
- return result
- }
- }
- }
+ for (const { ecosystem, root } of excludedBuilds) {
+ disableBuild(draft, { ecosystem, root, sharesDir: true })
}
-
- if (!included.length) {
- logger.log('')
- logger.success('Recursive setup complete.')
- return notCanceled()
+ for (const { dir, ecosystems } of sortByDepthThenPath(excluded, realCwd)) {
+ disableDirectory(draft, dir, ecosystems)
}
-
- logger.log('')
- const wantsIndividual = await askYesNo(
- 'Configure other build roots found beneath this one, individually?',
- )
- if (wantsIndividual === undefined || wantsIndividual === null) {
- return canceledByUser()
- }
- if (!wantsIndividual) {
- logger.log('')
- logger.success('Recursive setup complete.')
- return notCanceled()
+ if (excludePaths?.length && !excluded.length && !excludedBuilds.length) {
+ logger.log('None matched --exclude-paths; nothing skipped.')
}
- logger.log('')
- logger.log(
- 'For each one, choose to configure it or leave it as-is. To disable one,',
+ const builds: WalkBuild[] = sortByDepthThenPath(included, realCwd).flatMap(
+ ({ ecosystem, roots }) =>
+ sortByDepthThenPath(roots, realCwd, ecosystem).map(root => ({
+ ecosystem,
+ root,
+ sharesDir: roots.length > 1,
+ })),
)
- logger.log('re-run with --exclude-paths instead.')
- logger.log('')
-
- // Per tool: a solution counts as covered only by .NET projects.
- const coveredByTool = new Map>()
- const coveredFor = (ecosystem: BuildTool) =>
- getOrInsert(coveredByTool, ecosystem, () => new Set())
- // cwd never appears in `included`, but a reactor root often sits exactly
- // at cwd - seed coverage from it first so its declared members still get
- // recognized.
- for (const ecosystem of ROOT_ECOSYSTEMS) {
- // eslint-disable-next-line no-await-in-loop
- const coverageResult = await markWorkspaceCoverage({
- covered: coveredFor(ecosystem),
- cwd: realCwd,
- dir: realCwd,
- ecosystem,
- excludePaths,
- rootSockJson,
- roots: (includedByTool.get(ecosystem) ?? []).filter(
- root => root.dir === realCwd,
- ),
- })
- if (!coverageResult.ok) {
- return coverageResult
- }
- }
-
- const counts = {
- configured: 0,
- covered: 0,
- inherited: 0,
- reenabled: 0,
- skipped: 0,
+ const walk: Walk = {
+ builds,
+ counts: { configured: 0, covered: 0, inherited: 0, skipped: 0 },
+ covered: new Map(),
+ cwd: realCwd,
+ decided: new Set(),
+ draft,
+ excludePaths,
+ skipped: new Map(),
}
- // A parent is prompted before its children, so a child's shown default
- // reflects the parent's just-written cascade.
- const orderedIncluded = sortByDepthThenPath(included, realCwd)
- for (const candidate of orderedIncluded) {
- const { ecosystem } = candidate
- if (
- candidate.roots.every(root =>
- isBuildRootCovered(coveredFor(ecosystem), ecosystem, root),
- )
- ) {
- counts.covered += 1
- continue
- }
- // eslint-disable-next-line no-await-in-loop
- const result = await processCandidate({
- cwd: realCwd,
- dir: candidate.dir,
+ // The builds at cwd are decided first, and loaded only for the review below:
+ // what they include isn't asked about.
+ const cwdBuilds: WalkBuild[] = ROOT_ECOSYSTEMS.flatMap(ecosystem => {
+ const roots = atCwd(ecosystem)
+ return roots.map(root => ({
ecosystem,
- rootSockJson,
- })
- if (!result.ok) {
+ root,
+ sharesDir: roots.length > 1,
+ }))
+ }).filter(build => {
+ const { disabledBy } = resolveDraftSettings(
+ draft,
+ build.ecosystem,
+ build.root,
+ )
+ return !disabledBy || !!disabledBy.key
+ })
+ const cwdLoads: Array<{ build: WalkBuild; into: LoadDecision['into'] }> = []
+ for (const build of cwdBuilds) {
+ // eslint-disable-next-line no-await-in-loop
+ const result = await reviewBuild(walk, build)
+ if (!result.ok || result.data.canceled) {
return result
}
- if (result.data.canceled) {
- // The cancellation itself (select Esc/Ctrl+C, or a sub-wizard's own
- // cancel) already logged "User canceled" - don't log it twice.
- return { ok: true, data: { canceled: true } }
- }
- counts[result.data.outcome] += 1
- if (result.data.reenabled) {
- // A qualifier on the 'configured'/'inherited' bucket just incremented
- // above, not a separate bucket - avoids double-counting the candidate.
- counts.reenabled += 1
+ const { load } = result.data
+ if (load) {
+ if (load.into === 'skipped') {
+ disableBuild(draft, build)
+ }
+ cwdLoads.push({ build, into: load.into })
}
+ }
- // Interleaved rather than a bulk pre-pass, so this (possibly slow)
- // build-tool invocation only runs for a project already prompted about.
- // eslint-disable-next-line no-await-in-loop
- const coverageResult = await markWorkspaceCoverage({
- covered: coveredFor(ecosystem),
- cwd: realCwd,
- dir: candidate.dir,
- ecosystem,
- excludePaths,
- rootSockJson,
- roots: candidate.roots,
- })
- if (!coverageResult.ok) {
- return coverageResult
+ if (builds.length) {
+ logger.log('')
+ const review = await askYesNo(
+ `Review the ${otherBuilds(builds.length)} found beneath this one?`,
+ )
+ if (review === null || review === undefined) {
+ return canceledByUser()
+ }
+ if (review) {
+ for (const { build, into } of cwdLoads) {
+ // eslint-disable-next-line no-await-in-loop
+ const result = await loadInto(walk, build, into, {
+ allowDirectory: false,
+ announce: false,
+ })
+ if (!result.ok || result.data.canceled) {
+ return result
+ }
+ }
+ for (const build of builds) {
+ if (walk.decided.has(buildKey(build))) {
+ continue
+ }
+ let loaded = false
+ for (const into of ['covered', 'skipped'] as const) {
+ if (
+ !loaded &&
+ // eslint-disable-next-line no-await-in-loop
+ (await isLoadedBy(
+ walk,
+ membershipFor(walk[into], build.ecosystem),
+ build,
+ ))
+ ) {
+ loaded = true
+ walk.decided.add(buildKey(build))
+ walk.counts[into] += 1
+ }
+ }
+ if (loaded) {
+ continue
+ }
+ // eslint-disable-next-line no-await-in-loop
+ const result = await reviewBuild(walk, build)
+ if (!result.ok || result.data.canceled) {
+ return result
+ }
+ const { load } = result.data
+ if (load) {
+ // eslint-disable-next-line no-await-in-loop
+ const loaded = await loadInto(walk, build, load.into, {
+ announce: load.announce,
+ })
+ if (!loaded.ok || loaded.data.canceled) {
+ return loaded
+ }
+ }
+ }
+ const { configured, covered, inherited, skipped } = walk.counts
+ logger.log('')
+ logger.log(
+ `${configured} configured, ${inherited} left as-is, ${skipped} skipped, ${covered} part of another build.`,
+ )
}
}
logger.log('')
- logger.log(
- `${counts.configured} configured (${counts.reenabled} re-enabled), ${counts.inherited} left as-is, ${counts.skipped} left disabled, ${counts.covered} covered by a parent build.`,
- )
-
+ const written = await confirmAndWrite(draft, realCwd)
+ if (!written.ok || written.data.canceled) {
+ return written
+ }
logger.log('')
logger.success('Recursive setup complete.')
return notCanceled()
diff --git a/src/commands/manifest/setup-recursive-manifest-config.test.mts b/src/commands/manifest/setup-recursive-manifest-config.test.mts
index 34ca9e0fb..d8dffe7c4 100644
--- a/src/commands/manifest/setup-recursive-manifest-config.test.mts
+++ b/src/commands/manifest/setup-recursive-manifest-config.test.mts
@@ -1,25 +1,15 @@
-import { promises as fs } from 'node:fs'
+import { existsSync, promises as fs } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
-import {
- afterAll,
- beforeAll,
- beforeEach,
- describe,
- expect,
- it,
- vi,
-} from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('./discover-manifest-roots.mts', async importOriginal => {
const actual =
await importOriginal()
return {
...actual,
- findBuildToolCandidates: vi.fn(),
- // Identity: test dirs are already-absolute plain strings, no symlinks involved.
- realpathOrResolved: vi.fn(async (dir: string) => dir),
+ findBuildToolCandidates: vi.fn(actual.findBuildToolCandidates),
}
})
vi.mock('./enumerate-workspaces.mts', () => ({
@@ -29,43 +19,26 @@ vi.mock('@socketsecurity/registry/lib/prompts', () => ({
select: vi.fn(),
}))
vi.mock('./setup-manifest-config.mts', () => ({
+ setupDotnet: vi.fn(),
setupGradle: vi.fn(),
setupMaven: vi.fn(),
setupSbt: vi.fn(),
}))
-vi.mock('../../utils/socket-json.mts', () => ({
- readOrDefaultSocketJson: vi.fn(),
- readSocketJsonCascade: vi.fn(),
- readSocketJsonSync: vi.fn(),
- writeSocketJson: vi.fn(),
-}))
import { logger } from '@socketsecurity/registry/lib/logger'
import { select } from '@socketsecurity/registry/lib/prompts'
-import {
- buildRootPath,
- findBuildToolCandidates,
-} from './discover-manifest-roots.mts'
+import { findBuildToolCandidates } from './discover-manifest-roots.mts'
import { enumerateWorkspaces } from './enumerate-workspaces.mts'
-import { setupGradle, setupMaven, setupSbt } from './setup-manifest-config.mts'
+import { setupMaven } from './setup-manifest-config.mts'
import {
- configureCandidate,
- disableExclusionRoot,
discoverBuildRoots,
- markWorkspaceCoverage,
- processCandidate,
scanBuildRoots,
setupRecursiveManifestConfig,
} from './setup-recursive-manifest-config.mts'
-import {
- readOrDefaultSocketJson,
- readSocketJsonCascade,
- readSocketJsonSync,
- writeSocketJson,
-} from '../../utils/socket-json.mts'
import type { BuildRoot } from './discover-manifest-roots.mts'
+import type { SocketFactsSbomProject } from './scripts/facts.mts'
import type { SocketJson } from '../../utils/socket-json.mts'
function dirRoots(...dirs: string[]): BuildRoot[] {
@@ -76,1435 +49,576 @@ function mavenRoots(...dirs: string[]): BuildRoot[] {
return dirs.map(dir => ({ dir, entryFile: 'pom.xml' }))
}
-function emptySockJson(): SocketJson {
- return { version: 1 } as SocketJson
-}
-
-// Declines the root questions, opts into the individual walk and leaves each
-// candidate inheriting. Returns the prompts asked, in order.
-function inheritEveryCandidate(): string[] {
- const messages: string[] = []
- vi.mocked(select).mockImplementation(async ({ message }) => {
- messages.push(message)
- if (message.startsWith('Configure other build roots')) {
- return true
- }
- return message.startsWith('Configure') ? false : 'inherit'
- })
- return messages
-}
-
-// In-memory `dir -> own gradle section` store backing readSocketJsonCascade/
-// readOrDefaultSocketJson/writeSocketJson, so a write made while processing
-// one candidate is visible (via cascade) to a later candidate nested under
-// it - the exact ordering behavior the "topmost only" tests exercise.
-function makeFakeGradleDisk(cwd: string) {
- const store = new Map>()
- const ancestorsFarToNear = (dir: string): string[] => {
- const chain: string[] = []
- let current = dir
- while (current !== cwd) {
- chain.unshift(current)
- const parent = current.slice(0, current.lastIndexOf('/'))
- if (!parent || parent === current) {
- break
- }
- current = parent
- }
- return chain
- }
+function project(subprojectDir: string): SocketFactsSbomProject {
return {
- readOrDefaultSocketJson: vi.fn((dir: string) => {
- const own = store.get(dir)
- return own
- ? ({
- version: 1,
- defaults: { manifest: { gradle: own } },
- } as SocketJson)
- : emptySockJson()
- }),
- readSocketJsonCascade: vi.fn((dir: string) => {
- let merged: Record = {}
- for (const ancestor of ancestorsFarToNear(dir)) {
- const own = store.get(ancestor)
- if (own) {
- merged = { ...merged, ...own }
- }
- }
- return {
- version: 1,
- defaults: { manifest: { gradle: merged } },
- } as SocketJson
- }),
- writeSocketJson: vi.fn(async (dir: string, sockJson: SocketJson) => {
- store.set(
- dir,
- (sockJson.defaults?.manifest?.gradle as Record) ?? {},
- )
- return { ok: true, data: undefined }
- }),
+ type: 'maven',
+ name: subprojectDir,
+ id: subprojectDir,
+ subprojectDir,
+ manifestFiles: [{ file: `${subprojectDir}/pom.xml` }],
+ children: [],
}
}
describe('scanBuildRoots', () => {
- const cwd = '/repo'
-
- beforeEach(() => {
- vi.mocked(findBuildToolCandidates).mockReset()
- })
-
it('runs the unfiltered and excludePaths-filtered walks once each and returns both', async () => {
const unfiltered = new Map([
['gradle', dirRoots('/repo/a', '/repo/legacy')],
])
const filtered = new Map([['gradle', dirRoots('/repo/a')]])
- vi.mocked(findBuildToolCandidates).mockImplementation(
- async ({ excludePaths }) =>
- excludePaths?.length ? filtered : unfiltered,
- )
+ const walk = async ({
+ excludePaths,
+ }: {
+ excludePaths?: string[] | undefined
+ }) => (excludePaths?.length ? filtered : unfiltered)
+ vi.mocked(findBuildToolCandidates)
+ .mockImplementationOnce(walk)
+ .mockImplementationOnce(walk)
const result = await scanBuildRoots({
- cwd,
+ cwd: '/repo',
excludePaths: ['legacy'],
- sockJson: emptySockJson(),
+ sockJson: { version: 1 } as SocketJson,
})
- expect(result).toEqual({
- fullByTool: unfiltered,
- includedByTool: filtered,
- })
- expect(findBuildToolCandidates).toHaveBeenCalledTimes(2)
+ expect(result).toEqual({ fullByTool: unfiltered, includedByTool: filtered })
})
})
describe('discoverBuildRoots', () => {
const cwd = '/repo'
- it('excludes cwd itself', async () => {
+ it('leaves out cwd itself', async () => {
const result = await discoverBuildRoots({
cwd,
fullByTool: new Map([['gradle', dirRoots(cwd)]]),
includedByTool: new Map([['gradle', dirRoots(cwd)]]),
})
- expect(result).toEqual({ excluded: [], included: [] })
+ expect(result).toEqual({ excluded: [], excludedBuilds: [], included: [] })
})
- it('marks a dir excluded when it is present in the full walk but absent from the excludePaths-filtered walk', async () => {
- const legacy = '/repo/legacy'
- const active = '/repo/active'
-
- const result = await discoverBuildRoots({
- cwd,
- excludePaths: ['legacy'],
- fullByTool: new Map([['gradle', dirRoots(legacy, active)]]),
- includedByTool: new Map([['gradle', dirRoots(active)]]),
- })
-
- expect(result).toEqual({
- excluded: [{ dir: legacy, ecosystems: ['gradle'] }],
- included: [{ dir: active, ecosystem: 'gradle', roots: dirRoots(active) }],
- })
- })
-
- it('groups sibling projects under a non-project excluded ancestor into a single exclusion root', async () => {
- // legacy/ itself has no build file of its own - only its two children do -
- // so it never appears as a candidate, but --exclude-paths=legacy should
- // still collapse both into one write at legacy, not two writes at
- // legacy/a and legacy/b.
- const a = '/repo/legacy/a'
- const b = '/repo/legacy/b'
-
+ it('groups the builds --exclude-paths excludes under the shallowest excluded directory', async () => {
const result = await discoverBuildRoots({
cwd,
excludePaths: ['legacy'],
fullByTool: new Map([
- ['maven', mavenRoots(a)],
- ['gradle', dirRoots(b)],
+ ['maven', mavenRoots('/repo/legacy/a', '/repo/active')],
+ ['gradle', dirRoots('/repo/legacy/b')],
]),
includedByTool: new Map([
- ['maven', mavenRoots()],
+ ['maven', mavenRoots('/repo/active')],
['gradle', dirRoots()],
]),
})
expect(result).toEqual({
excluded: [{ dir: '/repo/legacy', ecosystems: ['gradle', 'maven'] }],
- included: [],
+ excludedBuilds: [],
+ included: [
+ {
+ dir: '/repo/active',
+ ecosystem: 'maven',
+ roots: mavenRoots('/repo/active'),
+ },
+ ],
})
})
- it('does not disable a directory to exclude one POM while a sibling POM there stays included', async () => {
+ it('excludes one POM by itself while a sibling POM there stays included', async () => {
const service = '/repo/service'
- const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
-
- try {
- const result = await discoverBuildRoots({
- cwd,
- excludePaths: ['service/app-dev.pom.xml'],
- fullByTool: new Map([
+ const result = await discoverBuildRoots({
+ cwd,
+ excludePaths: ['service/app-dev.pom.xml'],
+ fullByTool: new Map([
+ [
+ 'maven',
[
- 'maven',
- [
- { dir: service, entryFile: 'app-dev.pom.xml' },
- { dir: service, entryFile: 'pom.xml' },
- ],
+ { dir: service, entryFile: 'app-dev.pom.xml' },
+ { dir: service, entryFile: 'pom.xml' },
],
- ]),
- includedByTool: new Map([['maven', mavenRoots(service)]]),
- })
-
- expect(result).toEqual({
- excluded: [],
- included: [
- { dir: service, ecosystem: 'maven', roots: mavenRoots(service) },
],
- })
- expect(warnSpy).toHaveBeenCalledWith(
- expect.stringContaining('Not disabling maven in service'),
- )
- } finally {
- warnSpy.mockRestore()
- }
- })
-})
-
-describe('markWorkspaceCoverage', () => {
- // Coverage counts only build files present on disk.
- let cwd: string
- let reactor: string
-
- beforeAll(async () => {
- cwd = await fs.realpath(
- await fs.mkdtemp(path.join(tmpdir(), 'workspace-coverage-')),
- )
- reactor = path.join(cwd, 'reactor')
- for (const file of [
- 'reactor/pom.xml',
- 'reactor/moduleA/pom.xml',
- 'reactor/moduleB/alt.xml',
- 'reactor/build/member/pom.xml',
- 'shared-lib/pom.xml',
- ]) {
- // eslint-disable-next-line no-await-in-loop
- await fs.mkdir(path.dirname(path.join(cwd, file)), { recursive: true })
- // eslint-disable-next-line no-await-in-loop
- await fs.writeFile(path.join(cwd, file), '')
- }
- })
- afterAll(async () => {
- await fs.rm(cwd, { recursive: true, force: true })
- })
-
- beforeEach(() => {
- vi.mocked(readSocketJsonCascade).mockReset()
- vi.mocked(readSocketJsonCascade).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: { manifest: { maven: { bin: 'mvn' } } },
- }) as SocketJson,
- )
- vi.mocked(enumerateWorkspaces).mockReset()
- })
-
- it('marks the build files its projects report as covered', async () => {
- vi.mocked(enumerateWorkspaces).mockResolvedValue({
- buildRoot: reactor,
- projects: [
- {
- type: 'maven',
- name: 'reactor',
- subprojectDir: '.',
- manifestFiles: [{ file: 'pom.xml' }],
- children: [],
- },
- {
- type: 'maven',
- name: 'moduleA',
- subprojectDir: 'moduleA',
- manifestFiles: [{ file: 'moduleA/pom.xml' }],
- children: [],
- },
- {
- type: 'maven',
- name: 'moduleB',
- subprojectDir: 'moduleB',
- manifestFiles: [{ file: 'moduleB/alt.xml' }],
- children: [],
- },
- ],
- })
- const covered = new Set()
-
- const result = await markWorkspaceCoverage({
- covered,
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- rootSockJson: emptySockJson(),
- roots: [{ dir: reactor, entryFile: 'pom.xml' }],
- })
-
- expect(result).toEqual({ ok: true, data: undefined })
- expect(covered).toEqual(
- new Set([
- `${reactor}/pom.xml`,
- `${reactor}/moduleA/pom.xml`,
- `${reactor}/moduleB/alt.xml`,
]),
- )
- })
-
- it('resolves build files against the reported build root, not the candidate directory', async () => {
- vi.mocked(enumerateWorkspaces).mockResolvedValue({
- buildRoot: `${reactor}/build`,
- projects: [
- {
- type: 'maven',
- name: 'member',
- subprojectDir: 'member',
- manifestFiles: [{ file: 'member/pom.xml' }],
- children: [],
- },
- ],
- })
- const covered = new Set()
-
- await markWorkspaceCoverage({
- covered,
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- rootSockJson: emptySockJson(),
- roots: [{ dir: reactor, entryFile: 'pom.xml' }],
+ includedByTool: new Map([['maven', mavenRoots(service)]]),
})
- expect(covered).toEqual(new Set([`${reactor}/build/member/pom.xml`]))
- })
-
- it('does not mark a build file that escapes the build root as covered', async () => {
- vi.mocked(enumerateWorkspaces).mockResolvedValue({
- buildRoot: reactor,
- projects: [
- {
- type: 'maven',
- name: 'moduleA',
- subprojectDir: 'moduleA',
- manifestFiles: [{ file: 'moduleA/pom.xml' }],
- children: [],
- },
- {
- type: 'maven',
- name: 'shared-lib',
- subprojectDir: '../shared-lib',
- manifestFiles: [{ file: '../shared-lib/pom.xml' }],
- children: [],
- },
- ],
- })
- const covered = new Set()
-
- await markWorkspaceCoverage({
- covered,
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- rootSockJson: emptySockJson(),
- roots: [{ dir: reactor, entryFile: 'pom.xml' }],
- })
-
- expect(covered).toEqual(new Set([`${reactor}/moduleA/pom.xml`]))
- })
-
- it('does not enumerate, and marks nothing covered, for a disabled candidate', async () => {
- vi.mocked(readSocketJsonCascade).mockReturnValue({
- version: 1,
- defaults: { manifest: { maven: { disabled: true } } },
- } as SocketJson)
- const covered = new Set()
-
- const result = await markWorkspaceCoverage({
- covered,
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- rootSockJson: emptySockJson(),
- roots: [{ dir: reactor, entryFile: 'pom.xml' }],
- })
-
- expect(result).toEqual({ ok: true, data: undefined })
- expect(enumerateWorkspaces).not.toHaveBeenCalled()
- expect(covered.size).toBe(0)
- })
-
- it('fails closed - marks nothing covered and reports failure - when enumeration fails', async () => {
- vi.mocked(enumerateWorkspaces).mockResolvedValue(undefined)
- const covered = new Set()
-
- const result = await markWorkspaceCoverage({
- covered,
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- rootSockJson: emptySockJson(),
- roots: [{ dir: reactor, entryFile: 'pom.xml' }],
- })
-
- expect(result.ok).toBe(false)
- expect(covered.size).toBe(0)
- })
-
- it('resolves settings once per directory and skips a POM an earlier one covers', async () => {
- vi.mocked(enumerateWorkspaces).mockResolvedValue({
- buildRoot: reactor,
- projects: [
+ expect(result).toEqual({
+ excluded: [],
+ excludedBuilds: [
{
- type: 'maven',
- name: 'reactor',
- subprojectDir: '.',
- manifestFiles: [{ file: 'pom.xml' }],
- children: [],
+ ecosystem: 'maven',
+ root: { dir: service, entryFile: 'app-dev.pom.xml' },
},
],
- })
-
- await markWorkspaceCoverage({
- covered: new Set(),
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- rootSockJson: emptySockJson(),
- roots: [
- { dir: reactor, entryFile: 'all.pom.xml' },
- { dir: reactor, entryFile: 'pom.xml' },
+ included: [
+ { dir: service, ecosystem: 'maven', roots: mavenRoots(service) },
],
})
-
- expect(readSocketJsonCascade).toHaveBeenCalledTimes(1)
- expect(
- vi.mocked(enumerateWorkspaces).mock.calls.map(([o]) => o.entryFile),
- ).toEqual(['all.pom.xml'])
- })
-
- it('forwards --exclude-paths anchored at the scan root, so a member excluded specifically to dodge a broken resolution is actually skipped', async () => {
- vi.mocked(enumerateWorkspaces).mockResolvedValue({
- buildRoot: reactor,
- projects: [],
- })
-
- await markWorkspaceCoverage({
- covered: new Set(),
- cwd,
- dir: reactor,
- ecosystem: 'maven',
- excludePaths: ['reactor/moduleB'],
- rootSockJson: emptySockJson(),
- roots: [{ dir: reactor, entryFile: 'pom.xml' }],
- })
-
- expect(enumerateWorkspaces).toHaveBeenCalledWith(
- expect.objectContaining({
- excludeBase: cwd,
- excludePaths: ['reactor/moduleB'],
- }),
- )
})
})
-describe('disableExclusionRoot', () => {
- const cwd = '/repo'
- const dir = '/repo/legacy'
-
- beforeEach(() => {
- vi.mocked(readSocketJsonCascade).mockReset()
- vi.mocked(readOrDefaultSocketJson).mockReset()
- vi.mocked(writeSocketJson).mockReset()
- vi.mocked(writeSocketJson).mockResolvedValue({ ok: true, data: undefined })
- })
+// Each prompt is answered by the first rule its message starts with; any
+// other prompt fails the test.
+type Answers = Array<[string, unknown]>
- it('no-ops when the cascade already shows every ecosystem disabled', async () => {
- vi.mocked(readSocketJsonCascade).mockReturnValue({
- version: 1,
- defaults: { manifest: { gradle: { disabled: true } } },
- } as SocketJson)
-
- await disableExclusionRoot({
- cwd,
- dir,
- ecosystems: ['gradle'],
- rootSockJson: emptySockJson(),
- })
+type Choice = { description?: string; name: string; value: string }
- expect(writeSocketJson).not.toHaveBeenCalled()
- })
-
- it('writes disabled:true, preserving other own-file content', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(readOrDefaultSocketJson).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: { manifest: { gradle: { bin: './gradlew' } } },
- }) as SocketJson,
- )
-
- await disableExclusionRoot({
- cwd,
- dir,
- ecosystems: ['gradle'],
- rootSockJson: emptySockJson(),
- })
-
- expect(writeSocketJson).toHaveBeenCalledWith(
- dir,
- expect.objectContaining({
- defaults: {
- manifest: { gradle: { bin: './gradlew', disabled: true } },
- },
- }),
- )
- })
-
- it('writes only the ecosystems not already covered by cascade', async () => {
- vi.mocked(readSocketJsonCascade).mockReturnValue({
- version: 1,
- defaults: { manifest: { gradle: { disabled: true } } },
- } as SocketJson)
- vi.mocked(readOrDefaultSocketJson).mockImplementation(() => emptySockJson())
-
- await disableExclusionRoot({
- cwd,
- dir,
- ecosystems: ['gradle', 'maven'],
- rootSockJson: emptySockJson(),
+describe('setupRecursiveManifestConfig', () => {
+ let repo = ''
+ let asked: string[] = []
+ let choicesOf = new Map()
+
+ const file = (rel: string) => path.join(repo, rel)
+ const readSettings = async (rel: string) =>
+ JSON.parse(
+ await fs.readFile(file(path.join(rel, 'socket.json')), 'utf8'),
+ ) as SocketJson
+ const writeSettings = async (rel: string, manifest: unknown) =>
+ await fs.writeFile(
+ file(path.join(rel, 'socket.json')),
+ JSON.stringify({ version: 1, defaults: { manifest } }),
+ )
+ // Each build loads its own POM and the modules listed for it; a build
+ // listed as undefined can't be loaded.
+ const loads = (modules: Record) =>
+ vi
+ .mocked(enumerateWorkspaces)
+ .mockImplementation(async ({ cwd, entryFile }) => {
+ const rel = path.relative(repo, path.join(cwd, entryFile ?? ''))
+ if (rel in modules && !modules[rel]) {
+ return undefined
+ }
+ return {
+ buildRoot: cwd,
+ projects: [
+ {
+ ...project('.'),
+ manifestFiles: [{ file: entryFile ?? 'pom.xml' }],
+ },
+ ...(modules[rel] ?? []).map(project),
+ ],
+ }
+ })
+ const answer = (answers: Answers) =>
+ vi.mocked(select).mockImplementation(async ({ choices, message }) => {
+ asked.push(message)
+ choicesOf.set(message, choices as Choice[])
+ const rule = answers.find(({ 0: start }) => message.startsWith(start))
+ if (!rule) {
+ throw new Error(`Unexpected prompt: ${message}`)
+ }
+ return rule[1] as never
})
- expect(writeSocketJson).toHaveBeenCalledWith(
- dir,
- expect.objectContaining({
- defaults: { manifest: { maven: { disabled: true } } },
- }),
+ beforeEach(async () => {
+ repo = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'setup-recursive-')),
)
- })
-})
-
-describe('configureCandidate', () => {
- const cwd = '/repo'
- const dir = '/repo/nested-gradle'
-
- beforeEach(() => {
- vi.mocked(readSocketJsonCascade).mockReset()
- vi.mocked(readOrDefaultSocketJson).mockReset()
- vi.mocked(writeSocketJson).mockReset()
- vi.mocked(writeSocketJson).mockResolvedValue({ ok: true, data: undefined })
- vi.mocked(setupGradle).mockReset()
- })
-
- it('seeds the sub-wizard with the cascaded value, own-file value winning, but only writes fields that actually differ from what dir would inherit', async () => {
- vi.mocked(readOrDefaultSocketJson).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: { manifest: { gradle: { javaHome: '/opt/jdk-17' } } },
- }) as SocketJson,
- )
- vi.mocked(readSocketJsonCascade).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: {
- manifest: { gradle: { bin: './gradlew', javaHome: '/opt/jdk-11' } },
- },
- }) as SocketJson,
- )
- vi.mocked(setupGradle).mockImplementation(async config => {
- expect(config).toEqual({ bin: './gradlew', javaHome: '/opt/jdk-17' })
- ;(config as Record)['gradleOpts'] = '--offline'
- return { ok: true, data: { canceled: false } }
- })
+ asked = []
+ choicesOf = new Map()
+ for (const rel of [
+ 'pom.xml',
+ 'api/pom.xml',
+ 'legacy/pom.xml',
+ 'legacy/old/pom.xml',
+ 'legacy/tool/pom.xml',
+ ]) {
+ // eslint-disable-next-line no-await-in-loop
+ await fs.mkdir(path.dirname(file(rel)), { recursive: true })
+ // eslint-disable-next-line no-await-in-loop
+ await fs.writeFile(file(rel), '')
+ }
+ vi.mocked(enumerateWorkspaces).mockReset()
+ vi.mocked(setupMaven).mockReset()
+ vi.spyOn(logger, 'log').mockImplementation(() => logger)
+ vi.spyOn(logger, 'info').mockImplementation(() => logger)
+ vi.spyOn(logger, 'success').mockImplementation(() => logger)
+ vi.spyOn(logger, 'fail').mockImplementation(() => logger)
+ })
+ afterEach(async () => {
+ vi.restoreAllMocks()
+ await fs.rm(repo, { recursive: true, force: true })
+ })
+
+ it('asks only about builds no other build includes, and writes nothing when nothing changes', async () => {
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'inherit'],
+ ['legacy/tool (maven)', 'inherit'],
+ ])
- const result = await configureCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
+ const result = await setupRecursiveManifestConfig(repo, false)
expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(writeSocketJson).toHaveBeenCalledWith(
- dir,
- expect.objectContaining({
- defaults: {
- manifest: {
- gradle: {
- // `bin` is omitted: it's identical to what dir already inherits
- // from its ancestors, so writing it would only pin a value that
- // should keep tracking the ancestor's own if that ever changes.
- javaHome: '/opt/jdk-17',
- gradleOpts: '--offline',
- },
- },
- },
- }),
- )
+ expect(asked).toEqual([
+ 'This project (maven)',
+ 'Review the 4 other builds found beneath this one?',
+ 'legacy (maven)',
+ 'legacy/tool (maven)',
+ ])
+ expect(existsSync(file('socket.json'))).toBe(false)
})
- it('propagates a cancellation from the sub-wizard without writing', async () => {
- vi.mocked(readOrDefaultSocketJson).mockImplementation(() => emptySockJson())
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(setupGradle).mockResolvedValue({
- ok: true,
- data: { canceled: true },
- })
-
- const result = await configureCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
-
- expect(result).toEqual({ ok: true, data: { canceled: true } })
- expect(writeSocketJson).not.toHaveBeenCalled()
- })
+ it('skips a build it can load with exactly what it loads, and keeps asking about the separate builds beneath it', async () => {
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'skip'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- it('writes nothing when nothing is inherited and every prompt is left blank', async () => {
- vi.mocked(readOrDefaultSocketJson).mockImplementation(() => emptySockJson())
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- // A no-op wizard: doesn't set a single field on the seed it's handed.
- vi.mocked(setupGradle).mockResolvedValue({
- ok: true,
- data: { canceled: false },
- })
+ await setupRecursiveManifestConfig(repo, false)
- const result = await configureCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
+ expect(asked).toContain('legacy/tool (maven)')
+ expect(asked.some(message => message.startsWith('legacy/old'))).toBe(false)
+ expect(await readSettings('legacy')).toMatchObject({
+ defaults: { manifest: { builds: { 'pom.xml': { disabled: true } } } },
})
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(writeSocketJson).not.toHaveBeenCalled()
- })
-})
-
-describe('processCandidate', () => {
- const cwd = '/repo'
- const dir = '/repo/legacy/nested'
-
- beforeEach(() => {
- vi.mocked(select).mockReset()
- vi.mocked(readSocketJsonCascade).mockReset()
- vi.mocked(readOrDefaultSocketJson).mockReset()
- vi.mocked(readOrDefaultSocketJson).mockImplementation(() => emptySockJson())
- vi.mocked(writeSocketJson).mockReset()
- vi.mocked(writeSocketJson).mockResolvedValue({ ok: true, data: undefined })
- vi.mocked(setupGradle).mockReset()
+ expect(vi.mocked(logger.success).mock.calls.map(c => c[0])).toContain(
+ 'Skipped legacy/pom.xml and its 1 module.',
+ )
})
- it('skips silently, without prompting, when the cascade already shows it disabled', async () => {
- vi.mocked(readSocketJsonCascade).mockReturnValue({
- version: 1,
- defaults: { manifest: { gradle: { disabled: true } } },
- } as SocketJson)
-
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
+ it('offers to fix a build it cannot load first, and otherwise skips its directory and everything beneath it', async () => {
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': undefined })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'skip'],
+ ['Socket could not load legacy (maven)', 'directory'],
+ ['Write it?', true],
+ ])
- expect(result).toEqual({
- ok: true,
- data: { canceled: false, outcome: 'skipped', reenabled: false },
- })
- expect(select).not.toHaveBeenCalled()
- })
+ await setupRecursiveManifestConfig(repo, false)
- it('offers to re-enable when the candidate own file (not just an ancestor) sets disabled:true, then still asks to configure it', async () => {
- vi.mocked(readSocketJsonCascade).mockReturnValue({
- version: 1,
- defaults: { manifest: { gradle: { disabled: true } } },
- } as SocketJson)
- vi.mocked(readOrDefaultSocketJson).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: {
- manifest: { gradle: { disabled: true, bin: './gradlew' } },
- },
- }) as SocketJson,
+ const message = asked.find(m =>
+ m.startsWith('Socket could not load legacy (maven)'),
+ )!
+ expect(message).toContain(
+ 'Builds Socket can load are built once with all their modules',
)
- vi.mocked(select).mockImplementation(async ({ message }) => {
- if (message.includes('re-enable')) {
- return true
- }
- // The configure-or-leave-as-is question, asked right after re-enabling.
- return 'inherit'
- })
-
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
-
- expect(result).toEqual({
- ok: true,
- data: { canceled: false, outcome: 'inherited', reenabled: true },
- })
- expect(writeSocketJson).toHaveBeenCalledWith(
- dir,
- expect.objectContaining({
- defaults: {
- manifest: { gradle: { disabled: false, bin: './gradlew' } },
- },
- }),
+ const choices = choicesOf.get(message)!
+ expect(choices.map(choice => choice.value)).toEqual([
+ 'configure',
+ 'directory',
+ 'stop',
+ ])
+ expect(choices[1]!.name).toBe(
+ 'Skip legacy/ and everything beneath it, without loading it',
)
- })
-
- it('declining the re-enable offer leaves the candidate disabled and writes nothing', async () => {
- vi.mocked(readSocketJsonCascade).mockReturnValue({
- version: 1,
- defaults: { manifest: { gradle: { disabled: true } } },
- } as SocketJson)
- vi.mocked(readOrDefaultSocketJson).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: { manifest: { gradle: { disabled: true } } },
- }) as SocketJson,
+ expect(choices[1]!.description).toBe(
+ "Also skips the 2 other builds in and beneath legacy/. To build one of them, run `socket manifest setup --dynamic-sbom-inference` in its directory. Modules of legacy/pom.xml outside legacy/ can't be found without loading it, so they are asked about separately.",
)
- vi.mocked(select).mockResolvedValue(false)
-
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
-
- expect(result).toEqual({
- ok: true,
- data: { canceled: false, outcome: 'skipped', reenabled: false },
+ expect(asked.some(m => m.startsWith('legacy/tool'))).toBe(false)
+ expect(await readSettings('legacy')).toMatchObject({
+ defaults: { manifest: { maven: { disabled: true } } },
})
- expect(writeSocketJson).not.toHaveBeenCalled()
})
- it('does not offer a disable choice - that stays --exclude-paths-only', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(select).mockImplementation(async ({ choices }) => {
- expect(choices.map((c: { value: unknown }) => c.value)).toEqual([
- 'inherit',
- 'configure',
- ])
- return 'inherit'
- })
-
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
-
- expect(result).toEqual({
- ok: true,
- data: { canceled: false, outcome: 'inherited', reenabled: false },
+ it('loads a build again after configuring it, and skips it once it loads', async () => {
+ let attempts = 0
+ vi.mocked(enumerateWorkspaces).mockImplementation(async ({ cwd }) => {
+ if (cwd === file('legacy') && (attempts += 1) === 1) {
+ return undefined
+ }
+ return { buildRoot: cwd, projects: [project('.')] }
})
- expect(writeSocketJson).not.toHaveBeenCalled()
- })
-
- it('configures the candidate when the user picks configure', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(select).mockResolvedValue('configure')
- vi.mocked(setupGradle).mockImplementation(async config => {
- ;(config as Record)['bin'] = './gradlew'
+ vi.mocked(setupMaven).mockImplementation(async config => {
+ config.javaHome = '/opt/jdk-8'
return { ok: true, data: { canceled: false } }
})
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'skip'],
+ ['Socket could not load legacy (maven)', 'configure'],
+ ['legacy/old (maven)', 'inherit'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['api (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
+ await setupRecursiveManifestConfig(repo, false)
- expect(result).toEqual({
- ok: true,
- data: { canceled: false, outcome: 'configured', reenabled: false },
+ expect(attempts).toBe(2)
+ expect(await readSettings('legacy')).toMatchObject({
+ defaults: {
+ manifest: {
+ builds: { 'pom.xml': { disabled: true } },
+ maven: { javaHome: '/opt/jdk-8' },
+ },
+ },
})
- expect(setupGradle).toHaveBeenCalledTimes(1)
- expect(writeSocketJson).toHaveBeenCalled()
})
- it('leaves the candidate untouched when the user picks inherit', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(select).mockResolvedValue('inherit')
-
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
- })
-
- expect(result).toEqual({
- ok: true,
- data: { canceled: false, outcome: 'inherited', reenabled: false },
+ it('asks about each build file of a directory, and keeps settings for one under its name', async () => {
+ await fs.writeFile(file('legacy/legacy-cli.pom.xml'), '')
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ vi.mocked(setupMaven).mockImplementation(async config => {
+ config.mavenOpts = '-Pcli'
+ return { ok: true, data: { canceled: false } }
})
- expect(writeSocketJson).not.toHaveBeenCalled()
- })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy/legacy-cli.pom.xml (maven)', 'configure'],
+ ['legacy/pom.xml (maven)', 'skip'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- it('reports a cancellation from the action prompt itself', async () => {
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(select).mockResolvedValue(null)
+ await setupRecursiveManifestConfig(repo, false)
- const result = await processCandidate({
- cwd,
- dir,
- ecosystem: 'gradle',
- rootSockJson: emptySockJson(),
+ expect(await readSettings('legacy')).toMatchObject({
+ defaults: {
+ manifest: {
+ builds: {
+ 'legacy-cli.pom.xml': { mavenOpts: '-Pcli' },
+ 'pom.xml': { disabled: true },
+ },
+ },
+ },
})
-
- expect(result.ok && result.data.canceled).toBe(true)
- expect(writeSocketJson).not.toHaveBeenCalled()
})
-})
-describe('setupRecursiveManifestConfig', () => {
- const cwd = '/repo'
-
- beforeEach(() => {
- vi.mocked(select).mockReset()
- vi.mocked(select).mockResolvedValue(false)
- vi.mocked(setupGradle).mockReset()
- vi.mocked(setupMaven).mockReset()
- vi.mocked(setupSbt).mockReset()
- vi.mocked(readSocketJsonSync).mockReset()
- vi.mocked(readSocketJsonSync).mockImplementation(() => ({
- ok: true,
- data: emptySockJson(),
- }))
- vi.mocked(findBuildToolCandidates).mockReset()
- // Default: nothing found anywhere - most tests override this per-scenario.
- vi.mocked(findBuildToolCandidates).mockResolvedValue(new Map())
- vi.mocked(readOrDefaultSocketJson).mockReset()
- vi.mocked(readOrDefaultSocketJson).mockImplementation(() => emptySockJson())
- vi.mocked(readSocketJsonCascade).mockReset()
- vi.mocked(readSocketJsonCascade).mockImplementation(() => emptySockJson())
- vi.mocked(writeSocketJson).mockReset()
- vi.mocked(writeSocketJson).mockResolvedValue({ ok: true, data: undefined })
- vi.mocked(enumerateWorkspaces).mockReset()
- // Default: no reactor members declared anywhere - most tests don't care
- // about pruning specifically, so nothing should get collapsed.
- vi.mocked(enumerateWorkspaces).mockResolvedValue({
- buildRoot: cwd,
- projects: [],
+ it('offers a build skipped under its own name back, and drops the skip when taken', async () => {
+ await writeSettings('legacy', {
+ builds: { 'pom.xml': { disabled: true } },
})
- })
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven) is skipped. Build it again?', true],
+ ['legacy (maven)', 'inherit'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- it('fails closed when the scan finds no build roots anywhere', async () => {
- const result = await setupRecursiveManifestConfig(cwd, false)
+ await setupRecursiveManifestConfig(repo, false)
- expect(result).toEqual({
- ok: false,
- code: 1,
- message:
- 'No Gradle, sbt, Maven or .NET build root was found beneath /repo.',
- })
- expect(select).not.toHaveBeenCalled()
- expect(setupGradle).not.toHaveBeenCalled()
- expect(setupMaven).not.toHaveBeenCalled()
- expect(setupSbt).not.toHaveBeenCalled()
- expect(writeSocketJson).not.toHaveBeenCalled()
- expect(findBuildToolCandidates).toHaveBeenCalled()
+ expect(
+ (await readSettings('legacy')).defaults?.manifest?.builds?.['pom.xml'],
+ ).toEqual({})
})
- it('still proceeds normally when build roots exist but --exclude-paths excludes all of them (a deliberate choice, not "none exist")', async () => {
- vi.mocked(findBuildToolCandidates).mockImplementation(
- async ({ excludePaths }) =>
- excludePaths?.length
- ? new Map()
- : new Map([['maven', mavenRoots(`${cwd}/service`)]]),
- )
-
- const result = await setupRecursiveManifestConfig(cwd, false, ['service'])
+ it('run beneath a skipped directory, offers to build the projects there again', async () => {
+ await writeSettings('legacy', { maven: { disabled: true } })
+ loads({ 'legacy/tool/pom.xml': [] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['', true],
+ ])
- expect(result.ok).toBe(true)
- })
+ await setupRecursiveManifestConfig(file('legacy/tool'), false)
- it('only asks about ecosystems detected somewhere in the tree, with plain phrasing', async () => {
- // Maven is detected (a candidate exists, elsewhere in the tree); gradle
- // and sbt have none anywhere, so neither should ever be asked about.
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(`${cwd}/service`)]]),
+ expect(asked[0]).toMatch(
+ /^\S*legacy\/socket\.json skips Maven builds in and beneath \S*legacy, including this directory\. Build the Maven projects here\?$/,
)
- const messages: string[] = []
- vi.mocked(select).mockImplementation(async ({ message }) => {
- messages.push(message)
- return false
+ expect(await readSettings('legacy/tool')).toMatchObject({
+ defaults: { manifest: { maven: { disabled: false } } },
})
-
- await setupRecursiveManifestConfig(cwd, false)
-
- expect(setupGradle).not.toHaveBeenCalled()
- expect(setupSbt).not.toHaveBeenCalled()
- // Exactly one root question (Maven) - phrased as a nested-project default
- // since the only maven candidate is elsewhere in the tree, not at cwd
- // itself - no "detected"/"root"/"anyway" wording either way.
- expect(messages[0]).toBe(
- 'Configure Maven defaults for any nested projects?',
- )
- expect(messages[0]).not.toMatch(/detected|root|anyway/i)
})
- it('distinguishes a project-specific ecosystem from a purely inherited one at the root', async () => {
- // cwd IS a maven project (e.g. a reactor root); gradle only exists in a
- // project nested somewhere beneath it (e.g. a submodule's own gradle
- // build) - the two questions must read differently, since the maven one
- // configures settings for cwd itself while the gradle one only ever sets
- // an inheritable default for something else.
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([
- ['maven', mavenRoots(cwd)],
- ['gradle', dirRoots(`${cwd}/module-b/standalone-gradle-lib`)],
- ]),
- )
- const messages: string[] = []
- vi.mocked(select).mockImplementation(async ({ message }) => {
- messages.push(message)
- return false
- })
+ it('asks about the build here like any other, offering its own skip back after the defaults', async () => {
+ await writeSettings('.', { builds: { 'pom.xml': { disabled: true } } })
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['Configure Maven defaults', false],
+ ['This project (maven) is skipped. Build it again?', true],
+ ['This project (maven)', 'inherit'],
+ ['Review the', false],
+ ['Write it?', true],
+ ])
- await setupRecursiveManifestConfig(cwd, false)
+ await setupRecursiveManifestConfig(repo, false)
- expect(messages).toEqual([
- 'Configure Maven settings for this project?',
- 'Configure Gradle defaults for any nested projects?',
- 'Configure other build roots found beneath this one, individually?',
+ // Its own entry would override defaults set through its prompt.
+ expect(asked).toEqual([
+ 'Configure Maven defaults for the projects here and beneath?',
+ 'This project (maven) is skipped. Build it again?',
+ 'This project (maven)',
+ 'Review the 4 other builds found beneath this one?',
+ 'Write it?',
+ ])
+ expect(
+ (await readSettings('.')).defaults?.manifest?.builds?.['pom.xml'],
+ ).toEqual({})
+ })
+
+ it('asks about each build file here, after the defaults they share', async () => {
+ await fs.writeFile(file('app-alt.pom.xml'), '')
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['Configure Maven defaults for the projects here and beneath?', false],
+ ['app-alt.pom.xml (maven)', 'skip'],
+ ['pom.xml (maven)', 'inherit'],
+ ['Review the', false],
+ ['Write it?', true],
])
- })
- it('scans with disabled flags stripped, so an already-disabled ecosystem is still detected', async () => {
- vi.mocked(readOrDefaultSocketJson).mockImplementation(
- () =>
- ({
- version: 1,
- defaults: { manifest: { maven: { disabled: true, bin: 'mvn' } } },
- }) as SocketJson,
- )
- const seenSockJsons: SocketJson[] = []
- vi.mocked(findBuildToolCandidates).mockImplementation(
- async ({ sockJson }) => {
- seenSockJsons.push(sockJson)
- return new Map([['maven', mavenRoots(cwd)]])
- },
- )
+ await setupRecursiveManifestConfig(repo, false)
- await setupRecursiveManifestConfig(cwd, false)
+ expect(asked.slice(0, 3)).toEqual([
+ 'Configure Maven defaults for the projects here and beneath?',
+ 'app-alt.pom.xml (maven)',
+ 'pom.xml (maven)',
+ ])
+ expect((await readSettings('.')).defaults?.manifest?.builds).toEqual({
+ 'app-alt.pom.xml': { disabled: true },
+ })
+ })
+
+ it("writes a Gradle build's choice under the build file its entry already names", async () => {
+ await fs.mkdir(file('app'))
+ await fs.writeFile(file('app/build.gradle'), '')
+ await fs.writeFile(file('app/settings.gradle'), '')
+ await writeSettings('app', {
+ builds: { 'settings.gradle': { disabled: false } },
+ })
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['Configure Gradle defaults', false],
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['app (gradle)', 'skip'],
+ ['legacy (maven)', 'inherit'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- expect(seenSockJsons.length).toBeGreaterThan(0)
- for (const seen of seenSockJsons) {
- expect(seen.defaults?.manifest?.maven?.disabled).toBe(false)
- }
- })
+ await setupRecursiveManifestConfig(repo, false)
- it('offers to re-enable an ecosystem disabled at the root, before asking to configure it', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(readSocketJsonSync).mockImplementation(() => ({
- ok: true,
- data: {
- version: 1,
- defaults: { manifest: { maven: { disabled: true, bin: 'mvn' } } },
- } as SocketJson,
- }))
- const messages: string[] = []
- vi.mocked(select).mockImplementation(async ({ message }) => {
- messages.push(message)
- if (message.includes('re-enable')) {
- return true
- }
- if (message.startsWith('Configure Maven')) {
- return false
- }
- return false
+ expect((await readSettings('app')).defaults?.manifest?.builds).toEqual({
+ 'settings.gradle': { disabled: true },
})
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(messages[0]).toBe('Maven is currently disabled here - re-enable it?')
- expect(writeSocketJson).toHaveBeenCalledWith(
- cwd,
- expect.objectContaining({
- defaults: { manifest: { maven: { disabled: false, bin: 'mvn' } } },
- }),
- )
})
- it('declining the root re-enable question leaves the ecosystem disabled and skips configuring it', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(readSocketJsonSync).mockImplementation(() => ({
- ok: true,
- data: {
- version: 1,
- defaults: { manifest: { maven: { disabled: true, bin: 'mvn' } } },
- } as SocketJson,
- }))
- vi.mocked(select).mockResolvedValueOnce(false)
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(setupMaven).not.toHaveBeenCalled()
- expect(writeSocketJson).not.toHaveBeenCalled()
- })
-
- it('stops when canceling the only detected root ecosystem question', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(select).mockResolvedValueOnce(null)
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result.ok && result.data.canceled).toBe(true)
- })
-
- it('stops when a root ecosystem sub-wizard is canceled', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(select).mockResolvedValueOnce(true)
- vi.mocked(setupMaven).mockResolvedValue({
- ok: true,
- data: { canceled: true },
+ it('drops an override for an answer the user leaves to its default', async () => {
+ await writeSettings('legacy', {
+ maven: { ignoreUnresolved: true, javaHome: '/opt/jdk-17', verbose: true },
})
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result.ok && result.data.canceled).toBe(true)
- })
-
- it('propagates a hard failure reading the root socket.json', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(readSocketJsonSync).mockImplementation(() => ({
- ok: false,
- message: 'boom',
- }))
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result.ok).toBe(false)
- expect(findBuildToolCandidates).toHaveBeenCalled()
- })
-
- it('configures maven at the root (itself the only candidate) and writes it, with no individual-configure gate', async () => {
- // The only maven candidate anywhere IS cwd itself, so there's nothing
- // left to configure individually afterward.
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(select)
- // Configure Maven? -> yes.
- .mockResolvedValueOnce(true)
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
vi.mocked(setupMaven).mockImplementation(async config => {
- ;(config as Record)['bin'] = './mvnw'
+ delete config.ignoreUnresolved
+ delete config.verbose
return { ok: true, data: { canceled: false } }
})
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'configure'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(setupMaven).toHaveBeenCalledTimes(1)
- expect(writeSocketJson).toHaveBeenCalledWith(cwd, expect.any(Object))
- // No individual-configure gate: nothing else was found to ask about.
- expect(select).toHaveBeenCalledTimes(1)
- })
+ await setupRecursiveManifestConfig(repo, false)
- it('does not write when the user says yes to configure Maven but leaves every prompt blank', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(cwd)]]),
- )
- vi.mocked(select).mockResolvedValueOnce(true)
- // A no-op wizard: doesn't set a single field.
- vi.mocked(setupMaven).mockResolvedValue({
- ok: true,
- data: { canceled: false },
+ expect((await readSettings('legacy')).defaults?.manifest?.maven).toEqual({
+ javaHome: '/opt/jdk-17',
})
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(setupMaven).toHaveBeenCalledTimes(1)
- expect(writeSocketJson).not.toHaveBeenCalled()
})
- it('applies --exclude-paths unconditionally, without asking to configure anything individually', async () => {
- const legacy = `${cwd}/legacy`
- vi.mocked(findBuildToolCandidates).mockImplementation(
- async ({ excludePaths }) =>
- excludePaths?.length
- ? new Map([['gradle', dirRoots()]])
- : new Map([['gradle', dirRoots(legacy)]]),
- )
+ it('drops an override the user sets back to the inherited value', async () => {
+ await writeSettings('.', { maven: { javaHome: '/opt/jdk-17' } })
+ await writeSettings('legacy', { maven: { javaHome: '/opt/jdk-8' } })
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ vi.mocked(setupMaven).mockImplementation(async config => {
+ config.javaHome = '/opt/jdk-17'
+ return { ok: true, data: { canceled: false } }
+ })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'configure'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', true],
+ ])
- const result = await setupRecursiveManifestConfig(cwd, false, ['legacy'])
+ await setupRecursiveManifestConfig(repo, false)
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(writeSocketJson).toHaveBeenCalledWith(
- legacy,
- expect.objectContaining({
- defaults: { manifest: { gradle: { disabled: true } } },
- }),
- )
- // Nothing left to configure individually (the only candidate was
- // excluded), so the individual-configure gate is never asked.
- expect(select).not.toHaveBeenCalled()
+ expect((await readSettings('legacy')).defaults?.manifest?.maven).toEqual({})
})
- it('reports nothing excluded without writing anything, then asks about the remaining candidate individually', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['gradle', dirRoots(`${cwd}/active`)]]),
- )
- vi.mocked(select)
- // Configure Gradle defaults? -> no (gradle is detected via `active`).
- .mockResolvedValueOnce(false)
- // Configure other build roots individually? -> no.
- .mockResolvedValueOnce(false)
+ it('loads a build left skipped under its own name, so its modules are not asked about', async () => {
+ await writeSettings('legacy', {
+ builds: { 'pom.xml': { disabled: true } },
+ })
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven) is skipped. Build it again?', false],
+ ['legacy/tool (maven)', 'inherit'],
+ ])
- const result = await setupRecursiveManifestConfig(cwd, false, ['legacy'])
+ await setupRecursiveManifestConfig(repo, false)
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(findBuildToolCandidates).toHaveBeenCalled()
- expect(writeSocketJson).not.toHaveBeenCalled()
+ expect(asked.some(m => m.startsWith('legacy/old'))).toBe(false)
})
- it('only writes disabled:true to the topmost of an excluded subtree', async () => {
- const legacy = `${cwd}/legacy`
- const nested = `${legacy}/nested`
- const fake = makeFakeGradleDisk(cwd)
- vi.mocked(readOrDefaultSocketJson).mockImplementation(
- fake.readOrDefaultSocketJson,
- )
- vi.mocked(readSocketJsonCascade).mockImplementation(
- fake.readSocketJsonCascade,
- )
- vi.mocked(writeSocketJson).mockImplementation(fake.writeSocketJson)
- vi.mocked(findBuildToolCandidates).mockImplementation(
- async ({ excludePaths }) =>
- excludePaths?.length
- ? new Map([['gradle', dirRoots()]])
- : new Map([['gradle', dirRoots(legacy, nested)]]),
- )
+ it('writes nothing when the user declines the changes', async () => {
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', 'skip'],
+ ['legacy/tool (maven)', 'inherit'],
+ ['Write it?', false],
+ ])
- const result = await setupRecursiveManifestConfig(cwd, false, ['legacy'])
+ await setupRecursiveManifestConfig(repo, false)
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(writeSocketJson).toHaveBeenCalledTimes(1)
- expect(writeSocketJson).toHaveBeenCalledWith(
- legacy,
- expect.objectContaining({
- defaults: { manifest: { gradle: { disabled: true } } },
- }),
- )
- // Everything was excluded, so the individual-configure gate is skipped.
- expect(select).not.toHaveBeenCalled()
+ expect(existsSync(file('legacy/socket.json'))).toBe(false)
})
- it('reports the discovered count and walks the remaining candidate even when no --exclude-paths is given', async () => {
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['gradle', dirRoots(`${cwd}/active`)]]),
- )
- const messages = inheritEveryCandidate()
- const logSpy = vi.spyOn(logger, 'log')
+ it('skips what --exclude-paths excludes without asking about it', async () => {
+ loads({ 'pom.xml': ['api'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['Write it?', true],
+ ])
- try {
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(writeSocketJson).not.toHaveBeenCalled()
- expect(messages).toContain(
- 'Configure other build roots found beneath this one, individually?',
- )
- const logged = logSpy.mock.calls.map(c => String(c[0])).join('\n')
- expect(logged).toMatch(/Detected: Gradle/)
- expect(logged).toMatch(/0 configured \(0 re-enabled\), 1 left as-is/)
- } finally {
- logSpy.mockRestore()
- }
- })
+ await setupRecursiveManifestConfig(repo, false, ['legacy'])
- it('configures and leaves candidates inheriting in a single pass, skipping candidates already disabled via cascade', async () => {
- const serviceA = `${cwd}/serviceA`
- const serviceB = `${cwd}/serviceB`
- const serviceBSubmodule = `${serviceB}/submodule`
- const serviceC = `${cwd}/serviceC`
-
- vi.mocked(select)
- // Configure Maven? -> no.
- .mockResolvedValueOnce(false)
- // Configure Gradle? -> no.
- .mockResolvedValueOnce(false)
- // Configure other build roots individually? -> yes.
- .mockResolvedValueOnce(true)
- // serviceA (depth 1) -> configure.
- .mockResolvedValueOnce('configure')
- // serviceC (depth 1) -> inherit.
- .mockResolvedValueOnce('inherit')
- // serviceB and serviceB/submodule are already disabled - via a prior
- // --exclude-paths run, say - so neither is ever prompted.
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([
- ['maven', mavenRoots(serviceA, serviceB, serviceBSubmodule)],
- ['gradle', dirRoots(serviceC)],
- ]),
- )
- vi.mocked(readSocketJsonCascade).mockImplementation(dir =>
- dir === serviceB || dir === serviceBSubmodule
- ? ({
- version: 1,
- defaults: { manifest: { maven: { disabled: true } } },
- } as SocketJson)
- : emptySockJson(),
- )
- vi.mocked(setupMaven).mockImplementation(async config => {
- ;(config as Record)['bin'] = './mvnw'
- return { ok: true, data: { canceled: false } }
+ expect(asked).toEqual([
+ 'This project (maven)',
+ 'Review the other build found beneath this one?',
+ 'Write it?',
+ ])
+ expect(await readSettings('legacy')).toMatchObject({
+ defaults: { manifest: { maven: { disabled: true } } },
})
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(select).toHaveBeenCalledTimes(5)
- expect(writeSocketJson).toHaveBeenCalledTimes(1)
- expect(writeSocketJson).toHaveBeenCalledWith(
- serviceA,
- expect.objectContaining({
- defaults: { manifest: { maven: { bin: './mvnw' } } },
- }),
- )
})
- it('asks once about a directory holding several POMs and enumerates each', async () => {
- const service = `${cwd}/service`
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([
- [
- 'maven',
- [
- { dir: service, entryFile: 'app-dev.pom.xml' },
- { dir: service, entryFile: 'pom.xml' },
- ],
- ],
- ]),
- )
- vi.mocked(enumerateWorkspaces).mockImplementation(async ({ cwd: dir }) => ({
- buildRoot: dir,
- projects: [],
- }))
- const messages = inheritEveryCandidate()
- const logSpy = vi.spyOn(logger, 'log')
-
- try {
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(messages.filter(m => m === 'service (maven)')).toHaveLength(1)
- expect(
- vi.mocked(enumerateWorkspaces).mock.calls.map(([o]) => o.entryFile),
- ).toEqual(['app-dev.pom.xml', 'pom.xml'])
- const logged = logSpy.mock.calls.map(c => String(c[0])).join('\n')
- expect(logged).toMatch(/0 configured \(0 re-enabled\), 1 left as-is/)
- } finally {
- logSpy.mockRestore()
- }
- })
-
- it('does not enumerate a POM at cwd that --exclude-paths excludes', async () => {
- vi.mocked(findBuildToolCandidates).mockImplementation(
- async ({ excludePaths }) =>
- new Map([
- [
- 'maven',
- excludePaths?.length
- ? mavenRoots(cwd, `${cwd}/service`)
- : [
- { dir: cwd, entryFile: 'broken.pom.xml' },
- ...mavenRoots(cwd, `${cwd}/service`),
- ],
- ],
- ]),
- )
- vi.mocked(enumerateWorkspaces).mockImplementation(async ({ cwd: dir }) => ({
- buildRoot: dir,
- projects: [],
- }))
- inheritEveryCandidate()
-
- const result = await setupRecursiveManifestConfig(cwd, false, [
- 'broken.pom.xml',
+ it('stops, writing nothing, when the user cancels a prompt', async () => {
+ loads({ 'pom.xml': ['api'], 'legacy/pom.xml': ['old'] })
+ answer([
+ ['This project (maven)', 'inherit'],
+ ['Review the', true],
+ ['legacy (maven)', null],
])
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- expect(
- vi
- .mocked(enumerateWorkspaces)
- .mock.calls.some(([o]) => o.entryFile === 'broken.pom.xml'),
- ).toBe(false)
- })
-
- it('does not enumerate a POM at cwd that another POM there already aggregates', async () => {
- const cwd = await fs.realpath(
- await fs.mkdtemp(path.join(tmpdir(), 'seed-coverage-poms-')),
- )
- await fs.writeFile(path.join(cwd, 'all.pom.xml'), '')
- await fs.writeFile(path.join(cwd, 'pom.xml'), '')
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([
- [
- 'maven',
- [
- { dir: cwd, entryFile: 'all.pom.xml' },
- ...mavenRoots(cwd, `${cwd}/service`),
- ],
- ],
- ]),
- )
- vi.mocked(enumerateWorkspaces).mockImplementation(async ({ cwd: dir }) => ({
- buildRoot: dir,
- projects:
- dir === cwd
- ? [
- {
- type: 'maven',
- name: 'core',
- subprojectDir: '',
- manifestFiles: [{ file: 'pom.xml' }],
- children: [],
- },
- ]
- : [],
- }))
- inheritEveryCandidate()
+ const result = await setupRecursiveManifestConfig(repo, false)
- try {
- await setupRecursiveManifestConfig(cwd, false)
-
- expect(
- vi
- .mocked(enumerateWorkspaces)
- .mock.calls.map(([o]) =>
- buildRootPath({ dir: o.cwd, entryFile: o.entryFile }),
- ),
- ).toEqual([
- path.join(cwd, 'all.pom.xml'),
- path.join(cwd, 'service/pom.xml'),
- ])
- } finally {
- await fs.rm(cwd, { recursive: true, force: true })
- }
+ expect(result).toEqual({ ok: true, data: { canceled: true } })
+ expect(existsSync(file('legacy/socket.json'))).toBe(false)
})
- it('seeds coverage from cwd itself, so a reactor rooted exactly at cwd still gets its declared members pruned', async () => {
- const cwd = await fs.realpath(
- await fs.mkdtemp(path.join(tmpdir(), 'seed-coverage-')),
+ it('fails when no build is found at all', async () => {
+ const empty = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'setup-recursive-empty-')),
)
- for (const member of ['module-a', 'module-b']) {
- // eslint-disable-next-line no-await-in-loop
- await fs.mkdir(path.join(cwd, member))
- // eslint-disable-next-line no-await-in-loop
- await fs.writeFile(path.join(cwd, member, 'pom.xml'), '')
- }
- // cwd IS the maven reactor root (has its own pom.xml) - discoverBuildRoots
- // always excludes cwd from `included` (it already got its own root
- // wizard), so without seeding coverage from cwd specifically, module-a
- // and module-b would never be recognized as reactor members.
- const independentService = `${cwd}/independent-service`
- const moduleA = `${cwd}/module-a`
- const moduleB = `${cwd}/module-b`
-
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([
- ['maven', mavenRoots(cwd, independentService, moduleA, moduleB)],
- ]),
- )
- vi.mocked(enumerateWorkspaces).mockImplementation(async ({ cwd: dir }) =>
- dir === cwd
- ? {
- buildRoot: cwd,
- projects: [
- {
- type: 'maven',
- name: 'module-a',
- subprojectDir: 'module-a',
- manifestFiles: [{ file: 'module-a/pom.xml' }],
- children: [],
- },
- {
- type: 'maven',
- name: 'module-b',
- subprojectDir: 'module-b',
- manifestFiles: [{ file: 'module-b/pom.xml' }],
- children: [],
- },
- ],
- }
- : { buildRoot: dir, projects: [] },
- )
- const messages = inheritEveryCandidate()
-
try {
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result).toEqual({ ok: true, data: { canceled: false } })
- // Only independent-service is ever asked about - module-a/module-b are
- // recognized as covered via cwd's own enumeration and never prompted.
- // cwd itself is a maven project (the reactor root), so the root question
- // is phrased as this project's own settings, not a nested-project default.
- expect(messages).toEqual([
- 'Configure Maven settings for this project?',
- 'Configure other build roots found beneath this one, individually?',
- 'independent-service (maven)',
- ])
+ const result = await setupRecursiveManifestConfig(empty, false)
+ expect(result).toMatchObject({ ok: false, code: 1 })
} finally {
- await fs.rm(cwd, { recursive: true, force: true })
+ await fs.rm(empty, { recursive: true, force: true })
}
})
-
- it("aborts the whole walk (fail-closed) when a candidate's workspace layout cannot be determined", async () => {
- const independentService = `${cwd}/independent-service`
- const laterCandidate = `${cwd}/zzz-later`
-
- vi.mocked(findBuildToolCandidates).mockResolvedValue(
- new Map([['maven', mavenRoots(independentService, laterCandidate)]]),
- )
- inheritEveryCandidate()
- // Simulates a missing $JAVA8_HOME env var reference: enumeration fails
- // for independent-service specifically.
- vi.mocked(enumerateWorkspaces).mockImplementation(async ({ cwd: dir }) =>
- dir === independentService ? undefined : { buildRoot: dir, projects: [] },
- )
-
- const result = await setupRecursiveManifestConfig(cwd, false)
-
- expect(result.ok).toBe(false)
- // The later candidate is never reached - the walk aborted right after
- // independent-service's own enumeration failed, not after processing
- // everything and reporting failures at the end.
- expect(enumerateWorkspaces).not.toHaveBeenCalledWith(
- expect.objectContaining({ cwd: laterCandidate }),
- )
- })
})
diff --git a/src/commands/scan/handle-create-new-scan.mts b/src/commands/scan/handle-create-new-scan.mts
index a60548033..d13c8a38e 100644
--- a/src/commands/scan/handle-create-new-scan.mts
+++ b/src/commands/scan/handle-create-new-scan.mts
@@ -14,7 +14,10 @@ import { finalizeTier1Scan } from './finalize-tier1-scan.mts'
import { handleScanReport } from './handle-scan-report.mts'
import { outputCreateNewScan } from './output-create-new-scan.mts'
import { performReachabilityAnalysis } from './perform-reachability-analysis.mts'
-import { runDynamicSbomInference } from './run-dynamic-sbom-inference.mts'
+import {
+ runDynamicSbomInference,
+ withoutDiscardedFacts,
+} from './run-dynamic-sbom-inference.mts'
import constants from '../../constants.mts'
import { checkCommandInput } from '../../utils/check-input.mts'
import {
@@ -161,6 +164,7 @@ async function createNewScan(
// Sidecar forwarded to reachability; populated only when reach runs.
let resolvedPathsSidecar: ResolvedPathsSidecar | undefined
let factsOnlyTypes: FactsOnlyTypes | undefined
+ let discardedFactsPaths: string[] | undefined
// sbt provisions its Scala toolchain under the directory passed as its
// isolated global base; reach's artifactPaths point into it, so it must
@@ -194,6 +198,7 @@ async function createNewScan(
)
resolvedPathsSidecar = dynamicResult.resolvedPathsSidecar
factsOnlyTypes = dynamicResult.factsOnlyTypes
+ discardedFactsPaths = dynamicResult.discardedFactsPaths
}
if (autoManifest) {
@@ -274,11 +279,14 @@ async function createNewScan(
const packagePaths =
scanFiles ??
- (await getPackageFilesForScan(scanTargets, supportedFiles, {
- additionalIgnores: additionalScaIgnores,
- config: socketConfig,
- cwd,
- }))
+ (await withoutDiscardedFacts(
+ await getPackageFilesForScan(scanTargets, supportedFiles, {
+ additionalIgnores: additionalScaIgnores,
+ config: socketConfig,
+ cwd,
+ }),
+ discardedFactsPaths,
+ ))
spinner.successAndStop(
`Found ${packagePaths.length} ${pluralize('file', packagePaths.length)} to include in scan.`,
diff --git a/src/commands/scan/handle-scan-reach.mts b/src/commands/scan/handle-scan-reach.mts
index 264ca5e50..1e54d6e5d 100644
--- a/src/commands/scan/handle-scan-reach.mts
+++ b/src/commands/scan/handle-scan-reach.mts
@@ -6,7 +6,10 @@ import { fetchSupportedScanFileNames } from './fetch-supported-scan-file-names.m
import { finalizeTier1Scan } from './finalize-tier1-scan.mts'
import { outputScanReach } from './output-scan-reach.mts'
import { performReachabilityAnalysis } from './perform-reachability-analysis.mts'
-import { runDynamicSbomInference } from './run-dynamic-sbom-inference.mts'
+import {
+ runDynamicSbomInference,
+ withoutDiscardedFacts,
+} from './run-dynamic-sbom-inference.mts'
import constants from '../../constants.mts'
import { checkCommandInput } from '../../utils/check-input.mts'
import { findSocketYmlSync } from '../../utils/config.mts'
@@ -93,14 +96,13 @@ async function runScanReach(
target: targets[0]!,
})
- const packagePaths = await getPackageFilesForScan(
- scanTargets,
- supportedFiles,
- {
+ const packagePaths = await withoutDiscardedFacts(
+ await getPackageFilesForScan(scanTargets, supportedFiles, {
additionalIgnores: additionalScaIgnores,
config: socketConfig,
cwd,
- },
+ }),
+ dynamicResult?.discardedFactsPaths,
)
spinner.successAndStop(
diff --git a/src/commands/scan/handle-scan-reach.test.mts b/src/commands/scan/handle-scan-reach.test.mts
index c35ce6a34..22c8b0afa 100644
--- a/src/commands/scan/handle-scan-reach.test.mts
+++ b/src/commands/scan/handle-scan-reach.test.mts
@@ -49,6 +49,8 @@ vi.mock('./perform-reachability-analysis.mts', () => ({
vi.mock('./run-dynamic-sbom-inference.mts', () => ({
runDynamicSbomInference: mockRunDynamicSbomInference,
+ withoutDiscardedFacts: async (paths: string[], discarded?: string[]) =>
+ paths.filter(p => !discarded?.includes(p)),
}))
vi.mock('../../constants.mts', () => ({
@@ -408,6 +410,35 @@ describe('handleScanReach', () => {
)
})
+ it('leaves the facts files of dropped runs out of the analysis', async () => {
+ mockRunDynamicSbomInference.mockResolvedValueOnce({
+ discardedFactsPaths: ['/repo/lib/pom.xml.socket.facts.json'],
+ factsOnlyTypes: ['maven'],
+ factsPaths: ['/repo/pom.xml.socket.facts.json'],
+ resolvedPathsSidecar: undefined,
+ })
+ mockGetPackageFilesForScan.mockResolvedValueOnce([
+ '/repo/lib/pom.xml.socket.facts.json',
+ '/repo/pom.xml.socket.facts.json',
+ ])
+
+ await handleScanReach({
+ cwd: '/repo',
+ interactive: false,
+ orgSlug: 'fakeOrg',
+ outputKind: 'text',
+ outputPath: '',
+ reachabilityOptions: baseReachabilityOptions,
+ targets: ['/repo'],
+ })
+
+ expect(mockPerformReachabilityAnalysis).toHaveBeenCalledWith(
+ expect.objectContaining({
+ packagePaths: ['/repo/pom.xml.socket.facts.json'],
+ }),
+ )
+ })
+
it('propagates a build-root failure instead of analyzing a partial set', async () => {
mockRunDynamicSbomInference.mockRejectedValueOnce(
new Error('One or more independent build roots failed'),
diff --git a/src/commands/scan/run-dynamic-sbom-inference.mts b/src/commands/scan/run-dynamic-sbom-inference.mts
index 25010c533..b49dc75bd 100644
--- a/src/commands/scan/run-dynamic-sbom-inference.mts
+++ b/src/commands/scan/run-dynamic-sbom-inference.mts
@@ -1,8 +1,11 @@
+import path from 'node:path'
+
import { joinOr } from '@socketsecurity/registry/lib/arrays'
import { logger } from '@socketsecurity/registry/lib/logger'
import { isFactsOnlyTypes } from '../../utils/coana.mts'
import { InputError } from '../../utils/errors.mts'
+import { realpathOrResolved } from '../../utils/fs.mts'
import { generateRecursiveManifests } from '../manifest/generate-recursive-manifests.mts'
import {
PURL_TYPE_BY_TOOL,
@@ -22,11 +25,16 @@ import type {
SidecarAccumulator,
} from '../manifest/scripts/sidecar.mts'
+const DSI_SETTINGS_DOCS_URL =
+ 'https://github.com/SocketDev/socket-cli/blob/v1.x/docs/dynamic-sbom-inference-settings.md'
+
// Who consumes the facts: Coana for `fix` and `reach` (which also resolves
// artifact paths), or only the scan upload.
export type FactsConsumer = 'fix' | 'reach' | 'upload'
export type DynamicSbomInferenceResult = {
+ // Facts files of dropped runs, which the scan must not collect.
+ discardedFactsPaths: string[]
// Each discovered build root's purl type, generated or not: Coana takes
// these types' artifacts only from facts files.
factsOnlyTypes: FactsOnlyTypes
@@ -119,7 +127,12 @@ export async function runDynamicSbomInference({
// matches handleManifestDynamicSbomInference's own check.
if (outcomes.some(o => o.status === 'failed')) {
throw new InputError(
- 'One or more independent build roots failed to generate Socket facts; aborting (see the errors above).',
+ [
+ 'One or more independent build roots failed to generate Socket facts; aborting (see the errors above).',
+ '',
+ `- To fix a build's settings or skip it, run \`${DSI_SETUP_COMMAND}\`.`,
+ `- The settings are documented at ${DSI_SETTINGS_DOCS_URL}.`,
+ ].join('\n'),
)
}
const factsPaths = outcomes
@@ -129,6 +142,9 @@ export async function runDynamicSbomInference({
checkFactsForCoana(outcomes, consumer, !!factsPaths.length)
}
return {
+ discardedFactsPaths: outcomes.flatMap(o =>
+ o.discardedFactsPath ? [o.discardedFactsPath] : [],
+ ),
factsOnlyTypes,
factsPaths,
resolvedPathsSidecar:
@@ -137,3 +153,23 @@ export async function runDynamicSbomInference({
: undefined,
}
}
+
+// `paths` without the facts files discovery discarded: each holds what was
+// there before the scan, or a dropped run's facts, not a kept build's.
+export async function withoutDiscardedFacts(
+ paths: string[],
+ discarded: readonly string[] | undefined,
+): Promise {
+ if (!discarded?.length) {
+ return paths
+ }
+ const names = new Set(discarded.map(p => path.basename(p)))
+ const keys = new Set(discarded)
+ const isDiscarded = await Promise.all(
+ paths.map(
+ async p =>
+ names.has(path.basename(p)) && keys.has(await realpathOrResolved(p)),
+ ),
+ )
+ return paths.filter((_, index) => !isDiscarded[index])
+}
diff --git a/src/commands/scan/run-dynamic-sbom-inference.test.mts b/src/commands/scan/run-dynamic-sbom-inference.test.mts
index 656fa02ee..15f85a220 100644
--- a/src/commands/scan/run-dynamic-sbom-inference.test.mts
+++ b/src/commands/scan/run-dynamic-sbom-inference.test.mts
@@ -1,6 +1,13 @@
+import { promises as fs } from 'node:fs'
+import { tmpdir } from 'node:os'
+import path from 'node:path'
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
-import { runDynamicSbomInference } from './run-dynamic-sbom-inference.mts'
+import {
+ runDynamicSbomInference,
+ withoutDiscardedFacts,
+} from './run-dynamic-sbom-inference.mts'
import type { FactsConsumer } from './run-dynamic-sbom-inference.mts'
import type { PURL_Type } from '../../utils/ecosystem.mts'
@@ -92,6 +99,7 @@ describe('runDynamicSbomInference', () => {
])
expect(await run('upload')).toEqual({
+ discardedFactsPaths: [],
factsOnlyTypes: ['maven'],
factsPaths: ['/repo/a/.socket.facts.json'],
resolvedPathsSidecar: undefined,
@@ -210,3 +218,33 @@ describe('runDynamicSbomInference', () => {
)
})
})
+
+describe('withoutDiscardedFacts', () => {
+ it('leaves out a discarded facts file, also through a symlinked path', async () => {
+ const real = await fs.realpath(
+ await fs.mkdtemp(path.join(tmpdir(), 'discarded-facts-')),
+ )
+ const link = `${real}-link`
+ try {
+ await fs.symlink(real, link)
+ const discarded = path.join(real, 'pom.xml.socket.facts.json')
+ const kept = path.join(real, 'app-cli.pom.xml.socket.facts.json')
+ await fs.writeFile(discarded, '{}')
+ await fs.writeFile(kept, '{}')
+
+ expect(
+ await withoutDiscardedFacts(
+ [
+ path.join(link, 'pom.xml.socket.facts.json'),
+ kept,
+ path.join(real, 'pom.xml'),
+ ],
+ [discarded],
+ ),
+ ).toEqual([kept, path.join(real, 'pom.xml')])
+ } finally {
+ await fs.rm(link, { force: true })
+ await fs.rm(real, { recursive: true, force: true })
+ }
+ })
+})
diff --git a/src/utils/socket-json.mts b/src/utils/socket-json.mts
index 3ed36ad06..761063070 100644
--- a/src/utils/socket-json.mts
+++ b/src/utils/socket-json.mts
@@ -50,6 +50,9 @@ export interface SocketJson {
perRepoTimeout?: number | undefined
verbose?: boolean | undefined
}
+ // Keyed by the name of a build file in this file's directory: settings
+ // of that build tool's section for that build alone.
+ builds?: Record | undefined
conda?: {
disabled?: boolean | undefined
infile?: string | undefined
@@ -151,6 +154,13 @@ export type ManifestDefaults<
K extends keyof NonNullable['manifest']>,
> = NonNullable['manifest']>[K]>
+export type SocketJsonBuildEntry = {
+ disabled?: boolean | undefined
+ [field: string]: unknown
+}
+
+export type SocketJsonInDir = { dir: string; sockJson: SocketJson }
+
export function readOrDefaultSocketJson(cwd: string): SocketJson {
const jsonCResult = readSocketJsonSync(cwd, true)
return jsonCResult.ok
@@ -222,33 +232,33 @@ function mergeManifestDefaults(
}
}
-// Cascades `defaults.manifest.*` from `rootSockJson` down to `dir`: each
-// ancestor's own socket.json, between `dir` and `boundaryDir` (exclusive),
-// merges in, nearest-to-`dir` winning field by field.
-export function readSocketJsonCascade(
- dir: string,
- boundaryDir: string,
- rootSockJson: SocketJson,
+// Merges `defaults.manifest.*` of `chain`, the nearest winning field by field.
+export function mergeSocketJsonChain(
+ chain: readonly SocketJsonInDir[],
): SocketJson {
- const boundary = path.resolve(boundaryDir)
+ return chain.reduce(
+ (merged, { sockJson }) => mergeManifestDefaults(merged, sockJson),
+ getDefaultSocketJson(),
+ )
+}
+
+// Every socket.json from the filesystem root down to `dir`, farthest first.
+export function readSocketJsonChain(dir: string): SocketJsonInDir[] {
+ const chain: SocketJsonInDir[] = []
let current = path.resolve(dir)
- // Farthest-from-dir first, so the merge loop below applies overrides in
- // increasing precedence and the nearest-to-dir file wins last.
- const ancestorsFarToNear: SocketJson[] = []
- while (current !== boundary) {
+ for (;;) {
if (existsSync(path.join(current, SOCKET_JSON))) {
const jsonCResult = readSocketJsonSync(current, true)
if (jsonCResult.ok) {
- ancestorsFarToNear.unshift(jsonCResult.data)
+ chain.unshift({ dir: current, sockJson: jsonCResult.data })
}
}
const parent = path.dirname(current)
if (parent === current) {
- break
+ return chain
}
current = parent
}
- return ancestorsFarToNear.reduce(mergeManifestDefaults, rootSockJson)
}
export function getDefaultSocketJson(): SocketJson {
diff --git a/src/utils/socket-json.test.mts b/src/utils/socket-json.test.mts
index f7b61e92f..d0ed1c46b 100644
--- a/src/utils/socket-json.test.mts
+++ b/src/utils/socket-json.test.mts
@@ -4,7 +4,7 @@ import path from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
-import { readSocketJsonCascade } from './socket-json.mts'
+import { mergeSocketJsonChain, readSocketJsonChain } from './socket-json.mts'
import type { SocketJson } from './socket-json.mts'
@@ -20,38 +20,39 @@ function mavenConfig(sockJson: SocketJson) {
return sockJson.defaults?.manifest?.maven
}
-describe('readSocketJsonCascade', () => {
+function readSocketJsonCascade(dir: string): SocketJson {
+ return mergeSocketJsonChain(readSocketJsonChain(dir))
+}
+
+describe('readSocketJsonChain and mergeSocketJsonChain', () => {
let root = ''
- let rootSockJson: SocketJson
beforeEach(async () => {
root = await fs.mkdtemp(path.join(tmpdir(), 'socket-json-cascade-'))
- rootSockJson = {
+ await writeSocketJson(root, {
version: 1,
defaults: {
manifest: {
maven: { bin: 'mvn', excludeConfigs: 'root-exclude' },
},
},
- } as SocketJson
+ })
})
afterEach(async () => {
await fs.rm(root, { recursive: true, force: true })
})
- it('returns rootSockJson unchanged when dir is the boundary itself', () => {
- expect(readSocketJsonCascade(root, root, rootSockJson)).toBe(rootSockJson)
- })
-
- it('returns rootSockJson unchanged when nothing is found between dir and the boundary', async () => {
+ it('reads the socket.json of every directory above, however far up', async () => {
const buildRoot = path.join(root, 'workspace', 'project')
await fs.mkdir(buildRoot, { recursive: true })
- const result = readSocketJsonCascade(buildRoot, root, rootSockJson)
- expect(result).toBe(rootSockJson)
+ expect(mavenConfig(readSocketJsonCascade(buildRoot))).toEqual({
+ bin: 'mvn',
+ excludeConfigs: 'root-exclude',
+ })
})
- it("merges the build root's own socket.json over the root config instead of replacing it", async () => {
+ it("merges the build root's own socket.json over the ones above instead of replacing them", async () => {
const buildRoot = path.join(root, 'project')
await fs.mkdir(buildRoot, { recursive: true })
await writeSocketJson(buildRoot, {
@@ -59,8 +60,7 @@ describe('readSocketJsonCascade', () => {
defaults: { manifest: { maven: { javaHome: '/opt/jdk-11' } } },
})
- const result = readSocketJsonCascade(buildRoot, root, rootSockJson)
- expect(mavenConfig(result)).toEqual({
+ expect(mavenConfig(readSocketJsonCascade(buildRoot))).toEqual({
bin: 'mvn',
excludeConfigs: 'root-exclude',
javaHome: '/opt/jdk-11',
@@ -68,7 +68,7 @@ describe('readSocketJsonCascade', () => {
})
it('does not touch an ecosystem the override never mentions', async () => {
- rootSockJson = {
+ await writeSocketJson(root, {
version: 1,
defaults: {
manifest: {
@@ -76,7 +76,7 @@ describe('readSocketJsonCascade', () => {
maven: { bin: 'mvn' },
},
},
- } as SocketJson
+ })
const buildRoot = path.join(root, 'project')
await fs.mkdir(buildRoot, { recursive: true })
await writeSocketJson(buildRoot, {
@@ -84,8 +84,9 @@ describe('readSocketJsonCascade', () => {
defaults: { manifest: { maven: { javaHome: '/opt/jdk-11' } } },
})
- const result = readSocketJsonCascade(buildRoot, root, rootSockJson)
- expect(result.defaults?.manifest?.gradle).toEqual({ bin: './gradlew' })
+ expect(readSocketJsonCascade(buildRoot).defaults?.manifest?.gradle).toEqual(
+ { bin: './gradlew' },
+ )
})
it('cascades multiple levels, nearest-to-dir winning per field', async () => {
@@ -108,8 +109,7 @@ describe('readSocketJsonCascade', () => {
defaults: { manifest: { maven: { javaHome: '/opt/jdk-11' } } },
})
- const result = readSocketJsonCascade(buildRoot, root, rootSockJson)
- expect(mavenConfig(result)).toEqual({
+ expect(mavenConfig(readSocketJsonCascade(buildRoot))).toEqual({
// From root, untouched by either override.
bin: 'mvn',
// Workspace overrides root; project doesn't mention it.
@@ -129,26 +129,9 @@ describe('readSocketJsonCascade', () => {
defaults: { manifest: { maven: { excludeConfigs: null } } },
})
- const result = readSocketJsonCascade(buildRoot, root, rootSockJson)
- expect(mavenConfig(result)).toEqual({
+ expect(mavenConfig(readSocketJsonCascade(buildRoot))).toEqual({
bin: 'mvn',
excludeConfigs: null,
})
})
-
- it('does not walk past the boundary even if an ancestor above it has one', async () => {
- await writeSocketJson(tmpdir(), {
- version: 1,
- defaults: { manifest: { maven: { javaHome: '/outside-scope' } } },
- })
- const buildRoot = path.join(root, 'project')
- await fs.mkdir(buildRoot, { recursive: true })
-
- try {
- const result = readSocketJsonCascade(buildRoot, root, rootSockJson)
- expect(result).toBe(rootSockJson)
- } finally {
- await fs.rm(path.join(tmpdir(), 'socket.json'), { force: true })
- }
- })
})