Skip to content
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

fix(nextjs): Use posix paths for sourcemap uploads #13603

Merged
merged 2 commits into from
Sep 6, 2024
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
36 changes: 21 additions & 15 deletions packages/nextjs/src/config/webpackPluginOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,43 @@ export function getWebpackPluginOptions(
buildContext: BuildContext,
sentryBuildOptions: SentryBuildOptions,
): SentryWebpackPluginOptions {
const { buildId, isServer, config: userNextConfig, dir: projectDir, nextRuntime } = buildContext;
const { buildId, isServer, config: userNextConfig, dir, nextRuntime } = buildContext;

const prefixInsert = !isServer ? 'Client' : nextRuntime === 'edge' ? 'Edge' : 'Node.js';

const distDirAbsPath = path.join(projectDir, (userNextConfig as NextConfigObject).distDir || '.next'); // `.next` is the default directory
// We need to convert paths to posix because Glob patterns use `\` to escape
// glob characters. This clashes with Windows path separators.
// See: https://www.npmjs.com/package/glob
const projectDir = dir.replace(/\\/g, '/');
// `.next` is the default directory
const distDir = (userNextConfig as NextConfigObject).distDir?.replace(/\\/g, '/') ?? '.next';
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if we could use path.posix.normalize instead of replace and whether it would make this a bit more robust 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Normalize uses the platform specific separator, so even with .posix it would keep backslashes.

e.g.

path.posix.normalize('C:\\a\\b\\c')
-> 'C:\\a\\b\\c'

const distDirAbsPath = path.posix.join(projectDir, distDir);

let sourcemapUploadAssets: string[] = [];
const sourcemapUploadIgnore: string[] = [];

if (isServer) {
sourcemapUploadAssets.push(
path.join(distDirAbsPath, 'server', '**'), // This is normally where Next.js outputs things
path.join(distDirAbsPath, 'serverless', '**'), // This was the output location for serverless Next.js
path.posix.join(distDirAbsPath, 'server', '**'), // This is normally where Next.js outputs things
path.posix.join(distDirAbsPath, 'serverless', '**'), // This was the output location for serverless Next.js
);
} else {
if (sentryBuildOptions.widenClientFileUpload) {
sourcemapUploadAssets.push(path.join(distDirAbsPath, 'static', 'chunks', '**'));
sourcemapUploadAssets.push(path.posix.join(distDirAbsPath, 'static', 'chunks', '**'));
} else {
sourcemapUploadAssets.push(
path.join(distDirAbsPath, 'static', 'chunks', 'pages', '**'),
path.join(distDirAbsPath, 'static', 'chunks', 'app', '**'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'pages', '**'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'app', '**'),
);
}

// TODO: We should think about uploading these when `widenClientFileUpload` is `true`. They may be useful in some situations.
sourcemapUploadIgnore.push(
path.join(distDirAbsPath, 'static', 'chunks', 'framework-*'),
path.join(distDirAbsPath, 'static', 'chunks', 'framework.*'),
path.join(distDirAbsPath, 'static', 'chunks', 'main-*'),
path.join(distDirAbsPath, 'static', 'chunks', 'polyfills-*'),
path.join(distDirAbsPath, 'static', 'chunks', 'webpack-*'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'framework-*'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'framework.*'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'main-*'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'polyfills-*'),
path.posix.join(distDirAbsPath, 'static', 'chunks', 'webpack-*'),
);
}

Expand Down Expand Up @@ -79,9 +85,9 @@ export function getWebpackPluginOptions(
// We only care to delete client bundle source maps because they would be the ones being served.
// Removing the server source maps crashes Vercel builds for (thus far) unknown reasons:
// /~https://github.com/getsentry/sentry-javascript/issues/13099
path.join(distDirAbsPath, 'static', '**', '*.js.map'),
path.join(distDirAbsPath, 'static', '**', '*.mjs.map'),
path.join(distDirAbsPath, 'static', '**', '*.cjs.map'),
path.posix.join(distDirAbsPath, 'static', '**', '*.js.map'),
path.posix.join(distDirAbsPath, 'static', '**', '*.mjs.map'),
path.posix.join(distDirAbsPath, 'static', '**', '*.cjs.map'),
]
: undefined,
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.sourcemaps,
Expand Down
22 changes: 21 additions & 1 deletion packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import type { BuildContext, NextConfigObject } from '../../../src/config/types';
import { getWebpackPluginOptions } from '../../../src/config/webpackPluginOptions';

function generateBuildContext(overrides: {
dir?: string;
isServer: boolean;
nextjsConfig?: NextConfigObject;
}): BuildContext {
return {
dev: false, // The plugin is not included in dev mode
isServer: overrides.isServer,
buildId: 'test-build-id',
dir: '/my/project/dir',
dir: overrides.dir ?? '/my/project/dir',
config: overrides.nextjsConfig ?? {},
totalPages: 2,
defaultLoaders: true,
Expand Down Expand Up @@ -171,4 +172,23 @@ describe('getWebpackPluginOptions()', () => {
assets: [],
});
});

it('passes posix paths to the plugin', () => {
const buildContext = generateBuildContext({
dir: 'C:\\my\\windows\\project\\dir',
nextjsConfig: { distDir: '.dist\\v1' },
isServer: false,
});
const generatedPluginOptions = getWebpackPluginOptions(buildContext, { widenClientFileUpload: true });
expect(generatedPluginOptions.sourcemaps).toMatchObject({
assets: ['C:/my/windows/project/dir/.dist/v1/static/chunks/**'],
ignore: [
'C:/my/windows/project/dir/.dist/v1/static/chunks/framework-*',
'C:/my/windows/project/dir/.dist/v1/static/chunks/framework.*',
'C:/my/windows/project/dir/.dist/v1/static/chunks/main-*',
'C:/my/windows/project/dir/.dist/v1/static/chunks/polyfills-*',
'C:/my/windows/project/dir/.dist/v1/static/chunks/webpack-*',
],
});
});
});
Loading