Repository: node
Issue: Namespaces are not disambiguated by star exports
Labels: confirmed-bug,v8 engine,esm,loaders

### Version

v22.4.0

### Platform

```text
Linux silica 6.8.0-36-generic #36-Ubuntu SMP PREEMPT_DYNAMIC Mon Jun 10 10:49:14 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
```


### Subsystem

loaders

### What steps will reproduce the bug?

This set of files reproduces. Execute `entry.mjs` to observe.

```js
// substance.mjs
export const value = 'Curiouser and curiouser.'
```

```js
// relay1.mjs
export * as substance from './substance.mjs'
```

```js
// relay2.mjs
export * as substance from './substance.mjs'
```

```js
// aggregate.mjs
export * from './relay1.mjs'
export * from './relay2.mjs'
```

```js
// entry.mjs
import { substance } from './aggregate.mjs'
console.log(substance)

// Firefox:
// Module { value: 'Curiouser and curiouser.' }

// Node.js:
// SyntaxError: The requested module './aggregate.mjs'
// contains conflicting star exports for name 'substance'
```

This line embeds the above files in a graph of data URLs to reproduce in a snippet. Firefox also succeeds on this one.

```console
node --import 'data:text/javascript;charset=utf-8;base64,aW1wb3J0IHsgc3Vic3RhbmNlIH0gZnJvbSAnZGF0YTp0ZXh0L2phdmFzY3JpcHQ7Y2hhcnNldD11dGYtODtiYXNlNjQsWlhod2IzSjBJQ29nWm5KdmJTQW5aR0YwWVRwMFpYaDBMMnBoZG1GelkzSnBjSFE3WTJoaGNuTmxkRDExZEdZdE9EdGlZWE5sTmpRc1RIbHdlVnBYZUdobFZFVnhUREpXTkdOSE9YbGtRMEZ4U1VkR2VrbElUakZaYms0d1dWYzFhbHBUUW0xamJUbDBTVU5rYTFsWVVtaFBibEpzWlVoUmRtRnRSakpaV0U1cVkyMXNkMlJFZEdwaFIwWjVZekpXTUZCWVZqQmFhVEEwVHpKS2FHTXlWVEpPUTNoaFYwZG9NMWxxVGt0TlJXeElWRzVhYVdKck5IZFRWV2hoWVVkS1NWWnRlRXBTUkVKdVUycENUMDFYVG5SaVNGcHJWMFUxYzFreWJFTmhSMHAwVlZka1drMHhXalZaVm1NMVRWZE5lVlp1YkUxaFYwMDVTbmM5UFNjTkNtVjRjRzl5ZENBcUlHWnliMjBnSjJSaGRHRTZkR1Y0ZEM5cVlYWmhjMk55YVhCME8yTm9ZWEp6WlhROWRYUm1MVGc3WW1GelpUWTBMRXg1Y0hsYVYzaG9aVlJKY1V3eVZqUmpSemw1WkVOQmNVbEhSbnBKU0U0eFdXNU9NRmxYTldwYVUwSnRZMjA1ZEVsRFpHdFpXRkpvVDI1U2JHVklVWFpoYlVZeVdWaE9hbU50Ykhka1JIUnFZVWRHZVdNeVZqQlFXRll3V21rd05FOHlTbWhqTWxVeVRrTjRZVmRIYUROWmFrNUxUVVZzU0ZSdVdtbGlhelIzVTFWb1lXRkhTa2xXYlhoS1VrUkNibE5xUWs5TlYwNTBZa2hhYTFkRk5YTlpNbXhEWVVkS2RGVlhaRnBOTVZvMVdWWmpOVTFYVFhsV2JteE5ZVmROT1VwM1BUMG4nDQpjb25zb2xlLmxvZyhzdWJzdGFuY2Up' --eval ''
```

### How often does it reproduce? Is there a required condition?

Always.

### What is the expected behavior? Why is that the expected behavior?

Different paths to the same module namespace should be disambiguated and load successfully.

![disambiguate-namespace](https://github.com/nodejs/node/assets/134245276/554fe600-0799-4ad1-a833-614770e6f23f)

---

This is the behavior specified in ECMAScript:

The relevant logic is [`ResolveExport`](https://262.ecma-international.org/#sec-resolveexport). This line toward the bottom defines the star export / namespace interaction:

```
3. If resolution.[[BindingName]] is not starResolution.[[BindingName]] and either resolution.[[BindingName]] or starResolution.[[BindingName]] is namespace, return ambiguous.
```

If either but not both are `namespace`, it's ambiguous. If both are `namespace` the end result is to disambiguate and consider them identical.

---

Node.js does disambiguate values within modules:

![disambiguate-value](https://github.com/nodejs/node/assets/134245276/ba422252-822f-4e54-9139-f83522f9d9c6)
```console
$ node resolve-value/entry.mjs
Curiouser and curiouser.
```

---

Firefox also disambiguates module namespaces. This file succeeds:

```html
<!DOCTYPE html>
<html>
  <head>
    <script type="module" src="./entry.mjs"></script>
  </head>
</html>
```

```console
Object { value: 'Curiouser and curiouser.' }
```

### What do you see instead?

SyntaxError: The requested module './aggregate.mjs' contains conflicting star exports for name 'substance'

### Additional information

There's a GitHub repo here with discussion.

https://github.com/bojavou/disambiguate-namespace