Repository: webpack/webpack (JavaScript)

Issue:
Module federation warns "Unable to find required version" when using package self referencing
<!-- Please don't delete this template because we'll close your issue -->
<!-- Before creating an issue please make sure you are using the latest version of webpack. -->

# Bug report

<!-- Please ask questions on StackOverflow or the webpack Gitter. -->
<!-- https://stackoverflow.com/questions/ask?tags=webpack -->
<!-- https://gitter.im/webpack/webpack -->
<!-- Issues which contain questions or support requests will be closed. -->

**What is the current behavior?**

In an esm package, I have two modules, one imports another by [self referencing](https://nodejs.org/api/packages.html#packages_self_referencing_a_package_using_its_name)

```json
{
  "name": "my-pkg",
  "type": "module",
  "exports": {
    "./module-a": "./module-a.js",
    "./module-b": "./module-b.js"
  }
}
```

```js
// module-a.js
export function a() {}
```

```js
// module-b.js
import { a } from "my-pkg/module-a";

export function b() {
  return a();
}
```

In another app, I use module federation and share the two modules:

```js
// Partial webpack.config.js
new ModuleFederationPlugin({
  shared: {
    "my-pkg/module-a": {
      singleton: true,
      version: "1.0.0"
    },
    "my-pkg/module-b": {
      singleton: true,
      version: "1.0.0"
    },
  }
});
```

When I start the dev-server, it reports a warning of:

```
WARNING in shared module my-pkg/module-a
No required version specified and unable to automatically determine one. Unable to find required version for "my-pkg" in description file (.../packages/my-pkg/package.json). It need to be in dependencies, devDependencies or peerDependencies.
```

And according to the warning, I added the package itself as a dependency, the warning disappears:

```json
{
  "name": "my-pkg",
  "type": "module",
  "exports": {
    "./module-a": "./module-a.js",
    "./module-b": "./module-b.js"
  },
  "dependencies": {
    "my-pkg": "*" // It's weird to do this...
  }
}
```

**If the current behavior is a bug, please provide the steps to reproduce.**

It's a bit complex in my real repo, I can provide a minimal repro if you require, when I got time.

<!-- A great way to do this is to provide your configuration via a GitHub repository -->
<!-- The most helpful is a minimal reproduction with instructions on how to reproduce -->
<!-- Repositories with too many files or large `webpack.config.js` files are not suitable -->
<!-- Please only add small code snippets directly into this issue -->
<!-- https://gist.github.com is a good place for longer code snippets -->
<!-- If your issue is caused by a plugin or loader, please create an issue on the loader/plugin repository instead -->

**What is the expected behavior?**

No warnings. It should use the description file (`my-pkg/package.json`) itself to determine the required version, since it's a self referencing.

<!-- "It should work" is not a helpful explanation -->
<!-- Explain exactly how it should behave -->

**Other relevant information:**
webpack version:  5.75.0
Node.js version: 16.15.0
Operating System: MacOS
Additional tools:

Module federation warns "Unable to find required version" when using package self referencing
<!-- Please don't delete this template because we'll close your issue -->
<!-- Before creating an issue please make sure you are using the latest version of webpack. -->

# Bug report

<!-- Please ask questions on StackOverflow or the webpack Gitter. -->
<!-- https://stackoverflow.com/questions/ask?tags=webpack -->
<!-- https://gitter.im/webpack/webpack -->
<!-- Issues which contain questions or support requests will be closed. -->

**What is the current behavior?**

In an esm package, I have two modules, one imports another by [self referencing](https://nodejs.org/api/packages.html#packages_self_referencing_a_package_using_its_name)

```json
{
  "name": "my-pkg",
  "type": "module",
  "exports": {
    "./module-a": "./module-a.js",
    "./module-b": "./module-b.js"
  }
}
```

```js
// module-a.js
export function a() {}
```

```js
// module-b.js
import { a } from "my-pkg/module-a";

export function b() {
  return a();
}
```

In another app, I use module federation and share the two modules:

```js
// Partial webpack.config.js
new ModuleFederationPlugin({
  shared: {
    "my-pkg/module-a": {
      singleton: true,
      version: "1.0.0"
    },
    "my-pkg/module-b": {
      singleton: true,
      version: "1.0.0"
    },
  }
});
```

When I start the dev-server, it reports a warning of:

```
WARNING in shared module my-pkg/module-a
No required version specified and unable to automatically determine one. Unable to find required version for "my-pkg" in description file (.../packages/my-pkg/package.json). It need to be in dependencies, devDependencies or peerDependencies.
```

And according to the warning, I added the package itself as a dependency, the warning disappears:

```json
{
  "name": "my-pkg",
  "type": "module",
  "exports": {
    "./module-a": "./module-a.js",
    "./module-b": "./module-b.js"
  },
  "dependencies": {
    "my-pkg": "*" // It's weird to do this...
  }
}
```

**If the current behavior is a bug, please provide the steps to reproduce.**

It's a bit complex in my real repo, I can provide a minimal repro if you require, when I got time.

<!-- A great way to do this is to provide your configuration via a GitHub repository -->
<!-- The most helpful is a minimal reproduction with instructions on how to reproduce -->
<!-- Repositories with too many files or large `webpack.config.js` files are not suitable -->
<!-- Please only add small code snippets directly into this issue -->
<!-- https://gist.github.com is a good place for longer code snippets -->
<!-- If your issue is caused by a plugin or loader, please create an issue on the loader/plugin repository instead -->

**What is the expected behavior?**

No warnings. It should use the description file (`my-pkg/package.json`) itself to determine the required version, since it's a self referencing.

<!-- "It should work" is not a helpful explanation -->
<!-- Explain exactly how it should behave -->

**Other relevant information:**
webpack version:  5.75.0
Node.js version: 16.15.0
Operating System: MacOS
Additional tools:



Hints:

Patch:
diff --git a/lib/sharing/ConsumeSharedPlugin.js b/lib/sharing/ConsumeSharedPlugin.js
--- a/lib/sharing/ConsumeSharedPlugin.js
+++ b/lib/sharing/ConsumeSharedPlugin.js
@@ -224,6 +224,10 @@ class ConsumeSharedPlugin {
 										);
 										return resolve();
 									}
+									if (data.name === packageName) {
+										// Package self-referencing
+										return resolve();
+									}
 									const requiredVersion = getRequiredVersionFromDescriptionFile(
 										data,
 										packageName


Test patch:
diff --git a/test/configCases/sharing/consume-self-reference/index.js b/test/configCases/sharing/consume-self-reference/index.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/index.js
@@ -0,0 +1,4 @@
+it("should be able to consume package self referencing", async () => {
+	const result = await import("my-middleware");
+	expect(result.m()).toBe("ABA");
+});
diff --git a/test/configCases/sharing/consume-self-reference/node_modules/my-middleware/index.js b/test/configCases/sharing/consume-self-reference/node_modules/my-middleware/index.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/node_modules/my-middleware/index.js
@@ -0,0 +1,6 @@
+import { a } from "my-module/a";
+import { b } from "my-module/b";
+
+export function m() {
+	return a() + b();
+}
diff --git a/test/configCases/sharing/consume-self-reference/node_modules/my-middleware/package.json b/test/configCases/sharing/consume-self-reference/node_modules/my-middleware/package.json
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/node_modules/my-middleware/package.json
@@ -0,0 +1,8 @@
+{
+  "name": "my-middleware",
+  "type": "module",
+  "version": "2.3.4",
+  "dependencies": {
+    "my-module": "*"
+  }
+}
diff --git a/test/configCases/sharing/consume-self-reference/node_modules/my-module/a.js b/test/configCases/sharing/consume-self-reference/node_modules/my-module/a.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/node_modules/my-module/a.js
@@ -0,0 +1,3 @@
+export function a() {
+	return "A";
+}
diff --git a/test/configCases/sharing/consume-self-reference/node_modules/my-module/b.js b/test/configCases/sharing/consume-self-reference/node_modules/my-module/b.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/node_modules/my-module/b.js
@@ -0,0 +1,5 @@
+import { a } from "my-module/a";
+
+export function b() {
+	return "B" + a();
+}
diff --git a/test/configCases/sharing/consume-self-reference/node_modules/my-module/package.json b/test/configCases/sharing/consume-self-reference/node_modules/my-module/package.json
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/node_modules/my-module/package.json
@@ -0,0 +1,9 @@
+{
+  "name": "my-module",
+  "type": "module",
+  "version": "1.2.3",
+  "exports": {
+    "./a": "./a.js",
+    "./b": "./b.js"
+  }
+}
diff --git a/test/configCases/sharing/consume-self-reference/package.json b/test/configCases/sharing/consume-self-reference/package.json
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/package.json
@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "my-middleware": "^2.3.0"
+  }
+}
diff --git a/test/configCases/sharing/consume-self-reference/webpack.config.js b/test/configCases/sharing/consume-self-reference/webpack.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/sharing/consume-self-reference/webpack.config.js
@@ -0,0 +1,26 @@
+// eslint-disable-next-line node/no-unpublished-require
+const { SharePlugin } = require("../../../../").sharing;
+
+/** @type {import("../../../../").Configuration} */
+module.exports = {
+	plugins: [
+		new SharePlugin({
+			shared: {
+				"my-middleware": {
+					singleton: true
+					// import: false
+				},
+				"my-module/a": {
+					singleton: true,
+					version: "1.2.3"
+					// import: false
+				},
+				"my-module/b": {
+					singleton: true,
+					version: "1.2.3"
+					// import: false
+				}
+			}
+		})
+	]
+};