#10725·bit

Strict Equality Object vs String Type Mismatch in Package Hoisting & Deduping

Author: codeCraft-RitikCreated Sep 23, 2026Updated Sep 23, 2026
Labelstype/bug

Issue : Strict Equality Object vs String Type Mismatch in Package Hoisting & Deduping

Metadata

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

typescript
// 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

  1. Contrast with handleVersionsOnly() in lines 200–208:
    typescript
    const mostCommonVersion = findMostCommonVersion(allVersions).version; // Extracted string!
    const filterFunc = (item) => {
      if (item.range === mostCommonVersion) return true;
      return false;
    };
  2. In handleRangesAndVersions(), mostCommonVersion is kept as the full object { version: SemverVersion, count: number } because mostCommonVersion.count is required for comparison at line 273.
  3. At line 276:
    typescript
    if (item.range === mostCommonVersion) return true;
    item.range is a string (e.g. '5.0.0'), whereas mostCommonVersion is { version: '5.0.0', count: 3 }.
  4. In JavaScript, '5.0.0' === { version: '5.0.0', count: 3 } is always false.

Real-World Impact

  • Inside addToComponentDependenciesMapInDeduped():
    typescript
    if (filterFunc && typeof filterFunc === 'function') {
      const toFilter = filterFunc(indexItem);
      if (toFilter) return; // Never triggered!
    }
  • Because toFilter is never true, none of the components that share the hoisted version are filtered out.
  • Every component retains the dependency locally in its componentDependenciesMap in addition to it being hoisted to rootDependencies.
  • 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:

diff
--- 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:

typescript
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;
  });
});