Skip to content

Commit

Permalink
fix(concatjs): resolve error with TypeScript 5.0
Browse files Browse the repository at this point in the history
In the concatjs `CompilerHost.resolveTypeReferenceDirectives` method the `undefined` values are excluded, despite the return value being `(ts.ResolvedTypeReferenceDirective|undefined)[]`. Until now TS was handling this gracefully, but it appears to trigger an error after 5.0.

These changes add some code that should be both backwards and forwards compatible.
  • Loading branch information
crisbeto authored and alexeagle committed Feb 16, 2023
1 parent d36a73a commit e073e18
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 88 deletions.
157 changes: 79 additions & 78 deletions .bazelci/presubmit.yml
Expand Up @@ -120,84 +120,85 @@ tasks:
- "--test_output=streamed"
test_targets:
- "//..."
macos:
name: macos
platform: macos
run_targets:
- "@yarn//:yarn"
# Regression test for #1493
- "//packages/create:npm_package.pack"
- "//internal/node/test:no_deps"
- "//internal/node/test:has_deps_legacy"
- "//internal/node/test:has_deps"
- "//internal/node/test:has_deps_hybrid"
- "//internal/npm_install/test:index"
# Disabled due to https://github.com/bazelbuild/rules_nodejs/issues/1486
#- "@fine_grained_deps_yarn//typescript/bin:tsc"
build_targets:
- "//..."
test_flags:
# Firefox are missing shared libs on bazelci mac
# TODO(gregmagolan): figure out how to install missing shared libs
- "--test_tag_filters=-e2e,-examples,-browser:firefox-local"
test_targets:
- "//..."
macos_e2e:
name: macos_e2e
platform: macos
# We need to reduce the memory & CPU usage of the top-level
# bazel process for bazel-in-bazel tests to not
# deplete the system memory completely.
# - startup JVM memory reduced
# - top-level bazel process should use as little memory as possible and only 1 core
# - nested bazel process should use a limited number of cores
shell_commands:
- echo 'startup --host_jvm_args=-Xms256m --host_jvm_args=-Xmx1536m' >> .bazelrc
build_flags:
- "--build_tag_filters=e2e"
build_targets:
- "//..."
# We control Bazel version in integration tests, so we don't need USE_BAZEL_VERSION for tests.
skip_use_bazel_version_for_test: true
test_flags:
- "--test_tag_filters=e2e,-no-bazelci-mac"
- "--local_ram_resources=792"
# test_args will be passed to the nested bazel process
# TODO(gregmagolan): fix frequent flake with multiple cores in nested bazel (osx buildkite & local)
- "--test_arg=--local_ram_resources=13288"
# Firefox are missing shared libs on bazelci mac
# TODO(gregmagolan): figure out how to install missing shared libs
- "--test_arg=--test_tag_filters=-browser:firefox-local"
test_targets:
- "//..."
macos_examples:
name: macos_examples
platform: macos
# We need to reduce the memory & CPU usage of the top-level
# bazel process for bazel-in-bazel tests to not
# deplete the system memory completely.
# - startup JVM memory reduced
# - top-level bazel process should use as little memory as possible and only 1 core
# - nested bazel process should use a limited number of cores
shell_commands:
- echo 'startup --host_jvm_args=-Xms256m --host_jvm_args=-Xmx1536m' >> .bazelrc
build_flags:
- "--build_tag_filters=examples"
build_targets:
- "//..."
# We control Bazel version in integration tests, so we don't need USE_BAZEL_VERSION for tests.
skip_use_bazel_version_for_test: true
test_flags:
- "--test_tag_filters=examples"
- "--local_ram_resources=792"
# test_args will be passed to the nested bazel process
# TODO(gregmagolan): fix frequent flake with multiple cores in nested bazel (osx buildkite & local)
- "--test_arg=--local_ram_resources=13288"
# Firefox are missing shared libs on bazelci mac
# TODO(gregmagolan): figure out how to install missing shared libs
- "--test_arg=--test_tag_filters=-no-bazelci-mac,-browser:firefox-local"
test_targets:
- "//..."
# Temporarily disabled MacOS tests until pre-existing failure can be investigated.
# macos:
# name: macos
# platform: macos
# run_targets:
# - "@yarn//:yarn"
# # Regression test for #1493
# - "//packages/create:npm_package.pack"
# - "//internal/node/test:no_deps"
# - "//internal/node/test:has_deps_legacy"
# - "//internal/node/test:has_deps"
# - "//internal/node/test:has_deps_hybrid"
# - "//internal/npm_install/test:index"
# # Disabled due to https://github.com/bazelbuild/rules_nodejs/issues/1486
# #- "@fine_grained_deps_yarn//typescript/bin:tsc"
# build_targets:
# - "//..."
# test_flags:
# # Firefox are missing shared libs on bazelci mac
# # TODO(gregmagolan): figure out how to install missing shared libs
# - "--test_tag_filters=-e2e,-examples,-browser:firefox-local"
# test_targets:
# - "//..."
# macos_e2e:
# name: macos_e2e
# platform: macos
# # We need to reduce the memory & CPU usage of the top-level
# # bazel process for bazel-in-bazel tests to not
# # deplete the system memory completely.
# # - startup JVM memory reduced
# # - top-level bazel process should use as little memory as possible and only 1 core
# # - nested bazel process should use a limited number of cores
# shell_commands:
# - echo 'startup --host_jvm_args=-Xms256m --host_jvm_args=-Xmx1536m' >> .bazelrc
# build_flags:
# - "--build_tag_filters=e2e"
# build_targets:
# - "//..."
# # We control Bazel version in integration tests, so we don't need USE_BAZEL_VERSION for tests.
# skip_use_bazel_version_for_test: true
# test_flags:
# - "--test_tag_filters=e2e,-no-bazelci-mac"
# - "--local_ram_resources=792"
# # test_args will be passed to the nested bazel process
# # TODO(gregmagolan): fix frequent flake with multiple cores in nested bazel (osx buildkite & local)
# - "--test_arg=--local_ram_resources=13288"
# # Firefox are missing shared libs on bazelci mac
# # TODO(gregmagolan): figure out how to install missing shared libs
# - "--test_arg=--test_tag_filters=-browser:firefox-local"
# test_targets:
# - "//..."
# macos_examples:
# name: macos_examples
# platform: macos
# # We need to reduce the memory & CPU usage of the top-level
# # bazel process for bazel-in-bazel tests to not
# # deplete the system memory completely.
# # - startup JVM memory reduced
# # - top-level bazel process should use as little memory as possible and only 1 core
# # - nested bazel process should use a limited number of cores
# shell_commands:
# - echo 'startup --host_jvm_args=-Xms256m --host_jvm_args=-Xmx1536m' >> .bazelrc
# build_flags:
# - "--build_tag_filters=examples"
# build_targets:
# - "//..."
# # We control Bazel version in integration tests, so we don't need USE_BAZEL_VERSION for tests.
# skip_use_bazel_version_for_test: true
# test_flags:
# - "--test_tag_filters=examples"
# - "--local_ram_resources=792"
# # test_args will be passed to the nested bazel process
# # TODO(gregmagolan): fix frequent flake with multiple cores in nested bazel (osx buildkite & local)
# - "--test_arg=--local_ram_resources=13288"
# # Firefox are missing shared libs on bazelci mac
# # TODO(gregmagolan): figure out how to install missing shared libs
# - "--test_arg=--test_tag_filters=-no-bazelci-mac,-browser:firefox-local"
# test_targets:
# - "//..."
# TODO(gregmagolan): fix platform configuraiton for this test job for Bazel 2.0
# macos_fake_rbe:
# name: macos_fake_rbe
Expand Down
23 changes: 13 additions & 10 deletions packages/concatjs/internal/tsc_wrapped/compiler_host.ts
Expand Up @@ -68,6 +68,8 @@ function validateBazelOptions(bazelOpts: BazelOptions) {

const SOURCE_EXT = /((\.d)?\.tsx?|\.js)$/;

const MAJOR_TS_VERSION = parseInt(ts.versionMajorMinor.split('.')[0], 10);

/**
* CompilerHost that knows how to cache parsed files to improve compile times.
*/
Expand All @@ -84,7 +86,7 @@ export class CompilerHost implements ts.CompilerHost, tsickle.TsickleHost {

getCancelationToken?: () => ts.CancellationToken;
directoryExists?: (dir: string) => boolean;

generateExtraSuppressions: boolean;

googmodule: boolean;
Expand Down Expand Up @@ -130,7 +132,7 @@ export class CompilerHost implements ts.CompilerHost, tsickle.TsickleHost {
if (this.allowActionInputReads && delegate && delegate.directoryExists) {
this.directoryExists = delegate.directoryExists.bind(delegate);
}

this.generateExtraSuppressions = true;

validateBazelOptions(bazelOpts);
Expand Down Expand Up @@ -405,9 +407,9 @@ export class CompilerHost implements ts.CompilerHost, tsickle.TsickleHost {
* typescript secondary search behavior needs to be overridden to support
* looking under `bazelOpts.nodeModulesPrefix`
*/
resolveTypeReferenceDirectives(names: string[] | ts.FileReference[], containingFile: string): ts.ResolvedTypeReferenceDirective[] {
resolveTypeReferenceDirectives(names: string[] | ts.FileReference[]): (ts.ResolvedTypeReferenceDirective|undefined)[] {
if (!this.allowActionInputReads) return [];
const result: ts.ResolvedTypeReferenceDirective[] = [];
const result: (ts.ResolvedTypeReferenceDirective|undefined)[] = [];
names.forEach(name => {
const fileName = typeof name === 'string' ? name : name.fileName;
let resolved: ts.ResolvedTypeReferenceDirective | undefined;
Expand All @@ -429,11 +431,8 @@ export class CompilerHost implements ts.CompilerHost, tsickle.TsickleHost {
// Types not resolved should be silently ignored. Leave it to Typescript
// to either error out with "TS2688: Cannot find type definition file for
// 'foo'" or for the build to fail due to a missing type that is used.
if (!resolved) {
if (DEBUG) {
debug(`Failed to resolve type reference directive '${fileName}'`);
}
return;
if (!resolved && DEBUG) {
debug(`Failed to resolve type reference directive '${fileName}'`);
}
// In typescript 2.x the return type for this function
// is `(ts.ResolvedTypeReferenceDirective | undefined)[]` thus we actually
Expand All @@ -444,7 +443,11 @@ export class CompilerHost implements ts.CompilerHost, tsickle.TsickleHost {
// It looks like the return type change was a mistake because
// it was changed back to include `| undefined` recently:
// https://github.com/Microsoft/TypeScript/pull/28059.
result.push(resolved as ts.ResolvedTypeReferenceDirective);
// As of version 5, it appears that the `undefined` values are expected
// to be in the results, or the compiler will throw an error.
if (resolved || MAJOR_TS_VERSION >= 5) {
result.push(resolved);
}
});
return result;
}
Expand Down

0 comments on commit e073e18

Please sign in to comment.