Repository: codesandbox/codesandbox-client (language: javascript)

Issue:
index file doesn't get resolved correctly when there's a folder called "index" too
# 🐛 bug report

## Preflight Checklist

<!-- Please ensure you've completed the following steps by replacing [ ] with [x]-->

- [x] I have read the [Contributing Guidelines][contributing] for this project.
- [x] I agree to follow the [Code of Conduct][code_of_conduct] that this project
      adheres to.
- [x] I have searched the issue tracker for an issue that matches the one I want
      to file, without success.

## Description of the problem

instead of resolving for `instantsearch.js/es/widgets` the file `instantsearch.js/es/widgets/index/index.js`, the file `instantsearch.js/es/widgets/index.js` should have been resolved

## How has this issue affected you? What are you trying to accomplish?

Complicated to debug error about a wrong keys of an import 😄 

### To Reproduce

<!--
Your best chance of getting this bug looked at quickly is to provide an example.
-->

```
import {searchBox} from 'instantsearch.js/es/widgets'

console.assert(searchBox !== undefined, 'searchBox should be defined')
```

### Link to sandbox: [link](https://codesandbox.io/s/github/algolia/solutions/tree/master/facet-selection-autofilter?file=/src/app.js)

### Your Environment

| Software         | Name/Version |
| ---------------- | ------------ |
| Сodesandbox      |      f8ba2bbeb        |
| Browser          |        -      |
| Operating System |    -          |

<!-- prettier-ignore-start -->
[code_of_conduct]: https://github.com/codesandbox/codesandbox-client/blob/master/CODE_OF_CONDUCT.md
[contributing]: https://github.com/codesandbox/codesandbox-client/blob/master/CONTRIBUTING.md
<!-- prettier-ignore-end -->



Discussion / hints:

Fix patch:
diff --git a/package.json b/package.json
index 4c19b0795d4..dc229ca66b4 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,7 @@
     "build:embed": "lerna run build:embed --scope app --stream && gulp",
     "build:prod": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" lerna run build  --scope homepage --stream && lerna run build --scope app --stream && lerna run copy-assets --scope app --stream",
     "commit": "concurrently \"yarn typecheck\" \"yarn lint\" && git commit -m",
-    "postinstall": "yarn lerna run install-dependencies --stream --concurrency 1 --ignore smooshpack --ignore react-smooshpack && opencollective postinstall",
+    "postinstall": "yarn lerna run install-dependencies --stream --concurrency 1 && opencollective postinstall",
     "contributors:add": "all-contributors add",
     "contributors:generate": "all-contributors generate",
     "lint": "lerna run lint --stream",
diff --git a/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/es/index.js b/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/es/index.js
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/es/widgets/index/index.js b/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/es/widgets/index/index.js
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/index.js b/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/index.js
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/package.json b/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/package.json
new file mode 100644
index 00000000000..0967ef424bc
--- /dev/null
+++ b/packages/sandpack-core/src/resolver/fixture/node_modules/instantsearch.js/package.json
@@ -0,0 +1,1 @@
+{}
diff --git a/packages/sandpack-core/src/resolver/resolver.ts b/packages/sandpack-core/src/resolver/resolver.ts
index f102dad1510..726f36cbdaa 100644
--- a/packages/sandpack-core/src/resolver/resolver.ts
+++ b/packages/sandpack-core/src/resolver/resolver.ts
@@ -310,8 +310,16 @@ function* getTSConfig(
 }
 
 export const resolver = gensync<
-  (moduleSpecifier: string, inputOpts: IResolveOptionsInput) => string
->(function* resolve(moduleSpecifier, inputOpts): Generator<any, string, any> {
+  (
+    moduleSpecifier: string,
+    inputOpts: IResolveOptionsInput,
+    skipIndexExpansion?: boolean
+  ) => string
+>(function* resolve(
+  moduleSpecifier,
+  inputOpts,
+  skipIndexExpansion = false
+): Generator<any, string, any> {
   const normalizedSpecifier = normalizeModuleSpecifier(moduleSpecifier);
   const opts = normalizeResolverOptions(inputOpts);
   const modulePath = yield* resolveModule(normalizedSpecifier, opts);
@@ -343,7 +351,7 @@ export const resolver = gensync<
   }
 
   let foundFile = yield* expandFile(modulePath, opts);
-  if (!foundFile) {
+  if (!foundFile && !skipIndexExpansion) {
     foundFile = yield* expandFile(pathUtils.join(modulePath, 'index'), opts);
 
     // In case alias adds an extension, we retry the entire resolution with an added /index
@@ -352,7 +360,7 @@ export const resolver = gensync<
       try {
         const parts = moduleSpecifier.split('/');
         if (!parts.length || !parts[parts.length - 1].startsWith('index')) {
-          foundFile = yield* resolve(moduleSpecifier + '/index', opts);
+          foundFile = yield* resolve(moduleSpecifier + '/index', opts, true);
         }
       } catch (err) {
         // should throw ModuleNotFound for original specifier, not new one


Test patch:
diff --git a/packages/sandpack-core/src/resolver/resolver.test.ts b/packages/sandpack-core/src/resolver/resolver.test.ts
index 571c3ef079f..1a39f955af7 100644
--- a/packages/sandpack-core/src/resolver/resolver.test.ts
+++ b/packages/sandpack-core/src/resolver/resolver.test.ts
@@ -258,6 +258,19 @@ describe('resolve', () => {
         new ModuleNotFoundError('unknown-module/test.js', '/nested/test.js')
       );
     });
+
+    it('should handle instantsearch.js index palooza', () => {
+      expect(() => {
+        resolveSync('instantsearch.js/es/widgets', {
+          filename: '/foo.js',
+          extensions: ['.ts', '.tsx', '.js', '.jsx'],
+          isFile,
+          readFile,
+        });
+      }).toThrow(
+        `Cannot find module 'instantsearch.js/es/widgets' from '/foo.js'`
+      );
+    });
   });
 
   describe('package#browser', () => {
@@ -469,14 +482,14 @@ describe('resolve', () => {
     });
 
     it('should not load exports from the root package.json', () => {
-      expect(() =>
+      expect(() => {
         resolveSync('a-custom-export', {
           filename: '/foo.js',
           extensions: ['.ts', '.tsx', '.js', '.jsx'],
           isFile,
           readFile,
-        })
-      ).toThrow();
+        });
+      }).toThrow();
     });
 
     it('should not fail on wildcard *.js and folder references', () => {