Skip to content

Commit 548a402

Browse files
committed
fix(nextjs): Use posix paths for sourcemap uploads
Currently, we use `path.join` to provide sourcemap directories. On Windows, the resulting strings would use `\` which is an escape character for glob patterns and thus no sourcemaps would be found. This fix ensures we use posix paths which glob then handles correctly on Windows too. Closes: #13270 Partly fixes: #13288
1 parent a8e5f59 commit 548a402

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

packages/nextjs/src/config/webpackPluginOptions.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,43 @@ export function getWebpackPluginOptions(
1111
buildContext: BuildContext,
1212
sentryBuildOptions: SentryBuildOptions,
1313
): SentryWebpackPluginOptions {
14-
const { buildId, isServer, config: userNextConfig, dir: projectDir, nextRuntime } = buildContext;
14+
const { buildId, isServer, config: userNextConfig, dir, nextRuntime } = buildContext;
1515

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

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

2026
let sourcemapUploadAssets: string[] = [];
2127
const sourcemapUploadIgnore: string[] = [];
2228

2329
if (isServer) {
2430
sourcemapUploadAssets.push(
25-
path.join(distDirAbsPath, 'server', '**'), // This is normally where Next.js outputs things
26-
path.join(distDirAbsPath, 'serverless', '**'), // This was the output location for serverless Next.js
31+
path.posix.join(distDirAbsPath, 'server', '**'), // This is normally where Next.js outputs things
32+
path.posix.join(distDirAbsPath, 'serverless', '**'), // This was the output location for serverless Next.js
2733
);
2834
} else {
2935
if (sentryBuildOptions.widenClientFileUpload) {
30-
sourcemapUploadAssets.push(path.join(distDirAbsPath, 'static', 'chunks', '**'));
36+
sourcemapUploadAssets.push(path.posix.join(distDirAbsPath, 'static', 'chunks', '**'));
3137
} else {
3238
sourcemapUploadAssets.push(
33-
path.join(distDirAbsPath, 'static', 'chunks', 'pages', '**'),
34-
path.join(distDirAbsPath, 'static', 'chunks', 'app', '**'),
39+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'pages', '**'),
40+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'app', '**'),
3541
);
3642
}
3743

3844
// TODO: We should think about uploading these when `widenClientFileUpload` is `true`. They may be useful in some situations.
3945
sourcemapUploadIgnore.push(
40-
path.join(distDirAbsPath, 'static', 'chunks', 'framework-*'),
41-
path.join(distDirAbsPath, 'static', 'chunks', 'framework.*'),
42-
path.join(distDirAbsPath, 'static', 'chunks', 'main-*'),
43-
path.join(distDirAbsPath, 'static', 'chunks', 'polyfills-*'),
44-
path.join(distDirAbsPath, 'static', 'chunks', 'webpack-*'),
46+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'framework-*'),
47+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'framework.*'),
48+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'main-*'),
49+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'polyfills-*'),
50+
path.posix.join(distDirAbsPath, 'static', 'chunks', 'webpack-*'),
4551
);
4652
}
4753

@@ -79,9 +85,9 @@ export function getWebpackPluginOptions(
7985
// We only care to delete client bundle source maps because they would be the ones being served.
8086
// Removing the server source maps crashes Vercel builds for (thus far) unknown reasons:
8187
// https://github.com/getsentry/sentry-javascript/issues/13099
82-
path.join(distDirAbsPath, 'static', '**', '*.js.map'),
83-
path.join(distDirAbsPath, 'static', '**', '*.mjs.map'),
84-
path.join(distDirAbsPath, 'static', '**', '*.cjs.map'),
88+
path.posix.join(distDirAbsPath, 'static', '**', '*.js.map'),
89+
path.posix.join(distDirAbsPath, 'static', '**', '*.mjs.map'),
90+
path.posix.join(distDirAbsPath, 'static', '**', '*.cjs.map'),
8591
]
8692
: undefined,
8793
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.sourcemaps,

packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import type { BuildContext, NextConfigObject } from '../../../src/config/types';
22
import { getWebpackPluginOptions } from '../../../src/config/webpackPluginOptions';
33

44
function generateBuildContext(overrides: {
5+
dir?: string;
56
isServer: boolean;
67
nextjsConfig?: NextConfigObject;
78
}): BuildContext {
89
return {
910
dev: false, // The plugin is not included in dev mode
1011
isServer: overrides.isServer,
1112
buildId: 'test-build-id',
12-
dir: '/my/project/dir',
13+
dir: overrides.dir ?? '/my/project/dir',
1314
config: overrides.nextjsConfig ?? {},
1415
totalPages: 2,
1516
defaultLoaders: true,
@@ -171,4 +172,19 @@ describe('getWebpackPluginOptions()', () => {
171172
assets: [],
172173
});
173174
});
175+
176+
it('passes posix paths to the plugin', () => {
177+
const buildContext = generateBuildContext({ dir: 'C:\\my\\windows\\project\\dir', isServer: false });
178+
const generatedPluginOptions = getWebpackPluginOptions(buildContext, { widenClientFileUpload: true });
179+
expect(generatedPluginOptions.sourcemaps).toMatchObject({
180+
assets: ['C:/my/windows/project/dir/.next/static/chunks/**'],
181+
ignore: [
182+
'C:/my/windows/project/dir/.next/static/chunks/framework-*',
183+
'C:/my/windows/project/dir/.next/static/chunks/framework.*',
184+
'C:/my/windows/project/dir/.next/static/chunks/main-*',
185+
'C:/my/windows/project/dir/.next/static/chunks/polyfills-*',
186+
'C:/my/windows/project/dir/.next/static/chunks/webpack-*',
187+
],
188+
});
189+
})
174190
});

0 commit comments

Comments
 (0)