Skip to content

Commit dac1832

Browse files
committed
fix(manifest): skip .NET where no SDK runs, anchor its exclusions at the scan root
- --auto-manifest, dynamic SBOM inference and the setup wizard skip .NET builds with one warning where no .NET SDK runs, instead of failing scans of repositories that only carry a stray project file; an explicit `socket manifest dotnet` still fails. - --exclude-paths reaches a solution's projects outside its own directory: for tools whose builds reach projects anywhere, patterns stay relative to the scan root, in both generation and the setup wizard. - An unreadable solution is skipped only when its twin is readable, so two unreadable twins no longer skip each other silently. - The setup wizard detects .NET build roots; messages and help name .NET.
1 parent 6fc28cb commit dac1832

25 files changed

Lines changed: 184 additions & 60 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
88

99
### Added
1010
- `socket manifest dotnet` [beta] generates Socket facts files for .NET solutions and projects from your own `dotnet restore`, covering PackageReference (central package management and lock files included) and packages.config projects.
11-
- `socket manifest auto`, `socket manifest setup` and `--auto-manifest` now detect .NET solutions and projects.
11+
- `socket manifest auto`, `socket manifest setup` and `--auto-manifest` now detect .NET solutions and projects, and skip them with a warning where no .NET SDK is installed.
1212

1313
### Fixed
1414
- `--exclude-paths` now also excludes modules a build reaches outside its own directory, such as Maven `../` modules and relocated Gradle projects, when Socket facts are generated for each build root.

