diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index ece28d8e4bc37..36b1c5b4f7496 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -116,12 +116,6 @@ export default async function build( ): Promise { const span = tracer.startSpan('next-build') return traceAsyncFn(span, async () => { - if (!(await isWriteable(dir))) { - throw new Error( - '> Build directory is not writeable. https://err.sh/vercel/next.js/build-dir-not-writeable' - ) - } - // attempt to load global env values so they are available in next.config.js const { loadedEnvFiles } = traceFn(tracer.startSpan('load-dotenv'), () => loadEnvConfig(dir, false, Log) @@ -362,9 +356,27 @@ export default async function build( i18n: config.i18n || undefined, })) - await traceAsyncFn(tracer.startSpan('create-distdir'), () => - promises.mkdir(distDir, { recursive: true }) + const distDirCreated = await traceAsyncFn( + tracer.startSpan('create-distdir'), + async () => { + try { + await promises.mkdir(distDir, { recursive: true }) + return true + } catch (err) { + if (err.code === 'EPERM') { + return false + } + throw err + } + } ) + + if (!distDirCreated || !(await isWriteable(distDir))) { + throw new Error( + '> Build directory is not writeable. https://err.sh/vercel/next.js/build-dir-not-writeable' + ) + } + // We need to write the manifest with rewrites before build // so serverless can import the manifest await traceAsyncFn(tracer.startSpan('write-routes-manifest'), () =>