fix(bazel): Disable sandbox on Mac OS (#30460)

Removing the sandbox improves build time by almost 40%.

For a hello world (ng new) application:
ng build with sandbox: 22.0 seconds
ng build without sandbox: 13.3 seconds

PR Close #30460
This commit is contained in:
Keen Yee Liau 2019-05-14 13:14:06 -07:00 committed by Jason Aden
parent 1aff524b63
commit b6b1aec22b
1 changed files with 21 additions and 0 deletions

View File

@ -10,6 +10,7 @@
import {spawn} from 'child_process';
import {copyFileSync, existsSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync} from 'fs';
import {platform} from 'os';
import {dirname, join, normalize} from 'path';
export type Executable = 'bazel' | 'ibazel';
@ -133,6 +134,24 @@ function replaceYarnWithNpm(source: string, dest: string) {
writeFileSync(dest, destContent);
}
/**
* Disable sandbox on Mac OS by setting spawn_strategy in .bazelrc.
* For a hello world (ng new) application, removing the sandbox improves build
* time by almost 40%.
* ng build with sandbox: 22.0 seconds
* ng build without sandbox: 13.3 seconds
*/
function disableSandbox(source: string, dest: string) {
const srcContent = readFileSync(source, 'utf-8');
const destContent = `${srcContent}
# Disable sandbox on Mac OS for performance reason.
build --spawn_strategy=local
run --spawn_strategy=local
test --spawn_strategy=local
`;
writeFileSync(dest, destContent);
}
/**
* Copy Bazel files (WORKSPACE, BUILD.bazel, etc) from the template directory to
* the project `root` directory, and return the absolute paths of the files
@ -154,6 +173,8 @@ export function copyBazelFiles(root: string, templateDir: string) {
if (!existsSync(dest)) {
if (!useYarn && name === 'WORKSPACE') {
replaceYarnWithNpm(source, dest);
} else if (platform() === 'darwin' && name === '.bazelrc') {
disableSandbox(source, dest);
} else {
copyFileSync(source, dest);
}