import * as assert from "assert" ; import { Extension , extensions } from "vscode" ; import { delayWhileSync , ext } from "../../extension.bundle" ; import { delay } from "./delay" ; import { writeToLog } from "./testLog" ; export async function ensureExtensionHasInitialized ( totalTimeout ) { async function ensureDotnetExtensionActivated ( ) { writeToLog ( ">>> Looking for dotnet extension " , true ) ; let extensionDotnet ; await delayWhileSync ( 5 * 1000 , ( ) => { extensionDotnet = extensions . getExtension ( "ms-dotnettools.vscode-dotnet-runtime" ) ; writeToLog ( extensionDotnet !== undefined ? "Dotnet extension found" : "Dotnet extension not found" , true ) ; return ! extensionDotnet ; } , 5 * 60 * 1000 ) ; await extensionDotnet ! . activate ( ) ; writeToLog ( ">>> Dotnet extension activated" , true ) ; } await ensureDotnetExtensionActivated ( ) ; async function waitForExtensionInitialization ( timeout ) { let start = Date . now ( ) ; writeToLog ( `Extension initialization state: ${ ext . extensionStartupComplete ? "Completed" : ext . extensionStartupComplete === undefined ? "Not started" : "In progress" }` , true ) ; while ( true ) { const extensionStartupComplete = ext . extensionStartupComplete ; if ( extensionStartupComplete ) { writeToLog ( `>>> Extension initialization complete` , true ) ; return true ; } else if ( ext . languageServerStartupError ) { writeToLog ( `>>> Extension initialization failed: ${ ext . extensionStartupError }` , true ) ; throw new Error ( ext . languageServerStartupError ) ; } if ( Date . now ( ) > start + timeout ) { return false ; } await delay ( 1000 ) ; } } const result1 = await waitForExtensionInitialization ( totalTimeout / 2 ) ; if ( result1 ) { return ; } writeToLog ( ">>> First timeout hit, will try to activate extension manually" , true ) ; let extension = extensions . getExtension ( ext . extensionId ) ; assert ( extension , `Couldn't find extension ${ ext . extensionId }` ) ; await extension . activate ( ) ; writeToLog ( ">>> Activation done" , true ) ; const result2 = await waitForExtensionInitialization ( totalTimeout / 2 ) ; if ( ! result2 ) { throw new Error ( "Timed out waiting for extension initialization to complete" ) ; } }