Skip to content

Commit dd67d73

Browse files
committed
fix: use tool-specific subdirectories for .env files to prevent overwrites
1 parent 512b3bd commit dd67d73

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

packages/core/src/custom-tools/custom-tool-registry.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,22 @@ export class CustomToolRegistry {
236236

237237
/**
238238
* Clear the TypeScript compilation cache (both in-memory and on disk).
239+
* This removes all tool-specific subdirectories and their contents.
239240
*/
240241
clearCache(): void {
241242
this.tsCache.clear()
242243

243244
if (fs.existsSync(this.cacheDir)) {
244245
try {
245-
const files = fs.readdirSync(this.cacheDir)
246-
for (const file of files) {
247-
if (file.endsWith(".mjs")) {
248-
fs.unlinkSync(path.join(this.cacheDir, file))
246+
const entries = fs.readdirSync(this.cacheDir, { withFileTypes: true })
247+
for (const entry of entries) {
248+
const entryPath = path.join(this.cacheDir, entry.name)
249+
if (entry.isDirectory()) {
250+
// Remove tool-specific subdirectory and all its contents.
251+
fs.rmSync(entryPath, { recursive: true, force: true })
252+
} else if (entry.name.endsWith(".mjs")) {
253+
// Also clean up any legacy flat .mjs files from older cache format.
254+
fs.unlinkSync(entryPath)
249255
}
250256
}
251257
} catch (error) {
@@ -282,11 +288,13 @@ export class CustomToolRegistry {
282288
return import(`file://${cachedPath}`)
283289
}
284290

285-
// Ensure cache directory exists.
286-
fs.mkdirSync(this.cacheDir, { recursive: true })
287-
288291
const hash = createHash("sha256").update(cacheKey).digest("hex").slice(0, 16)
289-
const tempFile = path.join(this.cacheDir, `${hash}.mjs`)
292+
293+
// Use a tool-specific subdirectory to avoid .env file conflicts between tools.
294+
const toolCacheDir = path.join(this.cacheDir, hash)
295+
fs.mkdirSync(toolCacheDir, { recursive: true })
296+
297+
const tempFile = path.join(toolCacheDir, "bundle.mjs")
290298

291299
// Check if we have a cached version on disk (from a previous run/instance).
292300
if (fs.existsSync(tempFile)) {
@@ -322,34 +330,37 @@ export class CustomToolRegistry {
322330
this.extensionPath,
323331
)
324332

325-
// Copy .env files from the tool's source directory to the cache directory.
326-
// This allows tools that use dotenv with __dirname to find their .env files.
327-
this.copyEnvFiles(toolDir)
333+
// Copy .env files from the tool's source directory to the tool-specific cache directory.
334+
// This allows tools that use dotenv with __dirname to find their .env files,
335+
// while ensuring different tools' .env files don't overwrite each other.
336+
this.copyEnvFiles(toolDir, toolCacheDir)
328337

329338
this.tsCache.set(cacheKey, tempFile)
330339
return import(`file://${tempFile}`)
331340
}
332341

333342
/**
334-
* Copy .env files from the tool's source directory to the cache directory.
335-
* This allows tools that use dotenv with __dirname to find their .env files.
343+
* Copy .env files from the tool's source directory to the tool-specific cache directory.
344+
* This allows tools that use dotenv with __dirname to find their .env files,
345+
* while ensuring different tools' .env files don't overwrite each other.
336346
*
337347
* @param toolDir - The directory containing the tool source files
348+
* @param destDir - The tool-specific cache directory to copy .env files to
338349
*/
339-
private copyEnvFiles(toolDir: string): void {
350+
private copyEnvFiles(toolDir: string, destDir: string): void {
340351
try {
341352
const files = fs.readdirSync(toolDir)
342353
const envFiles = files.filter((f) => f === ".env" || f.startsWith(".env."))
343354

344355
for (const envFile of envFiles) {
345356
const srcPath = path.join(toolDir, envFile)
346-
const destPath = path.join(this.cacheDir, envFile)
357+
const destPath = path.join(destDir, envFile)
347358

348359
// Only copy if source is a file (not a directory).
349360
const stat = fs.statSync(srcPath)
350361
if (stat.isFile()) {
351362
fs.copyFileSync(srcPath, destPath)
352-
console.log(`[CustomToolRegistry] copied ${envFile} to cache directory`)
363+
console.log(`[CustomToolRegistry] copied ${envFile} to tool cache directory`)
353364
}
354365
}
355366
} catch (error) {

0 commit comments

Comments
 (0)