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

Add support for nuget restore commands #6603

Merged
merged 6 commits into from
Nov 3, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
- Debug from .csproj and .sln [#5876](https://github.com/dotnet/vscode-csharp/issues/5876)

## Latest
* Update Roslyn to 4.9.0-1.23530.4 (PR: [#6603](https://github.com/dotnet/vscode-csharp/pull/6603))
* Enable NuGet restore commands `dotnet.restore.all` and `dotnet.restore.project` (PR: [#70588](https://github.com/dotnet/roslyn/pull/70588))
* Fix issue where server did not reload projects after NuGet restore (PR: [#70602](https://github.com/dotnet/roslyn/pull/70602))
* Update debugger to 2.9.0 (PR: [#6623](https://github.com/dotnet/vscode-csharp/pull/6623))
* Flush `Console.Write` buffer more often (Fixes: [#6598](https://github.com/dotnet/vscode-csharp/issues/6598))
* Fix logpoint freezing on start (Fixes: [#6585](https://github.com/dotnet/vscode-csharp/issues/6585))
Expand Down
3 changes: 3 additions & 0 deletions l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
"Choose": "Choose",
"Choose and set default": "Choose and set default",
"Do not load any": "Do not load any",
"Restore already in progress": "Restore already in progress",
"Restore": "Restore",
"Sending request": "Sending request",
"C# configuration has changed. Would you like to reload the window to apply your changes?": "C# configuration has changed. Would you like to reload the window to apply your changes?",
"Pick a fix all scope": "Pick a fix all scope",
"Fix All Code Action": "Fix All Code Action",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
},
"defaults": {
"roslyn": "4.9.0-1.23526.14",
"roslyn": "4.9.0-1.23530.4",
"omniSharp": "1.39.10",
"razor": "7.0.0-preview.23528.1",
"razorOmnisharp": "7.0.0-preview.23363.1",
Expand Down Expand Up @@ -2038,13 +2038,13 @@
"command": "dotnet.restore.project",
"title": "%command.dotnet.restore.project%",
"category": ".NET",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activatedStandalone"
},
{
"command": "dotnet.restore.all",
"title": "%command.dotnet.restore.all%",
"category": ".NET",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activatedStandalone"
},
{
"command": "csharp.downloadDebugger",
Expand Down
108 changes: 108 additions & 0 deletions src/lsptoolshost/restore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { RoslynLanguageServer } from './roslynLanguageServer';
import { RestorableProjects, RestoreParams, RestorePartialResult, RestoreRequest } from './roslynProtocol';
import path = require('path');

let _restoreInProgress = false;

export function registerRestoreCommands(
context: vscode.ExtensionContext,
languageServer: RoslynLanguageServer,
restoreChannel: vscode.OutputChannel
) {
context.subscriptions.push(
vscode.commands.registerCommand('dotnet.restore.project', async (_request): Promise<void> => {
return chooseProjectAndRestore(languageServer, restoreChannel);
})
);
context.subscriptions.push(
vscode.commands.registerCommand('dotnet.restore.all', async (): Promise<void> => {
return restore(languageServer, restoreChannel);
})
);
}
async function chooseProjectAndRestore(
languageServer: RoslynLanguageServer,
restoreChannel: vscode.OutputChannel
): Promise<void> {
const projects = await languageServer.sendRequest0(
RestorableProjects.type,
new vscode.CancellationTokenSource().token
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not a better cancellation token to use for a command, or some UI that can be created for this? I don't think it's important to, as much as I am wondering out loud what the expected pattern is for things like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could put a progress bar on this and use cancellation there. But it didn't seem necessary in this case - the request barely does anything and immediately shows a quick pick when its done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah a progress bar does seem overkill. I wasn't sure if there was a simple pattern for commands with async work that's commonly used.

Copy link
Member Author

@dibarbet dibarbet Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know of a different one :(

);

const items = projects.map((p) => {
const projectName = path.basename(p);
const item: vscode.QuickPickItem = {
label: vscode.l10n.t(`Restore {0}`, projectName),
description: p,
};
return item;
});

const pickedItem = await vscode.window.showQuickPick(items);
if (!pickedItem) {
return;
}

await restore(languageServer, restoreChannel, pickedItem.description);
}

async function restore(
languageServer: RoslynLanguageServer,
restoreChannel: vscode.OutputChannel,
projectFile?: string
): Promise<void> {
if (_restoreInProgress) {
vscode.window.showErrorMessage(vscode.l10n.t('Restore already in progress'));
return;
}
_restoreInProgress = true;
restoreChannel.show(true);

const request: RestoreParams = { projectFilePath: projectFile };
await vscode.window
.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: vscode.l10n.t('Restore'),
cancellable: true,
},
async (progress, token) => {
const writeOutput = (output: RestorePartialResult) => {
if (output.message) {
restoreChannel.appendLine(output.message);
}

progress.report({ message: output.stage });
};

progress.report({ message: vscode.l10n.t('Sending request') });
const responsePromise = languageServer.sendRequestWithProgress(
RestoreRequest.type,
request,
async (p) => writeOutput(p),
token
);

await responsePromise.then(
(result) => result.forEach((r) => writeOutput(r)),
(err) => restoreChannel.appendLine(err)
);
}
)
.then(
() => {
_restoreInProgress = false;
},
() => {
_restoreInProgress = false;
}
);

return;
}
4 changes: 4 additions & 0 deletions src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { registerCodeActionFixAllCommands } from './fixAllCodeAction';
import { commonOptions, languageServerOptions, omnisharpOptions } from '../shared/options';
import { NamedPipeInformation } from './roslynProtocol';
import { IDisposable } from '../disposable';
import { registerRestoreCommands } from './restore';

let _channel: vscode.OutputChannel;
let _traceChannel: vscode.OutputChannel;
Expand Down Expand Up @@ -835,6 +836,7 @@ export async function activateRoslynLanguageServer(
optionObservable: Observable<void>,
outputChannel: vscode.OutputChannel,
dotnetTestChannel: vscode.OutputChannel,
dotnetChannel: vscode.OutputChannel,
reporter: TelemetryReporter,
languageServerEvents: RoslynLanguageServerEvents
): Promise<RoslynLanguageServer> {
Expand Down Expand Up @@ -872,6 +874,8 @@ export async function activateRoslynLanguageServer(
// Register any needed debugger components that need to communicate with the language server.
registerDebugger(context, languageServer, languageServerEvents, platformInfo, _channel);

registerRestoreCommands(context, languageServer, dotnetChannel);

registerOnAutoInsert(languageServer);

context.subscriptions.push(registerLanguageServerOptionChanges(optionObservable));
Expand Down
31 changes: 31 additions & 0 deletions src/lsptoolshost/roslynProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ export interface NamedPipeInformation {
pipeName: string;
}

export interface RestoreParams extends lsp.WorkDoneProgressParams, lsp.PartialResultParams {
/**
* An optional file path to restore.
* If none is specified, the solution (or all loaded projects) are restored.
*/
projectFilePath?: string;
}

export interface RestorePartialResult {
stage: string;
message: string;
}

export namespace WorkspaceDebugConfigurationRequest {
export const method = 'workspace/debugConfiguration';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
Expand Down Expand Up @@ -229,3 +242,21 @@ export namespace CodeActionFixAllResolveRequest {
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
export const type = new lsp.RequestType<RoslynFixAllCodeAction, RoslynFixAllCodeAction, void>(method);
}

export namespace RestoreRequest {
export const method = 'workspace/_roslyn_restore';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
export const type = new lsp.ProtocolRequestType<
RestoreParams,
RestorePartialResult[],
RestorePartialResult,
void,
void
>(method);
}

export namespace RestorableProjects {
export const method = 'workspace/_roslyn_restorableProjects';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
export const type = new lsp.RequestType0<string[], void>(method);
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function activate(

const csharpChannel = vscode.window.createOutputChannel('C#');
const dotnetTestChannel = vscode.window.createOutputChannel('.NET Test Log');
const dotnetChannel = vscode.window.createOutputChannel('.NET NuGet Restore');
const csharpchannelObserver = new CsharpChannelObserver(csharpChannel);
const csharpLogObserver = new CsharpLoggerObserver(csharpChannel);
eventStream.subscribe(csharpchannelObserver.post);
Expand Down Expand Up @@ -175,11 +176,11 @@ export async function activate(
optionStream,
csharpChannel,
dotnetTestChannel,
dotnetChannel,
reporter,
roslynLanguageServerEvents
);
} else {
const dotnetChannel = vscode.window.createOutputChannel('.NET');
const dotnetChannelObserver = new DotNetChannelObserver(dotnetChannel);
const dotnetLoggerObserver = new DotnetLoggerObserver(dotnetChannel);
eventStream.subscribe(dotnetChannelObserver.post);
Expand Down
Loading