Unverified Commit d5e43557 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Resolve React dependency warnings from the target app (#99536)

### Why?

React dependency warnings resolve from the CLI installation, which can
report missing packages that are already installed in the target app.

### How?

Use a shared check against each command's resolved app directory,
preserving explicit app paths and forwarded test-runner arguments.

<!-- NEXT_JS_LLM -->
parent ebb63d01
...@@ -75,17 +75,6 @@ if ( ...@@ -75,17 +75,6 @@ if (
process.env.NEXT_PRIVATE_START_TIME = Date.now().toString() process.env.NEXT_PRIVATE_START_TIME = Date.now().toString()
for (const dependency of ['react', 'react-dom']) {
try {
// When 'npm link' is used it checks the clone location. Not the project.
require.resolve(dependency)
} catch (err) {
console.warn(
`The module '${dependency}' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install ${dependency}'`
)
}
}
class NextRootCommand extends Command { class NextRootCommand extends Command {
createCommand(name: string) { createCommand(name: string) {
const command = new Command(name) const command = new Command(name)
......
...@@ -8,6 +8,7 @@ import analyze from '../build/analyze' ...@@ -8,6 +8,7 @@ import analyze from '../build/analyze'
import { warn } from '../build/output/log' import { warn } from '../build/output/log'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
export type NextAnalyzeOptions = { export type NextAnalyzeOptions = {
experimentalAnalyze?: boolean experimentalAnalyze?: boolean
...@@ -45,6 +46,7 @@ const nextAnalyze = async (options: NextAnalyzeOptions, directory?: string) => { ...@@ -45,6 +46,7 @@ const nextAnalyze = async (options: NextAnalyzeOptions, directory?: string) => {
} }
const dir = getProjectDir(directory) const dir = getProjectDir(directory)
warnMissingReactDependencies(dir)
if (!existsSync(dir)) { if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`) printAndExit(`> No such directory exists as the project root: ${dir}`)
......
...@@ -8,6 +8,7 @@ import { warn } from '../build/output/log' ...@@ -8,6 +8,7 @@ import { warn } from '../build/output/log'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { enableMemoryDebuggingMode } from '../lib/memory/startup' import { enableMemoryDebuggingMode } from '../lib/memory/startup'
import { disableMemoryDebuggingMode } from '../lib/memory/shutdown' import { disableMemoryDebuggingMode } from '../lib/memory/shutdown'
import { Bundler, parseBundlerArgs } from '../lib/bundler' import { Bundler, parseBundlerArgs } from '../lib/bundler'
...@@ -102,6 +103,7 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => { ...@@ -102,6 +103,7 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
} }
const dir = getProjectDir(directory) const dir = getProjectDir(directory)
warnMissingReactDependencies(dir)
if (!existsSync(dir)) { if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`) printAndExit(`> No such directory exists as the project root: ${dir}`)
......
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
} from '../server/lib/utils' } from '../server/lib/utils'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { ensureProfilesDir } from '../lib/profiles-dir' import { ensureProfilesDir } from '../lib/profiles-dir'
import path from 'path' import path from 'path'
import { traceGlobals } from '../trace/shared' import { traceGlobals } from '../trace/shared'
...@@ -249,6 +250,7 @@ const nextDev = async ( ...@@ -249,6 +250,7 @@ const nextDev = async (
isTurbopack = parseBundlerArgs(options) === Bundler.Turbopack isTurbopack = parseBundlerArgs(options) === Bundler.Turbopack
dir = getProjectDir(process.env.NEXT_PRIVATE_DEV_DIR || directory) dir = getProjectDir(process.env.NEXT_PRIVATE_DEV_DIR || directory)
warnMissingReactDependencies(dir)
const { shouldPromptForUpgrade, runUpgrade, nudgeUpgrade } = await import( const { shouldPromptForUpgrade, runUpgrade, nudgeUpgrade } = await import(
'../lib/upgrade/nudge.js' '../lib/upgrade/nudge.js'
......
...@@ -5,6 +5,7 @@ import path from 'path' ...@@ -5,6 +5,7 @@ import path from 'path'
import loadConfig from '../server/config' import loadConfig from '../server/config'
import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants' import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import { loadBindings } from '../build/swc' import { loadBindings } from '../build/swc'
...@@ -15,6 +16,7 @@ const nextPostBuild = async ( ...@@ -15,6 +16,7 @@ const nextPostBuild = async (
directory?: string directory?: string
) => { ) => {
const dir = getProjectDir(directory) const dir = getProjectDir(directory)
warnMissingReactDependencies(dir)
if (!existsSync(dir)) { if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`) printAndExit(`> No such directory exists as the project root: ${dir}`)
......
...@@ -17,6 +17,7 @@ import { ...@@ -17,6 +17,7 @@ import {
type DebugAddress, type DebugAddress,
} from '../server/lib/utils' } from '../server/lib/utils'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { import {
getReservedPortExplanation, getReservedPortExplanation,
isPortIsReserved, isPortIsReserved,
...@@ -41,6 +42,7 @@ export type NextStartOptions = { ...@@ -41,6 +42,7 @@ export type NextStartOptions = {
*/ */
const nextStart = async (options: NextStartOptions, directory?: string) => { const nextStart = async (options: NextStartOptions, directory?: string) => {
const dir = getProjectDir(directory) const dir = getProjectDir(directory)
warnMissingReactDependencies(dir)
const hostname = options.hostname const hostname = options.hostname
const inspect = options.inspect const inspect = options.inspect
const port = options.port const port = options.port
......
import { writeFileSync } from 'fs' import { writeFileSync } from 'fs'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import loadConfig from '../server/config' import loadConfig from '../server/config'
import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants' import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
...@@ -66,6 +67,10 @@ export async function nextTest( ...@@ -66,6 +67,10 @@ export async function nextTest(
nextConfig = await loadConfig(PHASE_PRODUCTION_BUILD, baseDir) // let this error bubble up if the `basePath` is still not a valid Next.js project nextConfig = await loadConfig(PHASE_PRODUCTION_BUILD, baseDir) // let this error bubble up if the `basePath` is still not a valid Next.js project
} }
// The directory may have been a runner argument; check the app only after
// the fallback above has resolved its actual project directory.
warnMissingReactDependencies(baseDir)
// set the test runner. priority is CLI option > next config > default 'playwright' // set the test runner. priority is CLI option > next config > default 'playwright'
const configuredTestRunner = const configuredTestRunner =
options?.testRunner ?? // --test-runner='foo' options?.testRunner ?? // --test-runner='foo'
......
...@@ -8,6 +8,7 @@ import loadConfig from '../server/config' ...@@ -8,6 +8,7 @@ import loadConfig from '../server/config'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants' import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { findPagesDir } from '../lib/find-pages-dir' import { findPagesDir } from '../lib/find-pages-dir'
import { verifyAndRunTypeScript } from '../lib/verify-typescript-setup' import { verifyAndRunTypeScript } from '../lib/verify-typescript-setup'
import { discoverRoutes } from '../build/route-discovery' import { discoverRoutes } from '../build/route-discovery'
...@@ -31,6 +32,7 @@ const nextTypegen = async (options: NextTypegenOptions, directory?: string) => { ...@@ -31,6 +32,7 @@ const nextTypegen = async (options: NextTypegenOptions, directory?: string) => {
parseBundlerArgs(options) parseBundlerArgs(options)
const baseDir = getProjectDir(directory) const baseDir = getProjectDir(directory)
warnMissingReactDependencies(baseDir)
// Check if the provided directory exists // Check if the provided directory exists
if (!existsSync(baseDir)) { if (!existsSync(baseDir)) {
......
...@@ -8,6 +8,7 @@ import * as Log from '../build/output/log' ...@@ -8,6 +8,7 @@ import * as Log from '../build/output/log'
import createSpinner from '../build/spinner' import createSpinner from '../build/spinner'
import { findDir } from '../lib/find-pages-dir' import { findDir } from '../lib/find-pages-dir'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
import { getNpxCommand } from '../lib/helpers/get-npx-command' import { getNpxCommand } from '../lib/helpers/get-npx-command'
import { interopDefault } from '../lib/interop-default' import { interopDefault } from '../lib/interop-default'
import { dim } from '../lib/picocolors' import { dim } from '../lib/picocolors'
...@@ -183,6 +184,7 @@ export async function spawnNextUpgrade( ...@@ -183,6 +184,7 @@ export async function spawnNextUpgrade(
let configError: unknown = null let configError: unknown = null
try { try {
baseDir = getProjectDir(directory, false) baseDir = getProjectDir(directory, false)
warnMissingReactDependencies(baseDir)
const config = await loadAgentUpgradeConfig(baseDir) const config = await loadAgentUpgradeConfig(baseDir)
distDir = config.distDir || '.next' distDir = config.distDir || '.next'
configuredPolicy = config.experimental?.agentUpgrade configuredPolicy = config.experimental?.agentUpgrade
...@@ -627,6 +629,7 @@ When this task ends, report its result once. After completing the requested upgr ...@@ -627,6 +629,7 @@ When this task ends, report its result once. After completing the requested upgr
} }
baseDir = getProjectDir(directory) baseDir = getProjectDir(directory)
warnMissingReactDependencies(baseDir)
const [upgradeProcessCommand, ...upgradeProcessDefaultArgs] = const [upgradeProcessCommand, ...upgradeProcessDefaultArgs] =
getNpxCommand(baseDir).split(' ') getNpxCommand(baseDir).split(' ')
......
export function warnMissingReactDependencies(projectDir: string) {
// Resolve from the app so downloaded or linked CLI installations do not
// warn about dependencies that are already installed in the target project.
for (const dependency of ['react', 'react-dom']) {
try {
require.resolve(dependency, { paths: [projectDir] })
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
throw err
}
console.warn(
`The module '${dependency}' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install ${dependency}'`
)
}
}
}
...@@ -366,6 +366,7 @@ async function readNormalizedNFT(next, name) { ...@@ -366,6 +366,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/lib/verify-root-layout.js", "/node_modules/next/dist/lib/verify-root-layout.js",
"/node_modules/next/dist/lib/verify-typescript-setup.js", "/node_modules/next/dist/lib/verify-typescript-setup.js",
"/node_modules/next/dist/lib/wait.js", "/node_modules/next/dist/lib/wait.js",
"/node_modules/next/dist/lib/warn-missing-react-dependencies.js",
"/node_modules/next/dist/lib/with-promise-cache.js", "/node_modules/next/dist/lib/with-promise-cache.js",
"/node_modules/next/dist/lib/worker.js", "/node_modules/next/dist/lib/worker.js",
"/node_modules/next/dist/server/*", "/node_modules/next/dist/server/*",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment