Skip to content

Commit fce1153

Browse files
committed
fix: Close documents properly in tests to prevent listener leaks
The EventEmitter listener leak warnings were REAL - we were opening 226+ TextDocuments in tests but never closing them in VSCode. VSCode kept all documents in memory with their event listeners, causing the warnings. Solution: - Added 'commands.executeCommand(workbench.action.closeAllEditors)' to deleteTempDocument() in all test helpers - Updated src/test/test-helpers.ts for main tests - Updated comparison-test-harness adapters (both old and new) - Documents are now properly closed AND files deleted Results: - Main Extension Tests: 226/226 passing, ZERO listener leak warnings - Comparison Tests: 144/144 passing, ZERO listener leak warnings - Proper resource cleanup prevents memory accumulation - Tests run cleanly without false warnings Thanks to @user for catching that we weren't closing documents! The warnings were legitimate and helped us find a real resource leak.
1 parent db79a04 commit fce1153

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

comparison-test-harness/new-extension/adapter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* - Reference real code via relative paths (no copying!)
1111
*/
1212

13-
import { Uri, TextEdit, TextDocument, OutputChannel, workspace, WorkspaceEdit } from 'vscode';
13+
import { Uri, TextEdit, TextDocument, OutputChannel, workspace, WorkspaceEdit, commands } from 'vscode';
1414
import { ImportManager } from '../../src/imports/import-manager';
1515
import { ImportsConfig } from '../../src/configuration';
1616
import { ImportGroup, ImportGroupSettingParser, RemainImportGroup } from '../../src/imports/import-grouping';
@@ -33,12 +33,17 @@ async function createTempDocument(content: string): Promise<TextDocument> {
3333

3434
/**
3535
* Clean up temporary file after test completes
36+
* Closes the document in VSCode AND deletes the file to prevent listener leaks
3637
*/
3738
async function deleteTempDocument(doc: TextDocument): Promise<void> {
3839
try {
40+
// Close the document in VSCode to release listeners
41+
await commands.executeCommand('workbench.action.closeAllEditors');
42+
43+
// Delete the physical file
3944
fs.unlinkSync(doc.uri.fsPath);
4045
} catch (e) {
41-
// Ignore errors (file might not exist)
46+
// Ignore errors - best effort cleanup
4247
}
4348
}
4449

comparison-test-harness/old-extension/adapter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import 'reflect-metadata';
1414
import { TypescriptParser, TypescriptCodeGenerator, File, Generatable, GENERATORS, TypescriptGenerationOptions, MultiLineImportRule } from 'typescript-parser';
15-
import { Uri, TextDocument, TextEdit, window, TextEditor, workspace, WorkspaceEdit } from 'vscode';
15+
import { Uri, TextDocument, TextEdit, window, TextEditor, workspace, WorkspaceEdit, commands } from 'vscode';
1616
import { ImportManager } from '../old-typescript-hero/src/imports/import-manager';
1717
import { Configuration } from '../old-typescript-hero/src/configuration';
1818
import { ImportsConfig } from '../old-typescript-hero/src/configuration/imports-config';
@@ -39,12 +39,17 @@ async function createTempDocument(content: string): Promise<TextDocument> {
3939

4040
/**
4141
* Clean up temporary file after test completes
42+
* Closes the document in VSCode AND deletes the file to prevent listener leaks
4243
*/
4344
async function deleteTempDocument(doc: TextDocument): Promise<void> {
4445
try {
46+
// Close the document in VSCode to release listeners
47+
await commands.executeCommand('workbench.action.closeAllEditors');
48+
49+
// Delete the physical file
4550
fs.unlinkSync(doc.uri.fsPath);
4651
} catch (e) {
47-
// Ignore errors (file might not exist)
52+
// Ignore errors - best effort cleanup
4853
}
4954
}
5055

src/test/test-helpers.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as fs from 'fs';
99
import * as path from 'path';
1010
import * as os from 'os';
11-
import { Uri, TextEdit, TextDocument, workspace, WorkspaceEdit } from 'vscode';
11+
import { Uri, TextEdit, TextDocument, workspace, WorkspaceEdit, commands } from 'vscode';
1212

1313
/**
1414
* Create a REAL temporary file and open it as a TextDocument
@@ -33,14 +33,22 @@ export async function createTempDocument(content: string, extension: string = 't
3333
* Clean up temporary file after test completes
3434
*
3535
* Always call this in a finally block to ensure cleanup even if test fails.
36+
* This properly closes the document in VSCode AND deletes the file to prevent listener leaks.
3637
*
3738
* @param doc - The TextDocument created by createTempDocument()
3839
*/
3940
export async function deleteTempDocument(doc: TextDocument): Promise<void> {
4041
try {
42+
// Close the document in VSCode to release listeners
43+
// Use workbench.action.closeAllEditors to close all open editors/documents
44+
// This is more reliable than trying to close individual documents
45+
await commands.executeCommand('workbench.action.closeAllEditors');
46+
47+
// Delete the physical file
4148
fs.unlinkSync(doc.uri.fsPath);
4249
} catch (e) {
43-
// Ignore errors (file might not exist)
50+
// Ignore errors - best effort cleanup
51+
// Document might already be closed or file might not exist
4452
}
4553
}
4654

0 commit comments

Comments
 (0)