Skip to content

Commit b56403a

Browse files
authored
fix: propagate the debug option to the cli (#810)
* fix: propagate the debug option to the cli * fix: only set the variable if not already set
1 parent a745b3a commit b56403a

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

packages/bundler-plugin-core/src/build-plugin-manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ export function createSentryBuildPluginManager(
220220
"SENTRY_PIPELINE"
221221
] = `${bundlerPluginMetaContext.buildTool}-plugin/${__PACKAGE_VERSION__}`;
222222

223+
// Propagate debug flag to Sentry CLI via environment variable
224+
// Only set if not already defined to respect user's explicit configuration
225+
if (options.debug && !process.env["SENTRY_LOG_LEVEL"]) {
226+
process.env["SENTRY_LOG_LEVEL"] = "debug";
227+
}
228+
223229
// Not a bulletproof check but should be good enough to at least sometimes determine
224230
// if the plugin is called in dev/watch mode or for a prod build. The important part
225231
// here is to avoid a false positive. False negatives are okay.

packages/bundler-plugin-core/test/build-plugin-manager.test.ts

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,174 @@ const mockPrepareBundleForDebugIdUpload = prepareBundleForDebugIdUpload as jest.
4444
describe("createSentryBuildPluginManager", () => {
4545
beforeEach(() => {
4646
jest.clearAllMocks();
47+
// Clean up environment variables
48+
delete process.env["SENTRY_LOG_LEVEL"];
49+
});
50+
51+
describe("debug option", () => {
52+
it("should set SENTRY_LOG_LEVEL environment variable when debug is true", () => {
53+
createSentryBuildPluginManager(
54+
{
55+
authToken: "test-token",
56+
org: "test-org",
57+
project: "test-project",
58+
debug: true,
59+
},
60+
{
61+
buildTool: "webpack",
62+
loggerPrefix: "[sentry-webpack-plugin]",
63+
}
64+
);
65+
66+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug");
67+
});
68+
69+
it("should NOT override existing SENTRY_LOG_LEVEL even when debug is true", () => {
70+
// User explicitly set SENTRY_LOG_LEVEL to "info"
71+
process.env["SENTRY_LOG_LEVEL"] = "info";
72+
73+
createSentryBuildPluginManager(
74+
{
75+
authToken: "test-token",
76+
org: "test-org",
77+
project: "test-project",
78+
debug: true,
79+
},
80+
{
81+
buildTool: "webpack",
82+
loggerPrefix: "[sentry-webpack-plugin]",
83+
}
84+
);
85+
86+
// Should respect the user's explicit setting
87+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("info");
88+
});
89+
90+
it("should not set SENTRY_LOG_LEVEL environment variable when debug is false", () => {
91+
createSentryBuildPluginManager(
92+
{
93+
authToken: "test-token",
94+
org: "test-org",
95+
project: "test-project",
96+
debug: false,
97+
},
98+
{
99+
buildTool: "webpack",
100+
loggerPrefix: "[sentry-webpack-plugin]",
101+
}
102+
);
103+
104+
expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined();
105+
});
106+
107+
it("should not set SENTRY_LOG_LEVEL environment variable when debug is not specified", () => {
108+
createSentryBuildPluginManager(
109+
{
110+
authToken: "test-token",
111+
org: "test-org",
112+
project: "test-project",
113+
},
114+
{
115+
buildTool: "webpack",
116+
loggerPrefix: "[sentry-webpack-plugin]",
117+
}
118+
);
119+
120+
expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined();
121+
});
122+
123+
it("should have SENTRY_LOG_LEVEL set when CLI operations are performed with debug enabled", async () => {
124+
mockCliExecute.mockImplementation(() => {
125+
// Verify the environment variable is set at the time the CLI is called
126+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug");
127+
return Promise.resolve(undefined);
128+
});
129+
130+
const buildPluginManager = createSentryBuildPluginManager(
131+
{
132+
authToken: "test-token",
133+
org: "test-org",
134+
project: "test-project",
135+
debug: true,
136+
},
137+
{
138+
buildTool: "webpack",
139+
loggerPrefix: "[sentry-webpack-plugin]",
140+
}
141+
);
142+
143+
// Verify it's set immediately after creation
144+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug");
145+
146+
// Perform a CLI operation and verify the env var is still set
147+
await buildPluginManager.injectDebugIds(["/path/to/bundle"]);
148+
149+
expect(mockCliExecute).toHaveBeenCalled();
150+
});
151+
152+
it("should have SENTRY_LOG_LEVEL set during error scenarios with debug enabled", async () => {
153+
// Simulate CLI error
154+
mockCliExecute.mockImplementation(() => {
155+
// Verify the environment variable is set even when CLI encounters an error
156+
// This ensures the CLI won't emit the "Add --log-level=debug" warning
157+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug");
158+
return Promise.reject(new Error("CLI error"));
159+
});
160+
161+
const buildPluginManager = createSentryBuildPluginManager(
162+
{
163+
authToken: "test-token",
164+
org: "test-org",
165+
project: "test-project",
166+
debug: true,
167+
},
168+
{
169+
buildTool: "webpack",
170+
loggerPrefix: "[sentry-webpack-plugin]",
171+
}
172+
);
173+
174+
// Verify it's set before the error
175+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug");
176+
177+
// Perform a CLI operation that will fail
178+
await buildPluginManager.injectDebugIds(["/path/to/bundle"]);
179+
180+
// The error should have been caught, but env var should still be set
181+
expect(process.env["SENTRY_LOG_LEVEL"]).toBe("debug");
182+
});
183+
184+
it("should NOT have SENTRY_LOG_LEVEL set during error scenarios when debug is disabled", async () => {
185+
// Simulate CLI error
186+
mockCliExecute.mockImplementation(() => {
187+
// Verify the environment variable is NOT set
188+
// In this case, the CLI WOULD emit the "Add --log-level=debug" warning
189+
expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined();
190+
return Promise.reject(new Error("CLI error"));
191+
});
192+
193+
const buildPluginManager = createSentryBuildPluginManager(
194+
{
195+
authToken: "test-token",
196+
org: "test-org",
197+
project: "test-project",
198+
debug: false,
199+
},
200+
{
201+
buildTool: "webpack",
202+
loggerPrefix: "[sentry-webpack-plugin]",
203+
}
204+
);
205+
206+
// Verify it's not set
207+
expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined();
208+
209+
// Perform a CLI operation that will fail
210+
await buildPluginManager.injectDebugIds(["/path/to/bundle"]);
211+
212+
// The error should have been caught, and env var should still not be set
213+
expect(process.env["SENTRY_LOG_LEVEL"]).toBeUndefined();
214+
});
47215
});
48216

49217
describe("when disabled", () => {

0 commit comments

Comments
 (0)