Skip to content

Commit 70b504b

Browse files
build: enable update of @angular/bazel (#26834)
* build: enable update of `@angular/bazel` Enables updating of `@angular/bazel`. We stopped because: * `@angular/bazel` was updated to no longer produce CommonJS devmode * It relied on `ts_library` patches to no longer produce CommonJS devmode. We can make these changes to unblock the update, but it would be a significant effort as all `nodejs_binary` targets would need to be updated to execute ESM natively (as there obviously is no CJS anymore). This is what we did as an experiment in framework to run tests/code natively using `.mjs`. We are not going to do it here as we would rather want to put the effort into switching to a toolchain that supports ESM properly.. without patches. For now we are just reversing the change in `ng_module` so that it still produces devmode CommonJS. Components repo will not use for tests etc, but it's there to allow for `nodejs_binary` CommonJS targets, and to avoid having to perform a large migration. See all the patches that are needed in framework: https://github.com/angular/angular/tree/main/tools/esm-interop * fixup! build: enable update of `@angular/bazel` --------- Co-authored-by: Alan Agius <[email protected]>
1 parent 1375505 commit 70b504b

File tree

8 files changed

+216
-194
lines changed

8 files changed

+216
-194
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ var_2: &docker-browser-image cimg/node:16.14.0-browsers
1313
# **Note**: When updating the beginning of the cache key, also update the cache key to match
1414
# the new cache key prefix. This allows us to take advantage of CircleCI's fallback caching.
1515
# Read more here: https://circleci.com/docs/2.0/caching/#restoring-cache.
16-
var_3: &cache_key v11-ng-mat-{{ checksum ".bazelversion" }}-{{ checksum "tools/postinstall/apply-patches.js" }}-{{ checksum "WORKSPACE" }}-{{ checksum "yarn.lock" }}
16+
var_3: &cache_key v16-ng-mat-{{ checksum ".bazelversion" }}-{{ checksum "tools/postinstall/apply-patches.js" }}-{{ checksum "WORKSPACE" }}-{{ checksum "yarn.lock" }}
1717
# We want to invalidate the cache if the postinstall patches change. In order to apply new
1818
# patches, a clean version of the node modules is needed. Additionally, we invalidate the cache
1919
# if the Bazel version changes. We do this because otherwise the `bazelisk` cache folder will
2020
# contain all previously used versions and ultimately cause the cache restoring to be slower.
21-
var_4: &cache_fallback_key v11-ng-mat-{{ checksum ".bazelversion" }}-{{ checksum "tools/postinstall/apply-patches.js" }}-
21+
var_4: &cache_fallback_key v16-ng-mat-{{ checksum ".bazelversion" }}-{{ checksum "tools/postinstall/apply-patches.js" }}-
2222

2323
# Settings common to each job
2424
var_5: &job_defaults

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ yarn_install(
7373
"//:.yarn/releases/yarn-1.22.17.cjs",
7474
"//:.yarnrc",
7575
"//:tools/postinstall/apply-patches.js",
76-
"//:tools/postinstall/devmode-es2020-bazel.patch",
76+
"//:tools/postinstall/patches/@angular+bazel+16.0.0-next.0.patch",
7777
],
7878
# Currently disabled due to:
7979
# 1. Missing Windows support currently.

goldens/ts-circular-deps.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[
22
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/directives/drop-list.ts"],
3-
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/drag-events.ts"],
43
[
54
"src/cdk/drag-drop/directives/drag.ts",
6-
"src/cdk/drag-drop/drag-events.ts",
7-
"src/cdk/drag-drop/directives/drop-list.ts"
5+
"src/cdk/drag-drop/directives/drop-list.ts",
6+
"src/cdk/drag-drop/drag-events.ts"
87
],
8+
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/drag-events.ts"],
99
["src/cdk/drag-drop/drag-ref.ts", "src/cdk/drag-drop/drop-list-ref.ts"],
1010
["src/cdk/scrolling/scroll-dispatcher.ts", "src/cdk/scrolling/scrollable.ts"],
1111
["src/cdk/scrolling/virtual-scroll-strategy.ts", "src/cdk/scrolling/virtual-scroll-viewport.ts"]

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"npm": "Please use Yarn instead of NPM to install dependencies. See: https://yarnpkg.com/lang/en/docs/install/"
1515
},
1616
"scripts": {
17-
"postinstall": "node tools/postinstall/apply-patches.js",
17+
"postinstall": "patch-package --patch-dir tools/postinstall/patches && node tools/postinstall/apply-patches.js",
1818
"ng-dev": "ts-node --esm --project .ng-dev/tsconfig.json --transpile-only node_modules/@angular/ng-dev/bundles/cli.mjs",
1919
"ng-dev:stamp": "yarn -s ng-dev release build-env-stamp --additional-stamping-script=tools/bazel-additional-stamp.mjs",
2020
"build": "ts-node --esm --project scripts/tsconfig.json ./scripts/build-packages-dist-main.mts",
@@ -73,12 +73,12 @@
7373
"@angular-devkit/build-angular": "^16.0.0-next.5",
7474
"@angular-devkit/core": "^16.0.0-next.5",
7575
"@angular-devkit/schematics": "^16.0.0-next.5",
76-
"@angular/bazel": "15.0.4",
77-
"@angular/build-tooling": "https://github.com/angular/dev-infra-private-build-tooling-builds.git#4c7d40592a36f061c23dd08f437f734081f9979d",
76+
"@angular/bazel": "https://github.com/angular/bazel-builds.git#8b1e899d38c5781662ff01812d5e71aa014b861c",
77+
"@angular/build-tooling": "https://github.com/angular/dev-infra-private-build-tooling-builds.git#a8012d366831d2dc2350de94b6c0ad926775408d",
7878
"@angular/cli": "^16.0.0-next.5",
7979
"@angular/compiler-cli": "^16.0.0-next.5",
8080
"@angular/localize": "^16.0.0-next.5",
81-
"@angular/ng-dev": "https://github.com/angular/dev-infra-private-ng-dev-builds.git#46a6cb28a6ca6a3a7a096656280ed27b02243e9a",
81+
"@angular/ng-dev": "https://github.com/angular/dev-infra-private-ng-dev-builds.git#68a6d97028583799726bdad5ef3234d3ec3463e6",
8282
"@angular/platform-browser-dynamic": "^16.0.0-next.5",
8383
"@angular/platform-server": "^16.0.0-next.5",
8484
"@angular/router": "^16.0.0-next.5",
@@ -196,6 +196,7 @@
196196
"moment": "^2.29.1",
197197
"node-fetch": "^2.6.0",
198198
"parse5": "^7.1.2",
199+
"patch-package": "^6.5.1",
199200
"postcss": "^8.4.17",
200201
"postcss-scss": "^4.0.4",
201202
"protractor": "^7.0.0",

tools/postinstall/apply-patches.js

Lines changed: 3 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,32 @@
11
/**
22
* Script that runs after node modules have been installed (including Bazel managed
3-
* node modules). This script can be used to apply postinstall patches. Similarly
4-
* to Bazel's "patches" attribute on repository fetch rules.
3+
* node modules). This script can be used to apply postinstall patches using commands.
4+
*
5+
* Most patches should be done using `patch-package` patches.
56
*/
67

78
const shelljs = require('shelljs');
89
const path = require('path');
9-
const fs = require('fs');
10-
const inquirer = require('inquirer');
11-
const chalk = require('chalk');
12-
13-
/**
14-
* Version of the post install patch. Needs to be incremented when
15-
* existing patches or edits have been modified.
16-
*/
17-
const PATCH_VERSION = 16;
1810

1911
/** Path to the project directory. */
2012
const projectDir = path.join(__dirname, '../..');
2113

22-
/**
23-
* Object that maps a given file path to a list of patches that need to be
24-
* applied.
25-
*/
26-
const PATCHES_PER_FILE = {};
27-
28-
const PATCH_MARKER_FILE_PATH = path.join(projectDir, 'node_modules/_ng-comp-patch-marker.json');
29-
30-
/** Registry of applied patches. */
31-
let registry = null;
32-
3314
main();
3415

3516
async function main() {
3617
shelljs.set('-e');
3718
shelljs.cd(projectDir);
3819

39-
registry = await readAndValidatePatchMarker();
40-
4120
// Apply all patches synchronously.
4221
try {
4322
applyPatches();
4423
} catch (error) {
4524
console.error(error);
4625
process.exit(1);
4726
}
48-
49-
// Write the patch marker file so that we don't accidentally re-apply patches
50-
// in subsequent Yarn installations.
51-
fs.writeFileSync(PATCH_MARKER_FILE_PATH, JSON.stringify(registry, null, 2));
5227
}
5328

5429
function applyPatches() {
55-
// Switches the devmode output for Angular Bazel to ES2020 target and module.
56-
applyPatch(path.join(__dirname, './devmode-es2020-bazel.patch'));
57-
5830
// Similar to the `rxjs` performance improvement below, see:
5931
// https://github.com/angular/angular/pull/46187.
6032
shelljs.rm('-rf', ['node_modules/@angular/common/locales']);
@@ -89,125 +61,4 @@ function applyPatches() {
8961
'node_modules/rxjs/Subscriber.*',
9062
'node_modules/rxjs/Subscription.*',
9163
]);
92-
93-
// Apply all collected patches on a per-file basis. This is necessary because
94-
// multiple edits might apply to the same file, and we only want to mark a given
95-
// file as patched once all edits have been made.
96-
Object.keys(PATCHES_PER_FILE).forEach(filePath => {
97-
if (isFilePatched(filePath)) {
98-
console.info('File ' + filePath + ' is already patched. Skipping..');
99-
return;
100-
}
101-
102-
let content = fs.readFileSync(filePath, 'utf8');
103-
const patchFunctions = PATCHES_PER_FILE[filePath];
104-
105-
console.info(`Patching file ${filePath} with ${patchFunctions.length} edits..`);
106-
patchFunctions.forEach(patchFn => (content = patchFn(content)));
107-
108-
fs.writeFileSync(filePath, content, 'utf8');
109-
captureFileAsPatched(filePath);
110-
});
111-
}
112-
113-
/**
114-
* Applies the given patch if not done already. Throws if the patch
115-
* does not apply cleanly.
116-
*/
117-
function applyPatch(patchFile) {
118-
if (isFilePatched(patchFile)) {
119-
console.info('Patch: ' + patchFile + ' has been applied already. Skipping..');
120-
return;
121-
}
122-
123-
shelljs.cat(patchFile).exec('patch -p0');
124-
captureFileAsPatched(patchFile);
125-
}
126-
127-
/**
128-
* Schedules an edit where the specified file is read and its content replaced based on
129-
* the given search expression and corresponding replacement. Throws if no changes were made
130-
* and the patch has not been applied.
131-
*/
132-
function searchAndReplace(search, replacement, relativeFilePath) {
133-
const filePath = path.join(projectDir, relativeFilePath);
134-
const fileEdits = PATCHES_PER_FILE[filePath] || (PATCHES_PER_FILE[filePath] = []);
135-
136-
fileEdits.push(originalContent => {
137-
const newFileContent = originalContent.replace(search, replacement);
138-
if (originalContent === newFileContent) {
139-
throw Error(
140-
`Could not perform replacement in: ${filePath}.\n` + `Searched for pattern: ${search}`,
141-
);
142-
}
143-
return newFileContent;
144-
});
145-
}
146-
147-
/** Gets a project unique id for a given file path. */
148-
function getIdForFile(filePath) {
149-
return path.relative(projectDir, filePath).replace(/\\/g, '/');
150-
}
151-
152-
/** Marks the specified file as patched. */
153-
function captureFileAsPatched(filePath) {
154-
registry.patched[getIdForFile(filePath)] = true;
155-
}
156-
157-
/** Checks whether the given file is patched. */
158-
function isFilePatched(filePath) {
159-
return registry.patched[getIdForFile(filePath)] === true;
160-
}
161-
162-
/**
163-
* Reads the patch marker from the node modules if present. Validates that applied
164-
* patches are up-to-date. If not, an error will be reported with a prompt that
165-
* allows convenient clean up of node modules in case those need to be cleaned up.
166-
*/
167-
async function readAndValidatePatchMarker() {
168-
if (!shelljs.test('-e', PATCH_MARKER_FILE_PATH)) {
169-
return {version: PATCH_VERSION, patched: {}};
170-
}
171-
const registry = JSON.parse(shelljs.cat(PATCH_MARKER_FILE_PATH));
172-
// If the node modules are up-to-date, return the parsed patch registry.
173-
if (registry.version === PATCH_VERSION) {
174-
return registry;
175-
}
176-
// Print errors that explain the current situation where patches from another
177-
// postinstall patch revision are applied in the current node modules.
178-
if (registry.version < PATCH_VERSION) {
179-
console.error(chalk.red('Your node modules have been patched by a previous Yarn install.'));
180-
console.error(chalk.red('The postinstall patches have changed since then, and in order to'));
181-
console.error(chalk.red('apply the most recent patches, your node modules need to be cleaned'));
182-
console.error(chalk.red('up from past changes.'));
183-
} else {
184-
console.error(chalk.red('Your node modules already have patches applied from a more recent.'));
185-
console.error(chalk.red('revision of the components repository. In order to be able to apply'));
186-
console.error(chalk.red('patches for the current revision, your node modules need to be'));
187-
console.error(chalk.red('cleaned up.'));
188-
}
189-
190-
let cleanupModules = true;
191-
192-
// Do not prompt if there is no TTY. Inquirer does not skip in non-tty environments.
193-
// TODO: Remove once inquirer has been updated to v8.x where TTY is respected.
194-
if (process.stdin.isTTY) {
195-
cleanupModules = (
196-
await inquirer.prompt({
197-
name: 'result',
198-
type: 'confirm',
199-
message: 'Clean up node modules automatically?',
200-
default: false,
201-
})
202-
).result;
203-
}
204-
205-
if (cleanupModules) {
206-
// This re-runs Yarn with `--check-files` mode. The postinstall will rerun afterwards,
207-
// so we can exit with a zero exit-code here.
208-
shelljs.exec('yarn --check-files --frozen-lockfile', {cwd: projectDir});
209-
process.exit(0);
210-
} else {
211-
process.exit(1);
212-
}
21364
}

tools/postinstall/devmode-es2020-bazel.patch

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)