nx icon indicating copy to clipboard operation
nx copied to clipboard

pnpm monorepo made with create-nx-workspace cannot immediately lint

Open troywweber7 opened this issue 2 years ago • 2 comments

Current Behavior

Cannot lint in a fresh monorepo without complaints about 'dotenv'. If I install dotenv globally, then it complains about more and more packages that need to be installed.

To get it to work, I had to install the following with pnpm add -D

  • "dotenv": "^16.0.1",
  • "eslint": "^8.21.0",
  • "@nrwl/eslint-plugin-nx": "^14.5.2",
  • "@typescript-eslint/eslint-plugin": "^5.32.0",
  • "eslint-config-prettier": "^8.5.0",
  • "eslint-plugin-unicorn": "^43.0.2",

Upon inspection, it makes sense that I would need these installed in order to be able to lint, and I know pnpm doesn't hoist by default (and I know why that is a benefit). Perhaps the issue is that when I use the create-nx-workspace and I add my first library, perhaps I expect that these should be automatically installed (when using pnpm) just as things like jest are installed the first time I create a library.

And if this is the wrong place to post this, I'm happy to create an issue elsewhere and close this one.

Expected Behavior

In a fresh pnpm monorepo, I should be able to create a library and lint said library without package errors.

Steps to Reproduce

pnpx create-nx-workspace && pnpm nx g library example && pnpm nx lint example

Failure Logs

$ pnpm nx lint example

 >  NX   Cannot find module 'dotenv'

   Require stack:
   - /home/troy/Code/work/didit/node_modules/.pnpm/@[email protected]/node_modules/@nrwl/nx-cloud/lib/utilities/environment.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/@[email protected]/node_modules/@nrwl/nx-cloud/lib/nx-cloud-tasks-runner.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/@[email protected]/node_modules/@nrwl/nx-cloud/index.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/[email protected]/node_modules/nx/src/tasks-runner/run-command.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/[email protected]/node_modules/nx/src/command-line/run-one.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/[email protected]/node_modules/nx/src/command-line/nx-commands.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/[email protected]/node_modules/nx/bin/init-local.js
   - /home/troy/Code/work/didit/node_modules/.pnpm/[email protected]/node_modules/nx/bin/nx.js
   Pass --verbose to see the stacktrace.

Environment

$ pnpm nx report

 >  NX   Report complete - copy this into the issue template

   Node : 16.16.0
   OS   : linux x64
   pnpm : 7.8.0
   
   nx : 14.5.1
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : Not Found
   @nrwl/eslint-plugin-nx : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 14.5.1
   @nrwl/js : Not Found
   @nrwl/linter : Not Found
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : 14.3.0
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 14.5.1
   typescript : 4.7.4
   ---------------------------------------
   Community plugins:

troywweber7 avatar Aug 04 '22 18:08 troywweber7

Hi @troywweber7,

Thank you for reporting the error. Can you tell me which preset you selected when generating the workspace? Based on the stack I can see you selected the nx-cloud and either apps or npm.

I tested both the latest and v14.5.1 and as you can see in the picture, installing the library should add those dependencies to package.json. Screenshot 2022-08-10 at 00 35 22

Can you let me know what your nx g library logs look like? Also, I would be interested in what the nx report looks like before and after running the generator.

I tested with the same pnpm and node version, so the only difference is our system (which should not make a big difference).

meeroslav avatar Aug 09 '22 22:08 meeroslav

I chose the app preset. When I can update with better detail to match your request, I'll hop on and do so again.

troywweber7 avatar Aug 10 '22 01:08 troywweber7

Hi @meeroslav, i've got the same error.

It appears that @nrwl/nx-cloud package requires dotenv but does not list it in its own dependencies.

This only breaks under certain pnpm configurations with hoisting disabled.

I'm running @nrwl/[email protected]

dmaretskyi avatar Aug 10 '22 20:08 dmaretskyi

@troywweber7, as a workaround you can place a .pnpmfile.cjs file in the root of your repo with the contents:

// https://pnpm.io/pnpmfile

function readPackage(pkg, context) {
  if(pkg.name === '@nrwl/nx-cloud') {
    pkg.dependencies['dotenv'] = '*'
  }

  return pkg
}

function afterAllResolved(lockfile, context) {
  return lockfile
}

module.exports = {
  hooks: {
    readPackage,
    afterAllResolved,
  }
}

dmaretskyi avatar Aug 10 '22 20:08 dmaretskyi

Hmmm...

Having:

strict-peer-dependencies=false
auto-install-peers=true

