Skip to content

fix(core): Disable release creation and source maps upload in dev mode #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -106,7 +111,7 @@ export function sentryUnpluginFactory({

const options = normalizeUserOptions(userOptions);

if (unpluginMetaContext.watchMode || options.disable) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is technically not the goal of this PR but thinking about this logic more holistically we probably don't want to disable the entire plugin for dev mode because we have other things that may be valuable in dev mode, like react component annotations or tree shaking utilities (I guess for debugging purposes).

Should we limit this check to just anything that has a side-effect/is invoking Sentry CLI?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, we can adjust this further. I think what we want to disable is release management and sourcemaps upload, right? release and debugId injection should be fine in dev as well, right?

if (options.disable) {
return [
{
name: "sentry-noop-plugin",
Expand Down Expand Up @@ -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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also took the liberty to relax the log level here. Releases aren't required anymore for debugId-based upload and I've seen build logs this and the log below were added (in fact multiple times due to multi-build-setups of meta frameworks like SvelteKit and Nuxt)

"No release name provided. Will not inject release. Please set the `release.name` option to identify your release."
);
} else {
Expand Down Expand Up @@ -316,9 +321,11 @@ 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 (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/"
Expand Down Expand Up @@ -367,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/"
Expand Down
41 changes: 41 additions & 0 deletions packages/rollup-plugin/test/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
});
});
41 changes: 41 additions & 0 deletions packages/vite-plugin/test/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
});
});
Loading