@@ -835,20 +835,23 @@ export class CTestDriver implements vscode.Disposable {
835835 log . error ( localize ( 'folder.not.found.in.test.explorer' , 'Folder is not found in Test Explorer: {0}' , sourceDir ) ) ;
836836 return ;
837837 }
838- // Clear all children and re-add later
839- testExplorerRoot . children . replace ( [ ] ) ;
838+
839+ // Keep track of all currently active test ids to remove the invalid ones afterwards
840+ const activeTestIDs = new Set < string > ( ) ;
840841
841842 if ( ! ctestArgs ) {
842843 // Happens when testPreset is not selected
843844 const testItem = initializedTestExplorer . createTestItem ( testPresetRequired , localize ( 'test.preset.required' , 'Select a test preset to discover tests' ) ) ;
844845 testExplorerRoot . children . add ( testItem ) ;
846+ activeTestIDs . add ( testItem . id ) ;
845847 return ;
846848 }
847849
848850 if ( testType === "LegacyCTest" && this . legacyTests !== undefined ) {
849851 // Legacy CTest tests
850852 for ( const test of this . legacyTests ) {
851853 testExplorerRoot . children . add ( initializedTestExplorer . createTestItem ( test . name , test . name ) ) ;
854+ activeTestIDs . add ( test . name ) ;
852855 }
853856 } else if ( testType === "CTestInfo" && this . tests !== undefined ) {
854857 if ( this . tests && this . tests . kind === 'ctestInfo' ) {
@@ -908,12 +911,25 @@ export class CTestDriver implements vscode.Disposable {
908911 if ( testTags . length !== 0 ) {
909912 testItem . tags = [ ...testItem . tags , ...testTags ] ;
910913 }
911-
912914 parentSuiteItem . children . add ( testItem ) ;
915+ activeTestIDs . add ( testItem . id ) ;
913916 } ) ;
914917 } ;
915918 }
916919
920+ const removeDeletedTests = ( collection : vscode . TestItemCollection ) => {
921+ collection . forEach ( ( item : vscode . TestItem , collection : vscode . TestItemCollection ) => {
922+ if ( item . children . size === 0 ) {
923+ if ( ! activeTestIDs . has ( item . id ) ) {
924+ collection . delete ( item . id ) ;
925+ }
926+ } else {
927+ removeDeletedTests ( item . children ) ;
928+ }
929+ } ) ;
930+ } ;
931+
932+ removeDeletedTests ( initializedTestExplorer . items ) ;
917933 }
918934
919935 /**
0 commit comments