Skip to content

Commit

Permalink
chore(NA): moving @elastic/datemath into bazel (elastic#94665)
Browse files Browse the repository at this point in the history
* chore(NA): majority of the changes needed to build elastic-datemath with bazel

* chore(NA): add missing bits on elastic-datemath package

* chore(NA): add missing build and watch scripts

* chore(NA): remove build scripts for elastic datemah

* chore(NA): remove typo from build baze production projects logs

* chore(NA): force install on CI

* chore(NA): introduce custom preserve symlinks resolver for jest

* chore(NA): update jest integration snapshot

* chore(NA): fix build for bazel packages

* chore(NA): correctly copy bazel built packages into final distributable build

* chore(NA): update kbn pm dist

* chore(NA): experimental new logic to handle bazel yarn rule rerun using yarn-integrity file

* chore(NA): update snapshots

* refact(NA): ensure yarn integrity exists into two methods

* chore(NA): fix ts error

* chore(NA): update snapshots

* chore(NA): update elastic-datemath build file to include ts_project rule

* chore(NA): update basic optimization test snapshots

* chore(NA): merge and solve conflicts cherry-pick from elastic#96066

* chore(NA): update package.json and yarn.lock file

* chore(NA): update bazel/bin into bazel-bin on kbn-pm build bazel packages

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
# Conflicts:
#	package.json
  • Loading branch information
mistic committed Apr 1, 2021
1 parent c32d4b6 commit 24a8441
Show file tree
Hide file tree
Showing 24 changed files with 368 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .bazelrc.common
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test:debug --test_output=streamed --test_strategy=exclusive --test_timeout=9999
run:debug --define=VERBOSE_LOGS=1 -- --node_options=--inspect-brk
# The following option will change the build output of certain rules such as terser and may not be desirable in all cases
# It will also output both the repo cache and action cache to a folder inside the repo
build:debug --compilation_mode=dbg --show_result=1 --disk_cache=bazel/disk-cache --repository_cache=bazel/repository-cache
build:debug --compilation_mode=dbg --show_result=1

# Turn off legacy external runfiles
# This prevents accidentally depending on this feature, which Bazel will remove.
Expand Down
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# other packages builds and need to be included as inputs
exports_files(
[
"tsconfig.base.json",
"tsconfig.json",
"package.json"
],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"@elastic/apm-rum": "^5.6.1",
"@elastic/apm-rum-react": "^1.2.5",
"@elastic/charts": "26.0.0",
"@elastic/datemath": "link:packages/elastic-datemath",
"@elastic/datemath": "link:bazel-bin/packages/elastic-datemath/npm_module",
"@elastic/elasticsearch": "npm:@elastic/elasticsearch-canary@7.13.0-canary.1",
"@elastic/ems-client": "7.12.0",
"@elastic/eui": "31.10.0",
Expand Down Expand Up @@ -438,6 +438,7 @@
"@babel/traverse": "^7.12.12",
"@babel/types": "^7.12.12",
"@bazel/ibazel": "^0.14.0",
"@bazel/typescript": "^3.2.3",
"@cypress/snapshot": "^2.1.7",
"@cypress/webpack-preprocessor": "^5.5.0",
"@elastic/apm-rum": "^5.6.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
# targets so we can build them all at once
filegroup(
name = "build",
srcs = [],
srcs = [
"//packages/elastic-datemath:build"
],
)
3 changes: 2 additions & 1 deletion packages/elastic-datemath/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/index.test.js
/jest.config.js
/tsconfig.json
/__tests__
76 changes: 76 additions & 0 deletions packages/elastic-datemath/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm")

PKG_BASE_NAME = "elastic-datemath"
PKG_REQUIRE_NAME = "@elastic/datemath"

SOURCE_FILES = [
"src/index.ts",
]

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = glob(SOURCE_FILES),
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
"README.md",
]

SRC_DEPS = [
"@npm//moment",
]

TYPES_DEPS = [
"@npm//@types/node",
]

DEPS = SRC_DEPS + TYPES_DEPS

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
],
)

ts_project(
name = "tsc",
srcs = SRCS,
deps = DEPS,
declaration = True,
declaration_map = True,
incremental = True,
out_dir = "target",
source_map = True,
root_dir = "src",
tsconfig = ":tsconfig",
)

js_library(
name = PKG_BASE_NAME,
srcs = [],
deps = [":tsc"] + DEPS,
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
srcs = NPM_MODULE_EXTRA_FILES,
deps = [
":%s" % PKG_BASE_NAME,
]
)

