Skip to content

Haste has troubles resolving modules inside @scoped modules #241

Closed
jestjs/jest
#6980

Description

@empyrical

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-*`.

Activity

  1. hramos commented on Sep 11, 2018

    @hramos
    Contributor

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

  2. rafeca commented on Sep 12, 2018

    @rafeca
    Contributor

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

  3. empyrical commented on Sep 12, 2018

    @empyrical
    ContributorAuthor

    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-*`.
    
  4. added a commit that references this issue on Mar 13, 2024
    27cf4a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions