Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update global variables and resolve dependency cycle issues #11791

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/codeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import isUUID from "validator/lib/isUUID";
import * as vscode from "vscode";
import { environmentVariableRegex } from "./constants";
import { commandIsRunning } from "./globalVariables";
import { getSystemInputs } from "./handlers";
import { getSystemInputs } from "./utils/environmentUtils";
import { TelemetryTriggerFrom } from "./telemetry/extTelemetryEvents";
import { localize } from "./utils/localizeUtils";
import * as _ from "lodash";
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/commonlib/azureLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
AccountType,
TelemetryErrorType,
} from "../telemetry/extTelemetryEvents";
import { VS_CODE_UI } from "../extension";
import { VS_CODE_UI } from "../qm/vsc_ui";
import { AzureScopes, globalStateGet, globalStateUpdate } from "@microsoft/teamsfx-core";
import { getDefaultString, localize } from "../utils/localizeUtils";
import {
Expand Down
21 changes: 8 additions & 13 deletions packages/vscode-extension/src/commonlib/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as vscode from "vscode";
import * as os from "os";
import * as path from "path";
// eslint-disable-next-line import/default
import Reporter from "@vscode/extension-telemetry";
import { TelemetryReporter, ConfigFolderName } from "@microsoft/teamsfx-api";
import {
getAllFeatureFlags,
getPackageVersion,
isFeatureFlagEnabled,
FeatureFlags,
anonymizeFilePaths,
} from "../utils/commonUtils";
import { anonymizeFilePaths } from "../utils/fileSystemUtils";
import { isFeatureFlagEnabled, FeatureFlags, getAllFeatureFlags } from "../featureFlags";
import { getPackageVersion } from "../utils/telemetryUtils";
import { TelemetryProperty } from "../telemetry/extTelemetryEvents";
import { getProjectMetadata } from "@microsoft/teamsfx-core";
import { Correlator } from "@microsoft/teamsfx-core";
import { Correlator, getProjectMetadata } from "@microsoft/teamsfx-core";
import { configure, getLogger, Logger } from "log4js";
import * as os from "os";
import * as path from "path";
import * as globalVariables from "../globalVariables";
import { workspaceUri } from "../globalVariables";

const TelemetryTestLoggerFile = "telemetryTest.log";

Expand Down Expand Up @@ -176,7 +171,7 @@ export class VSCodeTelemetryReporter extends vscode.Disposable implements Teleme

private checkAndOverwriteSharedProperty(properties: { [p: string]: string }) {
if (!properties[TelemetryProperty.ProjectId]) {
const fixedProjectSettings = getProjectMetadata(globalVariables.workspaceUri?.fsPath);
const fixedProjectSettings = getProjectMetadata(workspaceUri?.fsPath);

if (fixedProjectSettings?.projectId) {
properties[TelemetryProperty.ProjectId] = fixedProjectSettings?.projectId;
Expand Down
25 changes: 4 additions & 21 deletions packages/vscode-extension/src/debug/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ import {
import * as fs from "fs-extra";
import * as path from "path";
import * as uuid from "uuid";
import * as vscode from "vscode";
import VsCodeLogInstance from "../commonlib/log";

import * as globalVariables from "../globalVariables";
import { core, getSystemInputs } from "../handlers";
import { workspaceUri } from "../globalVariables";
import { ExtTelemetry } from "../telemetry/extTelemetry";
import { allRunningDebugSessions } from "./teamsfxTaskHandler";

import { ExtensionErrors, ExtensionSource } from "../error";
import { VS_CODE_UI } from "../extension";

export async function getProjectRoot(
folderPath: string,
Expand All @@ -40,10 +35,10 @@ export async function getNpmInstallLogInfo(): Promise<any> {

export async function getTestToolLogInfo(): Promise<string | undefined> {
const localEnvManager = new LocalEnvManager(VsCodeLogInstance, ExtTelemetry.reporter);
if (!globalVariables.workspaceUri?.fsPath) {
if (!workspaceUri?.fsPath) {
return undefined;
}
return await localEnvManager.getTestToolLogInfo(globalVariables.workspaceUri?.fsPath);
return await localEnvManager.getTestToolLogInfo(workspaceUri?.fsPath);
}

export class LocalDebugSession {
Expand Down Expand Up @@ -137,7 +132,7 @@ export async function getV3TeamsAppId(projectPath: string, env: string): Promise
}

export async function getTeamsAppKeyName(env?: string): Promise<string | undefined> {
const templatePath = pathUtils.getYmlFilePath(globalVariables.workspaceUri!.fsPath, env);
const templatePath = pathUtils.getYmlFilePath(workspaceUri!.fsPath, env);
const maybeProjectModel = await metadataUtil.parse(templatePath, env);
if (maybeProjectModel.isErr()) {
return undefined;
Expand All @@ -153,18 +148,6 @@ export async function getTeamsAppKeyName(env?: string): Promise<string | undefin
return undefined;
}

export async function triggerV3Migration(): Promise<void> {
const inputs = getSystemInputs();
inputs.stage = Stage.debug;
const result = await core.phantomMigrationV3(inputs);
if (result.isErr()) {
await vscode.debug.stopDebugging();
throw result.error;
}
// reload window to terminate debugging
await VS_CODE_UI.reload();
}

// Only work in ts/js project
export function isTestToolEnabledProject(workspacePath: string): boolean {
const testToolYmlPath = path.join(workspacePath, MetadataV3.testToolConfigFile);
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/debug/launch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { VS_CODE_UI } from "../extension";
import { VS_CODE_UI } from "../qm/vsc_ui";
import * as constants from "./constants";
import VsCodeLogInstance from "../commonlib/log";
import { Hub } from "@microsoft/teamsfx-core";
Expand Down
10 changes: 5 additions & 5 deletions packages/vscode-extension/src/debug/prerequisitesHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import { signedOut } from "../commonlib/common/constant";
import VsCodeLogInstance from "../commonlib/log";
import M365TokenInstance from "../commonlib/m365Login";
import { ExtensionErrors, ExtensionSource } from "../error";
import { VS_CODE_UI } from "../extension";
import * as globalVariables from "../globalVariables";
import { checkCopilotCallback, openAccountHelpHandler, tools } from "../handlers";
import { VS_CODE_UI } from "../qm/vsc_ui";
import { tools, workspaceUri } from "../globalVariables";
import { checkCopilotCallback, openAccountHelpHandler } from "../handlers";
import { ProgressHandler } from "../progressHandler";
import { ExtTelemetry } from "../telemetry/extTelemetry";
import { TelemetryEvent, TelemetryProperty } from "../telemetry/extTelemetryEvents";
Expand Down Expand Up @@ -621,7 +621,7 @@ async function checkNode(
try {
VsCodeLogInstance.outputChannel.appendLine(`${prefix} ${ProgressMessage[nodeDep]} ...`);
const nodeStatus = await depsManager.ensureDependency(nodeDep, true, {
projectPath: globalVariables.workspaceUri?.fsPath,
projectPath: workspaceUri?.fsPath,
});
return {
checker: nodeStatus.name,
Expand Down Expand Up @@ -790,7 +790,7 @@ async function checkFailure(
}

function getOrderedCheckersForGetStarted(): PrerequisiteOrderedChecker[] {
const workspacePath = globalVariables.workspaceUri?.fsPath;
const workspacePath = workspaceUri?.fsPath;
return [
{
info: { checker: workspacePath ? DepsType.ProjectNode : DepsType.LtsNode },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import { TaskDefaultValue, TunnelType } from "@microsoft/teamsfx-core";

import VsCodeLogInstance from "../../commonlib/log";
import { ExtensionErrors } from "../../error";
import { VS_CODE_UI } from "../../extension";
import { tools } from "../../handlers";
import { VS_CODE_UI } from "../../qm/vsc_ui";
import { tools } from "../../globalVariables";
import { ExtTelemetry } from "../../telemetry/extTelemetry";
import {
TelemetryEvent,
TelemetryProperty,
TelemetrySuccess,
} from "../../telemetry/extTelemetryEvents";
import { FeatureFlags, isFeatureFlagEnabled } from "../../utils/commonUtils";
import { FeatureFlags, isFeatureFlagEnabled } from "../../featureFlags";
import { devTunnelDisplayMessages } from "../constants";
import { maskValue } from "../localTelemetryReporter";
import { BaseTaskTerminal } from "./baseTaskTerminal";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryE
import { ExtensionErrors, ExtensionSource } from "../../error";
import { getDefaultString, localize } from "../../utils/localizeUtils";
import { openTerminalDisplayMessage, openTerminalMessage } from "../constants";
import { core, getSystemInputs } from "../../handlers";
import { getSystemInputs } from "../../utils/environmentUtils";
import { core } from "../../globalVariables";
import * as path from "path";

interface LaunchDesktopClientArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import * as util from "util";
import * as vscode from "vscode";
import VsCodeLogInstance from "../../commonlib/log";
import { ExtensionErrors, ExtensionSource } from "../../error";
import * as globalVariables from "../../globalVariables";
import { core, getSystemInputs } from "../../handlers";
import { core, workspaceUri } from "../../globalVariables";
import { getSystemInputs } from "../../utils/environmentUtils";
import { TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryEvents";
import { getDefaultString, localize } from "../../utils/localizeUtils";
import { getLocalDebugSession } from "../commonUtils";
Expand Down Expand Up @@ -89,7 +89,7 @@ export class LaunchTeamsClientTerminal extends BaseTaskTerminal {
private openUrl(url: string): Promise<Result<Void, FxError>> {
return new Promise<Result<Void, FxError>>((resolve) => {
const options: cp.SpawnOptions = {
cwd: globalVariables.workspaceUri?.fsPath ?? "",
cwd: workspaceUri?.fsPath ?? "",
shell: false,
detached: false,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/**
* @author Xiaofu Huang <[email protected]>
*/
import * as path from "path";
import * as vscode from "vscode";
import { err, FxError, ok, Result, Stage, Void } from "@microsoft/teamsfx-api";
import { TaskDefaultValue } from "@microsoft/teamsfx-core";
import { Correlator } from "@microsoft/teamsfx-core";
import * as globalVariables from "../../globalVariables";
import { getSystemInputs, runCommand } from "../../handlers";
import { Correlator, TaskDefaultValue } from "@microsoft/teamsfx-core";
import { workspaceUri } from "../../globalVariables";
import { runCommand } from "../../handlers";
import { TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryEvents";
import * as commonUtils from "../commonUtils";
import { getLocalDebugSession } from "../commonUtils";
import { localTelemetryReporter, maskValue } from "../localTelemetryReporter";
import { BaseTaskTerminal } from "./baseTaskTerminal";
import { getSystemInputs } from "../../utils/environmentUtils";

interface LifecycleArgs {
template?: string;
Expand Down Expand Up @@ -43,7 +42,7 @@ export class LifecycleTaskTerminal extends BaseTaskTerminal {
[TelemetryProperty.DebugLifecycle]: this.stage,
};

return Correlator.runWithId(commonUtils.getLocalDebugSession().id, () =>
return Correlator.runWithId(getLocalDebugSession().id, () =>
localTelemetryReporter.runWithTelemetryProperties(
TelemetryEvent.DebugLifecycleTask,
telemetryProperties,
Expand All @@ -66,7 +65,7 @@ export class LifecycleTaskTerminal extends BaseTaskTerminal {
inputs.isLocalDebug = true;
if (this.args.template) {
inputs.workflowFilePath = path.resolve(
globalVariables.workspaceUri?.fsPath ?? "",
workspaceUri?.fsPath ?? "",
BaseTaskTerminal.resolveTeamsFxVariables(this.args.template)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as vscode from "vscode";

import { FxError, ok, Result, Void } from "@microsoft/teamsfx-api";
import * as commonUtils from "../commonUtils";
import { BaseTaskTerminal } from "./baseTaskTerminal";
import { triggerV3Migration } from "../../utils/migrationUtils";

export class MigrateTaskTerminal extends BaseTaskTerminal {
constructor(taskDefinition: vscode.TaskDefinition) {
super(taskDefinition);
}

async do(): Promise<Result<Void, FxError>> {
await commonUtils.triggerV3Migration();
await triggerV3Migration();
return ok(Void);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Mutex, withTimeout } from "async-mutex";
import * as fs from "fs-extra";
import * as path from "path";
import { isFeatureFlagEnabled } from "@microsoft/teamsfx-core";
import * as globalVariables from "../../../globalVariables";
import { FeatureFlags } from "../../../utils/commonUtils";
import { context, workspaceUri } from "../../../globalVariables";
import { FeatureFlags } from "../../../featureFlags";

interface IDevTunnelState {
tunnelId?: string;
Expand Down Expand Up @@ -91,25 +91,23 @@ interface IStateService {
class VSCodeStateService implements IStateService {
get<T>(key: string): Promise<T | undefined> {
return new Promise((resolve) => {
resolve(globalVariables.context.workspaceState.get<T>(key));
resolve(context.workspaceState.get<T>(key));
});
}

async update(key: string, value: any): Promise<void> {
await globalVariables.context.workspaceState.update(key, value);
await context.workspaceState.update(key, value);
}
}

class FileStateService implements IStateService {
private readonly stateFileName = "devtunnel.state.json";
async get<T>(key: string): Promise<T | undefined> {
try {
if (!globalVariables.workspaceUri?.fsPath) {
if (!workspaceUri?.fsPath) {
return undefined;
}
const data = await fs.readJson(
path.resolve(globalVariables.workspaceUri.fsPath, this.stateFileName)
);
const data = await fs.readJson(path.resolve(workspaceUri.fsPath, this.stateFileName));

return data?.[key] as T;
} catch {
Expand All @@ -119,10 +117,10 @@ class FileStateService implements IStateService {

async update(key: string, value: any): Promise<void> {
try {
if (!globalVariables.workspaceUri?.fsPath) {
if (!workspaceUri?.fsPath) {
return;
}
const stateFilePath = path.resolve(globalVariables.workspaceUri.fsPath, this.stateFileName);
const stateFilePath = path.resolve(workspaceUri.fsPath, this.stateFileName);
let data: { [key: string]: any } = {};
try {
data = await fs.readJson(stateFilePath);
Expand Down
15 changes: 9 additions & 6 deletions packages/vscode-extension/src/debug/teamsfxDebugProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import {
import VsCodeLogInstance from "../commonlib/log";
import M365TokenInstance from "../commonlib/m365Login";
import { ExtensionSource } from "../error";
import { core, getSystemInputs, showError } from "../handlers";
import { showError } from "../handlers";
import { core } from "../globalVariables";
import { TelemetryEvent, TelemetryProperty } from "../telemetry/extTelemetryEvents";
import * as commonUtils from "./commonUtils";
import { getLocalDebugSessionId, endLocalDebugSession } from "./commonUtils";
import { accountHintPlaceholder, Host, sideloadingDisplayMessages } from "./constants";
import { localTelemetryReporter, sendDebugAllEvent } from "./localTelemetryReporter";
import { terminateAllRunningTeamsfxTasks } from "./teamsfxTaskHandler";
import { triggerV3Migration } from "../utils/migrationUtils";
import { getSystemInputs } from "../utils/environmentUtils";

export interface TeamsfxDebugConfiguration extends vscode.DebugConfiguration {
teamsfxIsRemote?: boolean;
Expand All @@ -40,7 +43,7 @@ export class TeamsfxDebugProvider implements vscode.DebugConfigurationProvider {
token?: vscode.CancellationToken
): Promise<vscode.DebugConfiguration | undefined> {
return await Correlator.runWithId(
commonUtils.getLocalDebugSessionId(),
getLocalDebugSessionId(),
this._resolveDebugConfiguration,
folder,
debugConfiguration,
Expand Down Expand Up @@ -69,7 +72,7 @@ export class TeamsfxDebugProvider implements vscode.DebugConfigurationProvider {

// migrate to v3
if (!isValidProjectV3(folder.uri.fsPath)) {
await commonUtils.triggerV3Migration();
await triggerV3Migration();
return debugConfiguration;
}

Expand Down Expand Up @@ -121,7 +124,7 @@ export class TeamsfxDebugProvider implements vscode.DebugConfigurationProvider {

// Attach correlation-id to DebugConfiguration so concurrent debug sessions are correctly handled in this stage.
// For backend and bot debug sessions, debugConfiguration.url is undefined so we need to set correlation id early.
debugConfiguration.teamsfxCorrelationId = commonUtils.getLocalDebugSessionId();
debugConfiguration.teamsfxCorrelationId = getLocalDebugSessionId();

const result = await localTelemetryReporter.runWithTelemetryExceptionProperties(
TelemetryEvent.DebugProviderResolveDebugConfiguration,
Expand Down Expand Up @@ -197,7 +200,7 @@ export class TeamsfxDebugProvider implements vscode.DebugConfigurationProvider {
if (telemetryIsRemote === false) {
await sendDebugAllEvent(error);
}
commonUtils.endLocalDebugSession();
endLocalDebugSession();
}
return debugConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

import VsCodeLogInstance from "../commonlib/log";
import { ExtensionErrors, ExtensionSource } from "../error";
import { VS_CODE_UI } from "../extension";
import { VS_CODE_UI } from "../qm/vsc_ui";
import * as globalVariables from "../globalVariables";
import {
TelemetryEvent,
Expand Down
Loading
Loading