Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error if app detected but config is missing #41696

Merged
merged 6 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions packages/next/lib/find-pages-dir.ts
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import * as Log from '../build/output/log'

export const existsSync = (f: string): boolean => {
try {
Expand Down Expand Up @@ -29,21 +30,25 @@ export function findPagesDir(
appDir: string | undefined
} {
const pagesDir = findDir(dir, 'pages') || undefined
let appDir: undefined | string
const appDir = findDir(dir, 'app') || undefined
styfle marked this conversation as resolved.
Show resolved Hide resolved

if (isAppDirEnabled) {
appDir = findDir(dir, 'app') || undefined
}
const hasAppDir =
!!appDir && fs.existsSync(appDir) && fs.statSync(appDir).isDirectory()

if (hasAppDir && appDir == null && pagesDir == null) {
if (isAppDirEnabled && appDir == null && pagesDir == null) {
throw new Error(
"> Couldn't find any `pages` or `app` directory. Please create one under the project root"
)
}

if (!isAppDirEnabled) {
if (appDir != null && pagesDir == null) {
throw new Error(
'> The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a warning instead? Given that you could already have an app dir

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only an error for the case where pages isn't present and only app is present

)
}
if (appDir != null && pagesDir != null) {
Log.warn(
'The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
)
}
if (pagesDir == null) {
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
Expand All @@ -53,6 +58,6 @@ export function findPagesDir(

return {
pagesDir,
appDir,
appDir: isAppDirEnabled ? appDir : undefined,
}
}
10 changes: 10 additions & 0 deletions test/integration/appdir-missing-config/app/layout.js
@@ -0,0 +1,10 @@
export default function Root({ children }) {
return (
<html>
<head>
<title>Missing Config</title>
</head>
<body>{children}</body>
</html>
)
}
3 changes: 3 additions & 0 deletions test/integration/appdir-missing-config/app/page.js
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello from app</p>
}
94 changes: 94 additions & 0 deletions test/integration/appdir-missing-config/test/index.test.ts
@@ -0,0 +1,94 @@
/* eslint-env jest */

import path from 'path'
import fs from 'fs-extra'
import {
killApp,
findPort,
launchApp,
nextBuild,
waitFor,
} from 'next-test-utils'

const dir = path.join(__dirname, '..')
const nextConfig = path.join(dir, 'next.config.js')
const pagesIndex = path.join(dir, 'pages', 'index.js')
const msg =
'The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'

function runTests(justPutIt: () => Promise<string>) {
it('should print error when missing config with app', async () => {
const output = await justPutIt()
expect(output).toMatch(`Error: > ${msg}`)
})
it('should print warning when missing config with app and pages', async () => {
await fs.outputFile(pagesIndex, 'module.exports = "index"')
const output = await justPutIt()
expect(output).toMatch(`warn - ${msg}`)
})
it('should not print when config found with app', async () => {
await fs.writeFile(
nextConfig,
'module.exports = {experimental:{appDir: true}}'
)
const output = await justPutIt()
expect(output).not.toMatch(`Error: > ${msg}`)
expect(output).not.toMatch(`warn - ${msg}`)
})
it('should not print when config found with app and pages', async () => {
await fs.outputFile(pagesIndex, 'module.exports = "index"')
await fs.writeFile(
nextConfig,
'module.exports = {experimental:{appDir: true}}'
)
const output = await justPutIt()
expect(output).not.toMatch(`Error: > ${msg}`)
expect(output).not.toMatch(`warn - ${msg}`)
})
}

describe('Error when app dir is present without experimental.appDir', () => {
describe('next dev', () => {
const justPutIt = async () => {
let app
try {
const appPort = await findPort()
let output = ''
app = await launchApp(dir, appPort, {
onStdout(data: string) {
output += data
},
onStderr(data: string) {
output += data
},
})
await waitFor(200)
return output
} finally {
if (app?.pid) killApp(app)
await fs.remove(nextConfig)
await fs.remove(path.join(dir, 'pages'))
}
}
runTests(justPutIt)
})

describe('next build', () => {
const justPutIt = async () => {
let app
try {
app = await nextBuild(dir, [], {
stdout: true,
stderr: true,
env: { NEXT_SKIP_APP_REACT_INSTALL: '1' },
})
return app.stdout + app.stderr
} finally {
if (app?.pid) killApp(app)
await fs.remove(nextConfig)
await fs.remove(path.join(dir, 'pages'))
}
}
runTests(justPutIt)
})
})