-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
auto-import favors import from target instead of source #38856
Comments
@chengB12 can you provide a repository with this problem? I think some important information got lost or mangled when you were distilling it down to a simple case. E.g., I think you’d need in your paths |
Hi, I changed in paths, still same issue I have created a sample project, |
@chengB12 thanks! This is helpful. I do see that an auto-import isn’t offered for Also, in the non-example version of this, are you using Lerna, yarn workspaces, or any other method of symlinking the packages together within node_modules? |
Here is something interesting, I can't reproduce it with bare minimal our original project having tons of export on ts-sharedlib, and our package1 has tons of import from ts-sharedlib. in this case vs code with ts 3.8 can infer from 'ts-sharedlib/src' and ts 3.9 can infer from 'ts-sharedlib/dist/src' however, when I trying to strip all other files from package1, leaving only one example, and suddenly, neither ts 3.8 nor ts 3.9 can infer import anymore. It sounds like vs code can be trained to auto import cross packages given enough success imports either way, it sounds like a bug to me if bare minimal can't infer imports |
Sorry I can't provide whole 'issue-reproducible' repo, if needed, I may be able to provide a few screenshots |
When you remove all existing references from But I don’t think that’s a bug. |
It’s not just the file that matters, though; it’s every file in the project and every file’s dependencies, those dependencies’ dependencies, and so on 😕. Would you be able to share TS Server logs? |
We have lint to ensure no code use import xx from 'ts-sharedlib/dist/src...' |
Alright! I think I have a repro thanks to your logs. Hopefully last question @chengB12, when you click the lightbulb here, do you get an option for both |
No, I only get hint from 'sharedlib/dist' |
Even if you add |
with "ts-sharedlib/": ["../ts-shardlib/"] it display for both now, what's different between it and "ts-sharedlib": ["../ts-shardlib"]? |
Are you using Lerna or yarn workspaces or something else that will symlink these packages together through The Regarding the relative import, can you check your VS Code setting for |
after change vs setting, it looks fine now, regards how to resolve ts-sharedlib, I don't know, we don't use symlink. we have this line in tsconfig.json though
probably this line do the trick? |
Nah, that line is correct. I think you just need to keep your path mapping with the wildcard, and I’ll fix the bug that makes the extra suggestion from |
How do imports from |
yes, we are publishing ts-sharedlib to ts-sharedlib/dist |
I believe this is caused by outDir -- see https://github.com/microsoft/vscode/issues/103087 -- I also have a repro to show that import will ONLY provide the Repro: https://github.com/bradennapier/typescript-auto-import-bug Note that adding |
What this issue is complaining about is exactly the behavior you would want, since the Auto import suggestions started showing up for me (maybe because of I don't see why this setting should affect proposed imports. The suggested imports should always be to the transpiled output, with |
The problem is target won't exists when inside development, before compiling, and who need auto-imports outside development, I am curious. Force compiling, otherwise, intelli-sense won't work properly doesn't sound right |
Yes literally the last thing you'd ever want is auto imports to try to import the eventual dist path because you're in the dev path - it should go to the relative (or absolute if there is a paths config) of the dev source so that once compiled the compilation step can either modify properly or do nothing if it is relative and will thus remain correct. There is not a single case where you would want to import a dist resource from your src resource. That's insane. |
@bradennapier let’s keep the conversation less shouty and incredulous. The problem is that you can invent scenarios where any of these paths do or don’t work depending on exactly what you package/deploy/run, and the compiler simply cannot know that information today. There is definitely a bug here, but anyone here saying words like “always” and “never” is making assumptions that don’t hold for all configurations and all runtime circumstances that aren’t reflected in configurations. |
My bad, didn't mean to imply shouting on the caps, just emphasis. Updated accordingly. |
Just a question - in what case would you want your files in the This recently started affecting our larger project for some reason, before it was only on a single package of ours that this was happening. Now ALL auto imports are essentially useless because they are defaulting to the If i have a
I would think it would realize that it should use I have my editor settings to do absolute specifically but that doesn't seem to fix anything. In this case the dist folder doesn't even exist either. I have added the You don't realize how much you have come to depend on a feature until it stops working :-P Interestingly, in VSCode, if I look at the |
Where importing from
In each, the Now, suppose you’re trying to import something from Your screenshot shows a relative import path to
It just doesn’t work like that, in this particular case. The reason we look in It seems like you’re concerned that we’re not going to fix this or something, but that’s not the case; I’m working on it 🤷 What would be tremendously enlightening to me, if not directly helpful with this issue, is if you could explain why you’re using |
Thanks @andrewbranch not worried it won't be fixed, was just curious for real cause I was trying to think of a case :-P, thanks for the descriptive response though! The paths are used to remove relative imports, which are extremely painful since our repo is quite massive, and is a fairly common request from people (there are lots of issues around this). It is a common pattern with I accomplish this in typescript by adding https://github.com/zerkalica/zerollup as a plugin, but this issue would occur for anyone using webpack or rollup as example as well, or https://github.com/dividab/tsconfig-paths -- but there are probably around 10-12 packages all to deal with these cases. eslint plugins also exist specifically for this https://github.com/alexgorbatchev/eslint-import-resolver-typescript the transformer plugin is the ideal because it just rewrites the paths to relative at compile time so it all matches up and there is no runtime hit. lerna would be nice here... unfortunately i was not apart of the original decision when the repo was made and that is quite difficult change atm. we use lerna in a few of the other repos that were made more recently ;-)
|
@andrewbranch I believe I'm still running into this, even with the current TS version: Other suggestions are all kinds of weird:
Tbh, I haven't figured out a good way to import sources from the current project with a relative path and from referenced projects using their absolute path. Sometimes it works, sometimes it doesn't 🤷♂️ |
@AlCalzone for me, I solve this issue by
{
paths: {
"zwave-js/src": [ "../zwave-js/src/*" ]
}
} |
Imports that refer to the target location are working for me, even when the source has not been built and the target location does not exist. And of course such imports work as expected at runtime. This works with just project references, without any need to configure a So a source file can import |
I do have paths configured, because the directory structure is a bit different than the package names that are released later on. My first problem should is not affected by paths though, since it is an "internal" import. The 2nd one does go away once there is one import in the current file that points to the correct location. When it is not, I think this heuristic makes sense:
|
from comment of #40253 this is fixed by checking paths first to look up matches, so changing paths to make what you want it to be recommended in auto-import works for me |
@AlCalzone can you confirm which nightly you’re using?
It’s affected by That said, I want to keep this thread focused on the original problem—there’s a lot of other auto-import work to be done in this release, and it will get harder for me to track if all the different issues become about auto-imports broadly. The only thing relevant to this issue is the fact that you got the |
20200909
That would be lovely!
Yep :)
|
@AlCalzone something in your build produces a bunch of |
Thanks, that explains it. In my case it is intentional, because it simulates Node.js's sub-module exports. |
In that case, you probably just need to tweak your tsconfig.json include/exclude to make sure the editor doesn’t see those files. |
@andrewbranch That seems to work :) |
TypeScript Version: 4.0.0-dev.20200529
Verify no issue with vs code version 1.45.1 + typescript vsrion: 3.8
Verify issue with vs code + typescript 3.9.3
still have issue with vs code + typescript 4.0.0-dev20200529
Search Terms:
import / auto import
Code
we have project configure as
where src is *.ts source code, dist is where compiled *.js files
sharedlib/tsconfig.json
package1/tsconfig.json
sharedlib/src/foo.ts
package1/src/bar.ts
Expected behavior:
When in visual studio code + typescript 3.8
auto import will prompt me to import as
Actual behavior:
On typescript 3.9.3+
first, before compile sharedlib, it can't recognize foo, thus no auto import
after compile, it auto import the js from
the problem are
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: