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

Sentry creates rewriteFramesHelper.js during Nextjs "standalone" static generation #4684

Closed
3 tasks done
calix opened this issue Mar 4, 2022 · 11 comments · Fixed by #5445
Closed
3 tasks done

Sentry creates rewriteFramesHelper.js during Nextjs "standalone" static generation #4684

calix opened this issue Mar 4, 2022 · 11 comments · Fixed by #5445

Comments

@calix
Copy link

calix commented Mar 4, 2022

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which package are you using?

@sentry/nextjs

SDK Version

6.18.1

Framework Version

No response

Link to Sentry event

No response

Steps to Reproduce

We are switching to generating a standalone bundle for our Nextjs project according to: https://nextjs.org/docs/advanced-features/output-file-tracing

This issue is possible to reproduce when bundling NextJS using:

module.exports = { experimental: { outputStandalone: true, }, }

next.config.js consumes the config using withSentryConfig as described in the docs:

module.exports = withSentryConfig(moduleExports, sentryWebpackPluginOptions);

We first detected the issue when our Azure pipeline failed at:

> Build error occurred [Error: ENOENT: no such file or directory, mkdir 'D:\a\1\s\.next\standalone\.next\server\pages\C:\Users\VSSADM~1\AppData\Local\Temp\sentry-Yr02pe'] { errno: -4058, code: 'ENOENT', syscall: 'mkdir', path: 'D:\\a\\1\\s\\.next\\standalone\\.next\\server\\pages\\C:\\Users\\VSSADM~1\\AppData\\Local\\Temp\\sentry-Yr02pe' } ##[error]Cmd.exe exited with code '1'.
Sentry is for some reason trying to generate a folder for the file rewritesFramesHelper.js during nextjs "Generating static pages".

When running locally, this directory is created in the root NextJS folder.

Expected Result

Expect sentry not to spam the project folder with directory sentry-xxxxx.

Actual Result

image

As you can see, Sentry is creating a folder Users...../sentry-xxxxxx containing rewritesFramesHelper.js.

@AbhiPrasad
Copy link
Member

This is the logic that concerns itself with this -

// Support non-default output directories by making the output path (easy to get here at build-time) available to the
// server SDK's default `RewriteFrames` instance (which needs it at runtime). Doesn't work when using the dev server
// because it somehow tricks the file watcher into thinking that compilation itself is a file change, triggering an
// infinite recompiling loop. (This should be fine because we don't upload sourcemaps in dev in any case.)
if (isServer && !isDev) {
const rewriteFramesHelper = path.resolve(
fs.mkdtempSync(path.resolve(os.tmpdir(), 'sentry-')),
'rewriteFramesHelper.js',
);
fs.writeFileSync(rewriteFramesHelper, `global.__rewriteFramesDistDir__ = '${userNextConfig.distDir}';\n`);
// stick our helper file ahead of the user's config file so the value is in the global namespace *before*
// `Sentry.init()` is called
filesToInject.unshift(rewriteFramesHelper);
}
, but it seems that it doesn't account for .next/standalone.

Adding to the backlog, but PRs are welcome for anyone who wants to help out!

@lobsterkatie
Copy link
Member

This is related to #4382, or rather, solving one will solve both.

Two possible approaches:

  1. Do what the webpack plugin does and rather than create a new file, have a placeholder file whose contents we replace. Potential problem: Can we guarantee we know where that file will be within node_modules?

  2. Instead of adding a new file to the webpack entry property, create a loader which inserts the code directly into the necessary files.

@calix
Copy link
Author

calix commented Mar 14, 2022

I can mention that we have temporarily solved this by patching (using patch-package) the node module for this library. Its not a pretty patch but seems to work.

@bogdankatishev
Copy link

Hello, we also have this problem, when building the nextjs app with outputStandalone: true on our Jenkins, we get the following error message:

> Build error occurred
[Error: EACCES: permission denied, mkdir '/var/lib/tmp'] {
  errno: -13,
  code: 'EACCES',
  syscall: 'mkdir',
  path: '/var/lib/tmp'
}

Of course this path is protected on our Jenkins.

When debugging locally, I found out that Sentry writes a JS file to the tmp dir:

cat /var/lib/tmp/sentry-BLeAPp/rewriteFramesHelper.js 
global.__rewriteFramesDistDir__ = '.next';

@lobsterkatie
Copy link
Member

lobsterkatie commented Mar 17, 2022

When debugging locally, I found out that Sentry writes a JS file to the tmp dir

Yup, that is indeed what we do. Interesting that it's protected... I would have said the whole point of a temp folder was for processes to be able to throw whatever temporary files they need to in there without having to worry about cleaning them up, and protecting it completely defeats that purpose.

Regardless, in the long run, I think it's reasonable to change our approach, using one of the two strategies I outlined above. In the short run, would any of the strategies listed here solve your problem?

@webPlayer7
Copy link

I met the same problem. rewriteFramesHelper.js is created in other folder. Is there any way we can fix this quickly?

@svict4
Copy link

svict4 commented May 13, 2022

I can mention that we have temporarily solved this by patching (using patch-package) the node module for this library. Its not a pretty patch but seems to work.

Hey @calix can you share your patch, however ugly?

@webPlayer7
Copy link

That error disappear when I build in Linux. It occurs when only windows OS

@GorlikItsMe
Copy link

I meet the same problem, during build sentry want create new folder but with weird path
D:\\MyProjects\\next-app\\.next\\standalone\\.next\\server\\pages\\C:\\Users\\USER\\AppData\\Local\\Temp\\sentry-zVQYAF
You see it try connect 2 paths. Error occurs on windows.

@lesderid
Copy link

Setting the TMP environment variable to a directory inside of my project tree fixed the mkdir issue for me.

@lobsterkatie
Copy link
Member

Sorry - This should have been marked as fixed in #5445, which was first included in version 7.8.0. I'm going to close this, but please let me know if it's still happening for you in a later version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants