From cd49631953cd3a928d6dbaadc722fe1d51191fa4 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 23 Jun 2022 03:59:06 +0100 Subject: [PATCH] [7.17] chore(NA): auto bootstrap after removing node modules manually (#134961) (#134969) * chore(NA): auto bootstrap after removing node modules manually (#134961) * chore(NA): mechanism for autobootstrap when manually removing node_modules * fix(NA): check folders with isDirectory * fix(NA): check folders with isDirectory * fix(NA): check folders with isDirectory * docs(NA): update typo on code comment (cherry picked from commit 9a1e1d00a43ff825892457463363551b3ecc05f5) # Conflicts: # packages/kbn-pm/dist/index.js # packages/kbn-pm/src/commands/bootstrap.ts # packages/kbn-pm/src/utils/bazel/index.ts * update kbn/pm dist * refact(NA): missing comma Co-authored-by: spalger --- packages/kbn-pm/dist/index.js | 34 ++++++++++-- packages/kbn-pm/src/commands/bootstrap.ts | 15 ++++-- packages/kbn-pm/src/utils/bazel/index.ts | 2 +- packages/kbn-pm/src/utils/bazel/yarn.ts | 53 +++++++++++++++++++ .../kbn-pm/src/utils/bazel/yarn_integrity.ts | 24 --------- 5 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 packages/kbn-pm/src/utils/bazel/yarn.ts delete mode 100644 packages/kbn-pm/src/utils/bazel/yarn_integrity.ts diff --git a/packages/kbn-pm/dist/index.js b/packages/kbn-pm/dist/index.js index 3afac928a42ce..75544dd6dc97b 100644 --- a/packages/kbn-pm/dist/index.js +++ b/packages/kbn-pm/dist/index.js @@ -8910,9 +8910,11 @@ const BootstrapCommand = { const kibanaProjectPath = ((_projects$get = projects.get('kibana')) === null || _projects$get === void 0 ? void 0 : _projects$get.path) || ''; const runOffline = (options === null || options === void 0 ? void 0 : options.offline) === true; const reporter = _kbn_dev_utils_ci_stats_reporter__WEBPACK_IMPORTED_MODULE_1__["CiStatsReporter"].fromEnv(_utils_log__WEBPACK_IMPORTED_MODULE_2__["log"]); - const timings = []; // Force install is set in case a flag is passed into yarn kbn bootstrap + const timings = []; // Force install is set in case a flag is passed into yarn kbn bootstrap or + // our custom logic have determined there is a chance node_modules have been manually deleted and as such bazel + // tracking mechanism is no longer valid - const forceInstall = !!options && options['force-install'] === true; // Install bazel machinery tools if needed + const forceInstall = !!options && options['force-install'] === true || (await Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["haveNodeModulesBeenManuallyDeleted"])(kibanaProjectPath)); // Install bazel machinery tools if needed await Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["installBazelTools"])(rootPath); // Setup remote cache settings in .bazelrc.cache if needed @@ -54857,8 +54859,10 @@ __webpack_require__.r(__webpack_exports__); /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "runIBazel", function() { return _run__WEBPACK_IMPORTED_MODULE_2__["runIBazel"]; }); -/* harmony import */ var _yarn_integrity__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(547); -/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "removeYarnIntegrityFileIfExists", function() { return _yarn_integrity__WEBPACK_IMPORTED_MODULE_3__["removeYarnIntegrityFileIfExists"]; }); +/* harmony import */ var _yarn__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(547); +/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "removeYarnIntegrityFileIfExists", function() { return _yarn__WEBPACK_IMPORTED_MODULE_3__["removeYarnIntegrityFileIfExists"]; }); + +/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "haveNodeModulesBeenManuallyDeleted", function() { return _yarn__WEBPACK_IMPORTED_MODULE_3__["haveNodeModulesBeenManuallyDeleted"]; }); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one @@ -61285,6 +61289,7 @@ function observeReadable(readable) { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeYarnIntegrityFileIfExists", function() { return removeYarnIntegrityFileIfExists; }); +/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "haveNodeModulesBeenManuallyDeleted", function() { return haveNodeModulesBeenManuallyDeleted; }); /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4); /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_0__); /* harmony import */ var _fs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(252); @@ -61296,6 +61301,7 @@ __webpack_require__.r(__webpack_exports__); * Side Public License, v 1. */ + // yarn integrity file checker async function removeYarnIntegrityFileIfExists(nodeModulesPath) { try { @@ -61307,6 +61313,26 @@ async function removeYarnIntegrityFileIfExists(nodeModulesPath) { } } catch {// no-op } +} // yarn and bazel integration checkers + +async function areNodeModulesPresent(kbnRootPath) { + try { + return await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'node_modules')); + } catch { + return false; + } +} + +async function haveBazelFoldersBeenCreatedBefore(kbnRootPath) { + try { + return (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'bazel-bin', 'packages'))) || (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'bazel-kibana', 'packages'))) || (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'bazel-out', 'host'))); + } catch { + return false; + } +} + +async function haveNodeModulesBeenManuallyDeleted(kbnRootPath) { + return !(await areNodeModulesPresent(kbnRootPath)) && (await haveBazelFoldersBeenCreatedBefore(kbnRootPath)); } /***/ }), diff --git a/packages/kbn-pm/src/commands/bootstrap.ts b/packages/kbn-pm/src/commands/bootstrap.ts index b5a9645047c09..8d95490b54614 100644 --- a/packages/kbn-pm/src/commands/bootstrap.ts +++ b/packages/kbn-pm/src/commands/bootstrap.ts @@ -17,7 +17,12 @@ import { ICommand } from './'; import { readYarnLock } from '../utils/yarn_lock'; import { sortPackageJson } from '../utils/sort_package_json'; import { validateDependencies } from '../utils/validate_dependencies'; -import { installBazelTools, removeYarnIntegrityFileIfExists, runBazel } from '../utils/bazel'; +import { + installBazelTools, + haveNodeModulesBeenManuallyDeleted, + removeYarnIntegrityFileIfExists, + runBazel, +} from '../utils/bazel'; import { setupRemoteCache } from '../utils/bazel/setup_remote_cache'; export const BootstrapCommand: ICommand = { @@ -37,8 +42,12 @@ export const BootstrapCommand: ICommand = { const reporter = CiStatsReporter.fromEnv(log); const timings = []; - // Force install is set in case a flag is passed into yarn kbn bootstrap - const forceInstall = !!options && options['force-install'] === true; + // Force install is set in case a flag is passed into yarn kbn bootstrap or + // our custom logic have determined there is a chance node_modules have been manually deleted and as such bazel + // tracking mechanism is no longer valid + const forceInstall = + (!!options && options['force-install'] === true) || + (await haveNodeModulesBeenManuallyDeleted(kibanaProjectPath)); // Install bazel machinery tools if needed await installBazelTools(rootPath); diff --git a/packages/kbn-pm/src/utils/bazel/index.ts b/packages/kbn-pm/src/utils/bazel/index.ts index 84e26a0b914e1..9007aaa65f29d 100644 --- a/packages/kbn-pm/src/utils/bazel/index.ts +++ b/packages/kbn-pm/src/utils/bazel/index.ts @@ -9,4 +9,4 @@ export * from './get_cache_folders'; export * from './install_tools'; export * from './run'; -export * from './yarn_integrity'; +export * from './yarn'; diff --git a/packages/kbn-pm/src/utils/bazel/yarn.ts b/packages/kbn-pm/src/utils/bazel/yarn.ts new file mode 100644 index 0000000000000..24e44be3b3cdf --- /dev/null +++ b/packages/kbn-pm/src/utils/bazel/yarn.ts @@ -0,0 +1,53 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { join, resolve } from 'path'; +import { isDirectory, isFile, tryRealpath, unlink } from '../fs'; + +// yarn integrity file checker +export async function removeYarnIntegrityFileIfExists(nodeModulesPath: string) { + try { + const nodeModulesRealPath = await tryRealpath(nodeModulesPath); + const yarnIntegrityFilePath = join(nodeModulesRealPath, '.yarn-integrity'); + + // check if the file exists and delete it in that case + if (await isFile(yarnIntegrityFilePath)) { + await unlink(yarnIntegrityFilePath); + } + } catch { + // no-op + } +} + +// yarn and bazel integration checkers +async function areNodeModulesPresent(kbnRootPath: string) { + try { + return await isDirectory(resolve(kbnRootPath, 'node_modules')); + } catch { + return false; + } +} + +async function haveBazelFoldersBeenCreatedBefore(kbnRootPath: string) { + try { + return ( + (await isDirectory(resolve(kbnRootPath, 'bazel-bin', 'packages'))) || + (await isDirectory(resolve(kbnRootPath, 'bazel-kibana', 'packages'))) || + (await isDirectory(resolve(kbnRootPath, 'bazel-out', 'host'))) + ); + } catch { + return false; + } +} + +export async function haveNodeModulesBeenManuallyDeleted(kbnRootPath: string) { + return ( + !(await areNodeModulesPresent(kbnRootPath)) && + (await haveBazelFoldersBeenCreatedBefore(kbnRootPath)) + ); +} diff --git a/packages/kbn-pm/src/utils/bazel/yarn_integrity.ts b/packages/kbn-pm/src/utils/bazel/yarn_integrity.ts deleted file mode 100644 index 1ac9bfeba1e3b..0000000000000 --- a/packages/kbn-pm/src/utils/bazel/yarn_integrity.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { join } from 'path'; -import { isFile, tryRealpath, unlink } from '../fs'; - -export async function removeYarnIntegrityFileIfExists(nodeModulesPath: string) { - try { - const nodeModulesRealPath = await tryRealpath(nodeModulesPath); - const yarnIntegrityFilePath = join(nodeModulesRealPath, '.yarn-integrity'); - - // check if the file exists and delete it in that case - if (await isFile(yarnIntegrityFilePath)) { - await unlink(yarnIntegrityFilePath); - } - } catch { - // no-op - } -}