Skip to content

build: validate bazel rollup globals #17173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,16 @@ jobs:
steps:
- *checkout_code
- *restore_cache
- *setup_bazel_ci_config
- *yarn_download
- *yarn_install
- *setup_bazel_binary

- run:
name: Checking rollup globals
command: |
bazel build //:rollup_globals
yarn check-rollup-globals $(bazel info bazel-bin)/rollup_globals.json

- run: ./scripts/circleci/lint-bazel-files.sh
- run: yarn gulp ci:lint
Expand Down
8 changes: 8 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package(default_visibility = ["//visibility:public"])

load("//:rollup-globals.bzl", "ROLLUP_GLOBALS")

exports_files(["LICENSE"])

genrule(
name = "rollup_globals",
outs = ["rollup_globals.json"],
cmd = "echo '%s' > $@" % ROLLUP_GLOBALS,
)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"format:ts": "git-clang-format HEAD $(git diff HEAD --name-only | grep -v \"\\.d\\.ts\")",
"format:bazel": "yarn -s bazel:buildifier --lint=fix --mode=fix",
"format": "yarn -s format:ts && yarn -s format:bazel",
"cherry-pick-patch": "ts-node --project tools/cherry-pick-patch/ tools/cherry-pick-patch/cherry-pick-patch.ts"
"cherry-pick-patch": "ts-node --project tools/cherry-pick-patch/ tools/cherry-pick-patch/cherry-pick-patch.ts",
"check-rollup-globals": "ts-node --project scripts/ scripts/check-rollup-globals.ts"
},
"version": "8.2.0",
"requiredAngularVersion": "^8.0.0 || ^9.0.0-0",
Expand Down
27 changes: 26 additions & 1 deletion rollup-globals.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@ load(
"MATERIAL_EXPERIMENTAL_TESTING_ENTRYPOINTS",
)

