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

[Bug]: Plugin "@netlify/plugin-nextjs" internal error: ENOENT: no such file or directory with runtime 5.0.0-beta.7 #2420

Open
1 of 3 tasks
caomicc opened this issue Feb 6, 2024 · 9 comments
Labels
type: bug code to address defects in shipped code v5 Issues for v5 version of the runtime

Comments

@caomicc
Copy link

caomicc commented Feb 6, 2024

Summary

I'm having an issue with getting our site to run on the beta runtime. We are currently on next 13.4.1 and testing with 5.0.0-beta.7

We are using turborepo for this site and have custom build settings in our netlify.toml to reflect that

Once committed, the deploy will build successfully and then fail after the onBuild with the following error:

11:15:26 AM: (@netlify/plugin-nextjs onEnd completed in 4ms)
11:15:27 AM: ​
11:15:27 AM: Plugin "@netlify/plugin-nextjs" internal error                
11:15:27 AM: ────────────────────────────────────────────────────────────────
11:15:27 AM: ​
11:15:27 AM:   Error message
11:15:27 AM:   Error: ENOENT: no such file or directory, open "/opt/build/repo/apps/web/required-server-files.json"
11:15:27 AM: ​
11:15:27 AM:   Plugin details
11:15:27 AM:   Package:        @netlify/plugin-nextjs
11:15:27 AM:   Version:        5.0.0-beta.7
11:15:27 AM:   Repository:     git+https://github.com/netlify/next-runtime-minimal.git
11:15:27 AM:   npm link:       https://www.npmjs.com/package/@netlify/plugin-nextjs
11:15:27 AM:   Report issues:  https://github.com/netlify/next-runtime-minimal/issues
11:15:27 AM: ​
11:15:27 AM:   Error location
11:15:27 AM:   In "onBuild" event in "@netlify/plugin-nextjs" from netlify.toml and package.json
11:15:27 AM: ​
11:15:27 AM:   Error properties
11:15:27 AM:   {
11:15:27 AM:     errno: -2,
11:15:27 AM:     code: "ENOENT",
11:15:27 AM:     syscall: "open",
11:15:27 AM:     path: "/opt/build/repo/apps/web/required-server-files.json"
11:15:27 AM:   }
11:15:28 AM: Failed during stage "building site": Build script returned non-zero exit code: 4
11:15:28 AM: Build failed due to an internal system error: Build script returned non-zero exit code: 4
11:15:28 AM: Failing build: Failed to build site
11:15:28 AM: Finished processing build request in 4m29.05s

For what its worth, I did attempt to submit an issue on the requested links in that error, but was getting a 404.

I am wondering if the missing file is related to the monorepo?

Thanks!

A link to a reproduction repository

https://app.netlify.com/sites/workhuman-next/deploys/65c25a12e9e90f000874c61a

Expected Result

No internal errors + build to deploy successfully

Actual Result

Plugin "@netlify/plugin-nextjs" internal error

Steps to reproduce

  1. Update to 5.0.0-beta.7 following documentation provided by Netlify Support
  2. Build

Next Runtime version

5.0.0-beta.7

Is your issue related to the app directory?

  • Yes, I am using the app directory

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
# Settings in the [build] context are global and are applied to
# all contexts unless otherwise overridden by more specific contexts.
[build]
  # Directory to change to before starting a build.
  # This is where we will look for package.json/.nvmrc/etc.
  # If not set, defaults to the root directory.
  base = "apps/web"

  # The ignore command provides custom, programmatic control over builds.
  # We use this to check if the respective site has been changed or not.
  ignore = "npx turbo-ignore"

  # Directory that contains the deploy-ready HTML files and
  # assets generated by the build. This is relative to the base
  # directory if one has been set.
  # publish = ".next"

  # Turborepo build command. We exit the base folder to make sure we can
  # access the root directory where our turbo config is.
  command = "cd ../.. && npx turbo run build --filter=web..."

[functions]
  included_files = [

    "!../../node_modules/sharp/vendor/*",

    "!../../node_modules/@esbuild/*",

    "!../../node_modules/@esbuild/*",

    "!../../node_modules/@swc/core-linux-x64-musl/*",

    "!../../node_modules/@swc/core-linux-x64-gnu/*",

    "!../../node_modules/@swc/core-darwin-x64/*",

    "!../../node_modules/sharp/vendor/8.14.5/linux-x64/lib/libvips-cpp.so.42",

    "!../../node_modules/@esbuild/linux-x64/bin/esbuild",

    "!../../node_modules/esbuild/lib/downloaded-@esbuild-linux-x64-esbuild",

    "!../../node_modules/esbuild/lib/downloaded-@esbuild-linux-x64-esbuild",

    "!../../node_modules/@fortawesome/pro-regular-svg-icons/index.js",

    "!../../node_modules/@fortawesome/pro-solid-svg-icons/index.js",

  ]

