sst / open-next

Open source Next.js serverless adapter
https://open-next.js.org
MIT License
3.7k stars 111 forks source link

ENOENT: no such file or directory, scandir '.open-next/...' #401

Closed Bathilde closed 3 months ago

Bathilde commented 3 months ago

I have a nextJS application that was working well until yesterday.

Now during my test deployment phase (npx sst deploy --stage test), I have the ENOENT error, where '.open-next/assets' is missing. This is only happening when I tried to build from gitlab, and if I try to run the command from my mac, it is perfectly working.

Note that I had a build 2h before perfectly working, and I relaunched it, it is now failing....with a similar error: ENOENT: no such file or directory, open '.next/routes-manifest.json'

What could be the reason ? Is there any cache I could remove to retry ?

Thank you.

khuezy commented 3 months ago

First thing I'd check is the OS and runtime of the gitlab action. That might explain this weird behavior. I don't use GitLab but check if there's a button somewhere to delete cache?

Bathilde commented 3 months ago

I don't have access to the machine directly, everything is controlled through a yml file containing the instructions. In my case I have the following instructions:

    - npm install
    - npm run build
    - npx sst telemetry disable
    - npx sst deploy --stage test
conico974 commented 3 months ago

Try removing .next and .open-next, there must be some kind of cache on the gitlab server

khuezy commented 3 months ago

Does GitLab CI/CD action configuration let you configure the OS and node runtime? I'm assuming it does. Can you share the rest of the yml file?

Try adding a command to delete the .next and .open-next folder. But ideally, you'd want to cache those folders if nothing changed.

bneilz commented 3 months ago

I'm working with Bathilde. We run the deploy job with a custom docker image :

FROM registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest

RUN apt-get update
RUN apt install -y ca-certificates curl gnupg
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt update
RUN apt install -y nodejs
RUN apt install gcc g++ make -y
RUN apt install -y python
RUN apt install -y postgresql
RUN apt install -y libpq-dev

And here is the Gitlab-CI job :

deploy-test:
  stage: deploy
  image: my-repo/sst-nodejs-build:latest
  rules:
  - if: '$CI_COMMIT_REF_NAME != "develop" || $CI_COMMIT_TAG'
    when: manual
  - if: $CI_COMMIT_REF_NAME
  environment:
    name: TEST
  script:
    - npm install
    - npm run build
    - npx sst deploy --stage test
khuezy commented 3 months ago

Can you try adding rm -rf .open-next .next (or where ever your next app and sst root is) before the npm run build step?

bneilz commented 3 months ago

We tried it. It changes nothing. And I added too a ls before run rm command and there are no .open-next and .next folders.

bneilz commented 3 months ago

Yesterday I tried to deploy from my computer and it worked. This morning I've deleted .next .open-next folders before deploying. npm run build create .next folder, but there is no .open-next folder. Then I run npx sst deploy --stage test and I got this error : ENOENT: no such file or directory, scandir 'C:\my-app-folder\front\.open-next\assets'

conico974 commented 3 months ago

This might not be necessary to run npm run build (and if it runs in parallel, that might explain the issue) sst deploy also run the build command from your package.json by default. Given the error that seems to change it's possible that it has something to do with tasks running in parallel. FIY open-next is not supported on windows https://github.com/sst/open-next/issues/385

bneilz commented 3 months ago

I added npm run build to try to fix our issue. Without it we have this error : ENOENT: no such file or directory, open 'C:\my-app-folder\.next\routes-manifest.json'

bneilz commented 3 months ago

And we have the same error on our gitlab job, which is not running on windows.

conico974 commented 3 months ago

What's your build command in package.json? And what's inside your sst NextJsSite construct?

bneilz commented 3 months ago

Our build command is next build. Our sst.config.ts is :

import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
import { SSTConfig } from "sst";
import { NextjsSite, NextjsSiteProps } from "sst/constructs";

export default {
  config(_input) {
    return {
      name: "our-app",
      region: "eu-west-3",
    };
  },
  stacks(app) {
    app.stack(function Site({ stack }: { stack: any }) {
      let options: NextjsSiteProps | undefined = undefined;
      if (!!process.env['HOST'] && !!process.env['CERT_ARN']) {
        options = {
          customDomain: {
            domainName: process.env['HOST'] as string,
            isExternalDomain: true,
            cdk: {
              // @ts-ignore
              certificate: Certificate.fromCertificateArn(stack, "MyCert", process.env['CERT_ARN'] as string),
            },
          }
        };
      }
      const site = new NextjsSite(stack, "site", options);
      stack.addOutputs({
        SiteUrl: site.url,
      });
    });
  },
} satisfies SSTConfig;
conico974 commented 3 months ago

Are you on a monorepo ? NX maybe ? SST and OpenNext runs from the current working directory, it will execute the build command from the package in the cwd. It also expects the .next output to be in the cwd where the process has been called

bneilz commented 3 months ago

I found a solution. npx sst deploy does not run open-next build anymore. So I added npx open-next build before npx sst deploy to fix our deployment. I don't know why open-next build is not run anymore by sst command but I'm pretty sure that it is not on "our" side because of this : image April 3, 2024 at 4:31:28 PM the job was OK and 2 hours later the same job (from same commit) was KO with no such file or directory, open '.next/routes-manifest.json'.

conico974 commented 3 months ago

Pretty sure the issue is not on the OpenNext side either, there is no cache or anything that would stop the open-next build command.

There might be some kind of caching from either gitlab or sst that may stop the command (maybe because it's the same commit)

I'll close this issue as it doesn't seem related to OpenNext, you could open an issue in the SST repo. Feel free to reopen the issue if it is indeed an error from OpenNext

icarns commented 1 month ago

We have a similar error when we add to the Next.js resource

        buildCommand: 'pnpm run build',

We noticed that by default it uses npm, while in our monorepo we use pnpm, so we wanted to use pnpm there as well. It works locally, but not in GH Actions. After deleting the cache from GH, we still get the same error.

khuezy commented 1 month ago

We have a similar error when we add to the Next.js resource

      buildCommand: 'pnpm run build',

We noticed that by default it uses npm, while in our monorepo we use pnpm, so we wanted to use pnpm there as well. It works locally, but not in GH Actions. After deleting the cache from GH, we still get the same error.

https://github.com/sst/open-next/blob/main/packages/open-next/src/build.ts#L157-L200 It should detect your packager and use the appropriate command. Did you install pnpm in your GHA?