Skip to content

Commit

Permalink
fix(bazel): Disable sandbox on Mac OS (angular#30460)
Browse files Browse the repository at this point in the history
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 angular#30460
  • Loading branch information
kyliau authored and BioPhoton committed May 21, 2019
1 parent 8fad49b commit 4a7afaa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/bazel/src/builders/bazel.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down

0 comments on commit 4a7afaa

Please sign in to comment.