[[redirects]]
  force = true
  from = "/munchkin.js"
  status = 200
  to = "https://862-jiq-698.mktoweb.com/munchkin.js"

[[redirects]]
  force = true
  from = "/uc.js"
  status = 200
  to = "https://consent.cookiebot.com/uc.js"

[[redirects]]
  force = true
  from = "/heap.js"
  status = 200
  to = "https://cdn.heapanalytics.com/js/heap-XXXXXX.js"

[[redirects]]
  force = true
  from = "/uwt.js"
  status = 200
  to = "https://static.ads-twitter.com/uwt.js"

[[edge_functions]]
  function = "add-slash"
  path = "/*"


[[plugins]]
  package = "@netlify/plugin-nextjs"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
/** @type {import('next').NextConfig} */
const nextI18nConfig = require('./next-i18next.config');

module.exports = {
  swcMinify: true,
  reactStrictMode: true,
  transpilePackages: [
    '@workhumanweb/ui',
    '@workhumanweb/api',
    '@workhumanweb/algolia-indexing-job',
    '@workhumanweb/common-i18n',
  ],
  eslint: {
    dirs: ['.'],
  },
  poweredByHeader: false,
  trailingSlash: true,
  // experimental: {
  //   adjustFontFallbacks: true,
  // },
  experimental: {
    forceSwcTransforms: true,
    // nextScriptWorkers: true,
    scrollRestoration: true,
  },
  api: {
    bodyParser: false,
  },
  images: {
    // dangerouslyAllowSVG: true,
    contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
    domains: [
      'www.workhuman.com',
      'www.workhumanpreprod.com',
      'images.ctfassets.net',
      'netlify.app',
    ],
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'images.ctfassets.net',
      },
    ],
  },
  env: {
    ...
  },
  i18n: nextI18nConfig.i18n,
  async headers() {
    ...
  },
  async rewrites() {
    return [
      {
        source: '/events/spotlight/:series',
        destination: '/spotlight-landing/:series',
      },
    ];
  },
};

Builds logs (or link to your logs)

Build logs
[Link to Logs](https://app.netlify.com/sites/workhuman-next/deploys/65c25a12e9e90f000874c61a)

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.
@caomicc caomicc added the type: bug code to address defects in shipped code label Feb 6, 2024
@jesseday
Copy link

11:15:27 AM: Error: ENOENT: no such file or directory, open "/opt/build/repo/apps/web/required-server-files.json"

I believe the file does exist at /opt/build/repo/apps/web/.next/required-server-files.json

I don't have a solution yet, but maybe someone working on the plugin knows where this bug is. Will dig a bit

@jesseday
Copy link

Based on testing in the codebase that @caomicc originally posted it appears that there's a bug in the 5.x-beta.7 and 8 version of the @netlify/plugin-nextjs that it assumes the wrong path. From a team member

It assumes that standalone files should live in .next/standalone/.next/ but the correct path is .next/standalone/apps/web/.next/ in monorepo configuration. I've tired many different configuration options without any success.

@SilverFox70
Copy link

We are now experiencing the same issue with it not being able to find the required-server-files.json. We tried pinning the version to "5.0.0-beta.4" but that did not help. We have also tried version "5.0.0-rc.0" with no luck.

@MarcL MarcL added v4 Issues related to the v4 Next.js runtime and removed Ecosystem: Frameworks v4 Issues related to the v4 Next.js runtime labels Mar 13, 2024
@MarcL
Copy link
Contributor

MarcL commented Mar 13, 2024

Hey @jesseday and @SilverFox70.

We just released an early access version that should fix many issues.
Can you try installing @netlify/plugin-nextjs@rc and let us know if you still have the problems?

Thanks!

@caomicc
Copy link
Author

caomicc commented Mar 13, 2024

@MarcL Hi, I too am still having this issue.

Once trying this update to rc, which installs ^5.0.0-rc.4, I can no longer install any packages and am getting a new error:

9:41:45 AM: npm ERR! code ERR_INVALID_ARG_TYPE
9:41:45 AM: npm ERR! The "from" argument must be of type string. Received undefined
9:41:45 AM: npm ERR! A complete log of this run can be found in: /opt/buildhome/.npm/_logs/2024-03-13T13_41_08_575Z-debug-0.log
9:41:45 AM: Error during npm install
9:41:45 AM: Failing build: Failed to install dependencies
9:41:45 AM: Failed during stage "Install dependencies": dependency_installation script returned non-zero exit code: 1

Reverting to ^5.0.0-beta.9 afterwards does continue to install dependencies however fails on the same spot in the build.

@MarcL
Copy link
Contributor

MarcL commented Apr 17, 2024

@caomicc @jesseday @SilverFox70 - We've released v5 now and started rolling it out. Can you remove your pinned dependencies and build and redeploy and check it now?

@MarcL MarcL added the v5 Issues for v5 version of the runtime label Apr 17, 2024
@dragons-library
Copy link

I ran into something similar, and I think I have a workaround.

TL;DR: instead of reading from .next/, read from .netlify/.next/. This feels like a temporary workaround and probably not the ideal solution.

  const path = require('path');

  module.exports = {
    async onPostBuild({ constants, inputs }) {
      const { PUBLISH_DIR } = constants; // where PUBLISH_DIR === '.next'
      const mediaDir = 'static/media';
<     const cwd = path.join(PUBLISH_DIR, mediaDir);
>     const cwd = path.join('.netlify', PUBLISH_DIR, mediaDir);
      // ...
    }
  };

After upgrading to v5 (currently on 5.1.2), I noticed our static assets weren't being uploaded to our CDN. We use a custom onPostBuild hook to grab all the nextJS assets and push them to the CDN.

Some digging, I found that .next directory now looks more like the built public directory. More digging and I found that .netlify/.next looks like the original .next output.

.next/
  _next/
  assets/
  audio/
  cms/
  downloads/
  img/
  android-chrome-192x192.png
  android-chrome-256x256.png
  apple-touch-icon.png
  browserconfig.xml
  favicon-16x16.png
  favicon-32x32.png
  favicon-512x512.png
  favicon.ico
  robots.txt
  site.webmanifest
  sitemap-0.xml
  sitemap.xml

.netlify/.next/
  cache/
  server/
  standalone/
  static/
  BUILD_ID
  build-manifest.json
  export-marker.json
  images-manifest.json
  next-minimal-server.js.nft.json
  next-server.js.nft.json
  package.json
  prerender-manifest.js
  prerender-manifest.json
  react-loadable-manifest.json
  required-server-files.json
  routes-manifest.json
  trace

FWIW, when I inspect the root .next directory outside of the plugin, it looks correct, but when debugging inside of the plugin it is as noted above.

@dragons-library
Copy link

dragons-library commented Apr 19, 2024

Root issue seems to stem from this behavior in the v5 plugin...

var onPostBuild = async (options) => {
  await tracer.withActiveSpan("onPostBuild", async () => {
    await publishStaticDir(new PluginContext(options));
  });
};
...
var onEnd = async (options) => {
  await tracer.withActiveSpan("onEnd", async () => {
    await unpublishStaticDir(new PluginContext(options));
  });
};

...

var publishStaticDir = async (ctx) => {
  try {
    await rm(ctx.tempPublishDir, { recursive: true, force: true });
    await mkdir(basename(ctx.tempPublishDir), { recursive: true });
    await rename(ctx.publishDir, ctx.tempPublishDir);
    await rename(ctx.staticDir, ctx.publishDir);
  } catch (error) {
    ctx.failBuild("Failed publishing static content", error instanceof Error ? { error } : {});
  }
};
var unpublishStaticDir = async (ctx) => {
  try {
    if (existsSync(ctx.tempPublishDir)) {
      await rename(ctx.publishDir, ctx.staticDir);
      await rename(ctx.tempPublishDir, ctx.publishDir);
    }
  } catch {
  }
};

So any plugin with an onPostBuild (or onSuccess/onEnd) event hook that runs after @netlify/plugin-nextjs will have the "wrong" .next directory contents.

Knowing this, a better workaround (instead of reading .netlify/.next) is probably to move @netlify/plugin-nextjs to the end of your plugins in netlify.toml:

-[[plugins]]
-  package = "@netlify/plugin-nextjs"

 [[plugins]]
   package = "@newrelic/netlify-plugin"

 [[plugins]]
   package = "./plugins/my-custom-plugin"
 
+[[plugins]]
+  package = "@netlify/plugin-nextjs"

@ascorbic
Copy link
Contributor

@dragons-library yes, I'd recommend just ensuring the Netlify plugin runs last, rather than relying on the internals of our build process.

Everyone else: in 5.1.2 there are some improvements to finding the build output on sites with custom setups, so if you have issues that relate to that then I'd recommend trying again to see if it now works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code v5 Issues for v5 version of the runtime
Projects
None yet
Development

No branches or pull requests

6 participants