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 1 commit
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
21 changes: 9 additions & 12 deletions packages/next/lib/find-pages-dir.ts
Expand Up @@ -29,30 +29,27 @@ 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 (pagesDir == null) {
if (!isAppDirEnabled && pagesDir == null) {
if (appDir != null) {
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
'> 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

)
}
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
}

return {
pagesDir,
appDir,
appDir: isAppDirEnabled ? appDir : undefined,
}
}
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>
}
61 changes: 61 additions & 0 deletions test/integration/appdir-missing-config/test/index.test.js
@@ -0,0 +1,61 @@
/* eslint-env jest */

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

let app
let appPort
const appDir = path.join(__dirname, '..')
let output = ''
const err =
/Error: > The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it/

function runTests() {
it('should print error for conflicting app/page', async () => {
styfle marked this conversation as resolved.
Show resolved Hide resolved
expect(output).toMatch(err)
})
}

describe('Conflict between app file and page file', () => {
styfle marked this conversation as resolved.
Show resolved Hide resolved
describe('next dev', () => {
beforeAll(async () => {
output = ''
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStdout(msg) {
output += msg || ''
},
onStderr(msg) {
output += msg || ''
},
})
await waitFor(200)
})
afterAll(() => {
if (app) killApp(app)
})
runTests()
})

describe('next build', () => {
beforeAll(async () => {
output = ''
const app = await nextBuild(appDir, [], {
stdout: true,
stderr: true,
env: { NEXT_SKIP_APP_REACT_INSTALL: '1' },
})
output = app.stdout + app.stderr
})
afterAll(() => {
if (app) killApp(app)
})
runTests()
})
})