-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[FEATURE] Add module_alias
resolution capabilities
#7185
[FEATURE] Add module_alias
resolution capabilities
#7185
Conversation
Hi @bradennapier! Looks like you've done very useful and highly-demanded task here! |
Hmm well theoretically yes it would work fine for a monorepo I do not see why not. You’d have to use a non relative “src” so it would bubble up in each repo to find the right root path for each but no reason it wouldn’t work great to solve that issue. I’d imagine if not then this at least provides the ground work for what would be needed to support it. I do have a monorepo I’ll test it on at some point when I have time. In the end this simply provides a mechanism to provide module-style resolved folders that act like normal app source instead of downloaded packages in flows eyes. With it you should be able to utilize a combination of this and name mapper to achieve most configurations I have seen. Hopefully the flow team sees this and agrees soon! For now I’m using it and it’s already done wonders for my programs. 😇 |
@bradennapier, sounds wonderful! No doubt in that!
Flow throws famous 'can not resolve module' error in every package. I definitely sure that you predict and avoid such issues in your PR, but it should be noticed though. Thank you! Can't wait for PR's being approved. |
Have you tried the following:
Not sure if the substitution is at all necessary but its how all mine are done :-P You could also try
which should resolve all directories in Try the BinaryIf you feel so inclined and use OSX just give it a try and let me know if it works ;-) https://github.com/bradennapier/flow-modules-bug-repro/tree/feature/fixed-by-pr-7185/bin the osx binary is in there. |
hi, @bradennapier! I've just tried a new binary in my lerna-managed monorepo with yarn workspaces enabled. and checked that i'm on new version (currenly i'm on 0.84 because of react-redux typings issue) All seems fine. And it definitely works in my "host" package As you can see, it provides path to module which, as I can assume, tells us that the module resolved. It does not provide neither module path, nor any module info.. But vscode plugin doesn't complain about it, so it probably works. To ensure that I ran PS. In order to simplify webpack config I use PPS. You set version=0.86 but such version already released and have issues with react-redux typings PPPS. Thank you for the great job you've done. Seems like all almost perfect ) |
Yeah I leave it out of the List which would define the alias as a “module” to Flow so it probably is the reason it won’t show up there as a module. It’s an alias so it’s simply resolving the given alias in a node_module way while preserving standard flow type checking that is removed for modules. Thanks for testing! Glad it works for your needs :) Edit: Just realized you mentioned it looks different in vscode in the two places but that it shows as module in one. I’ll have to look into that but it’s likely something with the vscode plugin. |
@jbrown215 any chance i can get a review on this PR? I think its a really solid feature and i miss it already since i updated to latest and havent yet built my version of it! I updated and fixed conflicts with newest version |
Hey @bradennapier: I really appreciate that you took the time to make this PR. Unfortunately, I'm not a good person to review it. I'll import it and see if anyone else can take a look, but I can't guarantee you someone will get to it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbrown215 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Thanks @jbrown215 ! Looks like build is failing with my conflict resolutions. If someone could review the concept and let me know they'd merge it I will absolutely fix it right away. I probably will anyway but would be nice to have someone with the proper authority or w/e approve the spec. |
I recommend waiting for someone to respond before you put more work into this. I don't want you to keep putting time into something that we may decide is something we don't want or cannot put in the time to support. |
Will do, thanks again! |
|
@jbrown215 Can you please review this as discussed on Twitter. |
I would note that this part of the codebase has changed a little bit so would need to adjust accordingly, but happy to look into that should the general concept of the feature be approved for further acceptance (which I would personally like to see). We do run into it on every project and it's fairly common practice to use |
I chatted briefly with @samwgoldman who had some concerns that this might cause problems with rechecks. I'm still looking into this potential issue and I'll have more to say on Monday. I'm optimistic that we can get this merged in some form in the near future. |
I spent some time investigating this today, and I think this should work out. If I'm missing something, hopefully @samwgoldman corrects me. Basically, the concern was that complicating module resolution in this way could lead us to miss rechecks. Specifically, if Fortunately, it looks to me like our logic for tracking this should generalize quite easily. In fact, I don't think this PR will need to do anything special for everything to fall into place. I could be misunderstanding something (this is my first time looking at this logic), but I'm optimistic that this can be merged shortly. @bradennapier, when you get a chance could you rebase this so that I can play around with it and make sure that everything behaves the way I expect? |
fd6bfa9
to
f4575b0
Compare
add tests for resolve_alias
f4575b0
to
15e9328
Compare
Alright so rebased and cleaned up the commits a bit here, confirmed build worked and the feature still operates as expected. There are a couple notes to make here on considerations. None of the behaviors discussed deviate from the general behaviors expected, but good notes to consider: Webpack Alias DeviationIn Webpack aliases you can do something like: resolve: [
'shared',
'node_modules',
'backup_modules'
] which would then resolve in the order provided if possible. However, in our scenario (which is the only way i have ever even considered using this feature, and the way i believe most people do use it), our custom aliases would also resolve before This is the desired behavior but worth mentioning. Good
|
Failure Case (Non Critical but Important)@see this commit where the highlighted value should not be an error since are essentially giving it an absolute path (after it is expanded from root) to look at. So the above example actually led me to finding a failure case in how this should work (and it also fails in the case of using Essentially it is not honoring relative paths properly. I also think it should properly handle Essentially at this time:
is resolving the same as:
and the following does not work at all (but should)
I'm guessing this is just a matter of using this func in one of the handlers using the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbrown215 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reviewed the module_js.ml part of this change only. @nmote please review the rest.
My main request is that you add a few tests around the incremental / recheck behavior. You can look at tests/incremental_path/
for an example which exercises the phantom_dependents
tracking behavior.
Yesterday I was concerned that the lazy_seq logic was buggy because we would not track the unvisited (due to laziness) paths. Now I think that it's actually OK due to precedence, but it would be nice to have at least one test to guarantee that.
I am especially curious about cases where there are (a) multiple resolver aliases (b) both resolver aliases and resolver dirnames, and those directories appear / disappear in a way that affects what a module reference resolves to.
It would also be nice if the issue with relative paths was addressed. I'm not thrilled with the prospect of merging a feature with known bugs.
As a parting note, we won't use this configuration at FB, which means that automated tests are particularly important to prevent regressions as this logic evolves. The "static" behavior is simple enough, but incremental logic is always complicated enough to warrant testing.
Hey, just to make sure that everyone is on the same page here: I'm planning to review the rest of the PR once @samwgoldman is satisfied with the part that he has started reviewing. |
I spoke with Sam and the only thing needed is to do the extra tests which I haven’t had time for due to moving across the country. Hope to get to it soon. |
Okay, no rush on our end, I just wanted to make sure you weren't waiting for me. |
@bradennapier are you available for this PR? |
Any updates on this PR? The current work-around is to have a messy flow config with a bunch of module name mappers, which becomes cumbersome. |
I haven't heard anything from @bradennapier in some time, so I'm going to close this. Anyone, feel free to ping me on a replacement PR if you are interested in driving this to completion. |
Hey Y'all!
So first of all, never used OCAML in my life so excuse any flaws in style. Mostly just tried to mimic styling I saw done elsewhere as I implemented this feature. I did add tests and it is definitely fixing the issues I have been having thus far!
So there are a ton of people that use things like
Webpack
,Rollup
, or something likebabel-plugin-module-resolver
(including Flow team recently I noticed ;-)).The Feature
This adds an option that is very similar to
module.system.node.resolve_dirname
but with slightly different handling. It is meant to be used in situations whereWebpack
'sresolve.modules
orbabel-plugin-module-resolver
(as well as multiple other bundlers which offer this type of functionality).It basically allows a separated place to define paths that should be aliased but type-checked normally since its actually part of the main codebase in this case.
In the case of something like the image below:
We can then do the following in
ComponentOne
orComponentTwo
:this would then mimic the
Webpack
configuration of:or the
babel-plugin-module-resolver
configuration of:The Problem This Fixes
The general idea is to remove the need to use relative paths everywhere and anywhere. At this point I can't even program without it! So essentially people will do a few possible things:
Have a Resolution to their Root
This way they can import from their root as modules (@see Webpack Documentation) (@see babel-plugin-module-resolver Documentation)
Use a customized styling for
node_modules
In my case, I generally use a
shared
folder and set it up to resolve the same asnode_modules
would. This is especially useful inReact
applications as it provides the ability for components to easily share pieces of code at any level.While the
resolve_dirname
kind of works better here (with the other example it breaks the code completely), the problem is that when you're building a file it won't work until its been imported fromoutside
a shared folder.https://github.com/bradennapier/flow-modules-bug-repro
This shows the issue when using it this way.
Why
resolve_dirname
doesn't work for thisThe issue with
resolve_dirname
is that doing this will generally break the entire application due to a change in0.57.0
This is extended to any defined
resolve_dirname
folders even though it's largely used for this purpose. We have since discovered that using a relative path can fix it but there are still lingering issues in these cases.Examples of People having issues that this solves
Just did some quick searches for examples
#5180
flow/flow-for-vscode#289
#4103
#5494
#5819
#5647
#5586
#5385
tleunen/babel-plugin-module-resolver#233
#5778
#5860
facebook/create-react-app#4105
#6966
tleunen/babel-plugin-module-resolver#235
As well as many more if you search through the various flow repos.
Testing on OSX
If you don't want to build the source and run OSX you can use the binary I built. There is a compiled binary for OSX located in the repro i linked within a branch: https://github.com/bradennapier/flow-modules-bug-repro/tree/feature/fixed-by-pr-7185/bin