In .npmrc should already solve missing peer dependencies on pnpm v7. Does your repo have this file?

meeroslav avatar Aug 11 '22 09:08 meeroslav

@meeroslav It's not in peer dependencies either:

{
  "name": "@nrwl/nx-cloud",
  "version": "14.3.0",
  "description": "Nx Cloud plugin for Nx",
  "keywords": [
    "Monorepo",
    "Nx",
    "Nx Cloud"
  ],
  "main": "index.js",
  "types": "index.d.ts",
  "author": "Victor Savkin",
  "license": "CC-BY-ND-3.0",
  "bugs": {
    "url": "https://github.com/nrwl/nx/issues"
  },
  "homepage": "https://nx.app",
  "generators": "./generators.json",
  "bin": {
    "nx-cloud": "./bin/nx-cloud.js"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "node-machine-id": "^1.1.12",
    "tar": "6.1.11",
    "strip-json-comments": "^3.1.1",
    "chalk": "4.1.0"
  },
  "typings": "./index.d.ts"
}

The problem appeared after I disabled hoisting. Probably some other package was installing dotenv and thats why it worked with hoisting. Without hoisting the package only has access to what's declared in it's package.json.

Anyway, I don't think its a good idea to rely hoisting for the package to work. The behaviour is different between package managers and could change.

dmaretskyi avatar Aug 11 '22 11:08 dmaretskyi

for more context, this only happens when the hoisting is set to false in your .npmrc. the default is true for pnpm (as of writing this at least with v7.8.0).

if you're currently blocked by this I'd recommend setting hoist=true in your .npmrc for the time being.

barbados-clemens avatar Aug 11 '22 14:08 barbados-clemens

This is now an issue with fs-extra as well. If nx "supports" pnpm, but requires hoisting to work, then in my opinion, nx does not support pnpm (expect for the sake of marketing only)

troywweber7 avatar Sep 13 '22 19:09 troywweber7

I'm running [email protected] and this is now an issue with fs-extra. It requires it in one of it's files, but doesn't list it anywhere in it's package.json. That means even if I enable hoisting, it will be hoisting and depending on it from another package, which, in general, is a bad practice.

troywweber7 avatar Sep 13 '22 19:09 troywweber7

@barbados-clemens I'm missing something with your latest comment. My .npmrc:

strict-peer-dependencies=false
auto-install-peers=true

My pnpm version is 7.9.3. Still an issue.

Is this going to get fixed in a future version of @nrwl/nx-cloud to list fs-extra? Right now I'm on the latest nx-cloud, which clearly depends on fs-extra in one of it's files, but doesn't list it in dependencies:

$ pnpm view @nrwl/nx-cloud

@nrwl/[email protected] | CC-BY-ND-3.0 | deps: 7 | versions: 250
Nx Cloud plugin for Nx
https://nx.app

keywords: Monorepo, Nx, Nx Cloud

bin: nx-cloud

dist
.tarball: https://registry.npmjs.org/@nrwl/nx-cloud/-/nx-cloud-14.6.2.tgz
.shasum: 2eef39b790ba93a7003042c4045981327a64af0c
.integrity: sha512-w8ujbFMOxE7lJg52O3JjnpTGlBKc+/vovxVTplIfGU9TYgEgAo/v4HS9svztzGd9Era7qmNaoYOmxm6Ebtbo5g==
.unpackedSize: 300.7 kB

dependencies:
axios: ^0.21.1              dotenv: ~10.0.0             strip-json-comments: ^3.1.1 yargs-parser: >=21.0.1      
chalk: 4.1.0                node-machine-id: ^1.1.12    tar: 6.1.11                 

maintainers:
- altan-nrwl <[email protected]>
- juristr <[email protected]>
- jack-nrwl <[email protected]>
- nrwl-jason <[email protected]>
- nrwlowner <[email protected]>

dist-tags:
latest: 14.6.2       next: 14.5.0-beta.4  

published 6 days ago by nrwlowner <[email protected]>

troywweber7 avatar Sep 13 '22 19:09 troywweber7

@troywweber7, sorry for the inconvenience.

I have reached out to our colleaguesthe from nx-cloud team and someone will pick it up soon. Please do not continue the thread on (this) closed issue, especially since you opened another dedicated one.

In the meantime, as @barbados-clemens mentioned you can switch back to hosting to until we roll out the fixed version.

meeroslav avatar Sep 14 '22 09:09 meeroslav

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 Mar 21 '23 21:03 github-actions[bot]