# Base rollup globals for everything in the repo.
# Base rollup globals for everything in the repo. Note that we want to disable
# sorting of the globals as we manually group dict entries.
# buildifier: disable=unsorted-dict-items
ROLLUP_GLOBALS = {
# Framework packages.
"@angular/animations": "ng.animations",
"@angular/common": "ng.common",
"@angular/common/http": "ng.common.http",
"@angular/common/http/testing": "ng.common.http.testing",
"@angular/common/testing": "ng.common.testing",
"@angular/core": "ng.core",
"@angular/core/testing": "ng.core.testing",
"@angular/forms": "ng.forms",
"@angular/platform-browser": "ng.platformBrowser",
"@angular/platform-browser-dynamic": "ng.platformBrowserDynamic",
"@angular/platform-browser-dynamic/testing": "ng.platformBrowserDynamic.testing",
"@angular/platform-browser/animations": "ng.platformBrowser.animations",
"@angular/platform-server": "ng.platformServer",
"@angular/router": "ng.router",

# Primary entry-points in the project.
"@angular/cdk": "ng.cdk",
"@angular/cdk-experimental": "ng.cdkExperimental",
"@angular/google-maps": "ng.googleMaps",
"@angular/material": "ng.material",
"@angular/material-experimental": "ng.materialExperimental",
"@angular/material-moment-adapter": "ng.materialMomentAdapter",
"@angular/youtube-player": "ng.youtubePlayer",

# MDC Web
Expand Down Expand Up @@ -47,7 +67,12 @@ ROLLUP_GLOBALS = {
"@material/tab-scroller": "mdc.tabScroller",
"@material/text-field": "mdc.textField",
"@material/top-app-bar": "mdc.topAppBar",

# Third-party libraries.
"moment": "moment",
"protractor": "protractor",
"rxjs": "rxjs",
"rxjs/operators": "rxjs.operators",
"tslib": "tslib",
}

Expand Down
73 changes: 73 additions & 0 deletions scripts/check-rollup-globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Script that goes through each source file that will be included in the
* release output and checks if any module imports are not part of the
* specified rollup globals file.
*
* This script is used to validate Bazel rollup globals. We use a genrule to
* convert the Starlark rollup globals dict into a JSON file which then can
* be passed to this script to ensure that the rollup globals are up-to-date.
*/

import chalk from 'chalk';
import {readFileSync} from 'fs';
import * as minimatch from 'minimatch';
import {join, relative} from 'path';
import * as ts from 'typescript';

const projectRoot = join(__dirname, '../');
const args = process.argv.slice(2);

if (args.length !== 1) {
console.error(chalk.red('No rollup globals file has been specified.'));
process.exit(1);
}

const rollupGlobals = JSON.parse(readFileSync(args[0], 'utf8'));
const configFile = ts.readJsonConfigFile(join(projectRoot, 'tsconfig.json'), ts.sys.readFile);
const parsedConfig = ts.parseJsonSourceFileConfigFileContent(configFile, ts.sys, projectRoot);
const filesToCheckGlob = [
'src/**/!(*.spec).ts',
'!src/+(a11y-demo|e2e-app|universal-app|dev-app)/**/*.ts',
'!src/**/schematics/**/*.ts',
];

const failures = new Map<string, string[]>();

parsedConfig.fileNames.forEach(fileName => {
const relativeFileName = relative(projectRoot, fileName);
if (!filesToCheckGlob.every(g => minimatch(relativeFileName, g))) {
return;
}

const sourceFile =
ts.createSourceFile(fileName, readFileSync(fileName, 'utf8'), ts.ScriptTarget.Latest, true);

const visitNode = (node: ts.Node) => {
if (ts.isImportDeclaration(node)) {
// Parse out the module name. The first and last characters are the quote marks.
const module = node.moduleSpecifier.getText().slice(1, -1);
const isExternal = !module.startsWith('.') && !module.startsWith('/');

// Check whether the module is external and whether it's in our rollup globals.
if (isExternal && !rollupGlobals[module]) {
failures.set(fileName, (failures.get(fileName) || []).concat(module));
}
}

ts.forEachChild(node, visitNode);
};

ts.forEachChild(sourceFile, visitNode);
});

if (failures.size) {
console.error(chalk.red(' ✘ Rollup globals are not up-to-date.'));
console.error();

failures.forEach((missingGlobals, fileName) => {
console.error(chalk.yellow(` ⮑ ${fileName}:`));
missingGlobals.forEach(g => console.error(` - ${g}`));
});
} else {
console.info(chalk.green(' ✓ Rollup globals are up-to-date.'));
}
11 changes: 6 additions & 5 deletions src/cdk/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ CDK_ENTRYPOINTS = [
"table",
"text-field",
"tree",

# NOTE: "testing" should not be listed here as it will be treated as its own
# package that will be included manually in the "ng_package".
"testing",
]

# List of all entry-point targets of the Angular Material package.
CDK_TARGETS = ["//src/cdk"] + ["//src/cdk/%s" % ep for ep in CDK_ENTRYPOINTS]
# List of all entry-point targets of the Angular Material package. Note that
# we do not want to include "testing" here as it will be treated as a standalone
# sub-package of the "ng_package".
CDK_TARGETS = ["//src/cdk"] + \
["//src/cdk/%s" % ep for ep in CDK_ENTRYPOINTS if not ep == "testing"]

# Within the CDK, only a few targets have sass libraries which need to be
# part of the release package. This list declares all CDK targets with sass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {ButtonHarnessFilters} from '@angular/material/button/testing/button-harness-filters';
import {ButtonHarnessFilters} from '@angular/material/button/testing';


/**
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-menu/testing/menu-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {MatMenuItemHarness} from './menu-item-harness';
import {MenuHarnessFilters} from '@angular/material/menu/testing/menu-harness-filters';
import {MenuHarnessFilters} from '@angular/material/menu/testing';

/**
* Harness for interacting with a MDC-based mat-menu in tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {MenuItemHarnessFilters} from '@angular/material/menu/testing/menu-harness-filters';
import {MenuItemHarnessFilters} from '@angular/material/menu/testing';


/**
Expand Down
2 changes: 1 addition & 1 deletion src/material/menu/testing/menu-item-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {MenuItemHarnessFilters} from '@angular/material/menu/testing/menu-harness-filters';
import {MenuItemHarnessFilters} from './menu-harness-filters';


/**
Expand Down
4 changes: 2 additions & 2 deletions src/material/radio/testing/radio-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {RadioButtonHarnessFilters, RadioGroupHarnessFilters} from '@angular/material/radio/testing/radio-harness-filters';
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {RadioButtonHarnessFilters, RadioGroupHarnessFilters} from './radio-harness-filters';

/**
* Harness for interacting with a standard mat-radio-group in tests.
Expand Down
25 changes: 25 additions & 0 deletions tools/package-tools/rollup-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ export const rollupGlobals = {
...rollupYouTubePlayerEntryPoints,
...rollupGoogleMapsEntryPoints,

// For each Angular Material secondary entry-point, we include a testing
// tertiary entry-point.
...matSecondaryEntryPoints.reduce(
(res, entryPoint) => {
return {...res, ...generateRollupEntryPoints(`material/${entryPoint}`, ['testing'])};
},
[]),

// For each Angular Material experimental secondary entry-point, we include a testing
// tertiary entry-point.
...materialExperimentalSecondaryEntryPoints.reduce(
(res, entryPoint) => {
return {
...res,
...generateRollupEntryPoints(`material-experimental/${entryPoint}`, ['testing'])
};
},
[]),

// Manual entry-points which are not detected automatically. Since we do
// not use this file for rollup-globals anymore, just as a backup if the Bazel
// migrations does not work out, we can add these entries manually for now.
'@angular/material-experimental/form-field/testing/control':
'ng.materialExperimental.formField.testing.control',

'@angular/cdk/private/testing': 'ng.cdk.private.testing',
'@angular/cdk/private/testing/e2e': 'ng.cdk.private.testing.e2e',

Expand Down
2 changes: 1 addition & 1 deletion tools/tslint-rules/missingRollupGlobalsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Walker extends Lint.RuleWalker {

this._configPath = path.resolve(process.cwd(), configPath);
this._config = require(this._configPath).rollupGlobals;
this._enabled = fileGlobs.some(p => minimatch(relativeFilePath, p));
this._enabled = fileGlobs.every(p => minimatch(relativeFilePath, p));
}

visitImportDeclaration(node: ts.ImportDeclaration) {
Expand Down
4 changes: 3 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@
"missing-rollup-globals": [
true,
"./tools/package-tools/rollup-globals.ts",
"src/!(a11y-demo|e2e-app|material-examples|universal-app|dev-app)/!(schematics)**/*.ts"
"src/**/!(*.spec).ts",
"!src/+(a11y-demo|e2e-app|universal-app|dev-app)/**/*.ts",
"!src/**/schematics/**/*.ts"
],
"file-name-casing": [true, {
// Exclude custom lint rule files since they have to always be camel-cased and end
Expand Down