Repository navigation
fix(scan): scan without facts when no build root is found #1817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,8 +32,8 @@ describe('generateSocketFactsForFix', () => { | |
| await fs.rm(tmpDir, { force: true, recursive: true }) | ||
| }) | ||
|
|
||
| async function generate() { | ||
| return await generateSocketFactsForFix({ | ||
| function generateOrNone() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [agent] The |
||
| return generateSocketFactsForFix({ | ||
| context: { coanaVersion: '1.2.3', orgSlug: 'acme' }, | ||
| cwd, | ||
| excludePaths: [], | ||
|
|
@@ -43,6 +43,14 @@ describe('generateSocketFactsForFix', () => { | |
| }) | ||
| } | ||
|
|
||
| async function generate() { | ||
| const generated = await generateOrNone() | ||
| if (!generated) { | ||
| throw new Error('Expected generated facts.') | ||
| } | ||
| return generated | ||
| } | ||
|
|
||
| it('restores the generated facts per attempt and the original facts afterwards', async () => { | ||
| mockRunDynamicSbomInference.mockImplementation(async () => { | ||
| await fs.writeFile(existing, 'regenerated') | ||
|
|
@@ -117,6 +125,17 @@ describe('generateSocketFactsForFix', () => { | |
| expect(JSON.parse(await fs.readFile(sidecarFile, 'utf8'))).toEqual([]) | ||
| }) | ||
|
|
||
| it('generates nothing when no build root was discovered', async () => { | ||
| mockRunDynamicSbomInference.mockResolvedValue({ | ||
| discardedFactsPaths: [], | ||
| factsOnlyTypes: undefined, | ||
| factsPaths: [], | ||
| }) | ||
|
|
||
| expect(await generateOrNone()).toBeUndefined() | ||
| expect(await fs.readFile(existing, 'utf8')).toBe('original') | ||
| }) | ||
|
|
||
| it('restores an existing facts file that generation does not rewrite', async () => { | ||
| mockRunDynamicSbomInference.mockImplementation(async () => { | ||
| await fs.writeFile(fresh, 'generated') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,8 +31,7 @@ export async function handleManifestDynamicSbomInference({ | |
|
|
||
| const result: CResult<RecursiveManifestOutcome[]> = !outcomes.length | ||
| ? { | ||
| ok: false, | ||
| code: 1, | ||
| ok: true, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [agent] With this branch now a success, the nesting does no work: an empty |
||
| message: | ||
| 'No Gradle, sbt, Maven or .NET build root was found beneath the given directory.', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [agent] This hardcoded list now becomes the normal success output, and it doesn't match the scan path's |
||
| data: outcomes, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -480,20 +480,19 @@ describe('handleCreateNewScan excludePaths', () => { | |
| expect(mockFetchCreateOrgFullScan).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('aborts when --dynamic-sbom-inference finds no build root', async () => { | ||
| it('scans without facts when --dynamic-sbom-inference finds no build root', async () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [agent] With the abort gone, this test and the next one ("build roots were found but none generated facts") have the same setup and assertions, differing only in the mocked outcomes. One |
||
| mockGenerateRecursiveManifests.mockResolvedValueOnce([]) | ||
|
|
||
| const config = createConfig({ autoManifest: false, targets: ['/repo'] }) | ||
| config.reach.dynamicSbomInference = ['maven', 'nuget'] | ||
|
|
||
| await expect(handleCreateNewScan(config)).rejects.toThrow( | ||
| /No .NET, Gradle, Maven, or sbt build root was found/, | ||
| ) | ||
| expect(mockGetPackageFilesForScan).not.toHaveBeenCalled() | ||
| expect(mockFetchCreateOrgFullScan).not.toHaveBeenCalled() | ||
| await handleCreateNewScan(config) | ||
|
|
||
| expect(mockGetPackageFilesForScan).toHaveBeenCalled() | ||
| expect(mockFetchCreateOrgFullScan).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not abort when build roots were found but none generated facts (empty/skippedDisabled), unlike genuinely finding none', async () => { | ||
| it('does not abort when build roots were found but none generated facts (empty/skippedDisabled)', async () => { | ||
| mockGenerateRecursiveManifests.mockResolvedValueOnce([ | ||
| { dir: '/repo/service-a', ecosystem: 'maven', status: 'empty' }, | ||
| { dir: '/repo/service-b', ecosystem: 'maven', status: 'skippedDisabled' }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,9 @@ 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 | ||
| // these types' artifacts only from facts files. Undefined when no build | ||
| // root was discovered. | ||
| factsOnlyTypes: FactsOnlyTypes | undefined | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [agent] "No build root" now has two shapes on adjacent layers: here it's a full result with Simpler: keep |
||
| factsPaths: string[] | ||
| } | ||
|
|
||
|
|
@@ -93,18 +94,17 @@ export async function runDynamicSbomInference({ | |
| const factsOnlyTypes = [ | ||
| ...new Set(outcomes.map(o => PURL_TYPE_BY_TOOL[o.ecosystem])), | ||
| ] | ||
| // No candidates discovered at all (distinct from candidates that were found | ||
| // but produced no generated facts - empty/skippedDisabled are already warned | ||
| // about elsewhere and are not this kind of mistake). | ||
| // A repository without such builds is scanned as without the flag, so the | ||
| // flag can be passed to every repository. | ||
| if (!isFactsOnlyTypes(factsOnlyTypes)) { | ||
| throw new InputError( | ||
| [ | ||
| `No ${joinOr(buildToolLabelsFor(purlTypes))} build root was found.`, | ||
| '', | ||
| '- Select other ecosystems with --dynamic-sbom-inference-ecosystems, or remove --dynamic-sbom-inference.', | ||
| '- Make sure to run it from the correct dir (use --cwd to target another dir).', | ||
| ].join('\n'), | ||
| logger.info( | ||
| `No ${joinOr(buildToolLabelsFor(purlTypes))} build root was found, so no Socket facts were generated.`, | ||
| ) | ||
| return { | ||
| discardedFactsPaths: [], | ||
| factsOnlyTypes: undefined, | ||
| factsPaths: [], | ||
| } | ||
| } | ||
| // Fail loud rather than silently proceed with a partial multi-root result: | ||
| // matches handleManifestDynamicSbomInference's own check. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[agent] With no build root, everything up to the early return at line 90 is wasted, and that is now a success path: a full repo walk via
findFactsPaths(), a backup of the existing facts files, and a work dir. The walk also repeats the onecoanaFixWithFactsalready did intoscanFilepaths.Cheaper: pass
scanFilepaths.filter(isSocketFactsFile)in for this first lookup and keepfindFactsPathsfor the cleanup in thecatch. That also saves the repeated walk when build roots do exist.