Unverified Commit 41a80533 authored by Sebastian Silbermann's avatar Sebastian Silbermann Committed by GitHub

Revert "Don't fail the build if @types/ packages appear to be missing" (#66130)

This reverts commit
https://github.com/vercel/next.js/commit/637033c41054ee1d50107b1fd63a91a2051e1653.

What wasn't clear originally is that we require that required packages
have their `package.json` resolvable. So either they don't have an
`exports` field, or their `package.json` is listed in the `exports`
field. The React beta types didn't have `package.json` entry in
`exports` so we accidentally thought they weren't installed.

I published [`19.0.0-beta.2` that has `package.json` in
`exports`](https://github.com/eps1lon/DefinitelyTyped/pull/31/commits/13879ee571bd3996ad2dffd58a288e9762bf31d1#diff-81cc573aa0c2bd0e13f9463499747741704aabccd7474f544db710befd7bcfc4R44)
so we can restore the old behavior. It's still questionable IMO to do
all that I/O work just for a nicer error message that may contain
false-positive but that's for another time.
parent 11c19cfb
......@@ -4,7 +4,23 @@ import { dirname, join, relative } from 'path'
export interface MissingDependency {
file: string
/**
* The package's package.json (e.g. require(`${pkg}/package.json`)) MUST resolve.
* If `exportsRestrict` is false, `${file}` MUST also resolve.
*/
pkg: string
/**
* If true, the pkg's package.json needs to be resolvable.
* If true, will resolve `file` relative to the real path of the package.json.
*
* For example, `{ file: '@types/react/index.d.ts', pkg: '@types/react', exportsRestrict: true }`
* will try to resolve '@types/react/package.json' first and then assume `@types/react/index.d.ts`
* resolves to `path.join(dirname(resolvedPackageJsonPath), 'index.d.ts')`.
*
* If false, will resolve `file` relative to the baseDir.
* ForFor example, `{ file: '@types/react/index.d.ts', pkg: '@types/react', exportsRestrict: true }`
* will try to resolve `@types/react/index.d.ts` directly.
*/
exportsRestrict: boolean
}
......
......@@ -2,10 +2,7 @@ import { bold, cyan, red, yellow } from './picocolors'
import path from 'path'
import { hasNecessaryDependencies } from './has-necessary-dependencies'
import type {
MissingDependency,
NecessaryDependencies,
} from './has-necessary-dependencies'
import type { NecessaryDependencies } from './has-necessary-dependencies'
import semver from 'next/dist/compiled/semver'
import { CompileError } from './compile-error'
import * as log from '../build/output/log'
......@@ -72,37 +69,11 @@ export async function verifyTypeScriptSetup({
requiredPackages
)
const missingTypeDeps: MissingDependency[] = []
const missingDepsExceptTypes: MissingDependency[] = []
for (const dep of deps.missing) {
if (dep.pkg.startsWith('@types/')) {
missingTypeDeps.push(dep)
} else {
missingDepsExceptTypes.push(dep)
}
}
// Just warn for potentially missing type deps.
// TODO: Fix false-positive in `hasNecessaryDependencies` when `overrides` are used in package managers
if (missingTypeDeps.length > 0) {
console.log(
bold(
yellow(
`It looks like you're trying to use TypeScript but do not have the required type package(s) installed.`
)
) +
'\n' +
'This may be a bug in Next.js. Please file an issue.\n' +
'Make sure the following type packages are installed:\n' +
missingTypeDeps.map((dep) => ` ${cyan(dep.pkg)}`).join('\n')
)
}
if (missingDepsExceptTypes.length > 0) {
if (deps.missing?.length > 0) {
if (isCI) {
// we don't attempt auto install in CI to avoid side-effects
// and instead log the error for installing needed packages
missingDepsError(dir, missingDepsExceptTypes)
missingDepsError(dir, deps.missing)
}
console.log(
bold(
......@@ -120,18 +91,16 @@ export async function verifyTypeScriptSetup({
) +
'\n'
)
await installDependencies(dir, missingDepsExceptTypes, true).catch(
(err) => {
if (err && typeof err === 'object' && 'command' in err) {
console.error(
`Failed to install required TypeScript dependencies, please install them manually to continue:\n` +
(err as any).command +
'\n'
)
}
throw err
await installDependencies(dir, deps.missing, true).catch((err) => {
if (err && typeof err === 'object' && 'command' in err) {
console.error(
`Failed to install required TypeScript dependencies, please install them manually to continue:\n` +
(err as any).command +
'\n'
)
}
)
throw err
})
deps = await hasNecessaryDependencies(dir, requiredPackages)
}
......
......@@ -68,15 +68,9 @@ describe('ci-missing-typescript-deps', () => {
})
try {
const nextBuild = await next.build()
// build doesn't fail because types/ may not be installed.
expect(nextBuild.cliOutput.split('\n')).toEqual(
expect.arrayContaining([
'This may be a bug in Next.js. Please file an issue.',
'Make sure the following type packages are installed:',
// In cyan color but this changes between CI and local.
// TODO: Use picocolors in tests to ensure we can test local and CI.
expect.stringContaining('@types/react'),
])
expect(nextBuild.cliOutput).toContain(
// matching the part of the success message that isn't colored.
`We detected TypeScript in your project and created`
)
} finally {
await next.destroy()
......
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