-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 of monorepo package not using node_modules specifier #51269
Comments
Just checking in to see if there's any updates here 🙂 Also reaching out because I'm wondering/hoping if anyone familiar with setting up TypeScript monorepos/project references could have a look at that sample repo and let us know if we're doing something fundamentally wrong in our setup. We're hitting lots of rough edges in our tooling up-and-down the stack since trying to move to project references, mostly by cobbling together info from various blog posts; my hunch is that this is on our end - surely lots of folks have a setup like this and it works better for them - so we'd be really grateful if someone could help point us on the right track. Thanks! |
Sorry, this slipped 4.9. I just took a quick look, and I don’t think it looks like you’re doing anything really wrong; I just see a couple of our own bugs that you pointed out. I did notice that the incorrect relative path bug goes away if you remove So there are two auto-import bugs here:
|
@andrewbranch one issue we are having... when auto-imports pick a correct relative path, but NOT the module path we want they both compile, but if these both get used, it's very dangerous as they are considered (rightfully) by TypeScript to be different modules. If it's just types and functions it's fine. If there is anything stateful in the module, then it breaks since now there are 2 different instances. as such, when the auto-import picks the relative path instead of the module, it causes bad problems. As such, we actually sort of WANT the auto-import to be broken if it's not the module one, so at least it's noticed to be wrong sooner. |
That’s what I labeled as the second bug above. But TypeScript should recognize those as the same module, actually. Assuming the path mapping points to the output file, there is code that recognizes the mapping between referenced project outputs and inputs. From TypeScript’s perspective, you should be able to point to either the outputs or the inputs or both, and by default, tsc’s source of truth will be the output |
@andrewbranch in our case, it definitely does not recognize them as the same module. |
I'll spent some time this weekend experimenting with those suggestions and figuring out why the mapping you're talking about isn't working for us. Thanks for all your help @andrewbranch! |
@bradedelman how are you determining that? Also, you can try the fix at #51492 in the meantime. @typescript-bot will comment with an npm-installable tarball shortly. |
Hm, #51492 isn’t quite right yet. I think it fixes your case but it lights up some other auto-imports that I think are undesirable. Let me ask this: why do you want to point your
|
Pushed an update, and it’s very hard to read intent from |
Thanks again, this is great. Haven't tried it yet but I will ASAP. As for why we're pointing
It does seem to be the source of at least a few of our problems. Is this nonsense? |
No, it’s not wrong, but it’s also not the only way to configure Webpack. You could forego |
I am determining it because if I have state, it fails.
For example, if I have an initialize function that creates state within the
module, it gets created, but it only works when the module is used via the
same path style. When used via the other path style, I crash because the
state has not been initialized.
…On Fri, Nov 11, 2022 at 1:56 PM Andrew Branch ***@***.***> wrote:
@bradedelman <https://github.com/bradedelman> how are you determining
that?
Also, you can try the fix at #51492
<#51492> in the meantime.
@typescript-bot <https://github.com/typescript-bot> will comment with an
npm-installable tarball shortly.
—
Reply to this email directly, view it on GitHub
<#51269 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA6LTZGOASH74BPCKRLQYDWH26JFANCNFSM6AAAAAARLSGFBY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@bradedelman I think some of the confusion here is that we're talking to the TypeScript team - the folks responsible for transpilation and language integration in VSCode - but the behaviour you're describing is a runtime behaviour that's under the purview of ts-node and/or webpack/babel-loader. From TypeScript's perspective, this mapping is working fine - that's how we've gotten debugging in VSCode, code search, etc. working; at runtime ts-node and (our very old version of) webpack aren't associating the compiled output and the original source files - I'm not sure if that's due to bugs on their end or misconfiguration on our part, but either way it might be out of scope here. @andrewbranch the fix in #51492 seems to work great! Do you figure that build is stable enough that we could use it while we wait for the change to hit official release? If not, how far off do you think we'll be waiting? In the mean time, we'll try to fix up the |
Exactly right 👍
So, once #51492 is merged, which should happen soon, it will be included in nightly |
Sure, here's a repo that has the issue.
This is based on a simplification of our own yarn monorepo. We've got a
common
project that contains stuff that's shared between frontend and backend (thebackend
project is omitted), and aweb
project that references it via a@common
alias. Each project has its owntsconfig.json
. I believe this is pretty similar to the example repo posted above. To see the problem, hop intoweb/src/Helper.ts
and try to use a "Quick Fix" to importsquare
from thecommon
repo.This example repo doesn't include any of this extra stuff because it suffices to show the issue, but for context, in our real repo
web
is a CRA-based Webpack app. We run it viayarn tsc --build --watch & react-app-rewired start
(we runtsc
separate becauseCRA
's setup doesn't understand building projects). We want to import the compiled code incommon/dist
via an alias (e.g.import { square } from '@common/MyModule'
).Our biggest issue is the one mentioned in the comment above - VSCode auto-import suggests relative paths to the original
.ts
file no matter what. There's a few issues here:.js
file, not the original.ts
fileWhat I mean by that last point is, if I try it with the "Import Module Specifier" preference set to "shortest", it works (although with a relative path, which is still undesirable):
But if I set "Import Module Specifier" to "non-relative", it curiously gives me:
Which is simply broken - an immediate compilation error.
It's totally possible - and would be great if! - this is just a configuration issue on our part and there's a different/better way to do it.
Thanks for your help!
Originally posted by @adamsmasher in #39778 (comment)
The text was updated successfully, but these errors were encountered: