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): Support custom distDir values in default RewriteFrames integration #4017

Merged
merged 7 commits into from
Oct 13, 2021

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Sep 28, 2021

This is a follow-up to #3990, which makes sure sourcemaps get uploaded from the correct spot when using the nextjs distDir option.

To make sure that the filenames in stacktrace frames match the names of said sourcemaps, we use the RewriteFrames integration. Up until now, we've been hardcoding the replacement it should make, based on nextjs's default output directory. This makes it dynamic, by injecting code at build time which adds the relevant value into global, where it is accessible at runtime when we're instantiating the integration.

TODO:
Integrate user includes in getWebpackPluginOptions - this will happen in a separate PR

@github-actions
Copy link
Contributor

github-actions bot commented Sep 29, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 22.34 KB (+0.01% 🔺)
@sentry/browser - Webpack 23.31 KB (0%)
@sentry/react - Webpack 23.34 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 29.79 KB (+0.01% 🔺)

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-distdir branch from bb5487d to e84bb3f Compare September 30, 2021 07:15
@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-distdir branch 2 times, most recently from 3172dcd to 84b06b9 Compare October 4, 2021 23:44
@lobsterkatie lobsterkatie marked this pull request as ready for review October 5, 2021 05:11
Copy link
Contributor

@iker-barriocanal iker-barriocanal left a comment

Choose a reason for hiding this comment

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

This still breaks the build of my Next.js project.

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-distdir branch from 478e857 to 2d7beeb Compare October 11, 2021 05:12
lobsterkatie added a commit that referenced this pull request Oct 11, 2021
…ig` (#4056)

This removes the logic which manipulates the user's webpack plugin options to include the nextjs config's `distDir` option (if set) in the plugin option's `include` property, as that manipulation results in more files than we want being uploaded (by essentially undoing #3845). 

A more selective version of this merging will be included in #4017.
@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-distdir branch from da18c45 to 55c1b63 Compare October 12, 2021 05:19
Comment on lines +152 to 154
// inject into all entry points which might contain user's code
for (const entryPointName in newEntryProperty) {
if (shouldAddSentryToEntryPoint(entryPointName)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to the PR, but if we set proper names we don't need this comment. What do you think of this suggestion? Do you have other alternatives?

Suggested change
// inject into all entry points which might contain user's code
for (const entryPointName in newEntryProperty) {
if (shouldAddSentryToEntryPoint(entryPointName)) {
for (const entryPointName in newEntryProperty) {
if (containsUserCode(entryPointName)) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm - interesting. I'd probably go for mightContainUserCode, but it's not a bad idea. I'm not going to do it in this PR, but I need to make a separate one adding _error to the filter, so I can do it there.

Comment on lines +53 to +57
// nextjs always puts the build directory at the project root level, which is also where you run `next start` from, so
// we can read in the project directory from the currently running process
const distDirAbsPath = path.resolve(process.cwd(), distDirName);
Copy link
Contributor

Choose a reason for hiding this comment

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

nextjs always puts the build directory at the project root level

IMO this isn't precise enough, since:

distDir should not leave your project directory. For example, ../build is an invalid directory.

From Next.js docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean your build directory could be < projAbsPath >/stuff/things/build? You'd still need to set distDir: "stuff/things/build" , so it would still work. Or am I misunderstanding your concern?

FWIW, the reason for doing it there, as cwd(), rather than including the project directory in the globally-stored value is that while it does run from < projAbsPath >/stuff/things/build on a normal deployment, when it runs on AWS it's /var/task/stuff/things/build.

Also, if it helps, you can see in the server constructor that it uses "." as the project directory and then sticks the distDir at that level of the file structure.

Copy link
Contributor

Choose a reason for hiding this comment

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

My comment was about the comment, not the code. IMO, if we're adding comments they should be as specific as possible, since it may create confusion over existing code. Instead of "always puts the build directory at the project root level", we can say "the build directory is a subdirectory of the project root". The difference is that the first sentence doesn't say anything about the restrictions of the value of what users set; while the second one does, and covers both the nextjs' and users' values (the only case in which this is not true is when users have distDir configured incorrectly, and this not happening is IMO an assumption we can make here).

Not blocking with this, so your call.

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-distdir branch from 55c1b63 to 79f795f Compare October 12, 2021 19:39
@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-distdir branch from 79f795f to c321ade Compare October 12, 2021 19:51
Copy link
Contributor

@iker-barriocanal iker-barriocanal left a comment

Choose a reason for hiding this comment

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

Works like a charm! 🚀

@lobsterkatie lobsterkatie merged commit 965714e into master Oct 13, 2021
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-support-distdir branch October 13, 2021 18:52
AbhiPrasad pushed a commit that referenced this pull request Nov 5, 2021
#4017 introduced a bug wherein running the dev server would lead to an infinite recompilation loop. (Something about the implementation tricks the file watcher into thinking there's been a change when there hasn't.) Fortunately, since we don't upload sourcemaps in dev, the change made by that PR (accounting for the nextjs distDir option in our server-side RewriteFrames integration, which in turn enables sourcemaps to work) is actually a moot point there. This PR solves the issue by simply not applying that change to the dev server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants