#10724·bit

Incorrect Dynamic Property Access in Package Data Resolver

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

Issue : Incorrect Dynamic Property Access in Package Data Resolver

Metadata

Problem Description

In scopes/dependencies/dependencies/resolve-pkg-data.ts, the function enrichDataFromDependent() resolves package information from a dependent component's package.json. It attempts to look up the package version or semver range defined in dependencies, devDependencies, or peerDependencies.

Instead of utilizing computed property bracket notation ([packageName]), the code uses dot notation with the literal identifier (.packageName).

Vulnerable Code

typescript
// scopes/dependencies/dependencies/resolve-pkg-data.ts: Lines 65-75

  const packageName = resolvePackageNameByPath(packageRelativePath);
  const packageVersion =
    dependentPackageJson.dependencies?.packageName ||
    dependentPackageJson.devDependencies?.packageName ||
    dependentPackageJson.peerDependencies?.packageName;
  if (packageVersion) {
    packageData.dependentPackageJsonPath = packageJsonInfo.path;
    packageData.name = packageName;
    packageData.versionUsedByDependent = packageVersion;
  }

Root Cause Analysis

  1. packageName is a variable holding the resolved package string (e.g. 'lodash', 'chalk', or '@teambit/base-ui').
  2. dependentPackageJson.dependencies?.packageName checks for the literal key "packageName" inside the dependencies map:
    json
    {
      "dependencies": {
        "packageName": "1.0.0"
      }
    }
  3. Unless a project literally depends on a package titled "packageName", packageVersion strictly evaluates to undefined.
  4. As a result, the condition if (packageVersion) at line 70 is never entered for any standard package.

Real-World Impact

According to the file's architectural documentation (lines 20–29):

"for a package that is not bit-component, we're interested in the range because that's how it was set in the first place and changing it to an exact version result in the component modified."

Because versionUsedByDependent is never populated from dependentPackageJson, Bit loses the author's specified semver range (e.g., ^1.2.0 or ~2.0.0). Bit falls back to reading concreteVersion from the package's internal package.json in node_modules (e.g., 1.2.4). This can lead to:

  • Bit marking components as modified unexpectedly due to forced exact version pinning.
  • Inability to retain and propagate semver ranges when snapping or tagging components.

Proposed Fix

Replace static dot notation .packageName with dynamic bracket notation [packageName]:

diff
--- a/scopes/dependencies/dependencies/resolve-pkg-data.ts
+++ b/scopes/dependencies/dependencies/resolve-pkg-data.ts
@@ -64,9 +64,9 @@ function enrichDataFromDependent(packageData: ResolvedPackageData, dependentDir:
 
   const packageName = resolvePackageNameByPath(packageRelativePath);
   const packageVersion =
-    dependentPackageJson.dependencies?.packageName ||
-    dependentPackageJson.devDependencies?.packageName ||
-    dependentPackageJson.peerDependencies?.packageName;
+    dependentPackageJson.dependencies?.[packageName] ||
+    dependentPackageJson.devDependencies?.[packageName] ||
+    dependentPackageJson.peerDependencies?.[packageName];
   if (packageVersion) {
     packageData.dependentPackageJsonPath = packageJsonInfo.path;
     packageData.name = packageName;

Verification & Unit Test

Add a test in resolve-pkg-data.spec.ts:

typescript
it('should populate versionUsedByDependent from dependent package.json dependencies', () => {
  const result = resolvePackageData(dependentFixtureDir, packageFullPath);
  expect(result).to.not.be.undefined;
  expect(result?.name).to.equal('lodash');
  expect(result?.versionUsedByDependent).to.equal('^4.17.21');
});