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

fix: #34711 ensure distDir inside project folder #34795

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
14 changes: 9 additions & 5 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -147,6 +147,7 @@ export default class HotReloader {
private buildId: string
private middlewares: any[]
private pagesDir: string
private distDir: string
private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete
Copy link
Contributor Author

Choose a reason for hiding this comment

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

console.log(this.config.distDir)
// '../../dist/apps/blog/.next'

private runtime?: 'nodejs' | 'edge'
Expand All @@ -170,12 +171,14 @@ export default class HotReloader {
{
config,
pagesDir,
distDir,
buildId,
previewProps,
rewrites,
}: {
config: NextConfigComplete
pagesDir: string
distDir: string
buildId: string
previewProps: __ApiPreviewProps
rewrites: CustomRoutes['rewrites']
Expand All @@ -185,6 +188,7 @@ export default class HotReloader {
this.dir = dir
this.middlewares = []
this.pagesDir = pagesDir
this.distDir = distDir
this.clientStats = null
this.serverStats = null
this.serverPrevDocumentHash = null
Expand Down Expand Up @@ -435,15 +439,15 @@ export default class HotReloader {
startSpan.stop() // Stop immediately to create an artificial parent span

await this.clean(startSpan)

// Ensure distDir exists before writing package.json
await fs.mkdir(this.config.distDir, { recursive: true })
await fs.mkdir(this.distDir, { recursive: true })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here fs.mkdir and fs.writeFile was trying to create a file from the root folder to a relative path that starts with ../../ which goes outside of the project.
therefore, I've replaced it with distDir from the place of the Instance creation, where distDir is an absolute path and the issue disappeared.


const distPackageJsonPath = join(this.distDir, 'package.json')

// Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
await fs.writeFile(
join(this.config.distDir, 'package.json'),
'{"type": "commonjs"}'
)
await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')

const configs = await this.getWebpackConfig(startSpan)

Expand Down
1 change: 1 addition & 0 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -380,6 +380,7 @@ export default class DevServer extends Server {

this.hotReloader = new HotReloader(this.dir, {
pagesDir: this.pagesDir!,
distDir: this.distDir,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this.distDir // here distDir is the absolute path

config: this.nextConfig,
previewProps: this.getPreviewProps(),
buildId: this.buildId,
Expand Down