Skip to content

feat(enhanced): Recursive search for versions of shared dependencies #3078

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

Conversation

barabaiiika
Copy link
Contributor

Description

Improved recursive version search of shared dependencies for cases where there is a parent package.json containing the required version. This is specific to monorepos and dual packages(esm/cjs), where the closest package.json is only used to specify the package type.

There is already an open pull request that solves this #2681, but it has a recursive call to getDescriptionFile, which already calls itself recursively. I just added an extra check that the package.json found contains a version description.

Related Issue

#2680
webpack/webpack#13457
This is also mentioned in the discussion #2917

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Sorry, something went wrong.

Verified

This commit was signed with the committer’s verified signature.
esbenbjerre Esben Bjerre
Copy link

changeset-bot bot commented Oct 14, 2024

🦋 Changeset detected

Latest commit: 32478b9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@module-federation/enhanced Patch
@module-federation/modern-js Patch
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/rsbuild-plugin Patch
@module-federation/storybook-addon Patch
@module-federation/modernjsapp Patch
@module-federation/runtime Patch
@module-federation/rspack Patch
@module-federation/webpack-bundler-runtime Patch
@module-federation/sdk Patch
@module-federation/runtime-tools Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/dts-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/devtools Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/retry-plugin Patch
@module-federation/data-prefetch Patch
@module-federation/error-codes Patch
@module-federation/esbuild Patch
@module-federation/utilities Patch
website-new Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Oct 14, 2024

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 32478b9
🔍 Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/672c008560a3dd00087336c3
😎 Deploy Preview https://deploy-preview-3078--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -259,15 +258,17 @@ class ConsumeSharedPlugin {
data,
packageName,
);
if (typeof requiredVersion !== 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should still keep the required version warning if the recursive lookup doesnt return the package for whatever reason.

What happens if someone has a package.json outside their app project, like in the root of their FS? This will crawl upwards across the whole fs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recursive lookup will stop under the following condition:

if (!parentDirectory || parentDirectory === directory) {

and this warning will be registered
`Unable to find description file in ${context}.`,

but we can actually reach the root of fs. Maybe we should stop searching at some upwards?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, maybe try with compiler.context as the edge boundary? Im not sure if that will work in your use case, but it probbably will be a good idea to somehow prevent it from searching the entire fs all the way up to root if it doesnt find something. Like keep it from leaving the current repo, usually i use compiler.context to find the boundary of the compiler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any ideas yet on how to limit the search tree upwards😔 compiler.context will help with dual-packages, but for monorepos search should go up to the monorepo package.json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Okay well I think we can run with this. I'll review and merge. Then will port to rust

const maybeRequiredVersion =
getRequiredVersionFromDescriptionFile(data, packageName);
return (
data['name'] === packageName ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if no version warning should still get logged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same warning will be registered

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but i see this messge removed

Unable to find required version for "${packageName}" in description file (${descriptionPath}). It need to be in dependencies, devDependencies or peerDependencies.,
);

                will same message report or does it fall back to unable to find description file only? 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I thought about the self-referencing warning that doesn't get registered.

Maybe accumulate warnings for all description file names (descriptionPath ) visited during recursive lookup to create a summary warning?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, if you want to update the PR to do so, feel free. Otherwise just make sure that it reports back that warning if it comes up dry, rather than removing it completely. Its useful to tell dev they should list the package in their deps etc if it cant find one, especially now that it can recurse upwards

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added accumulation of checked files to the warning if the version is not found in them. I think the output looks clearer now. But the implementation looks a bit ugly 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. I'll pull locally and touch it up a little then merge

@ScriptedAlchemy
Copy link
Member

@barabaiiika i have updated this PR, please double check it on your end

@ScriptedAlchemy ScriptedAlchemy force-pushed the feat/search_package_json_for_dual_mode branch from e46e637 to c0ef1cd Compare October 31, 2024 18:26
// Create an object to hold the function and the checkedFilePaths
const satisfiesDescriptionFileDataInternal = {
check: satisfiesDescriptionFileData,
checkedFilePaths: new Set<string>(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ScriptedAlchemy but this will create a new Set instance for each getDescriptionFile call and only the filepaths from the last instance will be passed to the callback?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay i added it as a default to the function, then pass the same set recursively down.

@@ -367,16 +369,15 @@ const getDescriptionFile = (
return callback(
null,
undefined,
satisfiesDescriptionFileDataInternal?.checkedFilePaths &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling code relies on the fact that checkedDescriptionFilePaths may be undefined:

if (checkedDescriptionFilePaths) {

maybe it's worth adding a length check to the checkedDescriptionFilePaths array now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barabaiiika could you help me update this part, dont fully understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a check on the length of the array of checked file paths to select the appropriate warning.

@ScriptedAlchemy
Copy link
Member

@barabaiiika kind bump ✨️

@barabaiiika
Copy link
Contributor Author

@barabaiiika kind bump ✨️

Sorry, I've been busy the last few days. I'll start today👌

@ScriptedAlchemy
Copy link
Member

Thank you!

@@ -241,7 +241,7 @@ class ConsumeSharedPlugin {
}
const { data } = /** @type {DescriptionFile} */ result || {};
if (!data) {
if (checkedDescriptionFilePaths) {
if (checkedDescriptionFilePaths?.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, i feel stupid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay, it's my mistake - I didn't explain it well☺️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge this tomorrow. Then get it synced with rspack and I've already opened the pull request to webpack core as well. Appreciate the contribution. This has definitely been long overdue

@ScriptedAlchemy ScriptedAlchemy merged commit 47fdbc2 into module-federation:main Nov 7, 2024
14 checks passed
@barabaiiika barabaiiika deleted the feat/search_package_json_for_dual_mode branch November 7, 2024 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants