|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import dedent from 'dedent'; |
| 21 | +import { parseDependencyTree, parseCircular, prettyCircular } from 'dpdm'; |
| 22 | +import { relative } from 'path'; |
| 23 | +import { getPluginSearchPaths } from '@kbn/config'; |
| 24 | +import { REPO_ROOT, run } from '@kbn/dev-utils'; |
| 25 | + |
| 26 | +interface Options { |
| 27 | + debug?: boolean; |
| 28 | + filter?: string; |
| 29 | +} |
| 30 | + |
| 31 | +type CircularDepList = Set<string>; |
| 32 | + |
| 33 | +const allowedList: CircularDepList = new Set([ |
| 34 | + 'src/plugins/charts -> src/plugins/expressions', |
| 35 | + 'src/plugins/charts -> src/plugins/vis_default_editor', |
| 36 | + 'src/plugins/data -> src/plugins/embeddable', |
| 37 | + 'src/plugins/data -> src/plugins/expressions', |
| 38 | + 'src/plugins/data -> src/plugins/ui_actions', |
| 39 | + 'src/plugins/embeddable -> src/plugins/ui_actions', |
| 40 | + 'src/plugins/expressions -> src/plugins/visualizations', |
| 41 | + 'src/plugins/vis_default_editor -> src/plugins/visualizations', |
| 42 | + 'src/plugins/vis_default_editor -> src/plugins/visualize', |
| 43 | + 'src/plugins/visualizations -> src/plugins/visualize', |
| 44 | + 'x-pack/plugins/actions -> x-pack/plugins/case', |
| 45 | + 'x-pack/plugins/apm -> x-pack/plugins/infra', |
| 46 | + 'x-pack/plugins/lists -> x-pack/plugins/security_solution', |
| 47 | + 'x-pack/plugins/security -> x-pack/plugins/spaces', |
| 48 | +]); |
| 49 | + |
| 50 | +run( |
| 51 | + async ({ flags, log }) => { |
| 52 | + const { debug, filter } = flags as Options; |
| 53 | + const foundList: CircularDepList = new Set(); |
| 54 | + |
| 55 | + const pluginSearchPathGlobs = getPluginSearchPaths({ |
| 56 | + rootDir: REPO_ROOT, |
| 57 | + oss: false, |
| 58 | + examples: true, |
| 59 | + }).map((pluginFolderPath) => `${relative(REPO_ROOT, pluginFolderPath)}/**/*`); |
| 60 | + |
| 61 | + const depTree = await parseDependencyTree(pluginSearchPathGlobs, { |
| 62 | + context: REPO_ROOT, |
| 63 | + }); |
| 64 | + |
| 65 | + // Build list of circular dependencies as well as the circular dependencies full paths |
| 66 | + const circularDependenciesFullPaths = parseCircular(depTree).filter((circularDeps) => { |
| 67 | + const first = circularDeps[0]; |
| 68 | + const last = circularDeps[circularDeps.length - 1]; |
| 69 | + const matchRegex = /(?<pluginFolder>(src|x-pack)\/plugins|examples|x-pack\/examples)\/(?<pluginName>[^\/]*)\/.*/; |
| 70 | + const firstMatch = first.match(matchRegex); |
| 71 | + const lastMatch = last.match(matchRegex); |
| 72 | + |
| 73 | + if ( |
| 74 | + firstMatch?.groups?.pluginFolder && |
| 75 | + firstMatch?.groups?.pluginName && |
| 76 | + lastMatch?.groups?.pluginFolder && |
| 77 | + lastMatch?.groups?.pluginName |
| 78 | + ) { |
| 79 | + const firstPlugin = `${firstMatch.groups.pluginFolder}/${firstMatch.groups.pluginName}`; |
| 80 | + const lastPlugin = `${lastMatch.groups.pluginFolder}/${lastMatch.groups.pluginName}`; |
| 81 | + const sortedPlugins = [firstPlugin, lastPlugin].sort(); |
| 82 | + |
| 83 | + // Exclude if both plugin paths involved in the circular dependency |
| 84 | + // doesn't includes the provided filter |
| 85 | + if (filter && !firstPlugin.includes(filter) && !lastPlugin.includes(filter)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + if (firstPlugin !== lastPlugin) { |
| 90 | + foundList.add(`${sortedPlugins[0]} -> ${sortedPlugins[1]}`); |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return false; |
| 96 | + }); |
| 97 | + |
| 98 | + if (!debug && filter) { |
| 99 | + log.warning( |
| 100 | + dedent(` |
| 101 | + !!!!!!!!!!!!!! WARNING: FILTER WITHOUT DEBUG !!!!!!!!!!!! |
| 102 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 103 | + ! Using the --filter flag without using --debug flag ! |
| 104 | + ! will not allow you to see the filtered list of ! |
| 105 | + ! the correct results. ! |
| 106 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 107 | + `) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + if (debug && filter) { |
| 112 | + log.warning( |
| 113 | + dedent(` |
| 114 | + !!!!!!!!!!!!!!! WARNING: FILTER FLAG IS ON !!!!!!!!!!!!!! |
| 115 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 116 | + ! Be aware the following results are not complete as ! |
| 117 | + ! --filter flag has been passed. Ignore suggestions ! |
| 118 | + ! to update the allowedList or any reports of failures ! |
| 119 | + ! or successes. ! |
| 120 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 121 | +
|
| 122 | + The following filter has peen passed: ${filter} |
| 123 | + `) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + // Log the full circular dependencies path if we are under debug flag |
| 128 | + if (debug && circularDependenciesFullPaths.length > 0) { |
| 129 | + log.debug( |
| 130 | + dedent(` |
| 131 | + !!!!!!!!!!!!!! CIRCULAR DEPENDENCIES FOUND !!!!!!!!!!!!!! |
| 132 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 133 | + ! Circular dependencies were found, you can find below ! |
| 134 | + ! all the paths involved. ! |
| 135 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 136 | + `) |
| 137 | + ); |
| 138 | + log.debug(`${prettyCircular(circularDependenciesFullPaths)}\n`); |
| 139 | + } |
| 140 | + |
| 141 | + // Always log the result of comparing the found list with the allowed list |
| 142 | + const diffSet = (first: CircularDepList, second: CircularDepList) => |
| 143 | + new Set([...first].filter((circularDep) => !second.has(circularDep))); |
| 144 | + |
| 145 | + const printList = (list: CircularDepList) => { |
| 146 | + return Array.from(list) |
| 147 | + .sort() |
| 148 | + .reduce((listStr, entry) => { |
| 149 | + return listStr ? `${listStr}\n'${entry}',` : `'${entry}',`; |
| 150 | + }, ''); |
| 151 | + }; |
| 152 | + |
| 153 | + const foundDifferences = diffSet(foundList, allowedList); |
| 154 | + |
| 155 | + if (debug && !foundDifferences.size) { |
| 156 | + log.debug( |
| 157 | + dedent(` |
| 158 | + !!!!!!!!!!!!!!!!! UP TO DATE ALLOWED LIST !!!!!!!!!!!!!!!!!! |
| 159 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 160 | + ! The declared circular dependencies allowed list is up ! |
| 161 | + ! to date and includes every plugin listed in above paths. ! |
| 162 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 163 | +
|
| 164 | + The allowed circular dependencies list is (#${allowedList.size}): |
| 165 | + ${printList(allowedList)} |
| 166 | + `) |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + if (foundDifferences.size > 0) { |
| 171 | + log.error( |
| 172 | + dedent(` |
| 173 | + !!!!!!!!!!!!!!!!! OUT OF DATE ALLOWED LIST !!!!!!!!!!!!!!!!! |
| 174 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 175 | + ! The declared circular dependencies allowed list is out ! |
| 176 | + ! of date. Please run the following locally to know more: ! |
| 177 | + ! ! |
| 178 | + ! 'node scripts/find_plugins_with_circular_deps --debug' ! |
| 179 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 180 | +
|
| 181 | + The allowed circular dependencies list is (#${allowedList.size}): |
| 182 | + ${printList(allowedList)} |
| 183 | +
|
| 184 | + The found circular dependencies list is (#${foundList.size}): |
| 185 | + ${printList(foundList)} |
| 186 | +
|
| 187 | + The differences between both are (#${foundDifferences.size}): |
| 188 | + ${printList(foundDifferences)} |
| 189 | +
|
| 190 | + FAILED: circular dependencies in the allowed list declared on the file '${__filename}' did not match the found ones. |
| 191 | + `) |
| 192 | + ); |
| 193 | + |
| 194 | + process.exit(1); |
| 195 | + } |
| 196 | + |
| 197 | + log.success('None non allowed circular dependencies were found'); |
| 198 | + }, |
| 199 | + { |
| 200 | + description: |
| 201 | + 'Searches circular dependencies between plugins located under src/plugins, x-pack/plugins, examples and x-pack/examples', |
| 202 | + flags: { |
| 203 | + boolean: ['debug'], |
| 204 | + string: ['filter'], |
| 205 | + default: { |
| 206 | + debug: false, |
| 207 | + }, |
| 208 | + help: ` |
| 209 | + --debug Run the script in debug mode which enables detailed path logs for circular dependencies |
| 210 | + --filter It will only include in the results circular deps where the plugin paths contains parts of the passed string in the filter |
| 211 | + `, |
| 212 | + }, |
| 213 | + } |
| 214 | +); |
0 commit comments