‎src/commands/manifest/cmd-manifest-dynamic-sbom-inference.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
const config: CliCommandConfig = {
2222
commandName: 'dynamic-sbom-inference',
2323
description:
24-
'Recursively discover gradle/sbt/maven build roots and generate a Socket facts SBOM for each',
24+
'Recursively discover gradle/sbt/maven/dotnet build roots and generate a Socket facts SBOM for each',
2525
hidden: false,
2626
flags: {
2727
...commonFlags,

‎src/commands/manifest/cmd-manifest-dynamic-sbom-inference.test.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('socket manifest dynamic-sbom-inference', async () => {
1818
cwd: testPath,
1919
})
2020
expect(stdout).toMatchInlineSnapshot(`
21-
"Recursively discover gradle/sbt/maven build roots and generate a Socket facts SBOM for each
21+
"Recursively discover gradle/sbt/maven/dotnet build roots and generate a Socket facts SBOM for each
2222
2323
Usage
2424
$ socket manifest dynamic-sbom-inference [options] [CWD=.]

‎src/commands/manifest/cmd-manifest-setup.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const config: CliCommandConfig = {
3636
dynamicSbomInference: {
3737
type: 'boolean',
3838
description:
39-
'Generates dynamic SBOMs via the Gradle/sbt/Maven package manager tools for more accurate results than static resolution. Scans every build root under CWD first, so the configurator only asks about ecosystems actually found; --exclude-paths matches are bulk-disabled, others configured individually.',
39+
'Generates dynamic SBOMs via the Gradle/sbt/Maven/.NET package manager tools for more accurate results than static resolution. Scans every build root under CWD first, so the configurator only asks about ecosystems actually found; --exclude-paths matches are bulk-disabled, others configured individually.',
4040
},
4141
},
4242
help: (command, config) => `

‎src/commands/manifest/cmd-manifest-setup.test.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('socket manifest setup', async () => {
2424
2525
Options
2626
--default-on-read-error If reading the socket.json fails, just use a default config? Warning: This might override the existing json file!
27-
--dynamic-sbom-inference Generates dynamic SBOMs via the Gradle/sbt/Maven package manager tools for more accurate results than static resolution. Scans every build root under CWD first, so the configurator only asks about ecosystems actually found; --exclude-paths matches are bulk-disabled, others configured individually.
27+
--dynamic-sbom-inference Generates dynamic SBOMs via the Gradle/sbt/Maven/.NET package manager tools for more accurate results than static resolution. Scans every build root under CWD first, so the configurator only asks about ecosystems actually found; --exclude-paths matches are bulk-disabled, others configured individually.
2828
--exclude-paths Build roots matching these glob patterns (and everything beneath them) are marked disabled. Patterns are anchored micromatch globs matched relative to CWD: \`legacy\` matches only \`<cwd>/legacy\`; use \`**/legacy\` to match at any depth. Negation patterns (\`!path\`) are not supported. Accepts a comma-separated value or multiple flags.
2929
3030
This command will try to detect all supported ecosystems in given CWD. Then

‎src/commands/manifest/cmd-manifest.test.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('socket manifest', async () => {
2828
cdxgen Run cdxgen for SBOM generation
2929
conda [beta] Convert a Conda environment.yml file to a python requirements.txt
3030
dotnet [beta] Generate Socket facts files from .NET solutions and projects
31-
dynamic-sbom-inference Recursively discover gradle/sbt/maven build roots and generate a Socket facts SBOM for each
31+
dynamic-sbom-inference Recursively discover gradle/sbt/maven/dotnet build roots and generate a Socket facts SBOM for each
3232
gradle [beta] Generate a Socket facts file (or \`pom.xml\` with --pom) for a Gradle/Java/Kotlin/etc project
3333
kotlin [beta] Generate a Socket facts file (or \`pom.xml\` with --pom) for a Kotlin project
3434
maven [beta] Generate a Socket facts file from a Maven \`pom.xml\` project

‎src/commands/manifest/discover-manifest-roots.mts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,33 @@ export function isBuildRootCovered(
135135

136136
// A dotnet solution is covered once every project it builds is, or, when the
137137
// SDK cannot read it, by a same-named solution in the other format beside it
138-
// (`dotnet sln migrate` writes App.slnx next to App.sln).
138+
// (`dotnet sln migrate` writes App.slnx next to App.sln) that it can read.
139139
export async function isSolutionCovered(
140140
covered: ReadonlySet<string>,
141141
root: BuildRoot,
142-
opts: Pick<ManifestScriptOptions, 'bin' | 'excludePaths' | 'toolOpts'>,
142+
opts: Pick<
143+
ManifestScriptOptions,
144+
'bin' | 'excludeBase' | 'excludePaths' | 'toolOpts'
145+
>,
143146
listMembers: typeof listDotnetEntryProjects = listDotnetEntryProjects,
144147
): Promise<boolean> {
145148
const { entryFile } = root
146-
if (!covered.size || !entryFile || !isDotnetSolution(entryFile)) {
149+
if (!entryFile || !isDotnetSolution(entryFile)) {
147150
return false
148151
}
149-
const listed = await listMembers({
150-
...opts,
151-
entryFile,
152-
projectDir: root.dir,
153-
})
152+
const twin = entryFile.replace(/\.slnx?$/i, ext =>
153+
ext.toLowerCase() === '.sln' ? '.slnx' : '.sln',
154+
)
155+
const hasTwin = existsSync(path.join(root.dir, twin))
156+
if (!covered.size && !hasTwin) {
157+
return false
158+
}
159+
const list = (file: string) =>
160+
listMembers({ ...opts, entryFile: file, projectDir: root.dir })
161+
const listed = await list(entryFile)
154162
if (!listed) {
155-
const twin = entryFile.replace(/\.slnx?$/i, ext =>
156-
ext.toLowerCase() === '.sln' ? '.slnx' : '.sln',
157-
)
158-
if (!existsSync(path.join(root.dir, twin))) {
163+
// Unless the twin is readable, this one is built and its failure shows.
164+
if (!hasTwin || !(await list(twin))) {
159165
return false
160166
}
161167
logger.warn(

‎src/commands/manifest/discover-manifest-roots.test.mts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,28 +351,55 @@ describe('isSolutionCovered', () => {
351351
await fs.writeFile(path.join(dir, 'App.sln'), '')
352352
await fs.writeFile(path.join(dir, 'App.slnx'), '')
353353
const covered = new Set(['/repo/src/A/A.csproj'])
354+
const slnxUnreadable = async ({ entryFile }: { entryFile?: string }) =>
355+
entryFile?.endsWith('.slnx') ? undefined : []
354356
expect(
355357
await isSolutionCovered(
356358
covered,
357359
{ dir, entryFile: 'App.slnx' },
358360
{},
359-
unreadable,
361+
slnxUnreadable,
362+
),
363+
).toBe(true)
364+
// Also before anything is covered, e.g. when the twin built nothing.
365+
expect(
366+
await isSolutionCovered(
367+
new Set(),
368+
{ dir, entryFile: 'App.slnx' },
369+
{},
370+
slnxUnreadable,
360371
),
361372
).toBe(true)
362373
expect(
363374
await isSolutionCovered(
364375
covered,
365376
{ dir, entryFile: 'Other.slnx' },
366377
{},
367-
unreadable,
378+
slnxUnreadable,
368379
),
369380
).toBe(false)
370381
} finally {
371382
await fs.rm(dir, { recursive: true, force: true })
372383
}
373384
})
374385

375-
it('lists no members while nothing is covered, or for a project or a POM', async () => {
386+
it('builds an unreadable solution whose twin is unreadable too, so its failure shows', async () => {
387+
const dir = await fs.mkdtemp(path.join(tmpdir(), 'entry-covered-'))
388+
try {
389+
await fs.writeFile(path.join(dir, 'App.sln'), '')
390+
await fs.writeFile(path.join(dir, 'App.slnx'), '')
391+
const covered = await Promise.all(
392+
['App.sln', 'App.slnx'].map(entryFile =>
393+
isSolutionCovered(new Set(), { dir, entryFile }, {}, unreadable),
394+
),
395+
)
396+
expect(covered).toEqual([false, false])
397+
} finally {
398+
await fs.rm(dir, { recursive: true, force: true })
399+
}
400+
})
401+
402+
it('lists no members while nothing is covered and no twin exists, or for a project or a POM', async () => {
376403
const list = failToList
377404
expect(await isSolutionCovered(new Set(), root, {}, list)).toBe(false)
378405
expect(

‎src/commands/manifest/generate-recursive-manifests.mts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
DSI_SETUP_COMMAND,
1919
REACH_CONTINUE_ON_INSTALL_ERRORS_FLAG,
2020
} from './scripts/resolution-report-render.mts'
21+
import { skipWithoutDotnetSdk } from './scripts/run.mts'
2122
import { SOCKET_JSON } from '../../constants.mts'
2223
import { loadSocketConfig } from '../../utils/config.mts'
2324
import { withTmpDir } from '../../utils/fs.mts'
@@ -40,6 +41,7 @@ export type RecursiveManifestOutcomeStatus =
4041
| 'generated'
4142
| 'skippedCovered'
4243
| 'skippedDisabled'
44+
| 'skippedNoSdk'
4345

4446
export type RecursiveManifestOutcome = BuildRoot & {
4547
ecosystem: BuildTool
@@ -222,11 +224,17 @@ async function runEcosystemCandidates({
222224
outcomes.push({ ...root, ecosystem, status: 'skippedDisabled' })
223225
continue
224226
}
227+
// eslint-disable-next-line no-await-in-loop
228+
if (ecosystem === 'dotnet' && (await skipWithoutDotnetSdk(bin, dir))) {
229+
outcomes.push({ ...root, ecosystem, status: 'skippedNoSdk' })
230+
continue
231+
}
225232
if (
226233
// eslint-disable-next-line no-await-in-loop
227234
await isSolutionCovered(covered, root, {
228235
bin,
229-
excludePaths: excludePathsForRoot,
236+
excludeBase: realCwd,
237+
excludePaths: scanExcludePaths,
230238
toolOpts: buildOpts,
231239
})
232240
) {
@@ -241,8 +249,8 @@ async function runEcosystemCandidates({
241249
cwd: dir,
242250
ecosystem,
243251
entryFile,
244-
excludeConfigs,
245252
excludeBase: realCwd,
253+
excludeConfigs,
246254
excludePaths: scanExcludePaths,
247255
hints: {
248256
continueOnInstallErrorsFlag: withFiles

‎src/commands/manifest/generate-recursive-manifests.test.mts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ vi.mock('../../utils/socket-json.mts', () => ({
1515
vi.mock('./run-manifest-facts.mts', () => ({
1616
runManifestFacts: vi.fn(),
1717
}))
18+
vi.mock('./scripts/run.mts', async importOriginal => ({
19+
...(await importOriginal<typeof import('./scripts/run.mts')>()),
20+
skipWithoutDotnetSdk: vi.fn(async () => false),
21+
}))
1822

1923
import { buildRootPath } from './discover-manifest-roots.mts'
2024
import { generateRecursiveManifests } from './generate-recursive-manifests.mts'
2125
import { runManifestFacts } from './run-manifest-facts.mts'
26+
import { skipWithoutDotnetSdk } from './scripts/run.mts'
2227
import { testPath } from '../../../test/utils.mts'
2328
import {
2429
readOrDefaultSocketJson,
@@ -518,6 +523,33 @@ describe('generateRecursiveManifests', () => {
518523
})
519524
})
520525

526+
it('skips .NET builds, without failing, where no .NET SDK runs', async () => {
527+
const dir = await fs.mkdtemp(path.join(tmpdir(), 'dsi-no-sdk-'))
528+
try {
529+
await fs.writeFile(path.join(dir, 'pom.xml'), '<project/>')
530+
await fs.mkdir(path.join(dir, 'samples'))
531+
await fs.writeFile(path.join(dir, 'samples', 'Stray.csproj'), '')
532+
vi.mocked(skipWithoutDotnetSdk).mockResolvedValueOnce(true)
533+
vi.mocked(runManifestFacts).mockImplementation(async ({ cwd }) => ({
534+
factsPath: path.join(cwd, 'pom.xml.socket.facts.json'),
535+
projects: [],
536+
}))
537+
const outcomes = await generateRecursiveManifests({
538+
cwd: dir,
539+
verbose: false,
540+
})
541+
542+
expect(
543+
outcomes.map(o => `${o.ecosystem}:${o.entryFile}:${o.status}`).sort(),
544+
).toEqual(['dotnet:Stray.csproj:skippedNoSdk', 'maven:pom.xml:generated'])
545+
expect(
546+
vi.mocked(runManifestFacts).mock.calls.map(([o]) => o.ecosystem),
547+
).toEqual(['maven'])
548+
} finally {
549+
await fs.rm(dir, { recursive: true, force: true })
550+
}
551+
})
552+
521553
it('resolves each build root its own nearest socket.json instead of only the root config', async () => {
522554
vi.mocked(readSocketJsonCascade).mockImplementation(
523555
(dir, _boundaryDir, fallback) =>

0 commit comments

Comments
 (0)