Strict Equality Object vs String Type Mismatch in Package Hoisting & Deduping
Issue : Strict Equality Object vs String Type Mismatch in Package Hoisting & Deduping
Metadata
- Subsystem:
scopes/dependencies/dependency-resolver - Source File:
scopes/dependencies/dependency-resolver/manifest/deduping/hoist-dependencies.ts - Line Numbers: 260–283
- Severity:
High - Keywords:
dependency-hoisting,deduplication,type-mismatch,semver,capsule-bloat
Problem Description
In hoist-dependencies.ts, when a dependency contains both semver ranges and exact versions across different workspace components, handleRangesAndVersions() calculates whether to hoist an intersecting range or the most common concrete version to the root manifest.
When the most common version satisfies more components than the range, it hoists that version to rootDependencies and constructs a filterFunc to exclude matching components from having the dependency duplicated in their individual component manifests.
However, filterFunc compares item.range (a string) against mostCommonVersion (an object).
Vulnerable Code
// scopes/dependencies/dependency-resolver/manifest/deduping/hoist-dependencies.ts: Lines 260-282
const allVersions = groups.versions.map((item) => item.range);
const mostCommonVersion = findMostCommonVersion(allVersions);
// Include versions here since we might have a specific version which match the best version as well
const rangesVersions = indexItems.map((item) => item.range);
const bestRange = findBestRange(rangesVersions);
const lifeCycleType = getLifecycleType(indexItems);
const depKeyName = KEY_NAME_BY_LIFECYCLE_TYPE[lifeCycleType];
let filterFunc = (item) => {
if (bestRange.ranges.includes(item.range)) return true;
return false;
};
if (bestRange.count < mostCommonVersion.count) {
dedupedDependencies.rootDependencies[depKeyName][packageName] = mostCommonVersion.version;
filterFunc = (item) => {
if (item.range === mostCommonVersion) return true; // <-- BUG: comparing string to object!
return false;
};
} else {
dedupedDependencies.rootDependencies[depKeyName][packageName] = bestRange.intersectedRange;
}
indexItems.forEach(addToComponentDependenciesMapInDeduped(dedupedDependencies, packageName, filterFunc));Root Cause Analysis
- Contrast with
handleVersionsOnly()in lines 200–208:const mostCommonVersion = findMostCommonVersion(allVersions).version; // Extracted string! const filterFunc = (item) => { if (item.range === mostCommonVersion) return true; return false; }; - In
handleRangesAndVersions(),mostCommonVersionis kept as the full object{ version: SemverVersion, count: number }becausemostCommonVersion.countis required for comparison at line 273. - At line 276:
if (item.range === mostCommonVersion) return true;item.rangeis a string (e.g.'5.0.0'), whereasmostCommonVersionis{ version: '5.0.0', count: 3 }. - In JavaScript,
'5.0.0' === { version: '5.0.0', count: 3 }is alwaysfalse.
Real-World Impact
- Inside
addToComponentDependenciesMapInDeduped():if (filterFunc && typeof filterFunc === 'function') { const toFilter = filterFunc(indexItem); if (toFilter) return; // Never triggered! } - Because
toFilteris never true, none of the components that share the hoisted version are filtered out. - Every component retains the dependency locally in its
componentDependenciesMapin addition to it being hoisted torootDependencies. - Result: Complete failure of package deduplication when exact versions win over ranges. Component capsules install redundant duplicate copies of packages, inflating disk usage, slowing down
bit install, and defeating the purpose of workspace hoisting.
Proposed Fix
Compare against mostCommonVersion.version and type the callback parameter:
--- a/scopes/dependencies/dependency-resolver/manifest/deduping/hoist-dependencies.ts
+++ b/scopes/dependencies/dependency-resolver/manifest/deduping/hoist-dependencies.ts
@@ -272,8 +272,8 @@ function handleRangesAndVersions(
if (bestRange.count < mostCommonVersion.count) {
dedupedDependencies.rootDependencies[depKeyName][packageName] = mostCommonVersion.version;
- filterFunc = (item) => {
- if (item.range === mostCommonVersion) return true;
+ filterFunc = (item: PackageNameIndexComponentItem) => {
+ if (item.range === mostCommonVersion.version) return true;
return false;
};
} else {Verification & Unit Test
In scopes/dependencies/dependency-resolver/manifest/deduping/hoist-dependencies.spec.ts:
describe('when there is a version which satisfy more components than the best range', () => {
it('should not put the dependency in components that matches the best version', () => {
// With the fix, matching components are filtered from componentDependenciesMap
expect(dedupedDependencies.componentDependenciesMap.get('dependent-component-1')).to.be.undefined;
expect(dedupedDependencies.componentDependenciesMap.get('dependent-component-2')).to.be.undefined;
expect(dedupedDependencies.componentDependenciesMap.get('dependent-component-3')).to.be.undefined;
});
});Source: teambit/bit