-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix: replace auto-detected root component with user-provided one #1421
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -23,10 +23,12 @@ import * as CDX from '@cyclonedx/cyclonedx-library' | |
| import type { Compilation, Module } from 'webpack' | ||
|
|
||
| import { | ||
| doComponentsMatch, | ||
| getPackageDescription, | ||
| isNonNullable, | ||
| normalizePackageManifest, | ||
| type PackageDescription, | ||
| type RootComponentCreationResult, | ||
| structuredClonePolyfill | ||
| } from './_helpers' | ||
|
|
||
|
|
@@ -50,7 +52,7 @@ export class Extractor { | |
| this.#leGatherer = leFetcher | ||
| } | ||
|
|
||
| generateComponents (modules: Iterable<Module>, collectEvidence: boolean, logger?: WebpackLogger): Iterable<CDX.Models.Component> { | ||
| generateComponents (modules: Iterable<Module>, collectEvidence: boolean, rootComponents: RootComponentCreationResult | undefined, logger?: WebpackLogger): Iterable<CDX.Models.Component> { | ||
| const pkgs: Record<string, CDX.Models.Component | undefined> = {} | ||
| const components = new Map<Module, CDX.Models.Component>() | ||
|
|
||
|
|
@@ -69,7 +71,7 @@ export class Extractor { | |
| if (component === undefined) { | ||
| logger?.log('try to build new Component from PkgPath:', pkg.path) | ||
| try { | ||
| component = this.makeComponent(pkg, collectEvidence, logger) | ||
| component = this.#makeComponent(pkg, collectEvidence, rootComponents, logger) | ||
| } catch (err) { | ||
| logger?.debug('unexpected error:', err) | ||
| logger?.warn('skipped Component from PkgPath', pkg.path) | ||
|
|
@@ -91,7 +93,7 @@ export class Extractor { | |
| /** | ||
| * @throws {@link Error} when no component could be fetched | ||
| */ | ||
| makeComponent (pkg: PackageDescription, collectEvidence: boolean, logger?: WebpackLogger): CDX.Models.Component { | ||
| #makeComponent (pkg: PackageDescription, collectEvidence: boolean, rootComponents: RootComponentCreationResult | undefined, logger?: WebpackLogger): CDX.Models.Component { | ||
|
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. why was this function made private? this is an unexpected change. |
||
| try { | ||
| // work with a deep copy, because `normalizePackageManifest()` might modify the data | ||
| /* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- ach */ | ||
|
|
@@ -102,14 +104,18 @@ export class Extractor { | |
| logger?.warn('normalizePackageJson from PkgPath', pkg.path, 'failed:', e) | ||
| } | ||
|
|
||
| const component = this.#componentBuilder.makeComponent( | ||
| let component = this.#componentBuilder.makeComponent( | ||
| /* @ts-expect-error TS2559 */ | ||
| pkg.packageJson as PackageDescription) /* eslint-disable-line @typescript-eslint/no-unsafe-type-assertion -- ack */ | ||
|
|
||
| if (component === undefined) { | ||
| throw new Error(`failed building Component from PkgPath ${pkg.path}`) | ||
| } | ||
|
|
||
| if (rootComponents?.detectedRootComponent !== undefined && doComponentsMatch(component, rootComponents.detectedRootComponent)) { | ||
| component = rootComponents.rootComponent | ||
|
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. unintended design change. a "makeSomething" function makes something - no matter what. |
||
| } | ||
|
|
||
| component.licenses.forEach(l => { | ||
| /* eslint-disable no-param-reassign -- intended */ | ||
| l.acknowledgement = CDX.Enums.LicenseAcknowledgement.Declared | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,11 +24,13 @@ import * as CDX from '@cyclonedx/cyclonedx-library' | |||||||||
| import { Compilation, type Compiler, sources, version as WEBPACK_VERSION } from 'webpack' | ||||||||||
|
|
||||||||||
| import { | ||||||||||
| doComponentsMatch, | ||||||||||
| getPackageDescription, | ||||||||||
| iterableSome, | ||||||||||
| loadJsonFile, | ||||||||||
| normalizePackageManifest, | ||||||||||
| type PackageDescription | ||||||||||
| type PackageDescription, | ||||||||||
| type RootComponentCreationResult | ||||||||||
| } from './_helpers' | ||||||||||
| import { Extractor } from './extractor' | ||||||||||
|
|
||||||||||
|
|
@@ -214,7 +216,8 @@ export class CycloneDxWebpackPlugin { | |||||||||
|
|
||||||||||
| const bom = new CDX.Models.Bom() | ||||||||||
| bom.metadata.lifecycles.add(CDX.Enums.LifecyclePhase.Build) | ||||||||||
| bom.metadata.component = this.#makeRootComponent(compilation.compiler.context, cdxComponentBuilder, logger.getChildLogger('RootComponentBuilder')) | ||||||||||
| const rootComponents = this.#makeRootComponent(compilation.compiler.context, cdxComponentBuilder, logger.getChildLogger('RootComponentBuilder')) | ||||||||||
| bom.metadata.component = rootComponents?.rootComponent | ||||||||||
|
Comment on lines
+219
to
+220
|
||||||||||
| const rootComponents = this.#makeRootComponent(compilation.compiler.context, cdxComponentBuilder, logger.getChildLogger('RootComponentBuilder')) | |
| bom.metadata.component = rootComponents?.rootComponent | |
| const rootComponentResult = this.#makeRootComponent(compilation.compiler.context, cdxComponentBuilder, logger.getChildLogger('RootComponentBuilder')) | |
| bom.metadata.component = rootComponentResult?.rootComponent |
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.
could you please explain what the idea behind the change of the algorithm of this function makeRootComponent is?
i mean, before, it had a name that pretty much described what it did.
now it does ... what?
Copilot
AI
Oct 29, 2025
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.
Missing null/undefined check for detectedRootPackageJson before calling normalizePackageManifest. If rootComponentAutodetect is false and detectedRootPackageJson is undefined, this will cause a runtime error when the condition is true.
Copilot
AI
Oct 29, 2025
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.
Missing space after 'if' keyword. Should be if (rootComponent === undefined) to maintain consistency with the project's coding style.
| if(rootComponent === undefined) { return undefined } | |
| if (rootComponent === undefined) { return undefined } |
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.
unintended design.
the function that returns this
RootComponentCreationResultis private. so the type definition for this very function should happen where this private function is defined.