Add some more verification and safety rules around custom `distDir` (#98997)
### What?
next will delete some or all of the contents of `distDir` on start
depending on mode. This is normally fine for the default `.next/` dir
but for a user specified one it can be dangerous if it is misconfigured.
Add some safety checks that the directory is in a valid location
I explored enforcing that the specified directory was either empty,
missing or filled only with files from a prior build/dev session, but
this is very ambiguous and would add a speed bump to upgrade paths.
re-compatibility: This PR is enforcing a constraint that wasn't
previously enforced. In the error case we are addressing (setting dist
dir to be the workspace root or appliction root), this is not considered
breaking because all such uses would be catastrophic (application files
deleted). The other enforcement is that the dist-dir is fully
_contained_, this was always strictly enforced for turbopack but for
webpack it would only fail later during dev-serving or SSG. So this is
just making an obscure error more eager and consistent.
Fixes #98958
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>