kibana/packages/kbn-repo-packages/modern
Pierre Gayvallet e398e7bc51
[Core plugin system] Add dynamic contract resolving (#167113)
### Summary

Fix https://github.com/elastic/kibana/issues/166688

Implements dynamic contract resolving for plugins, allowing to retrieve
contracts after their respective lifecycle is completed, and therefore
working around cyclic dependencies.

In term of workflow execution, we're basically going from

<img width="842" alt="Screenshot 2023-09-27 at 08 09 27"
src="251637d1-ec97-4071-a445-2f59512ce187">
 
to:

<img width="1092" alt="Screenshot 2023-09-27 at 08 09 32"
src="de466cda-7e43-4fd3-81ec-4339d05d279d">

### API

This functionality is exposed by the now publicly exposed `plugins`
service contracts:

```ts
setup(core) {
  core.plugins.onSetup<{pluginA: SetupContractA, pluginB: SetupContractA}>('pluginA', 'pluginB')
      .then(({ pluginA, pluginB }) => {
        if(pluginA.found && pluginB.found) {
          // do something with pluginA.contract and pluginB.contract
        }
      });
}
```  

```ts
start(core) {
  core.plugins.onStart<{pluginA: StartContractA, pluginB: StartContractA}>('pluginA', 'pluginB')
      .then(({ pluginA, pluginB }) => {
        if(pluginA.found && pluginB.found) {
          // do something with pluginA.contract and pluginB.contract
        }
      });
}
```

**remark:** the `setup` contract exposed both `onSetup` and `onStart`,
while the `start` contract only exposed `onStart`. The intent is to
avoid fully disrupting the concept of lifecycle stages.

### Guardrails

To prevent developer from abusing this new API, or at least to add some
visibility on its adoption, plugins can only perforn dynamic contract
resolving against dependencies explicitly defined in their manifest:
- any required dependencies (*existing concept*)
- any optional dependencies (*existing concept*)
- any runtime dependencies (**new concept**)

Runtime dependencies must be specified using the new
`runtimePluginDependencies` field of a plugin's manifest.

```json
{
  "type": "plugin",
  "id": "@kbn/some-id",
  "owner": "@elastic/kibana-core",
  "plugin": {
    "id": "some-id",
    "...": "...",
    "runtimePluginDependencies" : ["someOtherPluginId"]
  }
}

```

Using the contract resolving API will throw at call time when trying to
resolve the contract for an undeclared dependency.

E.g this would throw at invocation time (not returning a rejected
promise - throw).

```ts
setup(core) {
  core.plugins.onSetup<{undeclaredDependency: SomeContract}>('undeclaredDependency');
}
```  

The reasoning behind throwing is that these errors should only occur
during the development process, and an hard fail is way more visible
than a promise rejection that should be more easily shallowed.

### Code reviews

This PR defines @elastic/kibana-core as codeowner of all `kibana.jsonc`
files in the `src/plugins` and `x-pack/plugins` directories, so that a
code review will be triggered whenever anyone changes something in any
manifest. The intent is to be able to monitor new usages of the feature,
via the addition of entries in the `runtimePluginDependencies` option of
the manifest.

### Remarks

Exposing this API, and therefore making possible cyclic dependencies
between plugins, opens the door to other questions.

For instance, cross-plugin type imports are not technically possible at
the moment, given that plugins are referencing each others via TS refs,
and refs forbid cyclic dependencies. Which means that to leverage this
to address cyclic dependency issues, the public types of **at least one
of the two** plugins will have to be extracted to a shared place (likely
a package).

Resolving, or trying to improve the developer experience around this
issue, is absolutely out of scope of the current PR (and the issue it
addresses).

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2023-10-24 02:32:09 -07:00
..
get_git_repo_root.js Hide error outputs from checking for git root (#157851) 2023-05-22 09:45:45 +02:00
get_packages.js [packages] migrate all plugins to packages (#148130) 2023-02-08 21:06:50 -06:00
get_repo_rels.js [packages] migrate all plugins to packages (#148130) 2023-02-08 21:06:50 -06:00
package.js fix(NA): using repo root scope when validating package manifests (#151765) 2023-02-23 18:45:20 +00:00
parse_helpers.js implement "plugin" package type (#149370) 2023-01-30 10:47:53 -07:00
parse_kbn_import_req.js Implement package linter (#148496) 2023-01-09 16:49:29 -07:00
parse_package_json.js Implement package linter (#148496) 2023-01-09 16:49:29 -07:00
parse_package_manifest.js [Core plugin system] Add dynamic contract resolving (#167113) 2023-10-24 02:32:09 -07:00
plugin_category_info.js [packages] migrate all plugins to packages (#148130) 2023-02-08 21:06:50 -06:00
plugins.js chore(NA): upgrade typescript into v4.7.4 (#162738) 2023-08-24 17:27:13 +01:00
types.ts [Core plugin system] Add dynamic contract resolving (#167113) 2023-10-24 02:32:09 -07:00