remix icon indicating copy to clipboard operation
remix copied to clipboard

1.4.0 breaks module resolution for linked modules

Open deimyts opened this issue 2 years ago • 2 comments

What version of Remix are you using?

1.4.0

Steps to Reproduce

  1. Create a new remix project using Remix 1.4.0
  2. Create a new npm package locally. I'm calling it my-module for purposes of documentation in this ticket, but the name doesn't matter.
  3. Import your new local package (my-module) somewhere in the new remix project (I put it in the index route).
  4. run npm link in your new module directory
  5. run npm link my-module in your remix project.
  6. Build the Remix project with npm run dev, and the dev build should fail.

Expected Behavior

Local packages installed with npm link or from a local file path should both be resolved correctly. Instead, the module resolution fails.

Actual Behavior

NPM packages installed with npm link or from a local file path are not resolved correctly. Attempting to build a remix project with linked modules produces the following error:

 ✘ [ERROR] [plugin server-bare-modules] Couldn't resolve the base path of "my-module". Searched inside the resolved main file path "/Users/.../my-module/index.js" using "node_modules/my-module"

Additional notes:

  • I've tracked down the source of the issue to a few recent changes to packages/remix-dev/compiler/plugins/serverBareModulesPlugin.ts. These are listed in the "Related PRs" section.
    • in resolveModuleBasePath(), the plugin compares the output of require.resolve('my-module') with a search string that includes node_modules, and throws an error if they don't match.
    • When installing a package with npm link, require.resolve('some-linked-module') returns the absolute path to the linked module, rather than the path to the symlink, so the path we're comparing doesn't contain node_modules and improperly triggers the error.
  • Replacing "searchForPathSection = node_modules${path.sep}${mod};" with "searchForPathSection = ${path.sep}${mod}" solves this particular issue, but I don't know if that change will create issues when resolving other non-standard imports.
  • I'd submit a PR with a failing test as recommended, but I'm not sure how generate a text fixture with a symlinked npm package to reproduce this, and haven't had too much time to dig into that yet.

Related PRs

  • #2694
  • #2767

#Related Issues

  • https://github.com/remix-run/remix/issues/2836 (looks like it's describing the same problem, or at least something similar)

deimyts avatar Apr 18 '22 20:04 deimyts

Hey @deimyts, is this still a problem with latest remix version?

machour avatar Jan 22 '23 09:01 machour

I am on the latest version of remix (1.11.1) and can confirm I am experiencing this issue currently.

moseslecce avatar Jan 25 '23 15:01 moseslecce