Skip to content

Commit

Permalink
Fix @kbn/import-resolver detection of nested node_modules (#162391)
Browse files Browse the repository at this point in the history
Ensure that a plugin containing a nested `node_modules` folder correctly
identifies imports from that folder as regular node modules instead of relative imports. 

Kibana should currently only have a single root `node_modules` folder, so this should
not be a real issue. However, if for some reason a stray `node_modules` folder is
created, this commit will make sure it's not introducing hard to debug ESLint errors.
  • Loading branch information
Thomas Watson authored Jul 25, 2023
1 parent a79e9c7 commit 70b337a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
!/src/dev/npm/integration_tests/__fixtures__/fixture1/node_modules
!/src/dev/notice/__fixtures__/node_modules
!/packages/kbn-import-resolver/src/__fixtures__/node_modules
!/packages/kbn-import-resolver/src/__fixtures__/packages/box/node_modules
trash
/optimize
/built_assets
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion packages/kbn-import-resolver/src/import_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ImportResolver {

getPackageIdForPath(path: string) {
const relative = Path.relative(this.cwd, path);
if (relative.startsWith('..')) {
if (relative.startsWith('..') || path.includes(NODE_MODULE_SEG)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ describe('#resolve()', () => {
`);
});

it('resolves nested node_module imports', () => {
expect(resolver.resolve('bar', Path.join(FIXTURES_DIR, 'packages', 'box')))
.toMatchInlineSnapshot(`
Object {
"absolute": <absolute path>/packages/kbn-import-resolver/src/__fixtures__/packages/box/node_modules/bar/index.js,
"nodeModule": "bar",
"type": "file",
}
`);
});

it('resolves requests to src/', () => {
expect(resolver.resolve('src/core/public', FIXTURES_DIR)).toMatchInlineSnapshot(`
Object {
Expand Down

0 comments on commit 70b337a

Please sign in to comment.