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

Clean-up error when starting next with non-existent dir #34830

Merged
merged 1 commit into from Feb 25, 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
34 changes: 23 additions & 11 deletions packages/next/lib/get-project-dir.ts
Expand Up @@ -3,17 +3,29 @@ import path from 'path'
import * as Log from '../build/output/log'

export function getProjectDir(dir?: string) {
const resolvedDir = path.resolve(dir || '.')
const realDir = fs.realpathSync.native(resolvedDir)
try {
const resolvedDir = path.resolve(dir || '.')
const realDir = fs.realpathSync.native(resolvedDir)

if (
resolvedDir !== realDir &&
resolvedDir.toLowerCase() === realDir.toLowerCase()
) {
Log.warn(
`Invalid casing detected for project dir, received ${resolvedDir} actual path ${realDir}, see more info here https://nextjs.org/docs/messages/invalid-project-dir-casing`
)
}
if (
resolvedDir !== realDir &&
resolvedDir.toLowerCase() === realDir.toLowerCase()
) {
Log.warn(
`Invalid casing detected for project dir, received ${resolvedDir} actual path ${realDir}, see more info here https://nextjs.org/docs/messages/invalid-project-dir-casing`
)
}

return realDir
return realDir
} catch (err: any) {
if (err.code === 'ENOENT') {
Log.error(
`Invalid project directory provided, no such directory: ${path.resolve(
dir || '.'
)}`
)
process.exit(1)
}
throw err
}
}
46 changes: 46 additions & 0 deletions test/integration/cli/test/index.test.js
Expand Up @@ -48,7 +48,17 @@ describe('CLI Usage', () => {
new RegExp(`Next\\.js v${pkg.version.replace(/\./g, '\\.')}`)
)
})

test('invalid directory', async () => {
const output = await runNextCommand(['non-existent'], {
stderr: true,
})
expect(output.stderr).toContain(
'Invalid project directory provided, no such directory'
Copy link
Member

Choose a reason for hiding this comment

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

In the case when no command is provided, we could also print a help screen.

For example next buidl could print:

Invalid project directory provided `buildl`, no such directory.

Did you mean to run a subcommand instead?

next build
next start
next export

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, maybe we can add detecting mis-spellings of the main commands here, will investigate in a follow-up

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened PR adding this here #34836

)
})
})

describe('build', () => {
test('--help', async () => {
const help = await runNextCommand(['build', '--help'], {
Expand Down Expand Up @@ -109,6 +119,15 @@ describe('CLI Usage', () => {
expect(code).toBe(expectedExitCode)
expect(signal).toBe(expectedExitSignal)
})

test('invalid directory', async () => {
const output = await runNextCommand(['build', 'non-existent'], {
stderr: true,
})
expect(output.stderr).toContain(
'Invalid project directory provided, no such directory'
)
})
})

describe('dev', () => {
Expand Down Expand Up @@ -253,6 +272,15 @@ describe('CLI Usage', () => {
expect(code).toBe(expectedExitCode)
expect(signal).toBe(expectedExitSignal)
})

test('invalid directory', async () => {
const output = await runNextCommand(['dev', 'non-existent'], {
stderr: true,
})
expect(output.stderr).toContain(
'Invalid project directory provided, no such directory'
)
})
})

describe('start', () => {
Expand Down Expand Up @@ -337,6 +365,15 @@ describe('CLI Usage', () => {
expect(code).toBe(expectedExitCode)
expect(signal).toBe(expectedExitSignal)
})

test('invalid directory', async () => {
const output = await runNextCommand(['start', 'non-existent'], {
stderr: true,
})
expect(output.stderr).toContain(
'Invalid project directory provided, no such directory'
)
})
})

describe('export', () => {
Expand Down Expand Up @@ -366,6 +403,15 @@ describe('CLI Usage', () => {
})
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})

test('invalid directory', async () => {
const output = await runNextCommand(['export', 'non-existent'], {
stderr: true,
})
expect(output.stderr).toContain(
'Invalid project directory provided, no such directory'
)
})
})

describe('telemetry', () => {
Expand Down