Description
sentryCloudflareVitePlugin() does not wrap the Worker when main in the Wrangler config is a package specifier and not a file path. The build succeeds, but the bundle contains no Sentry code, and the plugin logs no warning.
@cloudflare/vite-plugin supports this config, and vinext generates it by default with vinext init --platform=cloudflare:
// vite.config.ts
export default defineConfig({
plugins: [
vinext(),
cloudflare({ viteEnvironment: { name: "rsc", childEnvironments: ["ssr"] } }),
sentryCloudflareVitePlugin(),
],
});
Tested with @sentry/cloudflare@11.0.0-rc.0, @cloudflare/vite-plugin@1.56.0, wrangler@4.135.0, vinext@1.0.0-beta.10 and Vite 8.3.
Cause
resolveWranglerConfig() calls unstable_readConfig() without preserveOriginalMain (wranglerConfig.ts#L70). Wrangler then resolves every main as a path relative to the config file:
main: "vinext/server/fetch-handler"
-> <project>/vinext/server/fetch-handler (file does not exist)
autoInstrument.ts compares this path with the module IDs in transform (L75), so no module matches. resolveInstrumentFile() also looks for instrument.server.* in the directory of this path (instrumentFile.ts#L23), and a package entry has no such file.
@cloudflare/vite-plugin handles the same value differently:
- It reads the config with
unstable_readConfig(..., { preserveOriginalMain: true }).
maybeResolveMain() makes main absolute only when it ends with a JS or TS extension.
- It resolves the entry with Vite:
await this.resolve(workerConfig.main).
Workaround
Add a local entry that re-exports the package entry, and set main to it. Put instrument.server.ts in the same directory:
// worker/index.ts
import handler from "vinext/server/fetch-handler";
export default handler;
Proposed fix
- Read the config with
preserveOriginalMain: true, and keep a package specifier unchanged.
- Resolve
main with this.resolve() (for example in buildStart, or lazily in transform), and compare the resolved module ID.
- When the resolved entry is in
node_modules, look for instrument.server.* in the Wrangler config directory (the Vite root) and not next to the entry.
- Log a warning when
main does not resolve to a module that the plugin transforms, so the failure is not silent.
- Add a test fixture where
main is a package specifier.
Description
sentryCloudflareVitePlugin()does not wrap the Worker whenmainin the Wrangler config is a package specifier and not a file path. The build succeeds, but the bundle contains no Sentry code, and the plugin logs no warning.@cloudflare/vite-pluginsupports this config, and vinext generates it by default withvinext init --platform=cloudflare:Tested with
@sentry/cloudflare@11.0.0-rc.0,@cloudflare/vite-plugin@1.56.0,wrangler@4.135.0,vinext@1.0.0-beta.10and Vite 8.3.Cause
resolveWranglerConfig()callsunstable_readConfig()withoutpreserveOriginalMain(wranglerConfig.ts#L70). Wrangler then resolves everymainas a path relative to the config file:autoInstrument.tscompares this path with the module IDs intransform(L75), so no module matches.resolveInstrumentFile()also looks forinstrument.server.*in the directory of this path (instrumentFile.ts#L23), and a package entry has no such file.@cloudflare/vite-pluginhandles the same value differently:unstable_readConfig(..., { preserveOriginalMain: true }).maybeResolveMain()makesmainabsolute only when it ends with a JS or TS extension.await this.resolve(workerConfig.main).Workaround
Add a local entry that re-exports the package entry, and set
mainto it. Putinstrument.server.tsin the same directory:{ "main": "./worker/index.ts" }Proposed fix
preserveOriginalMain: true, and keep a package specifier unchanged.mainwiththis.resolve()(for example inbuildStart, or lazily intransform), and compare the resolved module ID.node_modules, look forinstrument.server.*in the Wrangler config directory (the Vite root) and not next to the entry.maindoes not resolve to a module that the plugin transforms, so the failure is not silent.mainis a package specifier.