From 4ebe4c45478b80537f852f8caac8c781f15034ee Mon Sep 17 00:00:00 2001 From: Vasu Bhog Date: Fri, 5 Aug 2022 16:18:03 -0700 Subject: [PATCH] [SQL-bindings] remove watcher for files (#20250) * remove watcher for files * nit --- .../src/common/azureFunctionsUtils.ts | 41 ------------------- extensions/sql-bindings/src/common/utils.ts | 14 ------- .../src/services/azureFunctionsService.ts | 20 ++------- .../service/azureFunctionsService.test.ts | 10 ----- 4 files changed, 3 insertions(+), 82 deletions(-) diff --git a/extensions/sql-bindings/src/common/azureFunctionsUtils.ts b/extensions/sql-bindings/src/common/azureFunctionsUtils.ts index d02fd03bb0..98948505b6 100644 --- a/extensions/sql-bindings/src/common/azureFunctionsUtils.ts +++ b/extensions/sql-bindings/src/common/azureFunctionsUtils.ts @@ -25,11 +25,6 @@ export interface ILocalSettingsJson { ConnectionStrings?: { [key: string]: string }; } -export interface IFileFunctionObject { - filePromise: Promise; - watcherDisposable: vscode.Disposable; -} - /** * copied and modified from vscode-azurefunctions extension * https://github.com/microsoft/vscode-azurefunctions/blob/main/src/funcConfig/local.settings.ts @@ -198,42 +193,6 @@ export async function getSettingsFile(projectFolder: string): Promise((resolve, _) => { - watcher.onDidCreate((e) => { - resolve(e.fsPath); - }); - }); - return { - filePromise, - watcherDisposable: watcher - }; -} - -/** - * Retrieves the new host project file once it has created and the watcher disposable - * @returns the host file path once created and the watcher disposable - */ -export function waitForNewHostFile(): IFileFunctionObject { - const watcher = vscode.workspace.createFileSystemWatcher('**/host.json', false, true, true); - const filePromise = new Promise((resolve, _) => { - watcher.onDidCreate((e) => { - resolve(e.fsPath); - }); - }); - return { - filePromise, - watcherDisposable: watcher - }; -} - /** * Adds the required nuget package to the project * @param selectedProjectFile is the users selected project file path diff --git a/extensions/sql-bindings/src/common/utils.ts b/extensions/sql-bindings/src/common/utils.ts index c11b9b6b06..3fa6122bda 100644 --- a/extensions/sql-bindings/src/common/utils.ts +++ b/extensions/sql-bindings/src/common/utils.ts @@ -83,20 +83,6 @@ export function generateQuotedFullName(schema: string, objectName: string): stri return `[${escapeClosingBrackets(schema)}].[${escapeClosingBrackets(objectName)}]`; } -/** - * Returns a promise that will reject after the specified timeout - * @param errorMessage error message to be returned in the rejection - * @param ms timeout in milliseconds. Default is 10 seconds - * @returns a promise that rejects after the specified timeout - */ -export function timeoutPromise(errorMessage: string, ms: number = 10000): Promise { - return new Promise((_, reject) => { - setTimeout(() => { - reject(new TimeoutError(errorMessage)); - }, ms); - }); -} - /** * Gets a unique file name * Increment the file name by adding 1 to function name if the file already exists diff --git a/extensions/sql-bindings/src/services/azureFunctionsService.ts b/extensions/sql-bindings/src/services/azureFunctionsService.ts index 64b0227d9e..47602dab75 100644 --- a/extensions/sql-bindings/src/services/azureFunctionsService.ts +++ b/extensions/sql-bindings/src/services/azureFunctionsService.ts @@ -26,7 +26,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise { TelemetryReporter.sendActionEvent(TelemetryViews.CreateAzureFunctionWithSqlBinding, TelemetryActions.startCreateAzureFunctionWithSqlBinding); let connectionInfo: IConnectionInfo | undefined; let isCreateNewProject: boolean = false; - let newFunctionFileObject: azureFunctionsUtils.IFileFunctionObject | undefined; try { // check to see if Azure Functions Extension is installed @@ -101,9 +100,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise { // user has an azure function project open projectFolder = path.dirname(projectFile); } - // create a system file watcher for the project folder - newFunctionFileObject = azureFunctionsUtils.waitForNewFunctionFile(projectFolder); - // Get connection string parameters and construct object name from prompt or connectionInfo given let objectName: string | undefined; let selectedBindingType: BindingType | undefined; @@ -263,9 +259,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise { .withAdditionalProperties(propertyBag) .withConnectionInfo(connectionInfo).send(); - // check for the new function file to be created and dispose of the file system watcher - const timeoutForFunctionFile = utils.timeoutPromise(constants.timeoutAzureFunctionFileError); - await Promise.race([newFunctionFileObject.filePromise, timeoutForFunctionFile]); telemetryStep = 'finishCreateFunction'; propertyBag.telemetryStep = telemetryStep; exitReason = ExitReason.finishCreate; @@ -275,15 +268,9 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise { } catch (error) { let errorType = utils.getErrorType(error); propertyBag.telemetryStep = telemetryStep; - if (errorType === 'TimeoutError') { - // this error can be cause by many different scenarios including timeout or error occurred during createFunction - exitReason = ExitReason.timeout; - console.log('Timed out waiting for Azure Function project to be created. This may not necessarily be an error, for example if the user canceled out of the create flow.'); - } else { - // else an error would occur during the createFunction - exitReason = ExitReason.error; - void vscode.window.showErrorMessage(constants.errorNewAzureFunction(error)); - } + // an error occurred during createFunction + exitReason = ExitReason.error; + void vscode.window.showErrorMessage(constants.errorNewAzureFunction(error)); TelemetryReporter.createErrorEvent(TelemetryViews.CreateAzureFunctionWithSqlBinding, TelemetryActions.exitCreateAzureFunctionQuickpick, undefined, errorType) .withAdditionalProperties(propertyBag).send(); return; @@ -292,7 +279,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise { propertyBag.exitReason = exitReason; TelemetryReporter.createActionEvent(TelemetryViews.CreateAzureFunctionWithSqlBinding, TelemetryActions.exitCreateAzureFunctionQuickpick) .withAdditionalProperties(propertyBag).send(); - newFunctionFileObject?.watcherDisposable.dispose(); } } diff --git a/extensions/sql-bindings/src/test/service/azureFunctionsService.test.ts b/extensions/sql-bindings/src/test/service/azureFunctionsService.test.ts index 65e4840db3..490e0816ed 100644 --- a/extensions/sql-bindings/src/test/service/azureFunctionsService.test.ts +++ b/extensions/sql-bindings/src/test/service/azureFunctionsService.test.ts @@ -7,7 +7,6 @@ import * as fs from 'fs'; import * as path from 'path'; import * as should from 'should'; import * as sinon from 'sinon'; -import * as TypeMoq from 'typemoq'; import * as vscode from 'vscode'; import * as azureFunctionUtils from '../../common/azureFunctionsUtils'; import * as constants from '../../common/constants'; @@ -90,9 +89,6 @@ describe('AzureFunctionsService', () => { sinon.stub(azureFunctionUtils, 'setLocalAppSetting').withArgs(sinon.match.any, 'SqlConnectionString', 'testConnectionString').resolves((true)); sinon.stub(utils, 'executeCommand').resolves('downloaded nuget package'); - const testWatcher = TypeMoq.Mock.ofType().object; - sinon.stub(azureFunctionUtils, 'waitForNewFunctionFile').withArgs(sinon.match.any).returns({ filePromise: Promise.resolve('TestFileCreated'), watcherDisposable: testWatcher }); - should(connectionInfo.database).equal('my_db', 'Initial ConnectionInfo database should be my_db'); await azureFunctionService.createAzureFunction(tableTestNode); @@ -127,9 +123,6 @@ describe('AzureFunctionsService', () => { sinon.stub(azureFunctionUtils, 'setLocalAppSetting').withArgs(sinon.match.any, 'SqlConnectionString', 'testConnectionString').resolves((true)); sinon.stub(utils, 'executeCommand').resolves('downloaded nuget package'); - const testWatcher = TypeMoq.Mock.ofType().object; - sinon.stub(azureFunctionUtils, 'waitForNewFunctionFile').withArgs(sinon.match.any).returns({ filePromise: Promise.resolve('TestFileCreated'), watcherDisposable: testWatcher }); - showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage'); // error message spy to be used for checking tests }); @@ -221,9 +214,6 @@ describe('AzureFunctionsService', () => { sinon.stub(azureFunctionUtils, 'setLocalAppSetting').withArgs(sinon.match.any, 'SqlConnectionString', 'testConnectionString').resolves((true)); sinon.stub(utils, 'executeCommand').resolves('downloaded nuget package'); - const testWatcher = TypeMoq.Mock.ofType().object; - sinon.stub(azureFunctionUtils, 'waitForNewFunctionFile').withArgs(sinon.match.any).returns({ filePromise: Promise.resolve('TestFileCreated'), watcherDisposable: testWatcher }); - showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage'); // error message spy to be used for checking tests });