Skip to content

ref(nextjs): Extract isBuild into an exported function #5444

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
Jul 26, 2022
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
21 changes: 3 additions & 18 deletions packages/nextjs/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { escapeStringForRegex, logger } from '@sentry/utils';
import * as domainModule from 'domain';
import * as path from 'path';

import { isBuild } from './utils/isBuild';
import { buildMetadata } from './utils/metadata';
import { NextjsOptions } from './utils/nextjsOptions';
import { addIntegration } from './utils/userIntegrations';
Expand All @@ -21,23 +22,6 @@ export { ErrorBoundary, showReportDialog, withErrorBoundary } from '@sentry/reac
type GlobalWithDistDir = typeof global & { __rewriteFramesDistDir__: string };
const domain = domainModule as typeof domainModule & { active: (domainModule.Domain & Carrier) | null };

// During build, the main process is invoked by
// `node next build`
// and child processes are invoked as
// `node <path>/node_modules/.../jest-worker/processChild.js`.
// The former is (obviously) easy to recognize, but the latter could happen at runtime as well. Fortunately, the main
// process hits this file before any of the child processes do, so we're able to set an env variable which the child
// processes can then check. During runtime, the main process is invoked as
// `node next start`
// or
// `node /var/runtime/index.js`,
// so we never drop into the `if` in the first place.
let isBuild = false;
if (process.argv.includes('build') || process.env.SENTRY_BUILD_PHASE) {
process.env.SENTRY_BUILD_PHASE = 'true';
isBuild = true;
}

const isVercel = !!process.env.VERCEL;

/** Inits the Sentry NextJS SDK on node. */
Expand Down Expand Up @@ -140,12 +124,13 @@ function addServerIntegrations(options: NextjsOptions): void {
export type { SentryWebpackPluginOptions } from './config/types';
export { withSentryConfig } from './config';
export { withSentry } from './utils/withSentry';
export { isBuild } from './utils/isBuild';

// Wrap various server methods to enable error monitoring and tracing. (Note: This only happens for non-Vercel
// deployments, because the current method of doing the wrapping a) crashes Next 12 apps deployed to Vercel and
// b) doesn't work on those apps anyway. We also don't do it during build, because there's no server running in that
// phase.)
if (!isVercel && !isBuild) {
if (!isVercel && !isBuild()) {
// Dynamically require the file because even importing from it causes Next 12 to crash on Vercel.
// In environments where the JS file doesn't exist, such as testing, import the TS file.
try {
Expand Down
22 changes: 22 additions & 0 deletions packages/nextjs/src/utils/isBuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Decide if the currently running process is part of the build phase or happening at runtime.
*/
export function isBuild(): boolean {
// During build, the main process is invoked by
// `node next build`
// and child processes are invoked as
// `node <path>/node_modules/.../jest-worker/processChild.js`.
// The former is (obviously) easy to recognize, but the latter could happen at runtime as well. Fortunately, the main
// process hits this file before any of the child processes do, so we're able to set an env variable which the child
// processes can then check. During runtime, the main process is invoked as
// `node next start`
// or
// `node /var/runtime/index.js`,
// so we never drop into the `if` in the first place.
if (process.argv.includes('build') || process.env.SENTRY_BUILD_PHASE) {
process.env.SENTRY_BUILD_PHASE = 'true';
return true;
}

return false;
}
51 changes: 51 additions & 0 deletions packages/nextjs/test/utils/isBuild.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { isBuild } from '../../src/utils/isBuild';

let originalEnv: typeof process.env;
let originalArgv: typeof process.argv;

function assertNoMagicValues(): void {
if (Object.keys(process.env).includes('SENTRY_BUILD_PHASE') || process.argv.includes('build')) {
throw new Error('Not starting test with a clean setup');
}
}

describe('isBuild()', () => {
beforeEach(() => {
assertNoMagicValues();
originalEnv = { ...process.env };
originalArgv = [...process.argv];
});

afterEach(() => {
process.env = originalEnv;
process.argv = originalArgv;
assertNoMagicValues();
});

it("detects 'build' in argv", () => {
// the result of calling `next build`
process.argv = ['/abs/path/to/node', '/abs/path/to/nextjs/excecutable', 'build'];
expect(isBuild()).toBe(true);
});

it("sets env var when 'build' in argv", () => {
// the result of calling `next build`
process.argv = ['/abs/path/to/node', '/abs/path/to/nextjs/excecutable', 'build'];
isBuild();
expect(Object.keys(process.env).includes('SENTRY_BUILD_PHASE')).toBe(true);
});

it("does not set env var when 'build' not in argv", () => {
isBuild();
expect(Object.keys(process.env).includes('SENTRY_BUILD_PHASE')).toBe(false);
});

it('detects env var', () => {
process.env.SENTRY_BUILD_PHASE = 'true';
expect(isBuild()).toBe(true);
});

it("returns false when 'build' not in `argv` and env var not present", () => {
expect(isBuild()).toBe(false);
});
});