Skip to content

Debugging problems with ts_project

Alex Eagle edited this page May 31, 2021 · 2 revisions

General tips

It helps to understand how tsc works. For example, it compiles files which are added to the program through several mechanisms - see the explanation on

Upgrading to TypeScript 4.2 or greater can be helpful, because error messages were improved, and new flags were added.

TS 4.1:

error TS6059: File '/private/var/tmp/_bazel_alex.eagle/efa8e81f99c35c1227ef40a83cd29a26/execroot/examples_jest/ts/test/index.test.ts' is not under 'rootDir' '/private/var/tmp/_bazel_alex.eagle/efa8e81f99c35c1227ef40a83cd29a26/execroot/examples_jest/ts/src'. 'rootDir' is expected to contain all source files.
Target //ts/src:src failed to build

TS 4.2:

error TS6059: File '/private/var/tmp/_bazel_alex.eagle/efa8e81f99c35c1227ef40a83cd29a26/execroot/examples_jest/ts/test/index.test.ts' is not under 'rootDir' '/private/var/tmp/_bazel_alex.eagle/efa8e81f99c35c1227ef40a83cd29a26/execroot/examples_jest/ts/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Matched by include pattern '**/*' in 'tsconfig.json'

The --explainFiles flag in TS 4.2 also gives information about why a given file was added to the program.

Module not resolved

ts_project is a thin wrapper around tsc, so you'll have to explain to TypeScript what's going on with resolving first-party dependencies from inside your repo.

The first thing to verify is that there is actually a .d.ts file for TypeScript to resolve. Check that the dependency library has the declarations = True flag set, and that the .d.ts files appear where you expect them under bazel-out.

The next check is that the rootDirs setting is correct in tsconfig.json and includes the path where the .d.ts files are present. Use --traceResolution to ask tsc to print all the places it tried to resolve the dependency, and why.

Finally, if you're using a package name to import, like @myco/file rather than a relative path like ../file, then you need to use a js_library target with the module_name field set, so that rules_nodejs "links" the package into the node_modules of the dependent target.

TS5033: EPERM

error TS5033: Could not write file 'bazel-out/x64_windows-fastbuild/bin/setup_script.js': EPERM: operation not permitted, open 'bazel-out/x64_windows-fastbuild/bin/setup_script.js'.

This likely means two different Bazel targets tried to write the same output file. Use --listFiles to ask tsc to show what files are in the program. Try --explainFiles (see above) to see how they got there.

You may find that the program contained a .ts file rather than the corresponding .d.ts file.

Also see https://github.com/microsoft/TypeScript/issues/22208 - it's possible that TypeScript is resolving a .ts input where it should have used a .d.ts from another compilation.

TS6059: File is not under 'rootDir'

This can happen under spawn_strategy=standalone (the default on Windows).

ts_project always sets the rootDir to the path where the BUILD file was located. This is because all sources should be within the Bazel package being compiled. If tsc is adding source files to the program outside of that folder, use the same tips above like --explainFiles to find out why tsc is including more files than it should.