sveltejs / vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
MIT License
864 stars 105 forks source link

`@sveltejs/package` missing `import` in `package.json.exports['.']`? #710

Closed janosh closed 1 year ago

janosh commented 1 year ago

Describe the bug

I started running into

Error: Failed to resolve entry for package "@sveltejs/package". The package may have incorrect main/module/exports specified in its package.json: No known conditions for "." specifier in "@sveltejs/package" package

Stacktrace ``` ❯ packageEntryFailure node_modules/vite/dist/node/chunks/dep-df561101.js:28691:11 ❯ resolvePackageEntry node_modules/vite/dist/node/chunks/dep-df561101.js:28686:9 ❯ tryNodeResolve node_modules/vite/dist/node/chunks/dep-df561101.js:28419:20 ❯ Context.resolveId node_modules/vite/dist/node/chunks/dep-df561101.js:28[18](https://github.com/janosh/elementari/actions/runs/5791065675/job/15695238618?pr=42#step:5:19)0:28 ❯ Object.resolveId node_modules/vite/dist/node/chunks/dep-df561101.js:44207:64 ❯ async file:/home/runner/work/elementari/elementari/node_modules/vite/dist/node/chunks/dep-df561101.js:65837:21 ❯ async file:/home/runner/work/elementari/elementari/node_modules/vite/dist/node/chunks/dep-df561101.js:44852:[20](https://github.com/janosh/elementari/actions/runs/5791065675/job/15695238618?pr=42#step:5:21) ❯ addManuallyIncludedOptimizeDeps node_modules/vite/dist/node/chunks/dep-df561101.js:46034:31 ❯ optimizeServerSsrDeps node_modules/vite/dist/node/chunks/dep-df561101.js:45638:5 ❯ createDevSsrDepsOptimizer node_modules/vite/dist/node/chunks/dep-df561101.js:45556:[22](https://github.com/janosh/elementari/actions/runs/5791065675/job/15695238618?pr=42#step:5:23) ```

I'm no packaging expert but looking at

https://github.com/sveltejs/kit/blob/a6fe5fcb1c7258281b4bf53b94543272e6e6c6d8/packages/package/package.json#L44-L50

isn't that missing an "import": "./src/exports/index.js" or similar (going by kit exports).

https://github.com/sveltejs/kit/blob/a6fe5fcb1c7258281b4bf53b94543272e6e6c6d8/packages/kit/package.json#L75-L100

Reproduction

Tried but failed to repro on StackBlitz.

Logs

No response

System Info

System:
    OS: macOS 13.4
    CPU: (12) arm64 Apple M2 Max
    Memory: 4.25 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.5.0 - /opt/homebrew/bin/node
    npm: 9.8.0 - /opt/homebrew/bin/npm
    pnpm: 8.6.2 - /opt/homebrew/bin/pnpm
  Browsers:
    Brave Browser: 115.1.56.20
    Chrome: 115.0.5790.170
    Safari: 16.5
  npmPackages:
    @sveltejs/adapter-static: 2.0.3 => 2.0.3 
    @sveltejs/kit: ^1.22.4 => 1.22.4 
    @sveltejs/package: ^2.2.0 => 2.2.0 
    svelte: 4.1.2 => 4.1.2 
    vite: ^4.4.9 => 4.4.9

Severity

blocking all usage of SvelteKit

Additional Information

No response

dummdidumm commented 1 year ago

It's correct because @sveltejs/package does not provide any JS to import, just types. Do you have import { .. } from '@sveltejs/package' anywhere by chance? Does it help to turn it into import type { .. } from '@sveltejs/package'? Without a reproducible we can't really help you.

ciscoheat commented 1 year ago

This happened to me as well with sveltekit-flash-message when updating to vitest ^0.34.1, and now with @sveltejs/package.

I opened an issue at https://github.com/vitest-dev/vitest/issues/3913 and got the explanation that Vitest didn't respect optimized dependencies in previous versions.

By adding a top-level export for sveltekit-flash-message and publishing a new patch version, it seems to work fine.

Edit: Version is "@sveltejs/package": "^2.2.0"

dummdidumm commented 1 year ago

@dominikg I believe vite-plugin-svelte does the automatic addition of these dependencies, right? Would it be possible to exclude packages which have an exports map that only includes types conditions? Also I don't understand from that issue link what optimized dependencies have to do with a required . export in your exports map.

dominikg commented 1 year ago

v-p-s only adds deps that have a svelte condition or use the svelte toplevel field, svelte-package should have neither. without a reproduction it's hard to tell what's going on here, it should not be a dependency of an application that is being built.

regarding optimizeDeps and requiring a . export, this is something to check with vite, cc @bluwy

ciscoheat commented 1 year ago

@dominikg Maybe it's not the reproduction you have in mind, but it's simple to reproduce the error I encountered:

Results in:

Error: Failed to resolve entry for package "@sveltejs/package". The package may have incorrect main/module/exports specified in its package.json: No known conditions for "." specifier in "@sveltejs/package" package
 ❯ packageEntryFailure node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:28691:11
 ❯ resolvePackageEntry node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:28686:9
 ❯ tryNodeResolve node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:28419:20
 ❯ Context.resolveId node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:28180:28
 ❯ Object.resolveId node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:44207:64
 ❯ async file:/E:/Projects/TEST/lib-vitest/node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:65837:21
 ❯ async file:/E:/Projects/TEST/lib-vitest/node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:44852:20
 ❯ addManuallyIncludedOptimizeDeps node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:46034:31
 ❯ optimizeServerSsrDeps node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:45638:5
 ❯ createDevSsrDepsOptimizer node_modules/.pnpm/vite@4.4.9_@types+node@20.4.9/node_modules/vite/dist/node/chunks/dep-df561101.js:45556:22
dominikg commented 1 year ago

moved the issue to vite-plugin-svelte.

It looks likevitefu adds @sveltejs/package and svelte2tsx to ssr.noExternal

 vite:vite-plugin-svelte extra config for dependencies generated by vitefu {
  optimizeDeps: {
    include: [
      '@sveltejs/package > chokidar',
      '@sveltejs/package > semver',
      '@sveltejs/package > svelte2tsx > dedent-js'
    ],
    exclude: []
  },
  ssr: {
    noExternal: [ '@sveltejs/package', 'svelte2tsx' ],
    external: [
      'chokidar',
      'kleur',
      'sade',
      'semver',
      'dedent-js',
      'pascal-case'
    ]
  }
}

and vite adds packages present in ssr.noExternal to optimizeDeps.include.

We may have to update some config or change the filter functions we use with vitefu to prevent these from showing up. I'm also a bit confused about some of the values in ssr.external.

bluwy commented 1 year ago

If the error is coming from Vitest 0.34 specifically, it's likely because that version started to use ssr.external and ssr.noExternal, instead of deps.external and deps.inline, that's causing these errors. Vite's ssr configs are usually lazy, so setting these packages incorrectly is fine as long as you don't import it, but Vitest seems to made it eager (which I don't exactly think it's a bug there).

The PR fix sent is probably the simplest way to fix this though.

dominikg commented 1 year ago

released fix in 2.4.5 please let us know if you still have this issue aftert updating

ciscoheat commented 1 year ago

Works fine for me now, thank you for the quick fix!

SteelAlloy commented 1 year ago

released fix in 2.4.5 please let us know if you still have this issue aftert updating

I'm having the same issue :

Failed to resolve entry for package "sveltekit-flash-message". The package may have incorrect main/module/exports specified in its package.json: No known conditions for "." specifier in "sveltekit-flash-message" package

Packages :

{
    "@sveltejs/kit": "1.25.1",
    "svelte": "4.2.1",
    "sveltekit-flash-message": "2.2.1",
    "vite": "4.4.10",
}
ciscoheat commented 1 year ago

Strange, I added a top-level export here for sveltekit-flash-message, to avoid this issue. Anything else that needs to be added?