Skip to content

Commit

Permalink
[kbn-pm] Default build and watch commands to use bazel (elastic#105674)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
jbudz and kibanamachine authored Jul 28, 2021
1 parent 57bdd8b commit 134b2fd
Show file tree
Hide file tree
Showing 8 changed files with 828 additions and 1,084 deletions.
14 changes: 2 additions & 12 deletions docs/developer/getting-started/monorepo-packages.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,14 @@ Non Bazel packages can be built independently with
yarn kbn run build -i PACKAGE_NAME
----

[discrete]
=== Watching Non Bazel Packages

Non Bazel packages can be watched independently with

[source,bash]
----
yarn kbn watch -i PACKAGE_NAME
----

[discrete]
=== Building Bazel Packages

Bazel packages are built as a whole for now. You can use:

[source,bash]
----
yarn kbn build-bazel
yarn kbn build
----

[discrete]
Expand All @@ -54,7 +44,7 @@ Bazel packages are watched as a whole for now. You can use:

[source,bash]
----
yarn kbn watch-bazel
yarn kbn watch
----


Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"lint:es": "node scripts/eslint",
"lint:style": "node scripts/stylelint",
"makelogs": "node scripts/makelogs",
"kbn:watch": "node scripts/kibana --dev --logging.json=false",
"build:types": "rm -rf ./target/types && tsc --p tsconfig.types.json",
"docs:acceptApiChanges": "node --max-old-space-size=6144 scripts/check_published_api_changes.js --accept",
"kbn:bootstrap": "node scripts/build_ts_refs --ignore-type-failures",
Expand Down
1,774 changes: 808 additions & 966 deletions packages/kbn-pm/dist/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import { runBazel } from '../utils/bazel';
import { ICommand } from './';

export const BuildBazelCommand: ICommand = {
export const BuildCommand: ICommand = {
description: 'Runs a build in the Bazel built packages',
name: 'build-bazel',
name: 'build',

async run(projects, projectGraph, { options }) {
const runOffline = options?.offline === true;
Expand Down
6 changes: 2 additions & 4 deletions packages/kbn-pm/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ export interface ICommand {
}

import { BootstrapCommand } from './bootstrap';
import { BuildBazelCommand } from './build_bazel';
import { BuildCommand } from './build';
import { CleanCommand } from './clean';
import { ResetCommand } from './reset';
import { RunCommand } from './run';
import { WatchCommand } from './watch';
import { WatchBazelCommand } from './watch_bazel';
import { Kibana } from '../utils/kibana';

export const commands: { [key: string]: ICommand } = {
bootstrap: BootstrapCommand,
'build-bazel': BuildBazelCommand,
build: BuildCommand,
clean: CleanCommand,
reset: ResetCommand,
run: RunCommand,
watch: WatchCommand,
'watch-bazel': WatchBazelCommand,
};
2 changes: 1 addition & 1 deletion packages/kbn-pm/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const RunCommand: ICommand = {
log.warning(dedent`
We are migrating packages into the Bazel build system and we will no longer support running npm scripts on
packages using 'yarn kbn run' on Bazel built packages. If the package you are trying to act on contains a
BUILD.bazel file please just use 'yarn kbn build-bazel' to build it or 'yarn kbn watch-bazel' to watch it
BUILD.bazel file please just use 'yarn kbn build' to build it or 'yarn kbn watch' to watch it
`);

const batchedProjects = topologicallyBatchProjects(projects, projectGraph);
Expand Down
83 changes: 13 additions & 70 deletions packages/kbn-pm/src/commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,23 @@
* Side Public License, v 1.
*/

import dedent from 'dedent';
import { CliError } from '../utils/errors';
import { log } from '../utils/log';
import { parallelizeBatches } from '../utils/parallelize';
import { ProjectMap, topologicallyBatchProjects } from '../utils/projects';
import { waitUntilWatchIsReady } from '../utils/watch';
import { runIBazel } from '../utils/bazel';
import { ICommand } from './';

/**
* Name of the script in the package/project package.json file to run during `kbn watch`.
*/
const watchScriptName = 'kbn:watch';

/**
* Name of the Kibana project.
*/
const kibanaProjectName = 'kibana';

/**
* Command that traverses through list of available projects/packages that have `kbn:watch` script in their
* package.json files, groups them into topology aware batches and then processes theses batches one by one
* running `kbn:watch` scripts in parallel within the same batch.
*
* Command internally relies on the fact that most of the build systems that are triggered by `kbn:watch`
* will emit special "marker" once build/watch process is ready that we can use as completion condition for
* the `kbn:watch` script and eventually for the entire batch. Currently we support completion "markers" for
* `webpack` and `tsc` only, for the rest we rely on predefined timeouts.
*/
export const WatchCommand: ICommand = {
description:
'Runs `kbn:watch` script for every project (only works on packages not using Bazel yet)',
description: 'Runs a build in the Bazel built packages and keeps watching them for changes',
name: 'watch',

async run(projects, projectGraph) {
log.warning(dedent`
We are migrating packages into the Bazel build system. If the package you are trying to watch
contains a BUILD.bazel file please just use 'yarn kbn watch-bazel'
`);

const projectsToWatch: ProjectMap = new Map();
for (const project of projects.values()) {
// We can't watch project that doesn't have `kbn:watch` script.
if (project.hasScript(watchScriptName)) {
projectsToWatch.set(project.name, project);
}
}

if (projectsToWatch.size === 0) {
throw new CliError(
`There are no projects to watch found. Make sure that projects define 'kbn:watch' script in 'package.json'.`
);
}

const projectNames = Array.from(projectsToWatch.keys());
log.info(`Running ${watchScriptName} scripts for [${projectNames.join(', ')}].`);

// Kibana should always be run the last, so we don't rely on automatic
// topological batching and push it to the last one-entry batch manually.
const shouldWatchKibanaProject = projectsToWatch.delete(kibanaProjectName);

const batchedProjects = topologicallyBatchProjects(projectsToWatch, projectGraph);

if (shouldWatchKibanaProject) {
batchedProjects.push([projects.get(kibanaProjectName)!]);
}

await parallelizeBatches(batchedProjects, async (pkg) => {
const completionHint = await waitUntilWatchIsReady(
pkg.runScriptStreaming(watchScriptName, {
debug: false,
}).stdout! // TypeScript note: As long as the proc stdio[1] is 'pipe', then stdout will not be null
);

log.success(`[${pkg.name}] Initial build completed (${completionHint}).`);
});
async run(projects, projectGraph, { options }) {
const runOffline = options?.offline === true;

// Call bazel with the target to build all available packages and run it through iBazel to watch it for changes
//
// Note: --run_output=false arg will disable the iBazel notifications about gazelle and buildozer when running it
// Can also be solved by adding a root `.bazel_fix_commands.json` but its not needed at the moment
await runIBazel(
['--run_output=false', 'build', '//packages:build', '--show_result=1'],
runOffline
);
},
};
28 changes: 0 additions & 28 deletions packages/kbn-pm/src/commands/watch_bazel.ts

This file was deleted.

0 comments on commit 134b2fd

Please sign in to comment.