Skip to content
Closed
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
7 changes: 6 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")
load("@devinfra_npm//:defs.bzl", "npm_link_all_packages")
load("//bazel/validation:defs.bzl", "validate_ts_version_matching")

npm_link_all_packages()

Expand All @@ -12,7 +13,6 @@ ts_config(

exports_files([
"package.json",
".yarnrc.yml",
])

js_binary(
Expand All @@ -28,3 +28,8 @@ js_binary(
entry_point = ".yarn/releases/yarn-1.22.17.cjs",
visibility = ["//bazel/integration/tests:__subpackages__"],
)

validate_ts_version_matching(
module_lock_file = "MODULE.bazel.lock",
package_json = "package.json",
)
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ rules_ts_ext = use_extension("@aspect_rules_ts//ts:extensions.bzl", "ext")
rules_ts_ext.deps(
# Obtained by: curl --silent https://registry.npmjs.org/typescript/5.9.2 | jq -r '.dist.integrity'
ts_integrity = "sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A==",
ts_version_from = "//bazel:package.json",
ts_version = "5.9.2",
)
use_repo(rules_ts_ext, "npm_typescript")

Expand Down
6 changes: 2 additions & 4 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions bazel/validation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")

copy_to_bin(
name = "verify-typescript",
srcs = [
"verify-typescript.mjs",
],
visibility = [
"//visibility:public",
],
)
15 changes: 15 additions & 0 deletions bazel/validation/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")

def validate_ts_version_matching(package_json, module_lock_file):
js_test(
name = "validate_ts_version_match",
data = [
package_json,
module_lock_file,
],
entry_point = "@devinfra//bazel/validation:verify-typescript",
fixed_args = [
"$(rlocationpath %s)" % package_json,
"$(rlocationpath %s)" % module_lock_file,
],
)
69 changes: 69 additions & 0 deletions bazel/validation/verify-typescript.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import {readFile} from 'node:fs/promises';
import {join} from 'node:path';

/** The runfiles directory for the script. */
const runfiles = process.env['RUNFILES'];

async function main([packageJsonPath, moduleLockFilePath]) {
/** The json contents of the BAZEL.module.lock file. */
const moduleLock = JSON.parse(await readFile(join(runfiles, moduleLockFilePath), 'utf8'));
/** The json contents of the package.json file. */
const packageJson = JSON.parse(await readFile(join(runfiles, packageJsonPath), 'utf8'));
/** The version of typescript extracted from the package.json file. */
let packageJsonVersion;
try {
packageJsonVersion =
packageJson['dependencies']?.['typescript'] || packageJson['devDependencies']?.['typescript'];
} catch {
console.error('Unable to find the typescript version within the package.json file.');
}

/** The version of typescript extracted from the BAZEL.module.lock file. */
let lockfileVersion;
try {
// The path to the generated repo specs is static based on the location of the extension
// used. The name of the generated repo is determined by the user so we instead need to take
// the first value/item from the `generaredRepoSpecs` property and get the version from the
// attributes there.
const generatedRepoSpecs =
moduleLock['moduleExtensions']?.['@@aspect_rules_ts~//ts:extensions.bzl%ext']?.['general']?.[
'generatedRepoSpecs'
];
lockfileVersion =
Object.values(generatedRepoSpecs || {})[0]?.['attributes']?.['version'] || 'unknown';
} catch {
console.error('Unable to find the typescript version within the MODULE.bazel.lock file.');
}

// If either version is undefined, the comparison is invalid and we should exit.
if (packageJsonVersion === undefined || lockfileVersion === undefined) {
process.exitCode = 1;
return;
}

// If the versions don't match, exit as a failure.
if (packageJsonVersion !== lockfileVersion) {
console.error(
`Typescript version mismatch between MODULE.bazel (${lockfileVersion}) and package.json (${packageJsonVersion})`,
);
process.exitCode = 1;
return;
}

console.info(
`Typescript version matches between MODULE.bazel and package.json: ${lockfileVersion}`,
);
}

main(process.argv.slice(2)).catch((e) => {
console.error(e);
process.exit(2);
});