Skip to content

Commit

Permalink
Require only build directory to be writeable for build (#20977)
Browse files Browse the repository at this point in the history
Allows building without permission to write to the application directory, useful for containers and sandboxes.
  • Loading branch information
ryan-lustre committed Jan 25, 2021
1 parent 75c5047 commit 8798c40
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/next/build/index.ts
Expand Up @@ -117,12 +117,6 @@ export default async function build(
): Promise<void> {
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)
Expand Down Expand Up @@ -363,9 +357,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'), () =>
Expand Down

0 comments on commit 8798c40

Please sign in to comment.