What happened?
Test suites are creating temporary directories but not cleaning them up after test completion, leading to:
- Disk space waste: ~6-8 temporary directories left behind per full test run (~50-100MB)
- Cluttered file system: Hundreds of directories accumulate over time in the OS temp folder
- Potential test interference: Leftover directories from previous runs could affect subsequent tests
- Developer confusion: Developers see mysterious temp directories and don't know if they're safe to delete
Root Causes Identified:
1. Missing cleanup in afterEach hooks (3 test files affected):
packages/core/src/tools/mcp-client.test.ts - 4 describe blocks creating testWorkspace without cleanup
packages/cli/src/config/extension-manager-scope.test.ts - Creating 2 temp directories with no cleanup
packages/cli/src/commands/extensions/configure.test.ts - Creating workspace directory without cleanup
Note: packages/cli/src/config/extension-manager.test.ts was initially flagged but is actually correct - tempWorkspaceDir is nested inside tempHomeDir, which is already deleted recursively in the existing afterEach.
2. Memory leaks:
WorkspaceContext objects created in beforeEach but never cleaned up in mcp-client.test.ts
- Event listeners and directory references held in memory across test runs
3. Misleading documentation:
- extension-manager-scope.test.ts had incorrect comments claiming parent/child relationship when directories are actually siblings
Example of the issue:
// In mcp-client.test.ts
beforeEach(() => {
testWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'gemini-agent-test-'));
workspaceContext = new WorkspaceContext(testWorkspace);
});
afterEach(async () => {
vi.restoreAllMocks(); // Mocks restored first
vi.useRealTimers();
// NO CLEANUP OF testWorkspace OR workspaceContext!
});
After running tests multiple times, the temp directory contains:
/tmp/gemini-agent-test-abc123/
/tmp/gemini-agent-test-def456/
/tmp/gemini-agent-test-ghi789/
... (hundreds more)
What did you expect to happen?
All temporary directories created during tests should be automatically cleaned up in afterEach hooks, leaving no trace after test completion.
Expected behavior:
- Temp directories created in
beforeEach
- Tests run using those directories
afterEach cleans up ALL temp directories and object references
- No leftover directories in OS temp folder
Client information
Client Information
Platform: Windows 11
Shell: bash
Node Version: v20.x (from .nvmrc)
Test Framework: Vitest 3.2.4
Project: @google/gemini-cli@0.35.0-nightly.20260311.657f19c1f
Login information
N/A - This is a test infrastructure issue, not related to authentication.
Anything else we need to know?
Impact Assessment:
- Severity: Medium (doesn't break functionality but wastes resources)
- Frequency: Every test run
- Affected Tests: 3 test files with missing cleanup, ~50+ test files with proper cleanup
- Disk Space: ~50-100MB per test run, accumulates over time
Proposed Solution:
Implement proper cleanup pattern in all affected test files:
afterEach(async () => {
// 1. Clean up resources FIRST (before restoring mocks)
try {
if (testWorkspace && fs.existsSync(testWorkspace)) {
// Windows-specific: wait for file handles to close
if (process.platform === 'win32') {
await new Promise((resolve) => setTimeout(resolve, 100));
}
fs.rmSync(testWorkspace, { recursive: true, force: true });
}
} catch {
// ignore (per ESLint no-console rule)
}
// 2. Clear object references to prevent memory leaks
workspaceContext = null as unknown as WorkspaceContext;
// 3. THEN restore mocks
vi.restoreAllMocks();
vi.useRealTimers();
});
Files Requiring Changes:
Critical (Must Fix):
packages/core/src/tools/mcp-client.test.ts - Add cleanup for testWorkspace and workspaceContext (4 locations)
packages/cli/src/config/extension-manager-scope.test.ts - Add afterEach with cleanup for both temp directories
packages/cli/src/commands/extensions/configure.test.ts - Add cleanup for tempWorkspaceDir
Already Correct (No Changes Needed):
4. packages/cli/src/config/extension-manager.test.ts - Already correct! tempWorkspaceDir is nested inside tempHomeDir, so the recursive deletion of tempHomeDir already cleans it up.
Important Considerations:
- Cleanup should run BEFORE mock restoration
- Windows requires 100ms delay for file handles to close
- Error handling should be silent (per ESLint no-console rule)
- Object references should be explicitly cleared to prevent memory leaks
Additional Context:
Most test files (~90%+) already have proper cleanup. This issue affects only 3 test files that were missing cleanup. The fix is straightforward and follows existing patterns used throughout the codebase.
What happened?
Test suites are creating temporary directories but not cleaning them up after test completion, leading to:
Root Causes Identified:
1. Missing cleanup in afterEach hooks (3 test files affected):
packages/core/src/tools/mcp-client.test.ts- 4 describe blocks creatingtestWorkspacewithout cleanuppackages/cli/src/config/extension-manager-scope.test.ts- Creating 2 temp directories with no cleanuppackages/cli/src/commands/extensions/configure.test.ts- Creating workspace directory without cleanupNote:
packages/cli/src/config/extension-manager.test.tswas initially flagged but is actually correct -tempWorkspaceDiris nested insidetempHomeDir, which is already deleted recursively in the existing afterEach.2. Memory leaks:
WorkspaceContextobjects created inbeforeEachbut never cleaned up in mcp-client.test.ts3. Misleading documentation:
Example of the issue:
After running tests multiple times, the temp directory contains:
What did you expect to happen?
All temporary directories created during tests should be automatically cleaned up in
afterEachhooks, leaving no trace after test completion.Expected behavior:
beforeEachafterEachcleans up ALL temp directories and object referencesClient information
Client Information
Platform: Windows 11
Shell: bash
Node Version: v20.x (from .nvmrc)
Test Framework: Vitest 3.2.4
Project:
@google/gemini-cli@0.35.0-nightly.20260311.657f19c1fLogin information
N/A - This is a test infrastructure issue, not related to authentication.
Anything else we need to know?
Impact Assessment:
Proposed Solution:
Implement proper cleanup pattern in all affected test files:
Files Requiring Changes:
Critical (Must Fix):
packages/core/src/tools/mcp-client.test.ts- Add cleanup fortestWorkspaceandworkspaceContext(4 locations)packages/cli/src/config/extension-manager-scope.test.ts- Add afterEach with cleanup for both temp directoriespackages/cli/src/commands/extensions/configure.test.ts- Add cleanup fortempWorkspaceDirAlready Correct (No Changes Needed):
4.
packages/cli/src/config/extension-manager.test.ts- Already correct!tempWorkspaceDiris nested insidetempHomeDir, so the recursive deletion oftempHomeDiralready cleans it up.Important Considerations:
Additional Context:
Most test files (~90%+) already have proper cleanup. This issue affects only 3 test files that were missing cleanup. The fix is straightforward and follows existing patterns used throughout the codebase.