Skip to content

Commit 7333706

Browse files
authored
fix: prevent dependency version checker finding node_modules outside the project (#6892)
1 parent 238b692 commit 7333706

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

packages/payload/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
407407
.join(', ')
408408

409409
throw new Error(
410-
`Mismatching payload dependency versions found: ${formattedVersionsWithPackageNameString}. All payload and @payloadcms/* packages must have the same version.`,
410+
`Mismatching payload dependency versions found: ${formattedVersionsWithPackageNameString}. All payload and @payloadcms/* packages must have the same version. This is an error with your set-up, caused by you, not a bug in payload. Please go to your package.json and ensure all payload and @payloadcms/* packages have the same version.`,
411411
)
412412
}
413413
}

packages/payload/src/utilities/dependencies/getDependencies.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
import { findUp } from 'find-up'
1818
import { existsSync, promises as fs } from 'fs'
19-
import { dirname } from 'path'
19+
import path from 'path'
20+
import { fileURLToPath } from 'url'
2021

2122
import { resolveFrom } from './resolveFrom.js'
2223

24+
const filename = fileURLToPath(import.meta.url)
25+
const dirname = path.dirname(filename)
26+
2327
export type NecessaryDependencies = {
2428
missing: string[]
2529
resolved: Map<
@@ -49,12 +53,31 @@ export async function getDependencies(
4953
try {
5054
const pkgPath = await fs.realpath(resolveFrom(baseDir, pkg))
5155

52-
const pkgDir = dirname(pkgPath)
56+
const pkgDir = path.dirname(pkgPath)
5357

5458
let packageJsonFilePath = null
5559

56-
await findUp('package.json', { type: 'file', cwd: pkgDir }).then((path) => {
57-
packageJsonFilePath = path
60+
const processCwd = process.cwd()
61+
const payloadPkgDirname = path.resolve(dirname, '../../../') // pkg dir (outside src)
62+
63+
// if node_modules is in payloadPkgDirname, go to parent dir which contains node_modules
64+
if (payloadPkgDirname.includes('node_modules')) {
65+
payloadPkgDirname.split('node_modules').slice(0, -1)
66+
}
67+
68+
await findUp('package.json', { type: 'file', cwd: pkgDir }).then((foundPath) => {
69+
if (foundPath) {
70+
const resolvedFoundPath = path.resolve(foundPath)
71+
const resolvedCwd = path.resolve(processCwd)
72+
73+
if (
74+
resolvedFoundPath.startsWith(resolvedCwd) ||
75+
resolvedFoundPath.startsWith(payloadPkgDirname)
76+
) {
77+
// We don't want to match node modules outside the user's project. Checking for both process.cwd and dirname is a reliable way to do this.
78+
packageJsonFilePath = foundPath
79+
}
80+
}
5881
})
5982

6083
if (packageJsonFilePath && existsSync(packageJsonFilePath)) {

packages/richtext-lexical/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
7878
.join(', ')
7979

8080
throw new Error(
81-
`Mismatching lexical dependency versions found: ${formattedVersionsWithPackageNameString}. All lexical and @lexical/* packages must have the same version.`,
81+
`Mismatching lexical dependency versions found: ${formattedVersionsWithPackageNameString}. All lexical and @lexical/* packages must have the same version. This is an error with your set-up, caused by you, not a bug in payload. Please go to your package.json and ensure all lexical and @lexical/* packages have the same version.`,
8282
)
8383
}
8484
}

0 commit comments

Comments
 (0)