GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(nextjs): Use posix paths for sourcemap uploads by andreiborza · Pull Request #13603 · getsentry/sentry-javascript

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

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.

Sign up for GitHub

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
andreiborza merged 2 commits into develop from ab/posix-paths-sourcemap-uploads
Sep 6, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
548a402
fix(nextjs): Use posix paths for sourcemap uploads
andreiborza Sep 6, 2024
714e451
Test `distDir` as well
andreiborza Sep 6, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

36 changes: 21 additions & 15 deletions 36 packages/nextjs/src/config/webpackPluginOptions.ts
Show comments
View file
Open in desktop
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 thread
Comment on lines +21 to +23

@lforst lforst Sep 6, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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 🤔

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andreiborza andreiborza Sep 6, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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'

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lforst reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
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 22 packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts
Show comments
View file
Open in desktop
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-*',
],
});
});
});
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.