Skip to content
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

Haste has troubles resolving modules inside @scoped modules #241

Closed
empyrical opened this issue Sep 11, 2018 · 3 comments · Fixed by jestjs/jest#6980
Closed

Haste has troubles resolving modules inside @scoped modules #241

empyrical opened this issue Sep 11, 2018 · 3 comments · Fixed by jestjs/jest#6980

Comments

@empyrical
Copy link
Contributor

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?

If you are outside of a @scoped/module, you can properly require() a haste module that is sitting inside @scoped/module

But if a JS file inside of @scoped/module tries to include a haste module, it's unable to resolve it.

What is the expected behavior?

Metro should be able to properly resolve Haste modules from inside scoped modules

Metro version: 0.45.1
Node version: 10.5.0

I am attempting to fix this, but haven't gotten anywhere yet. I've made a Jest test that goes inside traverseDependencies-integration-test.js to test against:

    it('should work with scoped providesModuleNodeModules (haste)', async () => {
      var root = '/root';
      setMockFileSystem({
        root: {
          'index.js': `
            /**
             * @providesModule index
             */
             require('shouldWork');
             require('Bar');
          `,
          node_modules: {
            'react-haste': {
              'package.json': JSON.stringify({
                name: 'react-haste',
                main: 'main.js',
              }),
              'main.js': [
                '/**',
                ' * @providesModule shouldWork',
                ' */',
              ].join('\n'),
            },
            '@org': {
              module: {
                'package.json': JSON.stringify({
                  name: '@org/module',
                  main: 'main.js',
                }),
                'main.js': '// Blank',
                'Bar.js': `
                  /**
                   * @providesModule Bar
                   */
                   require('shouldWork');
                `,
              },
            },
          },
        },
      });

      const opts = {
        ...defaults,
        providesModuleNodeModules: [...defaults.providesModuleNodeModules, '@org/module'],
        watchFolders: [root],
      };
      await processDgraph(opts, async dgraph => {
        const deps = await getOrderedDependenciesAsJSON(
          dgraph,
          '/root/index.js',
        );
        expect(deps).toMatchSnapshot();
      });
    });

Which fails with this error:

  ● traverseDependencies › node_modules (posix) › should work with scoped providesModuleNodeModules (haste)

    Unable to resolve module `shouldWork` from `/root/node_modules/@org/module/Bar.js`: Module `shouldWork` does not exist in the Haste module map

    This might be related to https://github.com/facebook/react-native/issues/4968
    To resolve try the following:
      1. Clear watchman watches: `watchman watch-del-all`.
      2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
      3. Reset Metro Bundler cache: `rm -rf /tmp/metro-bundler-cache-*` or `npm start -- --reset-cache`.
      4. Remove haste cache: `rm -rf /tmp/haste-map-react-native-packager-*`.
@hramos
Copy link
Contributor

hramos commented Sep 11, 2018

cc @rafeca @mjesun and rest of Metro folks: @empyrical is one of our core contributors over at React Native.

@rafeca
Copy link
Contributor

rafeca commented Sep 12, 2018

Thanks for the really good repro steps!! 😃 I'll look at this later on the week

@empyrical
Copy link
Contributor Author

No problem! 😄

I also just tested, and it has issues with Windows - while top-level JS was able to find Bar on Unix, it wasn't able to do that on Windows. I suspect that may be an issue with the different path separators on windows. Here's a test I wrote for that too:

    it('should work with scoped providesModuleNodeModules (haste, win32)', async () => {
      var root = 'C:\\root';
      setMockFileSystem({
        root: {
          'index.js': `
            /**
             * @providesModule index
             */
             require('shouldWork');
             require('Bar');
          `,
          node_modules: {
            'react-haste': {
              'package.json': JSON.stringify({
                name: 'react-haste',
                main: 'main.js',
              }),
              'main.js': [
                '/**',
                ' * @providesModule shouldWork',
                ' */',
              ].join('\n'),
            },
            '@org': {
              module: {
                'package.json': JSON.stringify({
                  name: '@org/module',
                  main: 'main.js',
                }),
                'main.js': '// Blank',
                'Bar.js': `
                  /**
                   * @providesModule Bar
                   */
                   require('shouldWork');
                `,
              },
            },
          },
        },
      });
    
      const opts = {
        ...defaults,
        providesModuleNodeModules: [...defaults.providesModuleNodeModules, '@org/module'],
        watchFolders: [root],
      };
      await processDgraph(opts, async dgraph => {
        const deps = await getOrderedDependenciesAsJSON(
          dgraph,
          'C:\\root\\index.js',
        );
        console.error(deps)
        expect(deps).toMatchSnapshot();
      });
    });

Which fails with this:

  ● traverseDependencies › node_modules (win32) › should work with scoped providesModuleNodeModules (haste, win32)

    Unable to resolve module `Bar` from `C:\root\index.js`: Module `Bar` does not exist in the Haste module map

    This might be related to https://github.com/facebook/react-native/issues/4968
    To resolve try the following:
      1. Clear watchman watches: `watchman watch-del-all`.
      2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
      3. Reset Metro Bundler cache: `rm -rf /tmp/metro-bundler-cache-*` or `npm start -- --reset-cache`.
      4. Remove haste cache: `rm -rf /tmp/haste-map-react-native-packager-*`.

facebook-github-bot pushed a commit to facebook/react-native that referenced this issue Sep 13, 2018
Summary:
This PR gives RNPM the ability to look for plugins in `scoped` modules.

The regexes for finding RNPM plugins will match these hypothetical examples:

 * `rnpm-plugin-foo`
 * `org/rnpm-plugin-foo`

The regexes for finding React Native plugins will match these hypothetical examples:

 * `react-native-foo`
 * `org/react-native-foo`
 * `The controller you requested could not be found./module` (will be useful in the slimmening)
 * `The controller you requested could not be found./module`

RNPM plugins will be able to benefit from this immediately, but React Native plugins will run into this Metro issue currently:

facebook/metro#241
Pull Request resolved: #21082

Differential Revision: D9809094

Pulled By: hramos

fbshipit-source-id: 4b0694ad4119b37dd5664af52c48e48ebe4d7404
gengjiawen pushed a commit to gengjiawen/react-native that referenced this issue Sep 14, 2018
…ook#21082)

Summary:
This PR gives RNPM the ability to look for plugins in `scoped` modules.

The regexes for finding RNPM plugins will match these hypothetical examples:

 * `rnpm-plugin-foo`
 * `org/rnpm-plugin-foo`

The regexes for finding React Native plugins will match these hypothetical examples:

 * `react-native-foo`
 * `org/react-native-foo`
 * `The controller you requested could not be found./module` (will be useful in the slimmening)
 * `The controller you requested could not be found./module`

RNPM plugins will be able to benefit from this immediately, but React Native plugins will run into this Metro issue currently:

facebook/metro#241
Pull Request resolved: facebook#21082

Differential Revision: D9809094

Pulled By: hramos

fbshipit-source-id: 4b0694ad4119b37dd5664af52c48e48ebe4d7404
kelset pushed a commit to facebook/react-native that referenced this issue Sep 21, 2018
Summary:
This PR gives RNPM the ability to look for plugins in `scoped` modules.

The regexes for finding RNPM plugins will match these hypothetical examples:

 * `rnpm-plugin-foo`
 * `org/rnpm-plugin-foo`

The regexes for finding React Native plugins will match these hypothetical examples:

 * `react-native-foo`
 * `org/react-native-foo`
 * `The controller you requested could not be found./module` (will be useful in the slimmening)
 * `The controller you requested could not be found./module`

RNPM plugins will be able to benefit from this immediately, but React Native plugins will run into this Metro issue currently:

facebook/metro#241
Pull Request resolved: #21082

Differential Revision: D9809094

Pulled By: hramos

fbshipit-source-id: 4b0694ad4119b37dd5664af52c48e48ebe4d7404
grabbou pushed a commit to react-native-community/cli that referenced this issue Sep 26, 2018
Summary:
This PR gives RNPM the ability to look for plugins in `scoped` modules.

The regexes for finding RNPM plugins will match these hypothetical examples:

 * `rnpm-plugin-foo`
 * `org/rnpm-plugin-foo`

The regexes for finding React Native plugins will match these hypothetical examples:

 * `react-native-foo`
 * `org/react-native-foo`
 * `The controller you requested could not be found./module` (will be useful in the slimmening)
 * `The controller you requested could not be found./module`

RNPM plugins will be able to benefit from this immediately, but React Native plugins will run into this Metro issue currently:

facebook/metro#241
Pull Request resolved: facebook/react-native#21082

Differential Revision: D9809094

Pulled By: hramos

fbshipit-source-id: 4b0694ad4119b37dd5664af52c48e48ebe4d7404
t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
…ook#21082)

Summary:
This PR gives RNPM the ability to look for plugins in `scoped` modules.

The regexes for finding RNPM plugins will match these hypothetical examples:

 * `rnpm-plugin-foo`
 * `org/rnpm-plugin-foo`

The regexes for finding React Native plugins will match these hypothetical examples:

 * `react-native-foo`
 * `org/react-native-foo`
 * `The controller you requested could not be found./module` (will be useful in the slimmening)
 * `The controller you requested could not be found./module`

RNPM plugins will be able to benefit from this immediately, but React Native plugins will run into this Metro issue currently:

facebook/metro#241
Pull Request resolved: facebook#21082

Differential Revision: D9809094

Pulled By: hramos

fbshipit-source-id: 4b0694ad4119b37dd5664af52c48e48ebe4d7404
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants