-
Notifications
You must be signed in to change notification settings - Fork 12k
Bad absolute paths in TS auto imports due to new baseUrl #5875
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
and that's bad practise because you're tying yourself to the file system |
To me, the relative import seems totally normal if the file is considered local to the project. If it's considered as a global ressource and imported in a absolute way, should not it be registered somewhere like Angular modules (like |
We've actually had a fair amount of requests for this in the past. As codebases grow bigger, relative imports get more and more inconvenient. I'll leave the issue open for discussion though. |
Can you explain why relative imports are inconvenient ? Relative imports is the convention I've always seen in TypeScript until now, and if project global directories (like "/app") are renamed, relative imports will still work, while all absolute imports won't. |
In that specific example if you rename Here are some issues in the past where the topic was brought up and discussed: #865, #1465, #2747. |
I see that there are multiple discussions related to this issue so I just want to get confirmation. Is absolute paths possible in angular cli just by changing the tsconfig.app.json? I have tried that but still the error 'Module not found: Error: Recursion in resolving' shows up. Since my project files and feature modules are increasing, relative paths are not an option anymore. I would like to be able to use the 'baseUrl' and 'paths' configuration in tsconfig but it turns out this only works for the typescript intellisense. |
@Nehmiabm should work, we test for it in https://github.com/angular/angular-cli/blob/master/tests/e2e/tests/build/ts-paths.ts |
For anyone wanting to get rid of unwieldy long relative paths my solution was to add alias paths in root tsconfig.json
then the imports would look like:
|
@filipesilva you were originally against relative paths as you said: |
It was mostly this thread and @cyrilletuzi's arguments. It was unintended that the editor auto imports would be absolute. This was also a problem for e2e test auto imports, since they shouldn't be based on #6861 fixed that by moving the baseUrl to each individual tsconfig. That way they can still be used, just aren't forced. Are you seeing any problems with the change? |
I guess with e2e the baseUrl doesn't make sense at all. I wonder if there's a way to tell VSC to use I think the actual fix would be for TS to support the ability to match configs based on file paths so you could have a single |
Ugh, that is a bad side effect indeed... but at least it's a side effect that doesn't happen automatically. Yeah I don't think we currently have a solution that really covers everything. |
Is there currently a solution for using absolute paths? In version Absolute paths are really nice for refactoring when moving components or services around because the path to a module is the same no matter where in the app it is being used. I can't tell from this discussion or from the related issues and PRs whether or not there is a way to accomplish this still. If there isn't and it's not going to be a support feature that's ok, I'd just like to know. Update-- If you set the root |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Versions.
cli 1.0.0 (since rc.2)
typescript 2.2.2 (which is installed by default by the cli)
Repro steps.
typescript 2.2 introduced quickfixes for auto importing missing classes (in VS Code, a class not imported will be underlined in red, you can click on it, then on the bulb, and it would add the import).
with angular-cli rc.1, it would generate relative imports (the default behavior) :
import { SomeService } from './some.service';
since angular-cli rc.2, as a
"baseUrl": "src"
has been introduced in the root tsconfig.json, the auto imports are now generated with absolute paths :import { SomeService } from 'app/feature/some.service';
And, unless I'm wrong, it's bad practice (in particular for refactoring).
@intellix says in #5353
baseUrl
was introduced because :But the CLI doesn't generate that, it generates this in
main.ts
:import { environment } from './environments/environment';
which is the convention and which works perfectly fine, without
baseUrl
.So except I'm missing something, this change should be reversed (I can do the PR if it's OK).
The text was updated successfully, but these errors were encountered: