Skip to content

Haste has troubles resolving modules inside @scoped modules #241

@empyrical

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions