nx icon indicating copy to clipboard operation
nx copied to clipboard

It failed when building a next application with config `output:standalone`.

Open ibufu opened this issue 1 year ago • 20 comments

Current Behavior

After integrating to an existed pnpm monorepo, it failed when building a next application with config output:standalone.

Expected Behavior

nx build next-app --verbose
Error: File exists (os error 17)
    at /nx-next-standalone/node_modules/.pnpm/[email protected]/node_modules/nx/src/tasks-runner/cache.js:145:17

GitHub Repo

https://github.com/ibufu/nx-next-standalone

Steps to Reproduce

  1. Clone the repo.
  2. pnpm i
  3. nx build next-app

Nx Report

Node           : 18.20.4
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 9.9.0

nx (global)  : 19.6.4
nx           : 19.6.4
@nx/eslint   : 19.6.4
@nx/next     : 19.6.4
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/next/plugin

Failure Logs

Error: File exists (os error 17)
    at /nx-next-standalone/node_modules/.pnpm/[email protected]/node_modules/nx/src/tasks-runner/cache.js:145:17

Package Manager Version

[email protected]

Operating System

  • [X] macOS
  • [ ] Linux
  • [ ] Windows
  • [ ] Other (Please specify)

Additional Information

No response

ibufu avatar Sep 01 '24 04:09 ibufu

Same here

jackhefoong avatar Nov 06 '24 03:11 jackhefoong

The issue persists in the latest Nx version (v20.0.11). Is there any additional action planned to address this? @jaysoo @ndcunningham

Thanks!

Error: File exists (os error 17)
    at /node_modules/.pnpm/[email protected]/node_modules/nx/src/tasks-runner/cache.js:317:17

malfianrasyidin avatar Nov 08 '24 09:11 malfianrasyidin

Just delete the scripts in package.json and only keep scripts in project.json

On Fri, 8 Nov 2024 at 1:22 PM Muhammad Alfian Rasyidin < @.***> wrote:

The issue persists in the latest Nx version (v20.0.11). Is there any additional action planned to address this? @jaysoo https://github.com/jaysoo @ndcunningham https://github.com/ndcunningham

Thanks!

Error: File exists (os error 17) at @.***/node_modules/nx/src/tasks-runner/cache.js:317:17

— Reply to this email directly, view it on GitHub https://github.com/nrwl/nx/issues/27724#issuecomment-2464214065, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5TWOAZONARH6NVIGBKFGTZ7R7G3AVCNFSM6AAAAABNOPB2H6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINRUGIYTIMBWGU . You are receiving this because you authored the thread.Message ID: @.***>

ibufu avatar Nov 08 '24 09:11 ibufu

Big help, thanks! @ibufu

jackhefoong avatar Nov 08 '24 10:11 jackhefoong

I'm getting the same error.

I'm trying to understand how not using package.json scripts would help? I'm also trying to understand how to update my project.json to actually build correctly.I went with a package.json script for this particular library because it has a couple of steps:

    "bundle": "tsx ./src/bundle.ts",
    "build": "rm -rf ./dist && tsc -p tsconfig.lib.json && pnpm run bundle"
  }

How would i convert that into a project.json syntax, and how would that fix the problem? Or would it?

counterbeing avatar Nov 08 '24 13:11 counterbeing

The root cause is that nx run the build script twice. You can use nx show to figure out it.

ibufu avatar Nov 08 '24 13:11 ibufu

I've moved to this structure in my project.json for this package:

{
  "name": "workflow-bundler",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "packages/workflow-bundler/src",
  "projectType": "library",
  "tags": [],
  "targets": {
    "build": {
      "executor": "nx:run-commands",
      "options": {
        "command": "say 'hello';rm -rf ./dist && tsc -p tsconfig.lib.json && tsx ./src/bundle.ts"
      }
    }
  }
}

That say 'hello' is just there so i can hear if it happens to run twice. It's now only running once. I've entirely removed the scripts section from my package.json. However i'm still getting:

    at /Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:84:24
    at _try (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:397:26)
    at _try (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:406:26)
    at async _try (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:406:20)
    at async _try (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:406:20)
    at async _try (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:406:20)
    at async _try (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/cache.js:406:20)
    at async Promise.all (index 0)
    at async TaskOrchestrator.postRunSteps (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/task-orchestrator.js:345:13)
    at async TaskOrchestrator.applyFromCacheOrRunTask (/Users/cory.logan/Code/my-project/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]__@[email protected]_t_rpge6wb46b7rpyw5yhycwpxahy/node_modules/nx/src/tasks-runner/task-orchestrator.js:295:9)

counterbeing avatar Nov 08 '24 14:11 counterbeing

I'm also experiencing this issue. On my end, there seems to be some conflict with the cache entries for Nx. It seems it is trying to write to the cache, but the key already exists, which causes this issue.

Running the command with --skipNxCache worked for me.

CristianHG2 avatar Jan 10 '25 04:01 CristianHG2

For my setup, the issue was also gone after adding --skipNxCache (which is not the ideal solution). Tested with the latest NX version: 20.7.1

caioquirino avatar Apr 03 '25 17:04 caioquirino

--skipNxCache worked around the issue for me too

OrenSayag avatar Apr 27 '25 07:04 OrenSayag

This is still broken in the latest NV v21.0.0. It's unfortunate because we have to disable standalone output when running outside of CI, which produces a different build, so it's difficult to run our true production compiled next.js app locally.

Steps to reproduce below:

  1. pnpx create-nx-workspace nx-21 using [email protected] with the following options:
  • "React" for stack
  • "Next.js" for framework
  • App Router: yes
  • src/ directory: yes
  • Unit test runner: Jest
  • E2E test runner: Playwright
  • Stylesheet: tailwind
  • Eslint: yes
  • Prettier: yes
  1. Add output: "standalone" to next.config.js
  2. Run inferred next.js build command - see output error

Besides react and next.js, I don't think the choices above make a difference - I haven't tried with a different test runner or style system for example. But just a basic [email protected] standalone next.js build fails pretty easily.

A lot of next.js builds are dockerized (to avoid high vercel pricing), which is why standalone is so important. Our team isn't completely blocked because we just disable nx caching in our CI pipeline. And yes, --skipNxCache does fix the issue, but obviously not a long term solution.

ianldgs for visibility, since we had a brief discussion on https://github.com/nrwl/nx/issues/30733 earlier.

nx report

Node : 20.19.0 OS : darwin-arm64 Native Target : aarch64-macos pnpm : 10.8.0

nx : 21.0.0 @nx/js : 21.0.0 @nx/jest : 21.0.0 @nx/eslint : 21.0.0 @nx/workspace : 21.0.0 @nx/devkit : 21.0.0 @nx/eslint-plugin : 21.0.0 @nx/module-federation : 21.0.0 @nx/next : 21.0.0 @nx/playwright : 21.0.0 @nx/react : 21.0.0 @nx/web : 21.0.0 @nx/webpack : 21.0.0 typescript : 5.7.3

Registered Plugins: @nx/js/typescript @nx/next/plugin @nx/playwright/plugin @nx/eslint/plugin @nx/jest/plugin

Cache Usage: 0.00 B / 92.64 GB

drewandre avatar May 06 '25 15:05 drewandre

Same here, I have another workaround option:

Add this to the package.json or project.json

{
  ...
  "targets": {
    "build": {
      "cache": false
    }
...

lucasvieirasilva avatar Jun 16 '25 19:06 lucasvieirasilva

any progress?

TalMikey avatar Jun 30 '25 09:06 TalMikey

Same issue here :(

gperdomor avatar Aug 02 '25 14:08 gperdomor

I solved this by using the target output {projectRoot}/.next/standalone instead of {projectRoot}/.next/standalone/**/*

eliobischof avatar Sep 03 '25 22:09 eliobischof

Any official solution, any update?

gperdomor avatar Oct 01 '25 12:10 gperdomor

I'm also experiencing the same on my side

PapaStef avatar Oct 09 '25 14:10 PapaStef

ditto here, also experiencing this

jontybrook avatar Oct 24 '25 15:10 jontybrook

still an issue in nx 22

marc-wilson avatar Oct 25 '25 23:10 marc-wilson

I'm still not sure why this is happening, yet I found the cleanest workaround for my needs.

The inferred plugin outputs paths are

{workspaceRoot}/apps/web/.next/!(cache)/**/*

{workspaceRoot}/apps/web/.next/!(cache)

I changed them by configuring the target with this output path:

{
   "targets": {
      "build": {
        "outputs": [
          "{projectRoot}/.next"
        ]
      }
    }
}

and it works.

I tried ignoring the .next/cache dir following the same pattern as the default imports in the nx.json:

{
  "targets": {
    "build": {
      "outputs": [
        "{projectRoot}/.next",
        "!{projectRoot}/.next/cache"
      ]
    }
  }
}

but had no luck.

sebastiandg7 avatar Dec 05 '25 19:12 sebastiandg7