#10727·bit

Cross-Platform Path Incompatibility & Unscoped Package Misparsing in Link Helper

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

Issue : Cross-Platform Path Incompatibility & Unscoped Package Misparsing in Link Helper

Metadata

Problem Description

getPackageNameFromTarget() extracts the package directory name relative to node_modules for logging and linking summaries in component-list-links.ts and core-aspects-links.ts.

The implementation contains two severe bugs:

  1. Windows Path Separator Failure: On Windows, filesystem paths contain backslashes (\). The code splits using a hardcoded forward slash .split('/'), which fails completely on Windows.
  2. Hardcoded Scoped Package Assumption: The code unconditionally executes .slice(0, 3), which assumes all packages are scoped (e.g., node_modules, @scope, package). For unscoped packages, it extracts internal file paths.

Vulnerable Code

typescript
// scopes/workspace/install/link/get-package-name-from-target.ts: Lines 1-6

export function getPackageNameFromTarget(targetPath: string): string {
  const subPath = targetPath.substring(targetPath.lastIndexOf('node_modules'));
  const packagePath = subPath.split('/').slice(0, 3).join('/');
  return `./${packagePath}`;
}

Root Cause Analysis

1. Windows Failure Mode

  • On Windows, paths use \. Given targetPath: C:\workspace\node_modules\@teambit\harmony\dist\index.js
  • subPath is: node_modules\@teambit\harmony\dist\index.js
  • subPath.split('/') does not find /, returning a 1-element array: ['node_modules\\@teambit\\harmony\\dist\\index.js']
  • .slice(0, 3) retains the entire string.
  • Output returned: ./node_modules\@teambit\harmony\dist\index.js (including trailing file and mixed slashes).

2. Unscoped Package Failure Mode (All Platforms)

  • Given an unscoped package target: /repo/node_modules/lodash/index.js
  • Segments after split('/'): ['node_modules', 'lodash', 'index.js']
  • .slice(0, 3) takes 3 segments: ['node_modules', 'lodash', 'index.js']
  • Output returned: ./node_modules/lodash/index.js instead of ./node_modules/lodash.

Real-World Impact

This function is called by packageListLinks() and coreAspectsLinks() when printing link summaries during bit install and bit link.

  • On Windows, users see ugly, malformed paths with mixed / and \ that link to individual files rather than package root directories.
  • On all operating systems, unscoped dependencies output file paths rather than clean package paths.

Proposed Fix

Normalize the incoming path using Bit's internal pathNormalizeToLinux utility, and dynamically inspect whether the package is scoped:

diff
--- a/scopes/workspace/install/link/get-package-name-from-target.ts
+++ b/scopes/workspace/install/link/get-package-name-from-target.ts
@@ -1,5 +1,14 @@
+import { pathNormalizeToLinux } from '@teambit/toolbox.path.path';
+
 export function getPackageNameFromTarget(targetPath: string): string {
-  const subPath = targetPath.substring(targetPath.lastIndexOf('node_modules'));
-  const packagePath = subPath.split('/').slice(0, 3).join('/');
+  const normalized = pathNormalizeToLinux(targetPath);
+  const nodeModulesIndex = normalized.lastIndexOf('node_modules');
+  if (nodeModulesIndex === -1) return targetPath;
+
+  const subPath = normalized.substring(nodeModulesIndex);
+  const segments = subPath.split('/');
+  const isScoped = segments[1]?.startsWith('@');
+  const sliceCount = isScoped ? 3 : 2;
+  const packagePath = segments.slice(0, sliceCount).join('/');
   return `./${packagePath}`;
 }

Verification & Unit Test

Add get-package-name-from-target.spec.ts:

typescript
import { expect } from 'chai';
import { getPackageNameFromTarget } from './get-package-name-from-target';

describe('getPackageNameFromTarget', () => {
  it('should handle POSIX scoped packages', () => {
    expect(getPackageNameFromTarget('/workspace/node_modules/@teambit/harmony/dist/index.js'))
      .to.equal('./node_modules/@teambit/harmony');
  });

  it('should handle Windows scoped packages with backslashes', () => {
    expect(getPackageNameFromTarget('C:\\workspace\\node_modules\\@teambit\\harmony\\dist\\index.js'))
      .to.equal('./node_modules/@teambit/harmony');
  });

  it('should handle POSIX unscoped packages', () => {
    expect(getPackageNameFromTarget('/workspace/node_modules/lodash/index.js'))
      .to.equal('./node_modules/lodash');
  });

  it('should handle Windows unscoped packages with backslashes', () => {
    expect(getPackageNameFromTarget('C:\\workspace\\node_modules\\lodash\\index.js'))
      .to.equal('./node_modules/lodash');
  });
});