chore(NA): redesign integration between bazel node_modules management and yarn kbn commands (#114048)

* chore(NA): redesign integration between bazel node_modules management and yarn kbn commands

* fix(NA): remove .yarn-integrity on force install

* fix(NA): include clean on force install

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Tiago Costa 2022-03-28 13:57:04 +01:00 committed by GitHub
parent dca47de2e0
commit 32eb83cfd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 307 additions and 365 deletions

View file

@ -58,7 +58,6 @@ yarn_install(
data = [
"//:.yarnrc",
"//:preinstall_check.js",
"//:node_modules/.yarn-integrity",
],
exports_directories_only = True,
symlink_node_modules = True,

File diff suppressed because it is too large Load diff

View file

@ -17,12 +17,7 @@ import { ICommand } from './';
import { readYarnLock } from '../utils/yarn_lock';
import { sortPackageJson } from '../utils/sort_package_json';
import { validateDependencies } from '../utils/validate_dependencies';
import {
ensureYarnIntegrityFileExists,
installBazelTools,
runBazel,
yarnIntegrityFileExists,
} from '../utils/bazel';
import { installBazelTools, removeYarnIntegrityFileIfExists, runBazel } from '../utils/bazel';
import { setupRemoteCache } from '../utils/bazel/setup_remote_cache';
export const BootstrapCommand: ICommand = {
@ -54,16 +49,8 @@ export const BootstrapCommand: ICommand = {
}
};
// Force install is set in case a flag is passed or
// if the `.yarn-integrity` file is not found which
// will be indicated by the return of yarnIntegrityFileExists.
const forceInstall =
(!!options && options['force-install'] === true) ||
!(await yarnIntegrityFileExists(resolve(kibanaProjectPath, 'node_modules')));
// Ensure we have a `node_modules/.yarn-integrity` file as we depend on it
// for bazel to know it has to re-install the node_modules after a reset or a clean
await ensureYarnIntegrityFileExists(resolve(kibanaProjectPath, 'node_modules'));
// Force install is set in case a flag `--force-install` is passed into kbn bootstrap
const forceInstall = !!options && options['force-install'] === true;
// Install bazel machinery tools if needed
await installBazelTools(rootPath);
@ -83,6 +70,8 @@ export const BootstrapCommand: ICommand = {
if (forceInstall) {
await time('force install dependencies', async () => {
await removeYarnIntegrityFileIfExists(resolve(kibanaProjectPath, 'node_modules'));
await runBazel(['clean']);
await runBazel(['run', '@nodejs//:yarn'], runOffline, {
env: {
SASS_BINARY_SITE:

View file

@ -17,7 +17,7 @@ import { log } from '../utils/log';
import { ICommand } from './';
export const CleanCommand: ICommand = {
description: 'Deletes output directories, node_modules and resets internal caches.',
description: 'Deletes output directories and resets internal caches.',
name: 'clean',
reportTiming: {
@ -27,20 +27,15 @@ export const CleanCommand: ICommand = {
async run(projects) {
log.warning(dedent`
This command is only necessary for the rare circumstance where you need to recover a consistent
This command is only necessary for the circumstance where you need to recover a consistent
state when problems arise. If you need to run this command often, please let us know by
filling out this form: https://ela.st/yarn-kbn-clean
filling out this form: https://ela.st/yarn-kbn-clean.
Please not it might not solve problems with node_modules. To solve problems around node_modules
you might need to run 'yarn kbn reset'.
`);
const toDelete = [];
for (const project of projects.values()) {
if (await isDirectory(project.nodeModulesLocation)) {
toDelete.push({
cwd: project.path,
pattern: relative(project.path, project.nodeModulesLocation),
});
}
if (await isDirectory(project.targetLocation)) {
toDelete.push({
cwd: project.path,

View file

@ -34,7 +34,8 @@ export const ResetCommand: ICommand = {
async run(projects) {
log.warning(dedent`
In most cases, 'yarn kbn clean' is all that should be needed to recover a consistent state when
problems arise. If you need to use this command, please let us know, as it should not be necessary.
problems arise. However for the rare cases where something get corrupt on node_modules you might need this command.
If you think you need to use this command very often (which is not normal), please let us know.
`);
const toDelete = [];

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
export * from './yarn_integrity';
export * from './get_cache_folders';
export * from './install_tools';
export * from './run';
export * from './yarn_integrity';

View file

@ -7,35 +7,18 @@
*/
import { join } from 'path';
import { isFile, mkdirp, tryRealpath, writeFile } from '../fs';
import { isFile, tryRealpath, unlink } from '../fs';
export async function yarnIntegrityFileExists(nodeModulesPath: string) {
export async function removeYarnIntegrityFileIfExists(nodeModulesPath: string) {
try {
const nodeModulesRealPath = await tryRealpath(nodeModulesPath);
const yarnIntegrityFilePath = join(nodeModulesRealPath, '.yarn-integrity');
// check if the file already exists
// check if the file exists and delete it in that case
if (await isFile(yarnIntegrityFilePath)) {
return true;
await unlink(yarnIntegrityFilePath);
}
} catch {
// no-op
}
return false;
}
export async function ensureYarnIntegrityFileExists(nodeModulesPath: string) {
try {
const nodeModulesRealPath = await tryRealpath(nodeModulesPath);
const yarnIntegrityFilePath = join(nodeModulesRealPath, '.yarn-integrity');
// ensure node_modules folder is created
await mkdirp(nodeModulesRealPath);
// write a blank file in case it doesn't exists
await writeFile(yarnIntegrityFilePath, '', { flag: 'wx' });
} catch {
// no-op
}
}