Skip to content

Commit 85cba55

Browse files
fix: do not create temp subdirectories for router generator (#4860)
1 parent 34014c6 commit 85cba55

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

packages/router-generator/src/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path'
2-
import { existsSync, mkdirSync, readFileSync } from 'node:fs'
2+
import { existsSync, readFileSync } from 'node:fs'
33
import { z } from 'zod'
44
import { virtualRootRouteSchema } from './filesystem/virtual/config'
55
import type { GeneratorPlugin } from './plugin/types'
@@ -127,7 +127,6 @@ export function getConfig(
127127
if (!path.isAbsolute(dir)) {
128128
dir = path.resolve(process.cwd(), dir)
129129
}
130-
mkdirSync(dir, { recursive: true })
131130
return dir
132131
}
133132

packages/router-generator/src/generator.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import * as fsp from 'node:fs/promises'
3-
import { mkdtempSync } from 'node:fs'
3+
import { mkdirSync } from 'node:fs'
44
import crypto from 'node:crypto'
55
import { deepEqual, rootRouteId } from '@tanstack/router-core'
66
import { logging } from './logger'
@@ -61,7 +61,6 @@ interface fs {
6161
stat: (
6262
filePath: string,
6363
) => Promise<{ mtimeMs: bigint; mode: number; uid: number; gid: number }>
64-
mkdtempSync: (prefix: string) => string
6564
rename: (oldPath: string, newPath: string) => Promise<void>
6665
writeFile: (filePath: string, content: string) => Promise<void>
6766
readFile: (
@@ -83,7 +82,6 @@ const DefaultFileSystem: fs = {
8382
gid: Number(res.gid),
8483
}
8584
},
86-
mkdtempSync: mkdtempSync,
8785
rename: (oldPath, newPath) => fsp.rename(oldPath, newPath),
8886
writeFile: (filePath, content) => fsp.writeFile(filePath, content),
8987
readFile: async (filePath: string) => {
@@ -178,7 +176,7 @@ export class Generator {
178176

179177
private root: string
180178
private routesDirectoryPath: string
181-
private tmpDir: string
179+
private sessionId?: string
182180
private fs: fs
183181
private logger: Logger
184182
private generatedRouteTreePath: string
@@ -196,9 +194,6 @@ export class Generator {
196194
this.logger = logging({ disabled: this.config.disableLogging })
197195
this.root = opts.root
198196
this.fs = opts.fs || DefaultFileSystem
199-
this.tmpDir = this.fs.mkdtempSync(
200-
path.join(this.config.tmpDir, 'router-generator-'),
201-
)
202197
this.generatedRouteTreePath = path.resolve(this.config.generatedRouteTree)
203198
this.targetTemplate = getTargetTemplate(this.config)
204199

@@ -1089,7 +1084,13 @@ ${acc.routeTree.map((child) => `${child.variableName}${exportName}: typeof ${get
10891084
private getTempFileName(filePath: string) {
10901085
const absPath = path.resolve(filePath)
10911086
const hash = crypto.createHash('md5').update(absPath).digest('hex')
1092-
return path.join(this.tmpDir, hash)
1087+
// lazy initialize sessionId to only create tmpDir when it is first needed
1088+
if (!this.sessionId) {
1089+
// ensure the directory exists
1090+
mkdirSync(this.config.tmpDir, { recursive: true })
1091+
this.sessionId = crypto.randomBytes(4).toString('hex')
1092+
}
1093+
return `${this.sessionId}-${hash}`
10931094
}
10941095

10951096
private async isRouteFileCacheFresh(node: RouteNode): Promise<

0 commit comments

Comments
 (0)