diff --git a/aio/BUILD.bazel b/aio/BUILD.bazel index c04a67620af5d7..996bab7af83035 100644 --- a/aio/BUILD.bazel +++ b/aio/BUILD.bazel @@ -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 @@ -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 diff --git a/aio/content/examples/examples.bzl b/aio/content/examples/examples.bzl index a229225705ac34..b1e6d3165fd6d8 100644 --- a/aio/content/examples/examples.bzl +++ b/aio/content/examples/examples.bzl @@ -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({ diff --git a/aio/karma.conf.js b/aio/karma.conf.js index 0e5efa968b46a6..de3db19e295f73 100644 --- a/aio/karma.conf.js +++ b/aio/karma.conf.js @@ -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({ diff --git a/aio/scripts/BUILD.bazel b/aio/scripts/BUILD.bazel index 20e7485c0fcb3a..a7f08b1f9d5189 100644 --- a/aio/scripts/BUILD.bazel +++ b/aio/scripts/BUILD.bazel @@ -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", diff --git a/aio/scripts/audit-web-app.mjs b/aio/scripts/audit-web-app.mjs index 4a59b5463d9a44..98420044946e70 100644 --- a/aio/scripts/audit-web-app.mjs +++ b/aio/scripts/audit-web-app.mjs @@ -27,9 +27,9 @@ 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']; @@ -37,7 +37,7 @@ const LIGHTHOUSE_FLAGS = {logLevel: process.env.CI ? 'error' : 'info'}; // Be l 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)); @@ -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; -} diff --git a/aio/tests/e2e/protractor.conf.js b/aio/tests/e2e/protractor.conf.js index 25b6524547f1bb..7737c06f82f053 100644 --- a/aio/tests/e2e/protractor.conf.js +++ b/aio/tests/e2e/protractor.conf.js @@ -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 } diff --git a/aio/tools/BUILD.bazel b/aio/tools/BUILD.bazel index e69de29bb2d1d6..f89443b416a722 100644 --- a/aio/tools/BUILD.bazel +++ b/aio/tools/BUILD.bazel @@ -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", + ], +) diff --git a/aio/tools/examples/run-example-e2e.mjs b/aio/tools/examples/run-example-e2e.mjs index e765c113ad07c9..217863299bafae 100644 --- a/aio/tools/examples/run-example-e2e.mjs +++ b/aio/tools/examples/run-example-e2e.mjs @@ -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. @@ -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); diff --git a/aio/tools/windows-chromium-path.js b/aio/tools/windows-chromium-path.js new file mode 100644 index 00000000000000..6a6bda48512e7f --- /dev/null +++ b/aio/tools/windows-chromium-path.js @@ -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; +}