Skip to content

Commit b6b1aec

Browse files
kyliaujasonaden
authored andcommitted
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
1 parent 1aff524 commit b6b1aec

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎packages/bazel/src/builders/bazel.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import {spawn} from 'child_process';
1212
import {copyFileSync, existsSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync} from 'fs';
13+
import {platform} from 'os';
1314
import {dirname, join, normalize} from 'path';
1415

1516
export type Executable = 'bazel' | 'ibazel';
@@ -133,6 +134,24 @@ function replaceYarnWithNpm(source: string, dest: string) {
133134
writeFileSync(dest, destContent);
134135
}
135136

137+
/**
138+
* Disable sandbox on Mac OS by setting spawn_strategy in .bazelrc.
139+
* For a hello world (ng new) application, removing the sandbox improves build
140+
* time by almost 40%.
141+
* ng build with sandbox: 22.0 seconds
142+
* ng build without sandbox: 13.3 seconds
143+
*/
144+
function disableSandbox(source: string, dest: string) {
145+
const srcContent = readFileSync(source, 'utf-8');
146+
const destContent = `${srcContent}
147+
# Disable sandbox on Mac OS for performance reason.
148+
build --spawn_strategy=local
149+
run --spawn_strategy=local
150+
test --spawn_strategy=local
151+
`;
152+
writeFileSync(dest, destContent);
153+
}
154+
136155
/**
137156
* Copy Bazel files (WORKSPACE, BUILD.bazel, etc) from the template directory to
138157
* the project `root` directory, and return the absolute paths of the files
@@ -154,6 +173,8 @@ export function copyBazelFiles(root: string, templateDir: string) {
154173
if (!existsSync(dest)) {
155174
if (!useYarn && name === 'WORKSPACE') {
156175
replaceYarnWithNpm(source, dest);
176+
} else if (platform() === 'darwin' && name === '.bazelrc') {
177+
disableSandbox(source, dest);
157178
} else {
158179
copyFileSync(source, dest);
159180
}

0 commit comments

Comments
 (0)