TypeScript: Use absolute file paths in error messages
Search Terms
absolute
Suggestion
Output absolute paths in type errors for tsc
, or at least a way to specify this.
Use Cases
When type checking in monorepositories, typically tsc
is run for each workspace. tsc
is then run relative to the workspace it is run from, whereas developers are mostly interested in either the absolute path, or the path relative to the project root.
I have configured my terminal emulator so I can CTRL-click a file and then the file is opened in VSCode, even on the correct line. This works for tsc
output in simple TypeScript projects, because the file paths are relative to the project root. This also works for example for ESLint output, because it prints absolute paths. This does not work for tsc
in mono repositories, because the output of yarn workspaces run tsc
is relative to that workspace.
Examples
Clone and install a TypeScript mono repository.
git clone https://gitlab.com/remcohaszing/koas.git
cd koas
yarn
Let’s open the project using VSCode, since its builtin terminal is preconfigured to work with CTRL-click.
Run yarn workspaces run tsc
in the VSCode terminal. This should work fine.
Now mess up any type. I changed an instance of koas.Middleware
to koas.Middlewar
in packages/koas-core/src/index.ts
.
Run yarn workspaces run tsc
in the VSCode terminal again. Now this outputs an error and a file path. The file path is not clickable, because it can’t be resolved.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 42
- Comments: 18
Commits related to this issue
- github actions cannot get the files anyway https://github.com/microsoft/TypeScript/issues/36221 — committed to zetavg/yarn-workspace-test by zetavg 5 months ago
- github actions cannot get the files anyway https://github.com/microsoft/TypeScript/issues/36221 — committed to zetavg/yarn-workspace-test by zetavg 5 months ago
I’ve released a very simple drop-in replacement tsc wrapper, tsc-absolute, that fixes this problem. It’s potentially preferable to other solutions (e.g. piping through awk/sed) as it generally keeps your npm scripts simpler, is portable across platforms, and ensures things like exit codes and signals are handled correctly. To install, it’s as simple as:
npm install --save-dev tsc-absolute
.tsc
withtsc-absolute
in your scripts.This is really useful in monorepo setups for developer experience. It allows you to click to files in the terminal reliably, as well as match on filenames in CI.
The README also includes a recommended GitHub problem matcher configuration, that I think catches the logs better (and supports the kinds of logs generated by monorepo tools like npm workspaces / yarn / nx / turborepo).
This is issue that exhibits for monorepos in IDEs, however also automatic recognition of file errors in GitHub Actions. Having the ability to switch to absolute paths… Either by default, through TSConfig, or through environment variable, would be great thing.
I have also though about this. At first this seems like the way to go (for any CLI tool that outputs file paths). However, TypeScript doesn’t just output file paths. It outputs
path:line:column
. E.g.../index.ts:123:42
. Various tools, such as VSCode, can handle this format to open text files on the specified line and column.I work with monorepos and vscode almost exclusively. I can never click to navigate to the errored file directly when working with TypeScript. This option would help productivity in my and similar use-cases.
The primary use-case for myself and several other users in this thread is having support for Github Actions’ problemMatcher functionality, which uses a regex to extract error file paths, severity levels, and messages from CI runs and display them inline in Pull Request diffs. Extracted file paths must either be absolute, or relative to where the root of the repository is checked out within the CI environment.
The link solution @mizdra suggested is an elegant solution for the local development, but most likely would not work in a portable or intuitive way with CI (for example, you’d probably have to write your own forked problemMatcher regex for filepath extraction rather than just being able to leverage the built-in functionality from
actions/setup-node
, and even that assumes that the problemMatchers run on console output before it’s been stripped of escape sequences).Adding absolute path output in tsc error messages, combined with the work I’ve done in yarn@v3.4.0 to enable unprefixed output, would give
tsc
users automatic error recognition whentsc
is used within a Github Actions workflow in a yarn monorepo.Other ideas to solve the problem:
How about outputting relative paths that are hyperlinked to absolute paths instead of absolute paths? That is, have tsc output logs like this
https://user-images.githubusercontent.com/9639995/220143165-5147572d-caf7-4c2d-94ac-389dc5d10342.mov
Many modern terminals seem to support this feature.
I think this approach would provide clickable paths without annoying the user with the length of the paths output.
I’ve got my own, similar, wrapper script to
tsc
that we’re using at our organization as a workaround. However, it’s becoming such a common use case that I would love it if the Typescript team could weigh in here. Previously, the team’s position was that they did not plan to allow for an additional option for absolute path output fromtsc
.Given it has been almost 7 years since that issue was closed and JavaScript development practices & tooling have undergone a significant shift towards monorepo-style development patterns, I would love to see this decision revisited.
FYI: Combining pipe-if-ci with reviewdog will work around the issue of GitHub Annotations not displaying. I think this will help you.
Another possibility would be to add mono-repo to TypeScript itself. If it’s possible to type check multiple projects in one command, not only could TypeScript print relative paths properly, but it could share the document registry internally, which would enhance a performance. E.g.:
If anyone is looking for a hack to get things working in GitHub actions:
It just prepends $PWD to lines formatted like errors.