Skip to content

Commit 216b150

Browse files
kormidejosephperrott
authored andcommitted
build(bazel): consolidate windows chromium path workaround
Add the hack to a central location so that it's easier to fix later.
1 parent f88f4b9 commit 216b150

File tree

9 files changed

+63
-99
lines changed

9 files changed

+63
-99
lines changed

aio/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ TEST_DEPS = APPLICATION_DEPS + [
145145
"@aio_npm//karma-coverage",
146146
"@aio_npm//karma-jasmine",
147147
"@aio_npm//karma-jasmine-html-reporter",
148+
"//aio/tools:windows-chromium-path",
148149
]
149150

150151
# All sources, specs, and config files required to serve the app
@@ -159,6 +160,7 @@ E2E_DEPS = APPLICATION_DEPS + [
159160
"@aio_npm//jasmine-spec-reporter",
160161
"@aio_npm//protractor",
161162
"@aio_npm//ts-node",
163+
"//aio/tools:windows-chromium-path",
162164
]
163165

164166
# Stamp npm_link targets for all dependencies that correspond to a

aio/content/examples/examples.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def docs_example(name, test = True, test_tags = [], test_exec_properties = {}):
191191
YARN_LABEL,
192192
"@aio_npm//@angular/build-tooling/bazel/browsers/chromium",
193193
"//aio/tools/examples:run-example-e2e",
194+
"//aio/tools:windows-chromium-path",
194195
# We install the whole node modules for runtime deps of e2e tests
195196
"@{workspace}//:node_modules_files".format(workspace = EXAMPLE_DEPS_WORKSPACE_NAME),
196197
] + select({

aio/karma.conf.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
// Karma configuration file, see link for more information
22
// https://karma-runner.github.io/1.0/config/configuration-file.html
33

4-
const os = require('os');
4+
const {getAdjustedChromeBinPathForWindows} = require('./tools/windows-chromium-path');
55

6-
if (os.platform() === 'win32') {
7-
/*
8-
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
9-
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
10-
under external/ in the execroot.
11-
12-
CHROME_BIN is set to the make var $(CHROMIUM) in aio/BUILD.bazel, which points to chrome
13-
under runfiles and thus starts with ../. Because we run architect with a chdir into aio,
14-
$(CHROMIUM) additionally has a second ../.
15-
16-
First, back out of
17-
bazel-out/x64_windows-fastbuild/bin/aio/test.bat.runfiles/angular/aio
18-
Then go into
19-
external/
20-
and then into
21-
org_chromium_chromium_windows/chrome-win
22-
to cancel out the leading ../../
23-
*/
24-
process.env.CHROME_BIN = `../../../../../../../external/org_chromium_chromium_windows/chrome-win/${process.env.CHROME_BIN}`;
25-
}
6+
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();
267

278
module.exports = function (config) {
289
config.set({

aio/scripts/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ nodejs_binary(
3939
name = "audit-web-app",
4040
testonly = True,
4141
data = [
42+
"//aio/tools:windows-chromium-path",
4243
"@aio_npm//@angular/build-tooling/bazel/browsers/chromium",
4344
"@aio_npm//lighthouse",
4445
"@aio_npm//lighthouse-logger",

aio/scripts/audit-web-app.mjs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
import lighthouse from 'lighthouse';
2828
import * as printer from 'lighthouse/lighthouse-cli/printer.js';
2929
import logger from 'lighthouse-logger';
30-
import os from 'os';
3130
import puppeteer from 'puppeteer-core';
3231
import path from 'path';
32+
import {getAdjustedChromeBinPathForWindows} from '../tools/windows-chromium-path.js';
3333

3434
// Constants
3535
const AUDIT_CATEGORIES = ['accessibility', 'best-practices', 'performance', 'pwa', 'seo'];
3636
const LIGHTHOUSE_FLAGS = {logLevel: process.env.CI ? 'error' : 'info'}; // Be less verbose on CI.
3737
const VIEWER_URL = 'https://googlechrome.github.io/lighthouse/viewer';
3838
const WAIT_FOR_SW_DELAY = 5000;
3939

40-
process.env.CHROME_BIN = adjustChromeBinPathForWindows();
40+
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();
4141

4242
// Run
4343
_main(process.argv.slice(2));
@@ -169,30 +169,3 @@ async function runLighthouse(browser, url, flags, config) {
169169
await browser.close();
170170
}
171171
}
172-
173-
// TODO: this is a hack; the root cause should be found and fixed
174-
function adjustChromeBinPathForWindows() {
175-
if (os.platform() === 'win32') {
176-
/*
177-
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
178-
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
179-
under external/ in the execroot.
180-
181-
CHROME_BIN is equal to the make var $(CHROMIUM), which points to chrome relative
182-
to the runfiles root.
183-
184-
The org_chromium_chromium_windows/ in the path below is needed to cancel out the
185-
leading ../ in CHROME_BIN.
186-
187-
First, back out of
188-
bazel-out/x64_windows-fastbuild/bin/aio/scripts/audit-web-app.bat.runfiles/angular
189-
Then go into`
190-
external/
191-
and then into
192-
org_chromium_chromium_windows/
193-
to cancel out the leading ../ in CHROME_BIN
194-
*/
195-
return path.resolve(`../../../../../../external/org_chromium_chromium_windows/${process.env.CHROME_BIN}`);
196-
}
197-
return process.env.CHROME_BIN;
198-
}

aio/tests/e2e/protractor.conf.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,9 @@
44

55
const { SpecReporter, StacktraceOption } = require('jasmine-spec-reporter');
66
const path = require('path')
7-
const os = require('os');
7+
const {getAdjustedChromeBinPathForWindows} = require('../../tools/windows-chromium-path');
88

9-
if (os.platform() === 'win32') {
10-
/*
11-
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
12-
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
13-
under external/ in the execroot.
14-
15-
CHROME_BIN is set to the make var $(CHROMIUM) in aio/BUILD.bazel, which points to chrome
16-
under runfiles and thus starts with ../. Because we run architect with a chdir into aio,
17-
$(CHROMIUM) additionally has a second ../.
18-
19-
First, back out of
20-
bazel-out/x64_windows-fastbuild/bin/aio/e2e.bat.runfiles/angular/aio
21-
Then go into
22-
external/
23-
and then into
24-
org_chromium_chromium_windows/chrome-win
25-
to cancel out the leading ../../
26-
*/
27-
process.env.CHROME_BIN = `../../../../../../../external/org_chromium_chromium_windows/chrome-win/${process.env.CHROME_BIN}`;
28-
}
9+
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();
2910

3011
/**
3112
* @type { import("protractor").Config }

aio/tools/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
2+
3+
package(default_visibility = ["//aio:__subpackages__"])
4+
5+
js_library(
6+
name = "windows-chromium-path",
7+
srcs = [
8+
"windows-chromium-path.js",
9+
],
10+
)

aio/tools/examples/run-example-e2e.mjs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import yargs from 'yargs';
99
import {hideBin} from 'yargs/helpers'
1010
import getPort from 'get-port';
1111
import {constructExampleSandbox} from "./example-sandbox.mjs";
12+
import { getAdjustedChromeBinPathForWindows } from '../windows-chromium-path.js';
1213

1314
shelljs.set('-e');
1415

15-
process.env.CHROME_BIN = adjustChromeBinPathForWindows();
16+
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();
1617

1718
// Resolve CHROME_BIN and CHROMEDRIVER_BIN from relative paths to absolute paths within the
1819
// runfiles tree so that subprocesses spawned in a different working directory can still find them.
@@ -329,31 +330,4 @@ function loadExampleConfig(exampleFolder) {
329330
return config;
330331
}
331332

332-
// TODO: this is a hack; the root cause should be found and fixed
333-
function adjustChromeBinPathForWindows() {
334-
if (os.platform() === 'win32') {
335-
/*
336-
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
337-
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
338-
under external/ in the execroot.
339-
340-
CHROME_BIN is equal to the make var $(CHROMIUM), which points to chrome relative
341-
to the runfiles root.
342-
343-
The org_chromium_chromium_windows/ in the path below is needed to cancel out the
344-
leading ../ in CHROME_BIN.
345-
346-
First, back out of
347-
bazel-out/x64_windows-fastbuild/bin/aio/content/examples/{EXAMPLE}/e2e.bat.runfiles/angular
348-
Then go into
349-
external/
350-
and then into
351-
org_chromium_chromium_windows/
352-
to cancel out the leading ../ in CHROME_BIN
353-
*/
354-
return path.join(`../../../../../../../../../external/org_chromium_chromium_windows/${process.env.CHROME_BIN}`);
355-
}
356-
return process.env.CHROME_BIN;
357-
}
358-
359333
runE2e(EXAMPLE_PATH);

aio/tools/windows-chromium-path.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const os = require('os');
2+
const path = require('path');
3+
4+
/*
5+
TODO: this is a hack; the root cause should be found and fixed.
6+
7+
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
8+
karma on Windows. To work around this, use chromium under the execroot in the external
9+
folder and point to that instead.
10+
11+
Return:
12+
The adjusted absolute chromium path to use on Windows. On other platforms this is a
13+
noop and returns the existing process.env.CHROME_BIN.
14+
*/
15+
exports.getAdjustedChromeBinPathForWindows = function() {
16+
if (os.platform() === 'win32') {
17+
const runfilesWorkspaceRoot = path.join(process.env.RUNFILES, 'angular');
18+
19+
// Get the path to chromium from the runfiles base folder. By default, the Bazel make var
20+
// $(CHROMIUM) (which CHROME_BIN is set to) has a preceeding '../' to escape the workspace
21+
// root within runfiles. Additional '../' may have been prepended to cancel out any chdirs.
22+
const chromiumPath = process.env.CHROME_BIN.replace(/^(\.\.\/)*/, '');
23+
24+
// Escape from runfiles to the exec root
25+
const execRootSlashWorkspace = 'execroot' + path.sep + 'angular';
26+
const index = runfilesWorkspaceRoot.indexOf(execRootSlashWorkspace);
27+
const execRootPath = runfilesWorkspaceRoot.substring(0, index + execRootSlashWorkspace.length);
28+
const runfilesToExecRoot = path.relative(runfilesWorkspaceRoot, execRootPath);
29+
30+
// Resolve the path to chromium under the execroot
31+
const chromiumExecRootPath = path.resolve(
32+
runfilesWorkspaceRoot,
33+
runfilesToExecRoot,
34+
'external',
35+
chromiumPath
36+
);
37+
38+
return chromiumExecRootPath;
39+
}
40+
return process.env.CHROME_BIN;
41+
}

0 commit comments

Comments
 (0)