From eef77e4e2cd79ac0258a2738aa416c69ff217ce3 Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Fri, 10 Oct 2025 15:25:06 +0300 Subject: [PATCH 1/2] fix: propagate the debug option to the cli --- .../src/build-plugin-manager.ts | 5 + .../test/build-plugin-manager.test.ts | 147 ++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/packages/bundler-plugin-core/src/build-plugin-manager.ts b/packages/bundler-plugin-core/src/build-plugin-manager.ts index 31f5a2eb..57004a0a 100644 --- a/packages/bundler-plugin-core/src/build-plugin-manager.ts +++ b/packages/bundler-plugin-core/src/build-plugin-manager.ts @@ -220,6 +220,11 @@ export function createSentryBuildPluginManager( "SENTRY_PIPELINE" ] = `${bundlerPluginMetaContext.buildTool}-plugin/${__PACKAGE_VERSION__}`; + // Propagate debug flag to Sentry CLI via environment variable + if (options.debug) { + process.env["SENTRY_LOG_LEVEL"] = "debug"; + } + // 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. diff --git a/packages/bundler-plugin-core/test/build-plugin-manager.test.ts b/packages/bundler-plugin-core/test/build-plugin-manager.test.ts index d9886ede..290b3749 100644 --- a/packages/bundler-plugin-core/test/build-plugin-manager.test.ts +++ b/packages/bundler-plugin-core/test/build-plugin-manager.test.ts @@ -44,6 +44,153 @@ const mockPrepareBundleForDebugIdUpload = prepareBundleForDebugIdUpload as jest. describe("createSentryBuildPluginManager", () => { beforeEach(() => { jest.clearAllMocks(); + // Clean up environment variables + delete process.env["SENTRY_LOG_LEVEL"]; + }); + + describe("debug option", () => { + it("should set SENTRY_LOG_LEVEL environment variable when debug is true", () => { + createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: true, + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); + }); + + it("should not set SENTRY_LOG_LEVEL environment variable when debug is false", () => { + createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: false, + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined(); + }); + + it("should not set SENTRY_LOG_LEVEL environment variable when debug is not specified", () => { + createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined(); + }); + + it("should have SENTRY_LOG_LEVEL set when CLI operations are performed with debug enabled", async () => { + mockCliExecute.mockImplementation(() => { + // Verify the environment variable is set at the time the CLI is called + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); + return Promise.resolve(undefined); + }); + + const buildPluginManager = createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: true, + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + // Verify it's set immediately after creation + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); + + // Perform a CLI operation and verify the env var is still set + await buildPluginManager.injectDebugIds(["/path/to/bundle"]); + + expect(mockCliExecute).toHaveBeenCalled(); + }); + + it("should have SENTRY_LOG_LEVEL set during error scenarios with debug enabled", async () => { + // Simulate CLI error + mockCliExecute.mockImplementation(() => { + // Verify the environment variable is set even when CLI encounters an error + // This ensures the CLI won't emit the "Add --log-level=debug" warning + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); + return Promise.reject(new Error("CLI error")); + }); + + const buildPluginManager = createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: true, + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + // Verify it's set before the error + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); + + // Perform a CLI operation that will fail + await buildPluginManager.injectDebugIds(["/path/to/bundle"]); + + // The error should have been caught, but env var should still be set + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); + }); + + it("should NOT have SENTRY_LOG_LEVEL set during error scenarios when debug is disabled", async () => { + // Simulate CLI error + mockCliExecute.mockImplementation(() => { + // Verify the environment variable is NOT set + // In this case, the CLI WOULD emit the "Add --log-level=debug" warning + expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined(); + return Promise.reject(new Error("CLI error")); + }); + + const buildPluginManager = createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: false, + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + // Verify it's not set + expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined(); + + // Perform a CLI operation that will fail + await buildPluginManager.injectDebugIds(["/path/to/bundle"]); + + // The error should have been caught, and env var should still not be set + expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined(); + }); }); describe("when disabled", () => { From ee430bd756a0ef49a4dbd3fc6b57827a163c776a Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Fri, 10 Oct 2025 16:35:04 +0300 Subject: [PATCH 2/2] fix: only set the variable if not already set --- .../src/build-plugin-manager.ts | 3 ++- .../test/build-plugin-manager.test.ts | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/bundler-plugin-core/src/build-plugin-manager.ts b/packages/bundler-plugin-core/src/build-plugin-manager.ts index 57004a0a..f0716178 100644 --- a/packages/bundler-plugin-core/src/build-plugin-manager.ts +++ b/packages/bundler-plugin-core/src/build-plugin-manager.ts @@ -221,7 +221,8 @@ export function createSentryBuildPluginManager( ] = `${bundlerPluginMetaContext.buildTool}-plugin/${__PACKAGE_VERSION__}`; // Propagate debug flag to Sentry CLI via environment variable - if (options.debug) { + // Only set if not already defined to respect user's explicit configuration + if (options.debug && !process.env["SENTRY_LOG_LEVEL"]) { process.env["SENTRY_LOG_LEVEL"] = "debug"; } diff --git a/packages/bundler-plugin-core/test/build-plugin-manager.test.ts b/packages/bundler-plugin-core/test/build-plugin-manager.test.ts index 290b3749..be6a2f20 100644 --- a/packages/bundler-plugin-core/test/build-plugin-manager.test.ts +++ b/packages/bundler-plugin-core/test/build-plugin-manager.test.ts @@ -66,6 +66,27 @@ describe("createSentryBuildPluginManager", () => { expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug"); }); + it("should NOT override existing SENTRY_LOG_LEVEL even when debug is true", () => { + // User explicitly set SENTRY_LOG_LEVEL to "info" + process.env["SENTRY_LOG_LEVEL"] = "info"; + + createSentryBuildPluginManager( + { + authToken: "test-token", + org: "test-org", + project: "test-project", + debug: true, + }, + { + buildTool: "webpack", + loggerPrefix: "[sentry-webpack-plugin]", + } + ); + + // Should respect the user's explicit setting + expect(process.env["SENTRY_LOG_LEVEL"]).toBe("info"); + }); + it("should not set SENTRY_LOG_LEVEL environment variable when debug is false", () => { createSentryBuildPluginManager( {