From e980cfd9323083e324261d65bb76990b388b7f99 Mon Sep 17 00:00:00 2001 From: Ivan Tanev Date: Mon, 4 Nov 2024 20:12:13 +0200 Subject: [PATCH 1/3] Disallow dependencies and prefer devDependencies in monorepo root --- README.md | 8 ++++---- ...PENDENCIES.ts => ROOT_HAS_PROD_DEPENDENCIES.ts} | 14 +++++++------- packages/cli/src/checks/index.ts | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) rename packages/cli/src/checks/{ROOT_HAS_DEV_DEPENDENCIES.ts => ROOT_HAS_PROD_DEPENDENCIES.ts} (63%) diff --git a/README.md b/README.md index 3a3898ca..56abcbe9 100644 --- a/README.md +++ b/README.md @@ -294,15 +294,15 @@ This is so that `peerDependencies` are available in the package during developme The range for the dependency specified in `peerDependencies` is added to `devDependencies` unless the package is already a non-peer dependency elsewhere in the repo in which, that range is used instead. -## Root has devDependencies +## Root has prod dependencies -Key: `ROOT_HAS_DEV_DEPENDENCIES` +Key: `ROOT_HAS_PROD_DEPENDENCIES` -The root package should not have any `devDependencies`, instead all dependencies should be in `dependencies` +The root package should not have any production `dependencies`, instead all dependencies should be in `devDependencies`. ### Why it's a rule -The root `package.json` of a monorepo is not published so whether a dependency is in `devDependencies` or `dependencies` does not make a difference and having one place to put dependencies in the root means that people do not have to arbitrarily decide where a dependency should go every time they install one. +The root `package.json` of a monorepo is not published so whether a dependency is in `devDependencies` or `dependencies` does not make a difference and having one place to put dependencies in the root means that people do not have to arbitrarily decide where a dependency should go every time they install one. We prefer `devDependencies` because a monorepo root should contain only tooling dependencies. ### How it's fixed diff --git a/packages/cli/src/checks/ROOT_HAS_DEV_DEPENDENCIES.ts b/packages/cli/src/checks/ROOT_HAS_PROD_DEPENDENCIES.ts similarity index 63% rename from packages/cli/src/checks/ROOT_HAS_DEV_DEPENDENCIES.ts rename to packages/cli/src/checks/ROOT_HAS_PROD_DEPENDENCIES.ts index 15898f0b..4944225f 100644 --- a/packages/cli/src/checks/ROOT_HAS_DEV_DEPENDENCIES.ts +++ b/packages/cli/src/checks/ROOT_HAS_PROD_DEPENDENCIES.ts @@ -3,31 +3,31 @@ import pc from "picocolors"; import { Package } from "@manypkg/get-packages"; type ErrorType = { - type: "ROOT_HAS_DEV_DEPENDENCIES"; + type: "ROOT_HAS_PROD_DEPENDENCIES"; workspace: Package; }; export default makeCheck({ type: "root", validate: (rootWorkspace) => { - if (rootWorkspace.packageJson.devDependencies) { - return [{ type: "ROOT_HAS_DEV_DEPENDENCIES", workspace: rootWorkspace }]; + if (rootWorkspace.packageJson.dependencies) { + return [{ type: "ROOT_HAS_PROD_DEPENDENCIES", workspace: rootWorkspace }]; } return []; }, fix: (error) => { - error.workspace.packageJson.dependencies = sortObject({ + error.workspace.packageJson.devDependencies = sortObject({ ...error.workspace.packageJson.devDependencies, ...error.workspace.packageJson.dependencies, }); - delete error.workspace.packageJson.devDependencies; + delete error.workspace.packageJson.dependencies; }, print: () => { return `the root package.json contains ${pc.yellow( - "devDependencies" - )}, this is disallowed as ${pc.yellow("devDependencies")} vs ${pc.green( "dependencies" + )}, this is disallowed as ${pc.yellow("dependencies")} vs ${pc.green( + "devDependencies" )} in a private package does not affect anything and creates confusion.`; }, }); diff --git a/packages/cli/src/checks/index.ts b/packages/cli/src/checks/index.ts index da492f96..0a823eb9 100644 --- a/packages/cli/src/checks/index.ts +++ b/packages/cli/src/checks/index.ts @@ -3,7 +3,7 @@ import INTERNAL_MISMATCH from "./INTERNAL_MISMATCH"; import INVALID_DEV_AND_PEER_DEPENDENCY_RELATIONSHIP from "./INVALID_DEV_AND_PEER_DEPENDENCY_RELATIONSHIP"; import INVALID_PACKAGE_NAME from "./INVALID_PACKAGE_NAME"; import MULTIPLE_DEPENDENCY_TYPES from "./MULTIPLE_DEPENDENCY_TYPES"; -import ROOT_HAS_DEV_DEPENDENCIES from "./ROOT_HAS_DEV_DEPENDENCIES"; +import ROOT_HAS_PROD_DEPENDENCIES from "./ROOT_HAS_PROD_DEPENDENCIES"; import UNSORTED_DEPENDENCIES from "./UNSORTED_DEPENDENCIES"; import INCORRECT_REPOSITORY_FIELD from "./INCORRECT_REPOSITORY_FIELD"; import WORKSPACE_REQUIRED from "./WORKSPACE_REQUIRED"; @@ -14,7 +14,7 @@ export let checks = { INVALID_DEV_AND_PEER_DEPENDENCY_RELATIONSHIP, INVALID_PACKAGE_NAME, MULTIPLE_DEPENDENCY_TYPES, - ROOT_HAS_DEV_DEPENDENCIES, + ROOT_HAS_PROD_DEPENDENCIES, UNSORTED_DEPENDENCIES, INCORRECT_REPOSITORY_FIELD, WORKSPACE_REQUIRED, From 8ef91899d1c7f95b2251ce31ce0fd85fda4c9b3a Mon Sep 17 00:00:00 2001 From: Ivan Tanev Date: Mon, 4 Nov 2024 20:12:38 +0200 Subject: [PATCH 2/3] Apply manypkg fix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f8be3170..8f7173f8 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "jest-watch-typeahead/testname" ] }, - "dependencies": { + "devDependencies": { "@babel/core": "^7.20.5", "@babel/plugin-transform-runtime": "^7.19.6", "@babel/preset-env": "^7.20.2", From 5354fed84a8a6fe31115a6648347b7f88bfb6a4f Mon Sep 17 00:00:00 2001 From: Ivan Tanev Date: Mon, 4 Nov 2024 20:15:10 +0200 Subject: [PATCH 3/3] Add changeset --- .changeset/wicked-points-own.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wicked-points-own.md diff --git a/.changeset/wicked-points-own.md b/.changeset/wicked-points-own.md new file mode 100644 index 00000000..28475583 --- /dev/null +++ b/.changeset/wicked-points-own.md @@ -0,0 +1,5 @@ +--- +"@manypkg/cli": minor +--- + +Change the `ROOT_HAS_DEV_DEPENDENCIES` rule to `ROOT_HAS_PROD_DEPENDENCIES`. Monorepo root should use `devDependencies` instead of `dependencies` for better support of production-only installs, useful in eg Docker builds.