nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.36k stars 2.33k forks source link

Missing dependencies in generated package.json #19226

Open jaqua opened 1 year ago

jaqua commented 1 year ago

Current Behavior

Using this file in a nestJS application

upload.resolver.ts

import { UseGuards } from '@nestjs/common'
import { Args, Mutation, Resolver } from '@nestjs/graphql'
import GraphQLUpload from 'graphql-upload/GraphQLUpload.js'

import { File, UploadResult } from '@jaqua/jaqua.de/graphql-server'
import { Roles } from '@jaqua/shared/feat/admin/backend'
import { GqlAuthGuard, RolesGuard } from '@jaqua/shared/util/auth-guard'

should result in a generated package.json file like this - which worked in v16.1. as expected.

{
  "name": "backend",
  "version": "0.0.1",
  "dependencies": {
    "@nestjs/common": "10.2.5",
    "@nestjs/graphql": "10.2.1",
    "graphql": "16.8.0",
    "graphql-upload": "15.0.2"
  },
  "main": "main.js"
}

After upgrading to 16.7.2 the graphql-upload is missing:

{
  "name": "backend",
  "version": "0.0.1",
  "dependencies": {
    "@nestjs/common": "10.2.5",
    "@nestjs/graphql": "10.2.1",
    "graphql": "16.8.0"
  },
  "main": "main.js"
}

If I add import "graphql-upload" to the file, the dependency gets added to the generated package.json file.

By the way: The latest version (16.8.1.) creates package.json files with empty dependencies. Is there any change of usage, which I didn't find in the docs?

{
  "name": "backend",
  "version": "0.0.1",
  "dependencies": {},
  "main": "main.js"
}

Expected Behavior

Should generate package.json file with all needed dependencies.

GitHub Repo

No response

Steps to Reproduce

1.

Nx Report

Node   : 18.12.1
   OS     : darwin-arm64
   npm    : 8.19.2

   nx                 : 16.7.2
   @nx/js             : 16.7.2
   @nx/jest           : 16.7.2
   @nx/linter         : 16.7.2
   @nx/workspace      : 16.7.2
   @nx/cypress        : 16.7.2
   @nx/detox          : 16.7.2
   @nx/devkit         : 16.7.2
   @nx/eslint-plugin  : 16.7.2
   @nx/nest           : 16.7.2
   @nx/next           : 16.7.2
   @nx/node           : 16.7.2
   @nx/react          : 16.7.2
   @nx/react-native   : 16.7.2
   @nrwl/tao          : 16.7.2
   @nx/web            : 16.7.2
   @nx/webpack        : 16.7.2
   typescript         : 5.1.6

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

n0cloud commented 12 months ago

Happened with the redlock package for me. Adding import 'redlock'; didn't solve the problem, so I had to create a package.json for the projet using the package.

Using nx 16.10.0

vire commented 11 months ago

I can confirm this happens with NX 17.0.1 and I'm using ` "executor": "@nx/esbuild:esbuild",

It did work nx@npm:16.6.0

edit: it works with stock npm but NOT working with yarn@4.0.1 PNP

grz-gajda commented 9 months ago

It happened to me too.

   Node   : 20.8.0
   OS     : darwin-arm64
   yarn   : 4.0.2

   nx                 : 17.2.8
   @nx/js             : 17.2.8
   @nx/eslint         : 17.2.8
   @nx/workspace      : 17.2.8
   @nx/devkit         : 17.2.8
   @nx/eslint-plugin  : 17.2.8
   @nx/plugin         : 17.2.8
   @nx/react          : 17.2.8
   @nx/webpack        : 17.2.8
   typescript         : 5.2.2
   ---------------------------------------
   Community plugins:
   @ngrx/effects : 17.0.1
   @ngrx/store   : 17.0.1
   ---------------------------------------
   Local workspace plugins:
         nx-customs

I had to fix it quickly so I patched the nx package:

diff --git a/src/plugins/js/package-json/create-package-json.js b/src/plugins/js/package-json/create-package-json.js
index 374e05e6baa2673c8e56ee71e43fba06348c875b..e8402c30d93fcfb95ed8e5c7201dea65d4afc6b8 100644
--- a/src/plugins/js/package-json/create-package-json.js
+++ b/src/plugins/js/package-json/create-package-json.js
@@ -143,8 +143,38 @@ function findAllNpmDeps(projectFileMap, projectNode, graph, npmDeps, seen, ignor
     const projectFiles = (0, task_hasher_1.filterUsingGlobPatterns)(projectNode.data.root, projectFileMap[projectNode.name] || [], rootPatterns ?? dependencyPatterns);
     const projectDependencies = new Set();
     projectFiles.forEach((fileData) => fileData.deps?.forEach((dep) => projectDependencies.add((0, project_graph_1.fileDataDepTarget)(dep))));
+
+    /**
+     * Issue is related to Yarn 4.0.2 and Nx. Because of that nx:webpack
+     * cannot properly generate package.json with required dependencies.
+     * It's a "dirty hack" to somehow find dependencies in project.
+     * @see https://github.com/nrwl/nx/issues/19226
+     * @see https://github.com/nrwl/nx/issues/20928
+     */
+    const externalNodesKeys = Object.keys(graph.externalNodes);
+    const findNode = (dep) => {
+        if (!dep.startsWith('npm:')) {
+            return undefined
+        }
+
+        const foundKeyRealName = externalNodesKeys.find(k => k.slice(0, k.lastIndexOf('@')) === dep);
+        if (foundKeyRealName) {
+            return graph.externalNodes[foundKeyRealName]
+        }
+
+        const foundKeySimilarName = externalNodesKeys.find(k => k.includes(dep))
+        if (foundKeySimilarName) {
+            return graph.externalNodes[foundKeySimilarName]
+        }
+
+        if (dep.includes('/')) {
+            return findNode(dep.slice(0, dep.lastIndexOf('/'))) 
+        }
+        return undefined
+    };
+
     for (const dep of projectDependencies) {
-        const node = graph.externalNodes[dep];
+        const node = findNode(dep);
         if (seen.has(dep)) {
             // if it's in peerDependencies, move it to regular dependencies
             // since this is a direct dependency of the project

I am not sure if the fix will work for everyone but I hope it'll help anyone using Yarn Berry instead of npm.

abuaboud commented 3 months ago

NX Version: 19.3.0

We are having the same issue, and it's with redlock package as well.

Edit: was able to fix after deleting node_modules and package-lock.json then ran npm install from scratch

https://github.com/activepieces/activepieces/pull/4944