Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add absolute path import handling in kibana #52995

Closed
wants to merge 5 commits into from

Conversation

pgayvallet
Copy link
Contributor

@pgayvallet pgayvallet commented Dec 13, 2019

Add absolute path imports support to Kibana

// x-pack/plugins/my-plugin/someplace
import { actualConcreteType } from 'src/core/server/somewhere'

Fix #40446

This currently only supports src and x-pack, but adding additional root folders is (almost) trivial if we want to.

This is widely based on @spalger work in #49766

Note: I want to keep this specific PR history clean, so I will use rebase strategy instead of merge to reintegrate master changes.

PR does the following:

  • Add a new (internal) babel plugin to our kbn-babel-preset to rewrite absolute imports to relative ones during babel transpilation
  • Modify the kbn-eslint-import-resolver-kibana to allow eslint to understand and resolves these absolute imports
  • Edit jest configurations to properly handles absolute imports

PR does not:

  • Add eslint rules to enforce (or fix) using absolute paths instead of relative ones for long (aka ../../../../../../[...] imports. This will ideally be done in a future PR as this means massive code edition for the auto-fix.

Note The last commit is a proof of concept on actual usages and will be reverted.

Open questions / discussions

the import/order rule consider that absolute imports should be done before relative ones

For example this is a violation:

import { INDEX_PATTERN_ELASTICSEARCH } from '../../common/constants';
import { ClusterDetails } from 'src/legacy/core_plugins/telemetry/server/collection_manager';

Does that makes sense to us, or should we add exception (if that's possible) for imports from our root paths?

Our module_migrations rules forbid x-pack absolute imports.

I.E is a violation:

// x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts
import { licenseMock } from 'x-pack/plugins/licensing/common/licensing.mock';

When I look at the actual rule implementation, there is a comment that makes me think this can safely be removed, but I would like confirmation for that:

  // support for toRelative added to migrate away from X-Pack being bundled
  // within node modules. after that migration, this can be removed.

The eslint resolver modifications 'fixed' (?) the import/order rule in x-pack

This was previously allowed in x-pack:

import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { ObjectType } from '@kbn/config-schema';

Which is now considered violations (see CI) as eslint properly recognize src/... as local sources instead of a module. I can just autofix these in these PR, but once again, would like confirmation on that.

@elastic elastic deleted a comment from elasticmachine Dec 13, 2019
@pgayvallet pgayvallet changed the title absolute path handling Add absolute path import handling in kibana Dec 16, 2019
@pgayvallet pgayvallet added the Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc label Dec 16, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@pgayvallet pgayvallet added the Team:Operations Team label for Operations Team label Dec 16, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-operations (Team:Operations)

Comment on lines -30 to -32
react: {
version: semver.valid(semver.coerce(PKG.dependencies.react)),
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was redundant as already present in packages/eslint-config-kibana/react.js

const absoluteImportPath = importPath[0] === '.' ? resolve(importPath, context) : undefined;
const absoluteImportPath = resolve(importPath, context);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paths not starting with . can be local now

Comment on lines -22 to 24
jest.mock('./read_config', () => ({
jest.mock('src/core/server/config/read_config', () => ({
getConfigFromFiles: mockGetConfigFromFiles,
}));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this commit will be deleted) This works even if read_config is imported using relative imports in test or sources import XXX from '../foo/read_config' (jest uses the resolved absolute path)

@pgayvallet pgayvallet requested review from epixa, spalger and a team December 16, 2019 15:25
@pgayvallet pgayvallet marked this pull request as ready for review December 16, 2019 15:25
@pgayvallet pgayvallet requested a review from a team as a code owner December 16, 2019 15:25
@pgayvallet pgayvallet added release_note:plugin_api_changes Contains a Plugin API changes section for the breaking plugin API changes section. v7.7.0 v8.0.0 labels Dec 16, 2019
@elasticmachine
Copy link
Contributor

💔 Build Failed

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@mshustov
Copy link
Contributor

ack: will review tonight

sourceFileAbsPath = sourceFileName;
} else if (filename && Path.isAbsolute(filename)) {
sourceFileAbsPath = filename;
} else if (!sourceFileAbsPath && root) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: !sourceFileAbsPath can be remove

} else if (!sourceFileAbsPath && root) {
sourceFileAbsPath = Path.resolve(root, sourceFileName || filename);
} else {
throw new Error(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this check done after all if/else branches? for cases when https://github.com/elastic/kibana/pull/52995/files#diff-0b4873344bd872a80014131ef6a02a27R73 fails

@@ -66,9 +68,20 @@ function tryNodeResolver(importRequest, file, config) {
);
}

function isImportedFromRoot(importRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js should integrate this resolution logic as well. Can we extract it in a common folder to share? Can be done in a separate PR.

@@ -3,6 +3,8 @@
"baseUrl": ".",
"paths": {
// Allows for importing from `kibana` package for the exported types.
"src/*": ["src/*"],
Copy link
Contributor

@mshustov mshustov Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we update x-pack tsconfig as well?
update: as I can see mapping from x-pack works without adding these paths(due to baseUrl set to the root folder probably?)


import {
ClusterDetailsGetter,
StatsCollectionConfig,
ClusterDetails,
} from '../../../../../../src/legacy/core_plugins/telemetry/server/collection_manager';
} from 'src/legacy/core_plugins/telemetry/server/collection_manager';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as I understand this is handled by built-in ts functionality. could you add an example for js code to make sure that server runs?

@@ -8,6 +8,7 @@
"babel-eslint": "^10.0.3"
},
"dependencies": {
"@kbn/dev-utils": "1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used?

@spalger
Copy link
Contributor

spalger commented Jun 3, 2020

Sorry, this fell off my radar. Is this still something you want to do? If so, please get it passing again and re-request review. Thanks!

@spalger spalger removed their request for review June 3, 2020 17:06
@pgayvallet
Copy link
Contributor Author

Sorry, this fell off my radar. Is this still something you want to do? If so, please get it passing again and re-request review. Thanks!

Given the team's load, it's not on top of my priority list atm. But hopefully this will be done this year.

@spalger spalger marked this pull request as draft October 15, 2020 16:04
@spalger
Copy link
Contributor

spalger commented Oct 15, 2020

@pgayvallet I'm going to close this now and we can reopen if we feel like it's necessary.

@spalger spalger closed this Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:plugin_api_changes Contains a Plugin API changes section for the breaking plugin API changes section. Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Team:Operations Team label for Operations Team v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

An alternative to relative imports for local source code
4 participants