From 629f09f133347f97459d8854c422150f0a3ac358 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 27 Jan 2025 13:15:55 +0100 Subject: [PATCH 1/2] fix(core): Check for dev mode via `process.env.NODE_ENV` --- packages/bundler-plugin-core/src/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 06ffb373..91c69c9a 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -84,6 +84,11 @@ export function sentryUnpluginFactory({ debug: userOptions.debug ?? false, }); + // Not a bulletproof check but should be good enough to at least sometimes determine + // if the plugin is called in dev/watch mode or for a prod build. The important part + // here is to avoid a false positive. False negatives are okay. + const isDevMode = process.env["NODE_ENV"] === "development"; + try { const dotenvFile = fs.readFileSync( path.join(process.cwd(), ".env.sentry-build-plugin"), @@ -106,7 +111,7 @@ export function sentryUnpluginFactory({ const options = normalizeUserOptions(userOptions); - if (unpluginMetaContext.watchMode || options.disable) { + if (isDevMode || options.disable) { return [ { name: "sentry-noop-plugin", @@ -274,7 +279,7 @@ export function sentryUnpluginFactory({ "Release injection disabled via `release.inject` option. Will not inject release." ); } else if (!options.release.name) { - logger.warn( + logger.debug( "No release name provided. Will not inject release. Please set the `release.name` option to identify your release." ); } else { @@ -316,7 +321,7 @@ export function sentryUnpluginFactory({ } if (!options.release.name) { - logger.warn( + logger.debug( "No release name provided. Will not create release. Please set the `release.name` option to identify your release." ); } else if (!options.authToken) { From e39a8b0a2044374731d67e93fcbe5c2b73a8942a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 28 Jan 2025 17:57:20 +0100 Subject: [PATCH 2/2] only disable CLI-using plugins, add tests --- packages/bundler-plugin-core/src/index.ts | 6 ++- .../rollup-plugin/test/public-api.test.ts | 41 +++++++++++++++++++ packages/vite-plugin/test/public-api.test.ts | 41 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 91c69c9a..159444bc 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -111,7 +111,7 @@ export function sentryUnpluginFactory({ const options = normalizeUserOptions(userOptions); - if (isDevMode || options.disable) { + if (options.disable) { return [ { name: "sentry-noop-plugin", @@ -324,6 +324,8 @@ export function sentryUnpluginFactory({ logger.debug( "No release name provided. Will not create release. Please set the `release.name` option to identify your release." ); + } else if (isDevMode) { + logger.debug("Running in development mode. Will not create release."); } else if (!options.authToken) { logger.warn( "No auth token provided. Will not create release. Please set the `authToken` option. You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/" @@ -372,6 +374,8 @@ export function sentryUnpluginFactory({ logger.debug( "Source map upload was disabled. Will not upload sourcemaps using debug ID process." ); + } else if (isDevMode) { + logger.debug("Running in development mode. Will not upload sourcemaps."); } else if (!options.authToken) { logger.warn( "No auth token provided. Will not upload source maps. Please set the `authToken` option. You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/" diff --git a/packages/rollup-plugin/test/public-api.test.ts b/packages/rollup-plugin/test/public-api.test.ts index c143ef91..ba69f60e 100644 --- a/packages/rollup-plugin/test/public-api.test.ts +++ b/packages/rollup-plugin/test/public-api.test.ts @@ -7,6 +7,10 @@ test("Rollup plugin should exist", () => { }); describe("sentryRollupPlugin", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + it("returns an array of rollup plugins", () => { const plugins = sentryRollupPlugin({ authToken: "test-token", @@ -27,4 +31,41 @@ describe("sentryRollupPlugin", () => { "sentry-file-deletion-plugin", ]); }); + + it("doesn't include release management and debug id upload plugins if NODE_ENV is 'development'", () => { + const originalNodeEnv = process.env["NODE_ENV"]; + process.env["NODE_ENV"] = "development"; + + const consoleSpy = jest.spyOn(console, "debug").mockImplementation(() => { + /* avoid test output pollution */ + }); + + const plugins = sentryRollupPlugin({ + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: true, + }) as Plugin[]; + + expect(Array.isArray(plugins)).toBe(true); + + const pluginNames = plugins.map((plugin) => plugin.name); + + expect(pluginNames).toEqual([ + "sentry-telemetry-plugin", + "sentry-rollup-release-injection-plugin", + "sentry-rollup-debug-id-injection-plugin", + "sentry-file-deletion-plugin", + ]); + + expect(consoleSpy).toHaveBeenCalledWith( + expect.stringContaining("Running in development mode. Will not create release.") + ); + + expect(consoleSpy).toHaveBeenCalledWith( + expect.stringContaining("Running in development mode. Will not upload sourcemaps.") + ); + + process.env["NODE_ENV"] = originalNodeEnv; + }); }); diff --git a/packages/vite-plugin/test/public-api.test.ts b/packages/vite-plugin/test/public-api.test.ts index a15fc336..ccf11af2 100644 --- a/packages/vite-plugin/test/public-api.test.ts +++ b/packages/vite-plugin/test/public-api.test.ts @@ -7,6 +7,10 @@ test("Vite plugin should exist", () => { }); describe("sentryVitePlugin", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + it("returns an array of Vite plugins", () => { const plugins = sentryVitePlugin({ authToken: "test-token", @@ -27,4 +31,41 @@ describe("sentryVitePlugin", () => { "sentry-file-deletion-plugin", ]); }); + + it("doesn't include release management and debug id upload plugins if NODE_ENV is 'development'", () => { + const originalNodeEnv = process.env["NODE_ENV"]; + process.env["NODE_ENV"] = "development"; + + const consoleSpy = jest.spyOn(console, "debug").mockImplementation(() => { + /* avoid test output pollution */ + }); + + const plugins = sentryVitePlugin({ + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: true, + }) as VitePlugin[]; + + expect(Array.isArray(plugins)).toBe(true); + + const pluginNames = plugins.map((plugin) => plugin.name); + + expect(pluginNames).toEqual([ + "sentry-telemetry-plugin", + "sentry-vite-release-injection-plugin", + "sentry-vite-debug-id-injection-plugin", + "sentry-file-deletion-plugin", + ]); + + expect(consoleSpy).toHaveBeenCalledWith( + expect.stringContaining("Running in development mode. Will not create release.") + ); + + expect(consoleSpy).toHaveBeenCalledWith( + expect.stringContaining("Running in development mode. Will not upload sourcemaps.") + ); + + process.env["NODE_ENV"] = originalNodeEnv; + }); });