Skip to content

Test suites leave behind temporary directories causing disk space waste #22032

@Drix10

Description

@Drix10

What happened?

Test suites are creating temporary directories but not cleaning them up after test completion, leading to:

  1. Disk space waste: ~6-8 temporary directories left behind per full test run (~50-100MB)
  2. Cluttered file system: Hundreds of directories accumulate over time in the OS temp folder
  3. Potential test interference: Leftover directories from previous runs could affect subsequent tests
  4. 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:

  1. Temp directories created in beforeEach
  2. Tests run using those directories
  3. afterEach cleans up ALL temp directories and object references
  4. 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):

  1. packages/core/src/tools/mcp-client.test.ts - Add cleanup for testWorkspace and workspaceContext (4 locations)
  2. packages/cli/src/config/extension-manager-scope.test.ts - Add afterEach with cleanup for both temp directories
  3. 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.

Metadata

Metadata

Assignees

Labels

area/coreIssues related to User Interface, OS Support, Core Functionalitytype/bug

Type

No fields configured for Bug.

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions