Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use absolute file paths in error messages #36221

Open
5 tasks done
remcohaszing opened this issue Jan 16, 2020 · 19 comments
Open
5 tasks done

Use absolute file paths in error messages #36221

remcohaszing opened this issue Jan 16, 2020 · 19 comments
Labels
Domain: Error Messages The issue relates to error messaging In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@remcohaszing
Copy link

remcohaszing commented Jan 16, 2020

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.
@DanielRosenwasser DanielRosenwasser changed the title Add option to output absolute file paths for tsc Use absolute file paths in error messages Jan 18, 2020
@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript Domain: Error Messages The issue relates to error messaging labels Jan 18, 2020
@rhyek
Copy link

rhyek commented May 25, 2021

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.

@younanjo
Copy link

This would be a fantastic change for anyone working with monorepos.

@michal-kocarek
Copy link

michal-kocarek commented May 2, 2022

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.

@arietrouw
Copy link

Super Thumbs up! I have a large mono repo using yarn3 and if there is an error in src/index.ts, clicking on it in vsCode just gives me a list of all the src/index.ts files in all the packages or takes me to the src/index.ts if that exists in the root package.
Neither of these are great and being able to click from a list of compile errors to the files where the errors occur improves development velocity substantially.

In a perfect world, tsc check the composite field in the tsconfig and if it is there, build a relative path back to the root (tsconfig without a composite field).

@austinpray-mixpanel
Copy link

austinpray-mixpanel commented Nov 29, 2022

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.

Yep, came here for this. The default problem matcher instrumented by https://github.com/actions/setup-node does not know how to show proper annotations since the paths are relative.

@austinpray-mixpanel
Copy link

austinpray-mixpanel commented Dec 7, 2022

If anyone is looking for a hack to get things working in GitHub actions:

tsc --pretty false | sed -E "s#^.+\(.+\): error TS.+\$#$PWD/\0#"

It just prepends $PWD to lines formatted like errors.

@qrohlf
Copy link

qrohlf commented Feb 15, 2023

It looks like this was actually the previous behavior of TSC at some point, interestingly enough: #7238

Agree that having this as an option would make using tsc with monorepos much easier.

@qrohlf
Copy link

qrohlf commented Feb 15, 2023

As far as I can tell, the main thing that would need to change is

const relativeFileName = convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName));

and

const relativeFileName = (fileName: string) => convertToRelativePath(fileName, program.getCurrentDirectory(), program.getCanonicalFileName);

In order to support absolute path outputs in build and watch mode.

@mizdra
Copy link

mizdra commented Feb 19, 2023

FYI: Combining pipe-if-ci with reviewdog will work around the issue of GitHub Annotations not displaying. I think this will help you.

@qrohlf
Copy link

qrohlf commented Feb 19, 2023

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 from tsc.

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.

@mizdra
Copy link

mizdra commented Feb 20, 2023

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

$ printf '\e]8;;file:///Users/mizdra/src/github.com/mizdra/eslint-interactive/src/index.ts\e\src/index.ts\e]8;;\e\e]8;;\e\e\e]n'
2023-02-21.0.07.04.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.

@remcohaszing
Copy link
Author

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.

@qrohlf
Copy link

qrohlf commented Feb 20, 2023

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 when tsc is used within a Github Actions workflow in a yarn monorepo.

@remcohaszing
Copy link
Author

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.:

tsc --project './packages/*' --project './examples/*'

@domdomegg
Copy link

domdomegg commented Mar 16, 2023

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:

  1. Run npm install --save-dev tsc-absolute.
  2. Replace tsc with tsc-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).

@gajus
Copy link

gajus commented May 6, 2023

Would a contribution for this change be accepted or are there considerations that have been not addressed?

@remcohaszing
Copy link
Author

This isn’t really a problem when you use project references, which you should when dealing with mono repos.

@michal-kocarek
Copy link

This isn’t really a problem when you use project references, which you should when dealing with mono repos.

This isn't helping for our projects, for example. We are using Rush.js, the Microsoft's monorepo management tool, and each project within a monorepo has its own tsconfig. Then this issue becomes really visible.

@Ainias
Copy link

Ainias commented May 13, 2024

I've created a patch-package to display the full path:

typescript+5.4.5.patch

diff --git a/node_modules/typescript/lib/tsc.js b/node_modules/typescript/lib/tsc.js
index f4e4fed..ea8bf64 100644
--- a/node_modules/typescript/lib/tsc.js
+++ b/node_modules/typescript/lib/tsc.js
@@ -5675,7 +5675,7 @@ function getRelativePathFromDirectory(fromDirectory, to, getCanonicalFileNameOrI
   return getPathFromPathComponents(pathComponents2);
 }
 function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) {
-  return !isRootedDiskPath(absoluteOrRelativePath) ? absoluteOrRelativePath : getRelativePathToDirectoryOrUrl(
+  return !isRootedDiskPath(absoluteOrRelativePath) ? absoluteOrRelativePath : basePath+"/"+getRelativePathToDirectoryOrUrl(
     basePath,
     absoluteOrRelativePath,
     basePath,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests