nx icon indicating copy to clipboard operation
nx copied to clipboard

Increase NX_SOCKET_DIR limit

Open wereHamster opened this issue 1 year ago • 3 comments

Current Behavior

$ nx dev some-project

 NX   Attempted to open socket that exceeds the maximum socket length.


Set NX_SOCKET_DIR to a shorter path (e.g. /tmp/nx-tmp) to avoid this issue.
Pass --verbose to see the stacktrace.

My TMPDIR is /var/folders/7s/dlmpd6y94313nwn6q4mtwmf00000gn/T/nix-shell.exKnBn and workspace folder /Users/<username>/src/<workspace> (length 27). Together close to the Nx-imposed limit of 95 characters.

I have to manually set NX_SOCKET_DIR to a shorter path (eg. /tmp), then I can start using Nx.

Expected Behavior

Nx works in 'standard' situations with default TMPDIR and a workspace in a reasonable location.

GitHub Repo

No response

Steps to Reproduce

Be on MacOS, set TMPDIR to /var/folders/7s/dlmpd6y94313nwn6q4mtwmf00000gn/T/ (or similarly long path), have the workspace in a folder whose path length is >40 characters, try to run any Nx task.

Nx Report

Node           : 20.15.1
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 9.6.0

nx                 : 19.6.4
@nx/js             : 19.6.4
@nx/linter         : 19.6.4
@nx/eslint         : 19.6.4
@nx/workspace      : 19.6.4
@nx/devkit         : 19.6.4
@nx/esbuild        : 19.6.4
@nx/eslint-plugin  : 19.6.4
@nx/next           : 19.6.4
@nx/react          : 19.6.4
@nrwl/tao          : 19.6.4
@nx/web            : 19.6.4
@nx/webpack        : 19.6.4
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/next/plugin

Failure Logs

No response

Package Manager Version

No response

Operating System

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

Additional Information

Relevant code which throws the error: https://github.com/nrwl/nx/blob/18e5535940a0aff86dc845f3714b07b94a0db8d8/packages/nx/src/daemon/socket-utils.ts#L39-L51

This seems to be a new behavior, it only started being an issue recently (maybe after my 19.6.3 -> 19.6.4 upgrade, but I'm not 100% certain).

wereHamster avatar Sep 01 '24 11:09 wereHamster

Are there any workarounds for this?

joeprivettsmart avatar Oct 07 '24 12:10 joeprivettsmart

When I hit this, I usually export NX_SOCKET_DIR=/tmp/nx-tmp. I think that's ok as long as you don't run two different Nx projects at the same time (like maybe a CI/CD build machine would).

wereHamster avatar Oct 07 '24 18:10 wereHamster

Hey! Thanks for reporting this. The socket dir limit isn't really nx-imposed, it comes from the underlying OS limits on socket file lengths: https://unix.stackexchange.com/questions/367008/why-is-socket-path-length-limited-to-a-hundred-chars

To make this work in as many scenarios as possibe, we already use a hash of your workspace path instead of using the whole workspace path, limiting that part of the socket path length to 20 chars, as you can see here:

https://github.com/nrwl/nx/blob/e0f9a5c5309f923fc3b3c9b39cc74f33e7b5803b/packages/nx/src/daemon/tmp-dir.ts#L49-L54

Still, if your tmpdir is too long for some reason, it might error. From the information you've given, that doesn't seem like the case, though.

Locally, could you modify node_modules/nx/src/daemon/tmp-dir.js and add a console.log(dir)? Then we can see exactly what your socket path is made of and why it is so long.

MaxKless avatar Oct 15 '24 17:10 MaxKless

My TMPDIR right now is /var/folders/7s/dlmpd6y94313nwn6q4mtwmf00000gn/T/nix-shell.3MvbCw (65 characters). I've never had problems before this limit was introduced in Nx, I've been using Nx long before without coming across any issues. Are you sure the limit is present on macOS as well or only on Linux?

wereHamster avatar Oct 23 '24 19:10 wereHamster

From my research, the limit is the same on macos.

Okay but even so, the following calculation would be under 95 chars, no? 65 chars (TMPDIR) + 20 chars (hash) + 6 chars (d.sock filename) + 2 chars (slashes) = 93 chars

So if you could add the console.log(dir) so we can see what the path that nx actually constructs by default, we can see how to solve this.

MaxKless avatar Oct 24 '24 09:10 MaxKless

Interesting that Nx constructs two different socket paths, the first one are ok but then it creates a different path that's 99 characters long.

assertValidSocketPath {
  path: '/var/folders/7s/dlmpd6y94313nwn6q4mtwmf00000gn/T/nix-shell.HtRwdP/bec15adfb3267b279179/d.sock'
}
assertValidSocketPath {
  path: '/var/folders/7s/dlmpd6y94313nwn6q4mtwmf00000gn/T/nix-shell.HtRwdP/bec15adfb3267b279179/d.sock'
}

   ⠙  Nx is waiting on 1 dependent project tasks before running tasks from explorer...
assertValidSocketPath {
  path: '/var/folders/7s/dlmpd6y94313nwn6q4mtwmf00000gn/T/nix-shell.HtRwdP/bec15adfb3267b279179/fp69339.sock'
}

———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Ran target build for project explorer and 1 task(s) they depend on (38ms)

   ✖  0/0 failed
   ✔  0/0 succeeded [0 read from cache]


 NX   Attempted to open socket that exceeds the maximum socket length.


Set NX_SOCKET_DIR to a shorter path (e.g. /tmp/nx-tmp) to avoid this issue.
Pass --verbose to see the stacktrace.

wereHamster avatar Nov 10 '24 11:11 wereHamster

I think we can safely reduce the workspace hash length to 10 chars, I made a PR for it here: https://github.com/nrwl/nx/pull/28920

Still, I feel like you could pretty easily set the NX_SOCKET_DIR yourself to fix this right away. Or double check your OS configuration to see why this nix-shell part is included in your tmp path. Mine just looks like this: /var/folders/y1/wnwfd1_x37sfd21k0ff71y180000gn/T/b6c6dccdf0e3e031cb8b/d.sock so it's independent of my username and things like it.

MaxKless avatar Nov 13 '24 14:11 MaxKless

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

github-actions[bot] avatar Dec 15 '24 00:12 github-actions[bot]