Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion components/legacy/utils/packages/resolve-pkg-name-by-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import { pathNormalizeToLinux } from '..';
* @returns {string} name of the package
*/
export function resolvePackageNameByPath(packagePath: string): string {
// pathNormalizeToLinux strips trailing slashes (`normalize-path('events/') === 'events'`),
// so we operate on the normalized form throughout — including for the single-segment branch.
// Returning the original `packagePath` there would leak a trailing slash like `events/`
// into downstream consumers (e.g. the MissingPackagesDependenciesOnFs issue), preventing
// them from matching the real package name `events`.
const packagePathLinux = pathNormalizeToLinux(packagePath);
const packagePathArr = packagePathLinux.split('/');
// Regular package without path. example - import _ from 'lodash'
if (packagePathArr.length === 1) return packagePath;
if (packagePathArr.length === 1) return packagePathLinux;
// sass-loader/webpack path. remove the tilda, it's not part of the package name
if (packagePathArr[0].startsWith('~')) packagePathArr[0] = packagePathArr[0].replace('~', '');
// Scoped package. example - import getSymbolIterator from '@angular/core/src/util.d.ts';
Expand Down
34 changes: 34 additions & 0 deletions e2e/harmony/missing-package-strips-trailing-slash.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect } from 'chai';
import { Helper } from '@teambit/legacy.e2e-helper';

// Regression test: a require/import with a trailing slash like `require('events/')` —
// a common pattern in webpack browser-fallback configs — should be reported in the
// MissingPackagesDependenciesOnFs issue under the *package* name (`events`), not the
// original path (`events/`). Before this fix, `resolvePackageNameByPath` returned the
// original (non-normalized) string for single-segment paths, so the issue surfaced
// `events/` and any downstream matching against the real package name failed.
describe('MissingPackagesDependenciesOnFs strips trailing slash from package names', function () {
this.timeout(0);
let helper: Helper;
let missingPackages: string[];
before(() => {
helper = new Helper();
helper.scopeHelper.setWorkspaceWithRemoteScope();
// `events` is a Node builtin; the trailing slash bypasses precinct's builtin filter and
// forces the resolver to look it up as an npm package — but it isn't installed, so it
// ends up in MissingPackagesDependenciesOnFs.
helper.fixtures.createComponentBarFoo(`require('events/');`);
helper.fixtures.addComponentBarFoo();
const status = helper.command.statusJson();
const issues = status.componentsWithIssues[0]?.issues || [];
const missingPkgIssue = issues.find((i: any) => i.type === 'MissingPackagesDependenciesOnFs');
missingPackages = missingPkgIssue?.data?.[0]?.missingPackages || [];
Comment thread
zkochan marked this conversation as resolved.
});
after(() => {
helper.scopeHelper.destroy();
});
it('records the missing package without the trailing slash', () => {
expect(missingPackages).to.include('events');
expect(missingPackages).to.not.include('events/');
});
});