Fix @kbn/import-resolver detection of nested node_modules (#162391)

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.
This commit is contained in:
Thomas Watson 2023-07-25 10:09:05 +02:00 committed by GitHub
parent a79e9c7374
commit 70b337a135
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 1 deletions

1
.gitignore vendored
View file

@ -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

View file

@ -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;
}

View file

@ -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 {