-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathregisterFunctionHostDebugView.ts
More file actions
169 lines (146 loc) · 6.99 KB
/
registerFunctionHostDebugView.ts
File metadata and controls
169 lines (146 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { registerCommand, type IActionContext } from '@microsoft/vscode-azext-utils';
import * as vscode from 'vscode';
import { getRecentLogsPlainText } from '../funcCoreTools/funcHostErrorUtils';
import { clearStoppedSessions, onRunningFuncTasksChanged, resolveAndNormalizeCwd, runningFuncTaskMap, type IRunningFuncTask } from '../funcCoreTools/funcHostTask';
import { localize } from '../localize';
import { stripAnsiControlCharacters } from '../utils/ansiUtils';
import { FuncHostDebugViewProvider, HostErrorNode, HostTaskNode, StoppedHostNode } from './FunctionHostDebugView';
import { getScopeLabel } from './nodes/funcHostDebugUtils';
const viewId = 'azureFunctions.funcHostDebugView';
function isHostTaskNode(node: unknown): node is HostTaskNode {
return node instanceof HostTaskNode;
}
function isStoppedHostNode(node: unknown): node is StoppedHostNode {
return node instanceof StoppedHostNode;
}
function isHostErrorNode(node: unknown): node is HostErrorNode {
return node instanceof HostErrorNode;
}
function formatErrorLogs(errorLogs: string[]): string {
return errorLogs.map(m => stripAnsiControlCharacters(m).trim()).filter(Boolean).join('\n\n')
|| localize('funcHostDebug.noErrors', 'No errors detected.');
}
function getNodeContext(args: unknown): { scopeLabel: string; portNumber: string; cwd?: string; errorOutput: string } | undefined {
if (isHostErrorNode(args)) {
return {
scopeLabel: getScopeLabel(args.workspaceFolder),
portNumber: args.portNumber,
cwd: args.cwd,
errorOutput: stripAnsiControlCharacters(args.message).trim() || args.message,
};
} else if (isHostTaskNode(args)) {
const task = runningFuncTaskMap.get(args.workspaceFolder, resolveAndNormalizeCwd(args.workspaceFolder, args.cwd));
return {
scopeLabel: getScopeLabel(args.workspaceFolder),
portNumber: args.portNumber,
cwd: args.cwd,
errorOutput: formatErrorLogs(task?.errorLogs ?? []),
};
} else if (isStoppedHostNode(args)) {
const stopped = args.stoppedTask;
return {
scopeLabel: getScopeLabel(stopped.workspaceFolder),
portNumber: stopped.portNumber,
cwd: stopped.cwd,
errorOutput: formatErrorLogs(stopped.errorLogs),
};
}
return undefined;
}
async function tryOpenDebugViewOnFirstFuncHostError(): Promise<void> {
const newlyErroredTasks: IRunningFuncTask[] = [];
for (const folder of vscode.workspace.workspaceFolders ?? []) {
for (const t of runningFuncTaskMap.getAll(folder)) {
if (!t) {
continue;
}
if ((t.errorLogs?.length ?? 0) > 0 && !t.hasReportedLiveErrors) {
newlyErroredTasks.push(t);
}
}
}
if (newlyErroredTasks.length === 0) {
return;
}
// Show Run & Debug view (Debug container) so the view (contributed under it) is visible.
try {
// Focus the specific tree view to expand it within the Debug sidebar.
await vscode.commands.executeCommand(`${viewId}.focus`);
// Mark as revealed only after the view open attempt, to avoid repeated calls.
for (const t of newlyErroredTasks) {
t.hasReportedLiveErrors = true;
}
} catch {
// If this fails, leave flags untouched so we can try again later.
}
}
export function registerFunctionHostDebugView(context: vscode.ExtensionContext): void {
const provider = new FuncHostDebugViewProvider();
context.subscriptions.push(
vscode.window.registerTreeDataProvider(viewId, provider),
onRunningFuncTasksChanged(() => {
provider.refresh();
void tryOpenDebugViewOnFirstFuncHostError();
}),
);
registerCommand('azureFunctions.funcHostDebug.clearStoppedSessions', async (actionContext: IActionContext) => {
actionContext.telemetry.properties.source = 'funcHostDebugView';
clearStoppedSessions();
});
registerCommand('azureFunctions.funcHostDebug.copyRecentLogs', async (actionContext: IActionContext, args: unknown) => {
actionContext.telemetry.properties.source = 'funcHostDebugView';
if (isHostTaskNode(args)) {
const task = runningFuncTaskMap.get(args.workspaceFolder, resolveAndNormalizeCwd(args.workspaceFolder, args.cwd));
const text = getRecentLogsPlainText(task);
await vscode.env.clipboard.writeText(text);
} else if (isStoppedHostNode(args)) {
const text = getRecentLogsPlainText(args.stoppedTask);
await vscode.env.clipboard.writeText(text);
}
});
registerCommand('azureFunctions.funcHostDebug.showRecentLogs', async (actionContext: IActionContext, args: unknown) => {
actionContext.telemetry.properties.source = 'funcHostDebugView';
let text: string | undefined;
if (isHostTaskNode(args)) {
const task = runningFuncTaskMap.get(args.workspaceFolder, resolveAndNormalizeCwd(args.workspaceFolder, args.cwd));
text = getRecentLogsPlainText(task);
} else if (isStoppedHostNode(args)) {
text = getRecentLogsPlainText(args.stoppedTask);
} else {
return;
}
const doc = await vscode.workspace.openTextDocument({
content: text || localize('funcHostDebug.noLogs', 'No logs captured yet.'),
language: 'log',
});
await vscode.window.showTextDocument(doc, { preview: true });
});
registerCommand('azureFunctions.funcHostDebug.askCopilot', async (actionContext: IActionContext, args: unknown) => {
actionContext.telemetry.properties.source = 'funcHostDebugView';
const ctx = getNodeContext(args);
if (!ctx) {
return;
}
const { scopeLabel, portNumber, cwd, errorOutput } = ctx;
const prompt = [
'I am debugging an Azure Functions project locally in VS Code.',
`Function Host Port: ${portNumber}`,
`Workspace: ${scopeLabel}`,
cwd ? `CWD: ${cwd}` : undefined,
'',
'The Functions host produced an error. Diagnose the likely cause and suggest concrete next steps to fix it.',
'',
'Error output:',
errorOutput,
].filter((l): l is string => Boolean(l)).join('\n');
await vscode.commands.executeCommand('workbench.action.chat.open', { mode: 'agent', query: prompt });
});
registerCommand('azureFunctions.funcHostDebug.refresh', async (actionContext: IActionContext) => {
actionContext.telemetry.properties.source = 'funcHostDebugView';
provider.refresh();
});
}