question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to use composite project where outDir is determined by build tool

See original GitHub issue

In https://github.com/microsoft/TypeScript/issues/37257 I discussed one way to host TS project references under Bazel, where each build step gets only .d.ts and tsconfig.json files from the referenced projects. This is broken because TS module resolution fails to locate the .d.ts files in their outDir.

In this issue, let’s try a different approach for Bazel, to satisfy current TS module resolution, each build step should get the .ts sources of its referenced projects, and the .tsbuildinfo output as well.

Bazel is picky about the setting of --outDir, because it supports cross-compiled builds in distinct output locations. For example if you’re on a Mac and building for local platform, the outDir is <workspace root>/bazel-out/darwin-fastbuild/bin. But if you’re on a Mac and building for a docker image, you need native binaries to be for the target platform so outDir is <workspace root>/bazel-out/k8-fastbuild/bin and if you want binaries with debug information it’s <workspace root>/bazel-out/k8-dbg/bin.

Since the outDir is platform-specific, we don’t want to check in a config file that hard-codes it. So we prefer to always have Bazel pass the --outDir option on the command-line to tsc, rather than include it in the tsconfig files.

Now we come to the problem. TypeScript “composite” setting enables up-to-dateness checking in tsc. With this input structure, and trying to tsc -p b

a/a.ts
a/tsconfig.json (no outDir specified; a was compiled with tsc --outDir=bazel-out/darwin-fastbuild/bin/a)
b/b.ts
b/tsconfig.json
bazel-out/darwin-fastbuild/bin/a/a.d.ts
bazel-out/darwin-fastbuild/bin/a/tsconfig.tsbuildinfo

we get b/b.ts(1,17): error TS6305: Output file '/private/var/tmp/_bazel_alx/df60115ea7f2a64e10fb4aa64b7d827f/sandbox/darwin-sandbox/54/execroot/ts_composite/a/a.d.ts' has not been built from source file '/private/var/tmp/_bazel_alx/df60115ea7f2a64e10fb4aa64b7d827f/sandbox/darwin-sandbox/54/execroot/ts_composite/a/a.ts'.

This indicates that TS is looking for a.d.ts next to a.ts. If we hard-code the platform-dependent outDir into a/tsconfig.json like I’ve done here https://github.com/alexeagle/ts_composite/pull/5 it solves this problem, but not in a way we can ship.

Note that the compilation of a/tsconfig.json produces a .tsbuildinfo file with a correct “outDir” (based on the --outDir flag that was passed to compilation of a

 "options": {
      "project": "../../../../a/tsconfig.json",
      "outDir": "./",
  }

So it seems like the behavior for compiling b is to read the outDir setting from a/tsconfig.json rather than trust the options.outDir reflected in the .tsbuildinfo output.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:11
  • Comments:30 (28 by maintainers)

github_iconTop GitHub Comments

4reactions
alexeaglecommented, Jun 17, 2022

Update: we now have better Bazel rules which don’t require this at all. They copy all the sources and the node_modules tree to the bazel-out folder, then run tsc with that working directory, so there is a single root and TypeScript works properly as-is.

https://github.com/aspect-build/rules_ts

4reactions
alexeaglecommented, Mar 10, 2022

@RyanCavanaugh the PR is up. I think if you accept this issue that will clue the bot into putting nicer labels on it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Project references in TypeScript 3 with separate `outDir`
The official recommendation, if I understand it correctly, is to set up a master output directory for the entire composite project and have ......
Read more >
TSConfig Option: rootDir - TypeScript
If composite is set, the default is instead the directory containing the tsconfig.json file. When TypeScript compiles files, it keeps the same directory ......
Read more >
Change the build output directory - Visual Studio (Windows)
You can skip some projects if you have some exceptional projects that should not use the common output directory. To set the intermediate...
Read more >
A guide through The Wild Wild West of setting up a mono repo ...
We have to modify the tsconfig.json of our packages. By setting “composite” to true, we let TypeScript know we want to use project...
Read more >
Setting up TypeScript project with tsconfig.json - LogicBig
This tutorial shows how to build a TypeScript project with the help of ... Use to control the output directory structure with --outDir....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found