Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Build worker fails due to "Cannot find type definition file for xxx" #449

Closed
ashi009 opened this issue May 18, 2019 · 1 comment
Closed
Labels

Comments

@ashi009
Copy link

ashi009 commented May 18, 2019

🐞 bug report

Affected Rule

The issue is caused by the rule:

ts_library

Is this a regression?

Not sure.

Description

When building a ts_library in worker mode, the build will fail if the rule has transitive @npm dependencies and their @types targets are not included as a direct dependency.

🔬 Minimal Reproduction

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "lib": [
      "esnext",
      "dom",
    ],
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "preserveSymlinks": true,
    "pretty": true,
    "strict": true,
  },
  "types": [],
}

index.tsx

import * as React from 'react';

BUILD.bazel

ts_library(
    name = "ts_default_library",
    srcs = ["index.tsx"],
    deps = [
        "@npm//@types/react",
        "@npm//react",
    ],
)

🔥 Exception or Error


$ bazel build --strategy=TypeScriptCompile=worker //test:ts_default_library

INFO: Analysed target //test:ts_default_library (0 packages loaded, 2 targets configured).
INFO: Found 1 target...
ERROR: /Users/xiaoyi/Projects/depot/test/BUILD.bazel:3:1: Compiling TypeScript (devmode) //test:ts_default_library failed (Exit 1)
error TS2688: Cannot find type definition file for 'classnames'.
error TS2688: Cannot find type definition file for 'connect'.
error TS2688: Cannot find type definition file for 'dom4'.
error TS2688: Cannot find type definition file for 'events'.
error TS2688: Cannot find type definition file for 'express'.
error TS2688: Cannot find type definition file for 'express-serve-static-core'.
error TS2688: Cannot find type definition file for 'faker'.
error TS2688: Cannot find type definition file for 'google-libphonenumber'.
error TS2688: Cannot find type definition file for 'google-protobuf'.
error TS2688: Cannot find type definition file for 'history'.
error TS2688: Cannot find type definition file for 'hoist-non-react-statics'.
error TS2688: Cannot find type definition file for 'http-proxy'.
error TS2688: Cannot find type definition file for 'http-proxy-middleware'.
error TS2688: Cannot find type definition file for 'jest'.
error TS2688: Cannot find type definition file for 'jwt-decode'.
error TS2688: Cannot find type definition file for 'loglevel'.
error TS2688: Cannot find type definition file for 'long'.
error TS2688: Cannot find type definition file for 'memory-fs'.
error TS2688: Cannot find type definition file for 'mime'.
error TS2688: Cannot find type definition file for 'node'.
error TS2688: Cannot find type definition file for 'object-path'.
error TS2688: Cannot find type definition file for 'pluralize'.
error TS2688: Cannot find type definition file for 'prop-types'.
error TS2688: Cannot find type definition file for 'qiniu-js'.
error TS2688: Cannot find type definition file for 'range-parser'.
error TS2688: Cannot find type definition file for 'react'.
error TS2688: Cannot find type definition file for 'react-dom'.
error TS2688: Cannot find type definition file for 'react-router'.
error TS2688: Cannot find type definition file for 'react-router-dom'.
error TS2688: Cannot find type definition file for 'recompose'.
error TS2688: Cannot find type definition file for 'serve-static'.
error TS2688: Cannot find type definition file for 'tapable'.
error TS2688: Cannot find type definition file for 'uglify-js'.
error TS2688: Cannot find type definition file for 'underscore'.
error TS2688: Cannot find type definition file for 'webpack'.
error TS2688: Cannot find type definition file for 'webpack-dev-middleware'.
error TS2688: Cannot find type definition file for 'webpack-dev-server'.
error TS2688: Cannot find type definition file for 'webpack-env'.
error TS2688: Cannot find type definition file for 'yup'.

🌍 Your Environment

Operating System:

  
OS X 10.14.4
  

Output of bazel version:

  
Build label: 0.24.1
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Apr 2 16:32:47 2019 (1554222767)
Build timestamp: 1554222767
Build timestamp as int: 1554222767
  

Rules version (SHA):

  
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.29.0/rules_nodejs-0.29.0.tar.gz"],
sha256 = "1db950bbd27fb2581866e307c0130983471d4c3cd49c46063a2503ca7b6770a4",

"@bazel/typescript@^0.29.0":
  version "0.29.0"
  resolved "https://registry.yarnpkg.com/@bazel/typescript/-/typescript-0.29.0.tgz#d276afe034f37b5f35ee1369c99dc33c637fc9f6"
  integrity sha512-Dp5ucrE1vXTORGiwEi6Ur4dlICpLsmZ1dscsEQT4ywF7xTT0/NmIG0ecBghiCFPFQTxt1D05TR3SH06rPtTAew==
  

Anything else relevant?

@alexeagle
Copy link
Contributor

Thanks this is a great bug report.

The problem is that TypeScript reads the @types directory and discovers all these typings files, because it has an aggressive and non-hermetic automatic discovery of ambient (non-imported) typings.
Under Bazel we only allow reads of files that are declared as inputs to an action, ensuring that the typescript compilation could be run remotely for example.

Our typical solution for this is to change tsconfig.json to disable that ambient typings discovery using "compilerOptions": { "types": [] }. Then in a compilation unit which actually requires some ambient typings we use the ///<reference types="foo"/> syntax to explicitly "import" that typing.

Some ideas:

  • we should improve the error message to indicate the possibility that typescript discovered a typing that isn't in the deps, and suggest what to do
  • figure out why TypeScript automatic discovery picks up these typings - I know we implemented a readDirectory in the compilerHost, maybe we can fix there
  • we could have Bazel always add the empty types option to your tsconfig (ideally we should do this only if the user didn't explicitly declare it, which means it should happen in tsconfig.ts where we read the options and set defaults)

@alexeagle alexeagle added the bug label May 21, 2019
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Jun 17, 2019
To make worker mode work with TypeScript ambient type discovery, always
zero out the compilerOptions#types in the generated tsconfig - users
should provide these as deps[] instead.

Fixes bazelbuild/rules_typescript#449
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants