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

Packages using self-referencing imports will fail to resolve ("moduleResolution": "node16" and package.json "exports") #9731

Closed
7 tasks done
segevfiner opened this issue Aug 18, 2022 · 9 comments · Fixed by #16068
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@segevfiner
Copy link
Contributor

Describe the bug

The given repository will fail in both dev & build due to a package using Self-referencing import that is defined to work for packages that specify exports in package.json under the newer node module resolution rules. AKA "moduleResolution": "node16" in tsconfig.json.

Reproduction

https://github.com/segevfiner/vite-self-reference

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 123.20 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.17.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.101
    Safari: 15.5


### Used Package Manager

pnpm

### Logs

<details>
<summary>Click to expand!</summary>

```shell
  vite:config bundled config file loaded in 61.18ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:vue',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   resolve: { alias: [ [Object], [Object], [Object] ] },
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     esbuildOptions: { preserveSymlinks: undefined }
  vite:config   },
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: {
  vite:config     format: 'esm',
  vite:config     target: 'node',
  vite:config     external: [],
  vite:config     optimizeDeps: { disabled: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   configFile: '/Users/segevfiner/junk/vite-self-reference/apps/web/vite.config.ts',
  vite:config   configFileDependencies: [
  vite:config     '/Users/segevfiner/junk/vite-self-reference/apps/web/vite.config.ts'
  vite:config   ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     optimizeDeps: { force: undefined },
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/segevfiner/junk/vite-self-reference/apps/web',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/segevfiner/junk/vite-self-reference/apps/web/public',
  vite:config   cacheDir: '/Users/segevfiner/junk/vite-self-reference/apps/web/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   isProduction: true,
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     middlewareMode: false,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       'vite:build-metadata',
  vite:config       'vite:pre-alias',
  vite:config       'alias',
  vite:config       'vite:modulepreload-polyfill',
  vite:config       'vite:resolve',
  vite:config       'vite:html-inline-proxy',
  vite:config       'vite:css',
  vite:config       'vite:esbuild',
  vite:config       'vite:json',
  vite:config       'vite:wasm-helper',
  vite:config       'vite:worker',
  vite:config       'vite:asset',
  vite:config       'vite:wasm-fallback',
  vite:config       'vite:define',
  vite:config       'vite:css-post',
  vite:config       'vite:build-html',
  vite:config       'vite:worker-import-meta-url',
  vite:config       'vite:watch-package-data',
  vite:config       'commonjs',
  vite:config       'vite:data-uri',
  vite:config       'vite:asset-import-meta-url',
  vite:config       'vite:dynamic-import-vars',
  vite:config       'vite:import-glob',
  vite:config       'vite:build-import-analysis',
  vite:config       'vite:esbuild-transpile',
  vite:config       'vite:terser',
  vite:config       'vite:reporter',
  vite:config       'vite:load-fallback'
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false }
  vite:config } +5ms
vite v3.0.8 building for production...
✓ 11 modules transformed.
[vite]: Rollup failed to resolve import "somelib" from "../../packages/somelib/browser.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
error during build:
Error: [vite]: Rollup failed to resolve import "somelib" from "../../packages/somelib/browser.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at onRollupWarning (file:///Users/segevfiner/junk/vite-self-reference/node_modules/.pnpm/vite@3.0.8/node_modules/vite/dist/node/chunks/dep-74663fff.js:43686:19)
    at onwarn (file:///Users/segevfiner/junk/vite-self-reference/node_modules/.pnpm/vite@3.0.8/node_modules/vite/dist/node/chunks/dep-74663fff.js:43485:13)
    at Object.onwarn (file:///Users/segevfiner/junk/vite-self-reference/node_modules/.pnpm/rollup@2.77.3/node_modules/rollup/dist/es/shared/rollup.js:23178:13)
    at ModuleLoader.handleResolveId (file:///Users/segevfiner/junk/vite-self-reference/node_modules/.pnpm/rollup@2.77.3/node_modules/rollup/dist/es/shared/rollup.js:22335:26)
    at file:///Users/segevfiner/junk/vite-self-reference/node_modules/.pnpm/rollup@2.77.3/node_modules/rollup/dist/es/shared/rollup.js:22296:26

Validations

@ambarvm
Copy link

ambarvm commented Aug 18, 2022

I am facing same issue with a third party package but issue only occurs on build and not on dev.
Stackblitz

Error on running vite build

[vite]: Rollup failed to resolve import "@cds/core/icon/shapes/exclamation-circle.js" from "node_modules/@cds/core/forms/register.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
error during build:
Error: [vite]: Rollup failed to resolve import "@cds/core/icon/shapes/exclamation-circle.js" from "node_modules/@cds/core/forms/register.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at onRollupWarning (file:///home/projects/vitejs-vite-gl8xl1/node_modules/vite/dist/node/chunks/dep-0f13c890.js:43723:19)
    at onwarn (file:///home/projects/vitejs-vite-gl8xl1/node_modules/vite/dist/node/chunks/dep-0f13c890.js:43513:13)
    at Object.eval [as onwarn] (file:///home/projects/vitejs-vite-gl8xl1/node_modules/rollup/dist/es/shared/rollup.js:23208:13)
    at ModuleLoader.handleResolveId (file:///home/projects/vitejs-vite-gl8xl1/node_modules/rollup/dist/es/shared/rollup.js:22365:26)
    at eval (file:///home/projects/vitejs-vite-gl8xl1/node_modules/rollup/dist/es/shared/rollup.js:22326:26)

@Azarattum
Copy link

I have a similar monorepo setup where a package references itself to avoid ../../../ hell. Everything works find if you import it outside. But when it tries to import itself (even when caused by an external import) an error is thrown:

Cannot find module '@project/ui' imported from 'C:/Users/username/Desktop/Project/packages/ui/lib/action/draggable.ts'
Error: Cannot find module '@project/ui' imported from 'C:/Users/username/Desktop/Project/packages/ui/lib/action/draggable.ts'
    at viteResolve (file:///C:/Users/username/Desktop/Project/node_modules/.pnpm/vite@3.0.8/node_modules/vite/dist/node/chunks/dep-74663fff.js:50536:25)
    at nodeImport (file:///C:/Users/username/Desktop/Project/node_modules/.pnpm/vite@3.0.8/node_modules/vite/dist/node/chunks/dep-74663fff.js:50571:15)
    at ssrImport (file:///C:/Users/username/Desktop/Project/node_modules/.pnpm/vite@3.0.8/node_modules/vite/dist/node/chunks/dep-74663fff.js:50466:20)
    at eval (/@fs/C:/Users/username/Desktop/Project/packages/ui/lib/action/draggable.ts:3:50)
    at instantiateModule (file:///C:/Users/username/Desktop/Project/node_modules/.pnpm/vite@3.0.8/node_modules/vite/dist/node/chunks/dep-74663fff.js:50512:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)```

@patroza
Copy link

patroza commented Dec 2, 2022

interestingly enough it works fine when the package with the same content is put into a real node module, and loaded then

@patroza
Copy link

patroza commented Dec 3, 2022

interestingly enough it works fine when the package with the same content is put into a real node module, and loaded then

There in seems to lie the clue. if you symlink your packages into the root node_modules, it starts working!
incase of pnpm it means in my root package.json I add my packages as version"workspace:*"

@dschmidt dschmidt mentioned this issue Dec 3, 2022
39 tasks
@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Mar 28, 2023
@hybridherbst
Copy link

Also running into this. Not sure why it happens only in some configurations though, in other projects it works fine to locally reference packages with self imports.

@ef4
Copy link

ef4 commented Aug 1, 2023

Not sure why it happens only in some configurations though

It happens only in some configurations because package manager sloppiness often hides the bug, by making a package's name resolvable in more than one way. You end up "falling back" to a higher-level node_modules folder that just happens to have a symlink to the package. But that's not what node itself does.

Vite's resolver plugin doesn't implement this feature correctly. The logical place to do it would be about here:

function resolveSubpathImports(

since that has already located the relevant package.json and is doing the imports part correctly while ignoring the exports part.

@datner
Copy link

datner commented Sep 11, 2023

Is this being worked on or...?

@sapphi-red
Copy link
Member

Another reproduction from #13409 (comment)
It seems it doesn't work when link: or workspace: is used.
https://stackblitz.com/edit/github-yylndb?file=packages%2Fweb%2Fpackage.json

@meduzen
Copy link
Contributor

meduzen commented Mar 9, 2024

Wanted to have this in Vitest but the related issues are closed, so I’m referencing them here for anyone interested:

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants