Skip to content

fix(sveltekit): sentryPlugins.find is not a function with Vite 7 during svelte-check #18742

@p0ns

Description

@p0ns

Description

When using @sentry/sveltekit with Vite 7.x and running svelte-check (or any operation that triggers Vite config resolution), the build fails with:

TypeError: sentryPlugins.find is not a function
    at makeCustomSentryVitePlugins (sourceMaps.js:74:55)
    at async sentrySvelteKit (sentryVitePlugins.js:64:31)
    at async Promise.all (index 0)
    at async asyncFlatten (config.js:2574:10)
    at async resolveConfig (config.js:35429:68)

Root Cause Analysis

After investigating the source code of @sentry/sveltekit, @sentry/vite-plugin, unplugin, and Vite, I found the issue:

1. unplugin returns inconsistent types

In [email protected] (which @sentry/vite-plugin depends on), the Vite adapter returns either a single plugin OR an array:

// unplugin/src/vite/index.ts
return plugins.length === 1 ? plugins[0] : plugins;  // Single object OR array!

2. @sentry/sveltekit assumes an array

In @sentry/sveltekit/src/vite/sourceMaps.ts (line 85-87):

const sentryPlugins: Plugin[] = await sentryVitePlugin(mergedOptions);
const sentryViteDebugIdUploadPlugin = sentryPlugins.find(  // FAILS if single object!
  plugin => plugin.name === 'sentry-vite-debug-id-upload-plugin',
);

3. During svelte-check, only one plugin is created

When svelte-check triggers config resolution, something causes sentryVitePlugin() to return only one plugin (likely the noop plugin when auth tokens aren't available), which unplugin converts to a single object instead of an array.

Steps to Reproduce

  1. Create a SvelteKit project with Vite 7.x
  2. Install @sentry/sveltekit
  3. Add sentrySvelteKit() to vite.config.ts plugins
  4. Run svelte-check or svelte-kit sync && svelte-check

Environment

  • @sentry/sveltekit: 10.32.1
  • vite: 7.3.0
  • @sveltejs/kit: 2.49.x
  • unplugin (transitive via @sentry/vite-plugin): 1.0.1

Workaround

Adding @sentry/sveltekit to ssr.external prevents Vite from bundling it during SSR/type-checking:

// vite.config.ts
export default defineConfig({
  plugins: [
    sentrySvelteKit({ /* ... */ }),
    sveltekit(),
  ],
  ssr: {
    external: ["@sentry/sveltekit"],  // Workaround for Vite 7 compatibility
  },
});

Suggested Fix

Either:

  1. Defensive check in sourceMaps.ts: Wrap the result in an array if it's not already:

    const sentryPlugins: Plugin[] = [await sentryVitePlugin(mergedOptions)].flat();
  2. Update unplugin dependency: The current @sentry/vite-plugin pins [email protected] (current is 2.3.11). Newer versions may handle this differently.

  3. Document the workaround: Add ssr.external to the SvelteKit setup docs for Vite 7 users.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions