Skip to content

Commit 8ab3992

Browse files
[8.19] [CI] Native module checker CLI should ignore optional transitive dependencies (#226277) (#226430)
# Backport This will backport the following commits from `main` to `8.19`: - [[CI] Native module checker CLI should ignore optional transitive dependencies (#226277)](#226277) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Alex Szabo","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-03T13:35:03Z","message":"[CI] Native module checker CLI should ignore optional transitive dependencies (#226277)\n\n## Summary\nThe native module usage checker CLI is used to prevent natively compiled\nmodules to be part of the production builds.\n\nSince transitive optional dependencies are not installed in production\nbuilds, it's not necessary to alert on these native modules appearing\nwhen optional.","sha":"cec1a92c27954ff93dedc580d3e71454bf8c6bf2","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Operations","release_note:skip","backport:version","v9.1.0","v8.19.0","v9.2.0","v8.18.4","v9.0.4"],"title":"[CI] Native module checker CLI should ignore optional transitive dependencies","number":226277,"url":"https://github.com/elastic/kibana/pull/226277","mergeCommit":{"message":"[CI] Native module checker CLI should ignore optional transitive dependencies (#226277)\n\n## Summary\nThe native module usage checker CLI is used to prevent natively compiled\nmodules to be part of the production builds.\n\nSince transitive optional dependencies are not installed in production\nbuilds, it's not necessary to alert on these native modules appearing\nwhen optional.","sha":"cec1a92c27954ff93dedc580d3e71454bf8c6bf2"}},"sourceBranch":"main","suggestedTargetBranches":["9.1","8.19","8.18","9.0"],"targetPullRequestStates":[{"branch":"9.1","label":"v9.1.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/226277","number":226277,"mergeCommit":{"message":"[CI] Native module checker CLI should ignore optional transitive dependencies (#226277)\n\n## Summary\nThe native module usage checker CLI is used to prevent natively compiled\nmodules to be part of the production builds.\n\nSince transitive optional dependencies are not installed in production\nbuilds, it's not necessary to alert on these native modules appearing\nwhen optional.","sha":"cec1a92c27954ff93dedc580d3e71454bf8c6bf2"}},{"branch":"8.18","label":"v8.18.4","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.0","label":"v9.0.4","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Alex Szabo <[email protected]>
1 parent 0acb267 commit 8ab3992

File tree

8 files changed

+78
-7
lines changed

8 files changed

+78
-7
lines changed

packages/kbn-check-prod-native-modules-cli/check_prod_native_modules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async function checkProdNativeModules(log: ToolingLog) {
107107

108108
try {
109109
// Gets all production dependencies based on package.json and then searches across transient dependencies using lock file
110-
const rawProductionDependencies = findProductionDependencies(log, await readYarnLock());
110+
const rawProductionDependencies = findProductionDependencies(log, await readYarnLock(), true);
111111

112112
// Converts rawProductionDependencies into a simple Map of production dependencies
113113
const productionDependencies: Map<string, boolean> = new Map();

packages/kbn-check-prod-native-modules-cli/integration_tests/__fixtures__/with_optional_native_modules/node_modules/optional-native-module/binding.gyp

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kbn-check-prod-native-modules-cli/integration_tests/__fixtures__/with_optional_native_modules/node_modules/optional-native-module/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kbn-check-prod-native-modules-cli/integration_tests/__fixtures__/with_optional_native_modules/node_modules/package-x/package.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "with-optional-native-modules-project",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"package-x": "^2.0.0"
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
optional-native-module@^1.0.0:
2+
version "1.0.0"
3+
resolved "https://registry.yarnpkg.com/optional-native-module/-/optional-native-module-1.0.0.tgz"
4+
integrity sha1-example789
5+
6+
package-x@^2.0.0:
7+
version "2.0.0"
8+
resolved "https://registry.yarnpkg.com/package-x/-/package-x-2.0.0.tgz"
9+
optionalDependencies:
10+
optional-native-module "^1.0.0"
11+
integrity sha1-example456

packages/kbn-check-prod-native-modules-cli/integration_tests/run_check_prod_native_modules.cli.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,34 @@ describe('checkProdNativeModules', () => {
149149
'Production native modules were detected and logged above'
150150
);
151151
});
152+
153+
it('should ignore optional dependencies when checking for native modules', async () => {
154+
// Use a fixture with optional dependencies
155+
const withOptionalNativeModulesDir = path.join(fixturesDir, 'with_optional_native_modules');
156+
const withOptionalNativeModulesPkgJsonPath = path.join(
157+
withOptionalNativeModulesDir,
158+
'package.json'
159+
);
160+
jest.spyOn(process, 'cwd').mockReturnValue(withOptionalNativeModulesDir);
161+
// eslint-disable-next-line @typescript-eslint/no-var-requires
162+
jest.replaceProperty(require('@kbn/repo-info'), 'REPO_ROOT', withOptionalNativeModulesDir);
163+
164+
const withOptionalNativeModulesPkgJson = JSON.parse(
165+
fs.readFileSync(withOptionalNativeModulesPkgJsonPath, 'utf8')
166+
);
167+
168+
jest.replaceProperty(
169+
// eslint-disable-next-line @typescript-eslint/no-var-requires
170+
require('@kbn/repo-info'),
171+
'kibanaPackageJson',
172+
withOptionalNativeModulesPkgJson
173+
);
174+
175+
const result = await checkProdNativeModules(mockLog);
176+
177+
expect(result).toBe(false);
178+
expect(mockLog.success).toHaveBeenCalledWith(
179+
'No production native modules installed were found'
180+
);
181+
});
152182
});

packages/kbn-yarn-lock-validator/src/find_production_dependencies.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ import { YarnLock } from './yarn_lock';
1717
* dependencies listed in package.json and then traversing deeply into the transitive
1818
* dependencies as declared by the yarn.lock file.
1919
*/
20-
export function findProductionDependencies(log: SomeDevLog, yarnLock: YarnLock) {
20+
export function findProductionDependencies(
21+
log: SomeDevLog,
22+
yarnLock: YarnLock,
23+
ignoreOptional = false
24+
) {
2125
const resolved = new Map<string, { name: string; version: string }>();
2226

2327
// queue of dependencies entries, we will add the transitive dependencies to
24-
// this queue as we itterate
28+
// this queue as we iterate
2529
const depQueue = Object.entries(kibanaPackageJson.dependencies);
2630

2731
for (const [name, versionRange] of depQueue) {
@@ -40,10 +44,9 @@ export function findProductionDependencies(log: SomeDevLog, yarnLock: YarnLock)
4044

4145
resolved.set(key, { name, version: pkg.version });
4246

43-
const allDepsEntries = [
44-
...Object.entries(pkg.dependencies || {}),
45-
...Object.entries(pkg.optionalDependencies || {}),
46-
];
47+
const allDepsEntries = Object.entries(pkg.dependencies || {}).concat(
48+
ignoreOptional ? [] : Object.entries(pkg.optionalDependencies || {})
49+
);
4750

4851
for (const [childName, childVersionRange] of allDepsEntries) {
4952
depQueue.push([childName, childVersionRange]);

0 commit comments

Comments
 (0)