Unverified Commit ead56eaa authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix invalid project dir casing breaking Next.js on Windows (#29205)

This fixes Next.js running into unexpected errors on Windows when invalid casing for a project directory is used. I was able to reproduce the issue on my local Windows machine and this resolved the issue there. 

<details>

<summary>screenshot of error while reproducing</summary>

<img width="838" alt="Screen Shot 2021-09-18 at 23 21 40" src="https://user-images.githubusercontent.com/22380829/133915825-ac4abdd2-fcf8-4309-9873-e6d88dfe485d.png">

</details>

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/27396
Fixes: https://github.com/vercel/next.js/issues/16535
Fixes: https://github.com/vercel/next.js/issues/17592
parent faa39038
# Invalid Project Directory Casing
#### Why This Error Occurred
When starting Next.js, the current directory is a different casing than the actual directory on your filesystem. This can cause files to resolve inconsistently.
This can occur when using a case-insensitive filesystem. For example, opening PowerShell on Windows navigating to `cd path/to/myproject` instead of `cd path/to/MyProject`.
#### Possible Ways to Fix It
Ensure the casing for the current working directory matches the actual case of the real directory. Use a terminal that enforces case-sensitivity.
### Useful Links
- [Next.js CLI documentation](https://nextjs.org/docs/api-reference/cli)
- [Case sensitivity in filesystems](https://en.wikipedia.org/wiki/Case_sensitivity#In_filesystems)
...@@ -470,6 +470,10 @@ ...@@ -470,6 +470,10 @@
{ {
"title": "invalid-api-status-body", "title": "invalid-api-status-body",
"path": "/errors/invalid-api-status-body.md" "path": "/errors/invalid-api-status-body.md"
},
{
"title": "invalid-project-dir-casing",
"path": "/errors/invalid-project-dir-casing.md"
} }
] ]
} }
......
#!/usr/bin/env node #!/usr/bin/env node
import { existsSync } from 'fs' import { existsSync } from 'fs'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import { resolve } from 'path'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
import build from '../build' import build from '../build'
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'
const nextBuild: cliCommand = (argv) => { const nextBuild: cliCommand = (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
...@@ -54,7 +54,7 @@ const nextBuild: cliCommand = (argv) => { ...@@ -54,7 +54,7 @@ const nextBuild: cliCommand = (argv) => {
if (args['--no-lint']) { if (args['--no-lint']) {
Log.warn('Linting is disabled') Log.warn('Linting is disabled')
} }
const dir = resolve(args._[0] || '.') const dir = getProjectDir(args._[0])
// Check if the provided directory exists // Check if the provided directory exists
if (!existsSync(dir)) { if (!existsSync(dir)) {
......
#!/usr/bin/env node #!/usr/bin/env node
import { resolve } from 'path'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import startServer from '../server/lib/start-server' import startServer from '../server/lib/start-server'
...@@ -8,6 +7,7 @@ import * as Log from '../build/output/log' ...@@ -8,6 +7,7 @@ import * as Log from '../build/output/log'
import { startedDevelopmentServer } from '../build/output' import { startedDevelopmentServer } from '../build/output'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
const nextDev: cliCommand = (argv) => { const nextDev: cliCommand = (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
...@@ -50,7 +50,7 @@ const nextDev: cliCommand = (argv) => { ...@@ -50,7 +50,7 @@ const nextDev: cliCommand = (argv) => {
process.exit(0) process.exit(0)
} }
const dir = resolve(args._[0] || '.') const dir = getProjectDir(args._[0])
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (!existsSync(dir)) { if (!existsSync(dir)) {
......
...@@ -7,6 +7,7 @@ import { printAndExit } from '../server/lib/utils' ...@@ -7,6 +7,7 @@ import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
import { trace } from '../trace' import { trace } from '../trace'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
const nextExport: cliCommand = (argv) => { const nextExport: cliCommand = (argv) => {
const nextExportCliSpan = trace('next-export-cli') const nextExportCliSpan = trace('next-export-cli')
...@@ -50,7 +51,7 @@ const nextExport: cliCommand = (argv) => { ...@@ -50,7 +51,7 @@ const nextExport: cliCommand = (argv) => {
process.exit(0) process.exit(0)
} }
const dir = resolve(args._[0] || '.') const dir = getProjectDir(args._[0])
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (!existsSync(dir)) { if (!existsSync(dir)) {
......
#!/usr/bin/env node #!/usr/bin/env node
import { existsSync } from 'fs' import { existsSync } from 'fs'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import { resolve, join } from 'path' import { join } from 'path'
import chalk from 'chalk' import chalk from 'chalk'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
...@@ -14,6 +14,7 @@ import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants' ...@@ -14,6 +14,7 @@ import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
import { eventLintCheckCompleted } from '../telemetry/events' import { eventLintCheckCompleted } from '../telemetry/events'
import { CompileError } from '../lib/compile-error' import { CompileError } from '../lib/compile-error'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
const eslintOptions = (args: arg.Spec, defaultCacheLocation: string) => ({ const eslintOptions = (args: arg.Spec, defaultCacheLocation: string) => ({
overrideConfigFile: args['--config'] || null, overrideConfigFile: args['--config'] || null,
...@@ -141,7 +142,7 @@ const nextLint: cliCommand = async (argv) => { ...@@ -141,7 +142,7 @@ const nextLint: cliCommand = async (argv) => {
) )
} }
const baseDir = resolve(args._[0] || '.') const baseDir = getProjectDir(args._[0])
// Check if the provided directory exists // Check if the provided directory exists
if (!existsSync(baseDir)) { if (!existsSync(baseDir)) {
......
#!/usr/bin/env node #!/usr/bin/env node
import { resolve } from 'path'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import startServer from '../server/lib/start-server' import startServer from '../server/lib/start-server'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
const nextStart: cliCommand = (argv) => { const nextStart: cliCommand = (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
...@@ -49,7 +49,7 @@ const nextStart: cliCommand = (argv) => { ...@@ -49,7 +49,7 @@ const nextStart: cliCommand = (argv) => {
process.exit(0) process.exit(0)
} }
const dir = resolve(args._[0] || '.') const dir = getProjectDir(args._[0])
let port: number = let port: number =
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000 args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000
const host = args['--hostname'] || '0.0.0.0' const host = args['--hostname'] || '0.0.0.0'
......
import fs from 'fs'
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)
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
}
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