aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.23k stars 44 forks source link

Discrepancies Between System Directories and Aliases #138

Open shellscape opened 4 months ago

shellscape commented 4 months ago

This may affect more than MacOS, but on MacOS /private/tmp is aliased to /tmp. When started, Vite (really Rollup) will start inspecting imports and any imports from /tmp will appear as /private/tmp/... as the importer. This is important, because the way that vite-tsconfig-paths configures the projectDir uses the alias of /tmp in this scenario.

To inspect what was happening, I added some debugging around this line: https://github.com/aleclarson/vite-tsconfig-paths/blob/ca4e2aa7f3be36facbce0305cc145f9d5f9693d1/src/index.ts#L165

  vite-tsconfig-paths {
  vite-tsconfig-paths   importer: '/private/tmp/jsx-email-test/fixtures/templates/paths.tsx',
  vite-tsconfig-paths   prevProjectDir: undefined,
  vite-tsconfig-paths   projectDir: '/private/tmp/jsx-email-test/fixtures/templates',
  vite-tsconfig-paths   resolversByDir: { '/tmp/jsx-email-test/fixtures': [ [AsyncFunction (anonymous)] ] }
  vite-tsconfig-paths }

And we can see that the matching is going to fail because the directory that the plugin is looking for doesn't include /private. When we start the Vite server targeting /private/temp/app instead, everything works as expected.

This also applies if one uses the $TMPDIR environment variable, which returns a value like /var/folders/07/ywbfgwc57_z4yx4m8vzhr8580000gp/T/, which is then prefixed with /private on MacOS to appear as /private/var/folders/07/ywbfgwc57_z4yx4m8vzhr8580000gp/T/ during resolution.

This is a pretty nuanced edge case, but I wanted to share for folks that might run into a similar problem. I think the package could also do with a good bit more debug output so people can triage without having to source dive.

To resolve this case, the plugin should always wrap paths with fs.realpathSync() or equivalent.