Skip to content

Commit

Permalink
build(bazel): consolidate windows chromium path workaround
Browse files Browse the repository at this point in the history
Add the hack to a central location so that it's easier to fix later.
  • Loading branch information
kormide authored and josephperrott committed Nov 22, 2022
1 parent de48b19 commit a61397f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 99 deletions.
2 changes: 2 additions & 0 deletions aio/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ TEST_DEPS = APPLICATION_DEPS + [
"@aio_npm//karma-coverage",
"@aio_npm//karma-jasmine",
"@aio_npm//karma-jasmine-html-reporter",
"//aio/tools:windows-chromium-path",
]

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

# Stamp npm_link targets for all dependencies that correspond to a
Expand Down
1 change: 1 addition & 0 deletions aio/content/examples/examples.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def docs_example(name, test = True, test_tags = [], test_exec_properties = {}):
YARN_LABEL,
"@aio_npm//@angular/build-tooling/bazel/browsers/chromium",
"//aio/tools/examples:run-example-e2e",
"//aio/tools:windows-chromium-path",
# We install the whole node modules for runtime deps of e2e tests
"@{workspace}//:node_modules_files".format(workspace = EXAMPLE_DEPS_WORKSPACE_NAME),
] + select({
Expand Down
23 changes: 2 additions & 21 deletions aio/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

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

if (os.platform() === 'win32') {
/*
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
under external/ in the execroot.
CHROME_BIN is set to the make var $(CHROMIUM) in aio/BUILD.bazel, which points to chrome
under runfiles and thus starts with ../. Because we run architect with a chdir into aio,
$(CHROMIUM) additionally has a second ../.
First, back out of
bazel-out/x64_windows-fastbuild/bin/aio/test.bat.runfiles/angular/aio
Then go into
external/
and then into
org_chromium_chromium_windows/chrome-win
to cancel out the leading ../../
*/
process.env.CHROME_BIN = `../../../../../../../external/org_chromium_chromium_windows/chrome-win/${process.env.CHROME_BIN}`;
}
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();

module.exports = function (config) {
config.set({
Expand Down
1 change: 1 addition & 0 deletions aio/scripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ nodejs_binary(
name = "audit-web-app",
testonly = True,
data = [
"//aio/tools:windows-chromium-path",
"@aio_npm//@angular/build-tooling/bazel/browsers/chromium",
"@aio_npm//lighthouse",
"@aio_npm//lighthouse-logger",
Expand Down
31 changes: 2 additions & 29 deletions aio/scripts/audit-web-app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
import lighthouse from 'lighthouse';
import * as printer from 'lighthouse/lighthouse-cli/printer.js';
import logger from 'lighthouse-logger';
import os from 'os';
import puppeteer from 'puppeteer-core';
import path from 'path';
import {getAdjustedChromeBinPathForWindows} from '../tools/windows-chromium-path.js';

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

process.env.CHROME_BIN = adjustChromeBinPathForWindows();
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();

// Run
_main(process.argv.slice(2));
Expand Down Expand Up @@ -169,30 +169,3 @@ async function runLighthouse(browser, url, flags, config) {
await browser.close();
}
}

// TODO: this is a hack; the root cause should be found and fixed
function adjustChromeBinPathForWindows() {
if (os.platform() === 'win32') {
/*
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
under external/ in the execroot.
CHROME_BIN is equal to the make var $(CHROMIUM), which points to chrome relative
to the runfiles root.
The org_chromium_chromium_windows/ in the path below is needed to cancel out the
leading ../ in CHROME_BIN.
First, back out of
bazel-out/x64_windows-fastbuild/bin/aio/scripts/audit-web-app.bat.runfiles/angular
Then go into`
external/
and then into
org_chromium_chromium_windows/
to cancel out the leading ../ in CHROME_BIN
*/
return path.resolve(`../../../../../../external/org_chromium_chromium_windows/${process.env.CHROME_BIN}`);
}
return process.env.CHROME_BIN;
}
23 changes: 2 additions & 21 deletions aio/tests/e2e/protractor.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,9 @@

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

if (os.platform() === 'win32') {
/*
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
under external/ in the execroot.
CHROME_BIN is set to the make var $(CHROMIUM) in aio/BUILD.bazel, which points to chrome
under runfiles and thus starts with ../. Because we run architect with a chdir into aio,
$(CHROMIUM) additionally has a second ../.
First, back out of
bazel-out/x64_windows-fastbuild/bin/aio/e2e.bat.runfiles/angular/aio
Then go into
external/
and then into
org_chromium_chromium_windows/chrome-win
to cancel out the leading ../../
*/
process.env.CHROME_BIN = `../../../../../../../external/org_chromium_chromium_windows/chrome-win/${process.env.CHROME_BIN}`;
}
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();

/**
* @type { import("protractor").Config }
Expand Down
10 changes: 10 additions & 0 deletions aio/tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")

package(default_visibility = ["//aio:__subpackages__"])

js_library(
name = "windows-chromium-path",
srcs = [
"windows-chromium-path.js",
],
)
30 changes: 2 additions & 28 deletions aio/tools/examples/run-example-e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import yargs from 'yargs';
import {hideBin} from 'yargs/helpers'
import getPort from 'get-port';
import {constructExampleSandbox} from "./example-sandbox.mjs";
import { getAdjustedChromeBinPathForWindows } from '../windows-chromium-path.js';

shelljs.set('-e');

process.env.CHROME_BIN = adjustChromeBinPathForWindows();
process.env.CHROME_BIN = getAdjustedChromeBinPathForWindows();

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

// TODO: this is a hack; the root cause should be found and fixed
function adjustChromeBinPathForWindows() {
if (os.platform() === 'win32') {
/*
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
karma on Windows. Instead, modify the CHROME_BIN env var to point to the chrome binary
under external/ in the execroot.
CHROME_BIN is equal to the make var $(CHROMIUM), which points to chrome relative
to the runfiles root.
The org_chromium_chromium_windows/ in the path below is needed to cancel out the
leading ../ in CHROME_BIN.
First, back out of
bazel-out/x64_windows-fastbuild/bin/aio/content/examples/{EXAMPLE}/e2e.bat.runfiles/angular
Then go into
external/
and then into
org_chromium_chromium_windows/
to cancel out the leading ../ in CHROME_BIN
*/
return path.join(`../../../../../../../../../external/org_chromium_chromium_windows/${process.env.CHROME_BIN}`);
}
return process.env.CHROME_BIN;
}

runE2e(EXAMPLE_PATH);
41 changes: 41 additions & 0 deletions aio/tools/windows-chromium-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const os = require('os');
const path = require('path');

/*
TODO: this is a hack; the root cause should be found and fixed.
For some unknown reason, the symlinked copy of chromium under runfiles won't run under
karma on Windows. To work around this, use chromium under the execroot in the external
folder and point to that instead.
Return:
The adjusted absolute chromium path to use on Windows. On other platforms this is a
noop and returns the existing process.env.CHROME_BIN.
*/
exports.getAdjustedChromeBinPathForWindows = function() {
if (os.platform() === 'win32') {
const runfilesWorkspaceRoot = path.join(process.env.RUNFILES, 'angular');

// Get the path to chromium from the runfiles base folder. By default, the Bazel make var
// $(CHROMIUM) (which CHROME_BIN is set to) has a preceeding '../' to escape the workspace
// root within runfiles. Additional '../' may have been prepended to cancel out any chdirs.
const chromiumPath = process.env.CHROME_BIN.replace(/^(\.\.\/)*/, '');

// Escape from runfiles to the exec root
const execRootSlashWorkspace = 'execroot' + path.sep + 'angular';
const index = runfilesWorkspaceRoot.indexOf(execRootSlashWorkspace);
const execRootPath = runfilesWorkspaceRoot.substring(0, index + execRootSlashWorkspace.length);
const runfilesToExecRoot = path.relative(runfilesWorkspaceRoot, execRootPath);

// Resolve the path to chromium under the execroot
const chromiumExecRootPath = path.resolve(
runfilesWorkspaceRoot,
runfilesToExecRoot,
'external',
chromiumPath
);

return chromiumExecRootPath;
}
return process.env.CHROME_BIN;
}

0 comments on commit a61397f

Please sign in to comment.