filegroup(
name = "build",
srcs = [
":npm_module",
],
visibility = ["//visibility:public"],
)
5 changes: 2 additions & 3 deletions packages/elastic-datemath/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"license": "Apache-2.0",
"main": "./target/index.js",
"types": "./target/index.d.ts",
"scripts": {
"build": "../../node_modules/.bin/tsc",
"kbn:bootstrap": "yarn build"
"peerDependencies": {
"moment": "^2.24.0"
}
}
4 changes: 2 additions & 2 deletions packages/elastic-datemath/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"incremental": false,
"outDir": "./target",
"declaration": true,
"declarationMap": true,
"outDir": "target",
"rootDir": "src",
"sourceMap": true,
"sourceRoot": "../../../../packages/elastic-datemath/src",
"types": [
Expand Down
69 changes: 53 additions & 16 deletions packages/kbn-pm/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ async function run(argv) {
},
default: {
cache: true,
'force-install': true,
'force-install': false,
offline: false,
validate: true
},
Expand Down Expand Up @@ -8910,8 +8910,11 @@ const BootstrapCommand = {
const nonBazelProjectsOnly = await Object(_utils_projects__WEBPACK_IMPORTED_MODULE_4__["getNonBazelProjectsOnly"])(projects);
const batchedNonBazelProjects = Object(_utils_projects__WEBPACK_IMPORTED_MODULE_4__["topologicallyBatchProjects"])(nonBazelProjectsOnly, projectGraph);
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 forceInstall = !!options && options['force-install'] === true; // Ensure we have a `node_modules/.yarn-integrity` file as we depend on it
const runOffline = (options === null || options === void 0 ? void 0 : options.offline) === true; // 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 Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["yarnIntegrityFileExists"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["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 Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["ensureYarnIntegrityFileExists"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kibanaProjectPath, 'node_modules')); // Install bazel machinery tools if needed
Expand All @@ -8925,9 +8928,6 @@ const BootstrapCommand = {
// That way non bazel projects could depend on bazel projects but not the other way around
// That is only intended during the migration process while non Bazel projects are not removed at all.
//
// Until we have our first package build within Bazel we will always need to directly call the yarn rule
// otherwise yarn install won't trigger as we don't have any npm dependency within Bazel
// TODO: Change CLI default in order to not force install as soon as we have our first Bazel package being built

if (forceInstall) {
await Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["runBazel"])(['run', '@nodejs//:yarn'], runOffline);
Expand Down Expand Up @@ -9105,6 +9105,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isDirectory", function() { return isDirectory; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isFile", function() { return isFile; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "createSymlink", function() { return createSymlink; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tryRealpath", function() { return tryRealpath; });
/* harmony import */ var cmd_shim__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(132);
/* harmony import */ var cmd_shim__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(cmd_shim__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var del__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(143);
Expand Down Expand Up @@ -9137,6 +9138,7 @@ const symlink = Object(util__WEBPACK_IMPORTED_MODULE_5__["promisify"])(fs__WEBPA
const chmod = Object(util__WEBPACK_IMPORTED_MODULE_5__["promisify"])(fs__WEBPACK_IMPORTED_MODULE_2___default.a.chmod);
const cmdShim = Object(util__WEBPACK_IMPORTED_MODULE_5__["promisify"])(cmd_shim__WEBPACK_IMPORTED_MODULE_0___default.a);
const mkdir = Object(util__WEBPACK_IMPORTED_MODULE_5__["promisify"])(fs__WEBPACK_IMPORTED_MODULE_2___default.a.mkdir);
const realpathNative = Object(util__WEBPACK_IMPORTED_MODULE_5__["promisify"])(fs__WEBPACK_IMPORTED_MODULE_2___default.a.realpath.native);
const mkdirp = async path => await mkdir(path, {
recursive: true
});
Expand Down Expand Up @@ -9220,6 +9222,20 @@ async function forceCreate(src, dest, type) {
await symlink(src, dest, type);
}

async function tryRealpath(path) {
let calculatedPath = path;

try {
calculatedPath = await realpathNative(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return calculatedPath;
}

/***/ }),
/* 132 */
/***/ (function(module, exports, __webpack_require__) {
Expand Down Expand Up @@ -22981,11 +22997,11 @@ class Project {

ensureValidProjectDependency(project) {
const relativePathToProject = normalizePath(path__WEBPACK_IMPORTED_MODULE_1___default.a.relative(this.path, project.path));
const relativePathToProjectIfBazelPkg = normalizePath(path__WEBPACK_IMPORTED_MODULE_1___default.a.relative(this.path, `bazel-bin/packages/${path__WEBPACK_IMPORTED_MODULE_1___default.a.basename(project.path)}`));
const relativePathToProjectIfBazelPkg = normalizePath(path__WEBPACK_IMPORTED_MODULE_1___default.a.relative(this.path, `${__dirname}/../../../bazel-bin/packages/${path__WEBPACK_IMPORTED_MODULE_1___default.a.basename(project.path)}/npm_module`));
const versionInPackageJson = this.allDependencies[project.name];
const expectedVersionInPackageJson = `link:${relativePathToProject}`;
const expectedVersionInPackageJsonIfBazelPkg = `link:${relativePathToProjectIfBazelPkg}`; // TODO: after introduce bazel to build all the packages and completely remove the support for kbn packages
// do not allow child projects to hold dependencies
// do not allow child projects to hold dependencies, unless they are meant to be published externally

if (versionInPackageJson === expectedVersionInPackageJson || versionInPackageJson === expectedVersionInPackageJsonIfBazelPkg) {
return;
Expand Down Expand Up @@ -23170,7 +23186,7 @@ function transformDependencies(dependencies = {}) {
}

if (isBazelPackageDependency(depVersion)) {
newDeps[name] = depVersion.replace('link:bazel-bin/', 'file:');
newDeps[name] = depVersion.replace('link:bazel-bin/', 'file:').replace('/npm_module', '');
continue;
}

Expand Down Expand Up @@ -48065,8 +48081,10 @@ function addProjectToTree(tree, pathParts, project) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _ensure_yarn_integrity_exists__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(373);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "ensureYarnIntegrityFileExists", function() { return _ensure_yarn_integrity_exists__WEBPACK_IMPORTED_MODULE_0__["ensureYarnIntegrityFileExists"]; });
/* harmony import */ var _yarn_integrity__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(373);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "yarnIntegrityFileExists", function() { return _yarn_integrity__WEBPACK_IMPORTED_MODULE_0__["yarnIntegrityFileExists"]; });

/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "ensureYarnIntegrityFileExists", function() { return _yarn_integrity__WEBPACK_IMPORTED_MODULE_0__["ensureYarnIntegrityFileExists"]; });

/* harmony import */ var _get_cache_folders__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(374);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "getBazelDiskCacheFolder", function() { return _get_cache_folders__WEBPACK_IMPORTED_MODULE_1__["getBazelDiskCacheFolder"]; });
Expand Down Expand Up @@ -48099,6 +48117,7 @@ __webpack_require__.r(__webpack_exports__);

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "yarnIntegrityFileExists", function() { return yarnIntegrityFileExists; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "ensureYarnIntegrityFileExists", function() { return ensureYarnIntegrityFileExists; });
/* 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__);
Expand All @@ -48112,9 +48131,27 @@ __webpack_require__.r(__webpack_exports__);
*/


async function yarnIntegrityFileExists(nodeModulesPath) {
try {
const nodeModulesRealPath = await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["tryRealpath"])(nodeModulesPath);
const yarnIntegrityFilePath = Object(path__WEBPACK_IMPORTED_MODULE_0__["join"])(nodeModulesRealPath, '.yarn-integrity'); // check if the file already exists

if (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isFile"])(yarnIntegrityFilePath)) {
return true;
}
} catch {// no-op
}

return false;
}
async function ensureYarnIntegrityFileExists(nodeModulesPath) {
try {
await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["writeFile"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["join"])(nodeModulesPath, '.yarn-integrity'), '', {
const nodeModulesRealPath = await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["tryRealpath"])(nodeModulesPath);
const yarnIntegrityFilePath = Object(path__WEBPACK_IMPORTED_MODULE_0__["join"])(nodeModulesRealPath, '.yarn-integrity'); // ensure node_modules folder is created

await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["mkdirp"])(nodeModulesRealPath); // write a blank file in case it doesn't exists

await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["writeFile"])(yarnIntegrityFilePath, '', {
flag: 'wx'
});
} catch {// no-op
Expand Down Expand Up @@ -63656,7 +63693,7 @@ async function buildBazelProductionProjects({
const projectNames = [...projects.values()].map(project => project.name);
_utils_log__WEBPACK_IMPORTED_MODULE_6__["log"].info(`Preparing Bazel projects production build for [${projectNames.join(', ')}]`);
await Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_4__["runBazel"])(['build', '//packages:build']);
_utils_log__WEBPACK_IMPORTED_MODULE_6__["log"].info(`All Bazel projects production builds for [${projectNames.join(', ')}] are complete}]`);
_utils_log__WEBPACK_IMPORTED_MODULE_6__["log"].info(`All Bazel projects production builds for [${projectNames.join(', ')}] are complete`);

for (const project of projects.values()) {
await copyToBuild(project, kibanaRoot, buildRoot);
Expand All @@ -63680,7 +63717,7 @@ async function copyToBuild(project, kibanaRoot, buildRoot) {
const relativeProjectPath = Object(path__WEBPACK_IMPORTED_MODULE_2__["relative"])(kibanaRoot, project.path);
const buildProjectPath = Object(path__WEBPACK_IMPORTED_MODULE_2__["resolve"])(buildRoot, relativeProjectPath);
await cpy__WEBPACK_IMPORTED_MODULE_0___default()(['**/*'], buildProjectPath, {
cwd: Object(path__WEBPACK_IMPORTED_MODULE_2__["join"])(kibanaRoot, 'bazel', 'bin', 'packages', Object(path__WEBPACK_IMPORTED_MODULE_2__["basename"])(buildProjectPath), 'npm_module'),
cwd: Object(path__WEBPACK_IMPORTED_MODULE_2__["join"])(kibanaRoot, 'bazel-bin', 'packages', Object(path__WEBPACK_IMPORTED_MODULE_2__["basename"])(buildProjectPath), 'npm_module'),
dot: true,
onlyFiles: true,
parents: true
Expand All @@ -63702,12 +63739,12 @@ async function applyCorrectPermissions(project, kibanaRoot, buildRoot) {
const buildProjectPath = Object(path__WEBPACK_IMPORTED_MODULE_2__["resolve"])(buildRoot, relativeProjectPath);
const allPluginPaths = await globby__WEBPACK_IMPORTED_MODULE_1___default()([`**/*`], {
onlyFiles: false,
cwd: Object(path__WEBPACK_IMPORTED_MODULE_2__["join"])(kibanaRoot, 'bazel', 'bin', 'packages', Object(path__WEBPACK_IMPORTED_MODULE_2__["basename"])(buildProjectPath), 'npm_module'),
cwd: buildProjectPath,
dot: true
});

for (const pluginPath of allPluginPaths) {
const resolvedPluginPath = Object(path__WEBPACK_IMPORTED_MODULE_2__["resolve"])(buildRoot, pluginPath);
const resolvedPluginPath = Object(path__WEBPACK_IMPORTED_MODULE_2__["resolve"])(buildProjectPath, pluginPath);

if (await Object(_utils_fs__WEBPACK_IMPORTED_MODULE_5__["isFile"])(resolvedPluginPath)) {
await Object(_utils_fs__WEBPACK_IMPORTED_MODULE_5__["chmod"])(resolvedPluginPath, 0o644);
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-pm/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function run(argv: string[]) {
},
default: {
cache: true,
'force-install': true,
'force-install': false,
offline: false,
validate: true,
},
Expand Down
18 changes: 13 additions & 5 deletions packages/kbn-pm/src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import { getAllChecksums } from '../utils/project_checksums';
import { BootstrapCacheFile } from '../utils/bootstrap_cache_file';
import { readYarnLock } from '../utils/yarn_lock';
import { validateDependencies } from '../utils/validate_dependencies';
import { ensureYarnIntegrityFileExists, installBazelTools, runBazel } from '../utils/bazel';
import {
ensureYarnIntegrityFileExists,
installBazelTools,
runBazel,
yarnIntegrityFileExists,
} from '../utils/bazel';

export const BootstrapCommand: ICommand = {
description: 'Install dependencies and crosslink projects',
Expand All @@ -33,7 +38,13 @@ export const BootstrapCommand: ICommand = {
const batchedNonBazelProjects = topologicallyBatchProjects(nonBazelProjectsOnly, projectGraph);
const kibanaProjectPath = projects.get('kibana')?.path || '';
const runOffline = options?.offline === true;
const forceInstall = !!options && options['force-install'] === true;

// 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
Expand All @@ -51,9 +62,6 @@ export const BootstrapCommand: ICommand = {
// That way non bazel projects could depend on bazel projects but not the other way around
// That is only intended during the migration process while non Bazel projects are not removed at all.
//
// Until we have our first package build within Bazel we will always need to directly call the yarn rule
// otherwise yarn install won't trigger as we don't have any npm dependency within Bazel
// TODO: Change CLI default in order to not force install as soon as we have our first Bazel package being built
if (forceInstall) {
await runBazel(['run', '@nodejs//:yarn'], runOffline);
}
Expand Down
Loading

0 comments on commit 24a8441

Please sign in to comment.