-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Proposal: Allow paths
compilerOption without baseUrl
#31869
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
Comments
We have a horrifying workaround that intentionally prevents resolving bare-specifiers against the It involves setting an impossible directory as {
"compilerOptions": {
"baseUrl": "\u0000",
"paths": {
"foo": ["../../foo"]
}
}
} ... which works today and achieves the same thing as the proposed example ... {
"compilerOptions": {
"paths": {
"foo": ["../foo"]
}
}
} It would be great to be able to drop this workaround. |
I'm pretty confused about what's going on here (since there is no file named "foo"). Can you provide some more examples of what you'd want this to do? What is the proposed difference between what you want and just setting |
Thanks for taking a look @RyanCavanaugh
The presence of a "foo" file is not intended to be relevant to this example. We mentioned it to show that specifying
When we set Desired behavior:tsconfig: {
"compilerOptions": {
"paths": {
"foo": ["../foo"]
}
}
} file systen:
1.ts should error: import two from "2"; // Error: Cannot find module '2' 2.ts contents (not relevant): export default 2; Actual behavior when setting
|
To give a more concrete example of why fixing this is important: setting For example: your code might be doing It's ideal if the only things that can override |
In the meanwhile, I discovered another workaround:
The other workaround caused some problems for me with Webstorm (and I think other tools), which didn't like having a |
There’s one ambiguity that needs to be cleared up in order to allow dropping
The former value is a relative path, while the latter is an unrooted path. Currently, both of these are resolved to the same location, relative to In the absence of a |
Thanks for looking into this. Re the |
Good point; the framing of my question was node-specific, but I think my question still stands without making any node-specific assumptions. I shouldn’t have implied that if we see an unrooted path ( |
Thanks for explaining, I understand better now. Fwiw I have no intuition about which behavior would be more intuitive in that case. I do like the idea of erroring, so the user can more explicitly express intent, but of course I lack the background to know what accords best with the design principles for compiler options. |
I do not think it is useful for bare-specifiers in If they were instead passed to the resolver directly, it then raises the question of whether that resolution would include further path resolution. {
"paths": {
"first": ["second"],
"second": ["./foo"],
}
} Path resolution is a demon-haunted world of complexity. I would strongly err towards simplicity rather than completeness, and because I don't have a use-case for bare-specifiers here, I'd suggest erroring for now (as Max said). If someone wants the feature, it can always be added later. |
Doing more than one path substitution would make circularities possible, and I can’t think of a real use for it. I think that, at least, we would not do. |
I know this is being worked on, but one consideration I wanted to mention is how auto-imports are suggested. I converted a big project into a monorepo that uses these path specifiers, and only later noticed (when I was actually writing code) that VS Code was suggesting imports like Am I correct in assuming that Or do I need to request a mode that's "relative, but respect |
Yep! |
Wonderful. I hope this can make it to 4.1. If only this were in 4.0 so we could use it now... 😃 |
`paths` can be defined without `baseUrl` since microsoft/TypeScript#31869 which landed in 4.1: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#paths-without-baseurl
Related issue: #28321
Search Terms
baseUrl, paths, compilerOptions
Suggestion
Do not require 'baseUrl' in compilerOptions when 'paths' is provided.
Use Cases
We want to use 'paths' for type-checking, but don't want to resolve non-module relative names:
./1.ts
./2.ts
Examples
tsconfig
Actual behavior of
paths
withoutbaseUrl
:Error:
Option 'paths' cannot be used without specifying '--baseUrl' option
Expected behavior of
paths
withoutbaseUrl
:paths
are resolved relative to the project directory (which I think means the same thing as "where the tsconfig is")Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: