Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Added
- Socket facts for a self-contained .NET build include its runtime packs.
- Tune reachability per build tool in `socket.json`: `reachability.build` compiles the project before the analysis, `reachability.javaHome` (Maven, Gradle, sbt) sets the JDK the analysis assumes, and `reachability.runtimeVersion` (.NET) sets the runtime it starts on.

### Changed
- Updated the Coana CLI to v `15.15.4`.
Expand Down
7 changes: 7 additions & 0 deletions docs/dynamic-sbom-inference-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ Under `defaults.manifest.maven`, `.gradle`, `.sbt` or `.dotnet`, or inside a
| `disabled` | `true` skips builds; see below. |
| `facts` | Gradle and sbt: `false` skips the build. |
| `verbose` | `true` streams the build tool's output. |
| `reachability.build` | `"compile"` builds the project before the analysis, and a failed build fails the scan; `"none"` or unset analyses it unbuilt. |
| `reachability.javaHome` | Maven, Gradle and sbt: the JDK the analysis takes the program to run on, passed to Coana as written. |
| `reachability.runtimeVersion` | .NET: the runtime a framework-dependent build's analysis starts on. `major.minor` takes its latest installed patch; `major.minor.patch[-prerelease]` takes exactly that runtime. |

Only reachability scans read the `reachability` fields, each cascading on its
own, and only for the build tools they run. An invalid value or a field the
build tool does not take fails the scan.

## Skipping builds

Expand Down
161 changes: 158 additions & 3 deletions src/commands/manifest/build-root-config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import path from 'node:path'
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 { joinAnd, joinOr } from '../../utils/arrays.mts'
import { InputError } from '../../utils/errors.mts'
import { getOrInsert } from '../../utils/get-or-insert.mts'
import { logger } from '../../utils/logger.mts'
import { isRecord } from '../../utils/objects.mts'
import {
REACHABILITY_BUILDS,
mergeBuildToolDefaults,
mergeSocketJsonChain,
readSocketJsonChain,
} from '../../utils/socket-json.mts'

import type { BuildRoot } from './discover-manifest-roots.mts'
import type { ReachabilitySettings } from './run-manifest-facts.mts'
import type { BuildTool } from './scripts/build-tool.mts'
import type {
SocketJson,
Expand All @@ -33,10 +37,77 @@ export type ResolvedBuildRootSettings = {
disabledBy: DisabledBy | undefined
// The key of the build's own `defaults.manifest.builds` entry.
entryKey: string | undefined
// Checks reachability settings in every socket.json that supplies them.
// An invalid setting raises an InputError naming the file and field.
reachability: () => ReachabilitySettings | undefined
// The cascaded settings, the build's own entries applied last.
sockJson: SocketJson
}

// major.minor, or major.minor.patch[-prerelease] as `RuntimeFrameworkVersion`
// takes a .NET runtime, written as SemVer 2.0.0 items 2 and 9 say.
const NUMBER = '(?:0|[1-9][0-9]*)'
const PRERELEASE_IDENTIFIER = `(?:${NUMBER}|[0-9]*[A-Za-z-][0-9A-Za-z-]*)`
const DOTNET_RUNTIME_VERSION = new RegExp(
`^${NUMBER}\\.${NUMBER}(?:\\.${NUMBER}(?:-${PRERELEASE_IDENTIFIER}(?:\\.${PRERELEASE_IDENTIFIER})*)?)?$`,
)

type ReachabilityField = {
// What a valid value is, as "set it to …" names it.
expected: string
takes: (value: unknown) => boolean
tools: readonly BuildTool[]
}

const REACHABILITY_FIELDS: Record<
keyof ReachabilitySettings,
ReachabilityField
> = {
__proto__: null,
build: {
expected: joinOr(REACHABILITY_BUILDS.map(b => `"${b}"`)),
takes: (value: unknown) =>
(REACHABILITY_BUILDS as readonly unknown[]).includes(value),
tools: BUILD_TOOLS,
},
javaHome: {
expected: 'a JDK path',
takes: (value: unknown) => typeof value === 'string' && !!value,
tools: ['gradle', 'maven', 'sbt'],
},
runtimeVersion: {
expected:
'a .NET runtime version: major.minor, such as "8.0", for its latest installed patch, or major.minor.patch[-prerelease], such as "8.0.11", for exactly that runtime',
takes: (value: unknown) =>
typeof value === 'string' && DOTNET_RUNTIME_VERSION.test(value),
tools: ['dotnet'],
},
} as unknown as Record<keyof ReachabilitySettings, ReachabilityField>

function checkReachability(
tool: BuildTool,
value: unknown,
setting: string,
socketJson: string,
): void {
if (value === undefined) {
return
}
if (!isRecord(value)) {
throw new InputError(
`\`${setting}\` in ${socketJson} is ${JSON.stringify(value)}; set it to an object, or remove it.`,
)
}
for (const { 0: field, 1: fieldValue } of Object.entries(value)) {
const problem = reachabilityProblem(tool, field, fieldValue)
if (problem) {
throw new InputError(
`\`${setting}.${field}\` in ${socketJson} ${problem}`,
)
}
}
}

// Gradle and sbt name a build by any build file of its directory.
function namesBuildFile(key: string): boolean {
return BUILD_TOOLS.some(tool => {
Expand All @@ -62,6 +133,27 @@ function ownEntryKeys(
)
}

function reachabilityKey(tool: BuildTool): string {
return `defaults.manifest.${tool}.reachability`
}

// Without the fields a socket.json cleared with null.
function reachabilitySettings(
value: unknown,
): ReachabilitySettings | undefined {
if (!isRecord(value)) {
return undefined
}
const settings = Object.fromEntries(
Object.entries(value).filter(
({ 1: setting }) => setting !== null && setting !== undefined,
),
)
return Object.keys(settings).length
? (settings as ReachabilitySettings)
: undefined
}

function validateBuilds({ dir, sockJson }: SocketJsonInDir): void {
const builds: unknown = sockJson.defaults?.manifest?.builds
if (builds === undefined) {
Expand Down Expand Up @@ -176,9 +268,11 @@ export function createBuildRootSettingsResolver(
? 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)
const entry = entryKey
? (builds![entryKey] as SocketJsonBuildEntry)
: undefined
if (entryKey && entry) {
Object.assign(section, mergeBuildToolDefaults(section, entry))
if (entry.disabled !== undefined) {
entryDisabledBy = { dir: root.dir, key: entryKey }
}
Expand All @@ -194,6 +288,19 @@ export function createBuildRootSettingsResolver(
return {
disabledBy,
entryKey,
reachability: () => {
const cascaded = cascadedReachability(tool, chain)
if (!entryKey || !entry) {
return cascaded
}
checkReachability(
tool,
entry['reachability'],
`${buildsSetting(entryKey)}.reachability`,
socketJsonPath(root.dir),
)
return reachabilitySettings(section['reachability'])
},
sockJson: {
...merged,
defaults: {
Expand Down Expand Up @@ -226,6 +333,54 @@ export function displayPath(file: string): string {
return rel.startsWith('..') || path.isAbsolute(rel) ? file : rel || '.'
}

// Returns a phrase explaining why the tool rejects this field value.
// Returns undefined for a valid value.
export function reachabilityProblem(
tool: BuildTool,
field: string,
value: unknown,
): string | undefined {
const spec = REACHABILITY_FIELDS[field as keyof ReachabilitySettings] as
| ReachabilityField
| undefined
if (!spec?.tools.includes(tool)) {
const fields = Object.keys(REACHABILITY_FIELDS).filter(name =>
REACHABILITY_FIELDS[name as keyof ReachabilitySettings].tools.includes(
tool,
),
)
return `is not a reachability setting of ${tool} builds, which take ${joinAnd(fields)}; remove it.`
}
if (value === null || spec.takes(value)) {
return undefined
}
return `is ${JSON.stringify(value)}; set it to ${spec.expected}, to null to clear an inherited value, or remove it.`
}

export function socketJsonPath(dir: string): string {
return displayPath(path.join(dir, SOCKET_JSON))
}

// The `reachability` settings of `tool` that `chain` cascades to, checked in
// every socket.json that sets them.
export function cascadedReachability(
tool: BuildTool,
chain: readonly SocketJsonInDir[],
): ReachabilitySettings | undefined {
for (const file of chain) {
socketJsonReachability(tool, file)
}
return reachabilitySettings(
mergeSocketJsonChain(chain).defaults?.manifest?.[tool]?.reachability,
)
}

// An invalid setting raises an InputError naming the file.
export function socketJsonReachability(
tool: BuildTool,
{ dir, sockJson }: SocketJsonInDir,
): ReachabilitySettings | undefined {
const value = sockJson.defaults?.manifest?.[tool]?.reachability
checkReachability(tool, value, reachabilityKey(tool), socketJsonPath(dir))
return reachabilitySettings(value)
}
Loading
Loading