Skip to content

Commit 819a271

Browse files
Fix dotnet restore for project.json
In fixing dotnet restore for .csproj, I unintentionally broke it for project.json. The problem here is that OmniSharp returns different information for `Path` with regard to project.json or .csproj. For project.json, it's the directory that the project.json lives in, and for .csproj, it's the actually file path. This change addresses that by adding a `FilePath` property to our `ProjectDescriptor` which represents the real file path and renamed `Path` to `Directory` to be a bit clearer.
1 parent 1d4a0bd commit 819a271

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/features/commands.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ interface Command {
8585

8686
function projectsToCommands(projects: protocol.ProjectDescriptor[]): Promise<Command>[] {
8787
return projects.map(project => {
88-
let projectDirectory = project.Path;
88+
let projectDirectory = project.Directory;
8989

9090
return new Promise<Command>((resolve, reject) => {
9191
fs.lstat(projectDirectory, (err, stats) => {
@@ -98,7 +98,7 @@ function projectsToCommands(projects: protocol.ProjectDescriptor[]): Promise<Com
9898
}
9999

100100
resolve({
101-
label: `dotnet restore - (${project.Name || path.basename(project.Path)})`,
101+
label: `dotnet restore - (${project.Name || path.basename(project.Directory)})`,
102102
description: projectDirectory,
103103
execute() {
104104
return dotnetRestore(projectDirectory);
@@ -117,13 +117,13 @@ export function dotnetRestoreAllProjects(server: OmniSharpServer) {
117117

118118
return serverUtils.requestWorkspaceInformation(server).then(info => {
119119

120-
let projectDescriptors = protocol.getDotNetCoreProjectDescriptors(info);
120+
let descriptors = protocol.getDotNetCoreProjectDescriptors(info);
121121

122-
if (projectDescriptors.length === 0) {
122+
if (descriptors.length === 0) {
123123
return Promise.reject("No .NET Core projects found");
124124
}
125125

126-
let commandPromises = projectsToCommands(projectDescriptors);
126+
let commandPromises = projectsToCommands(descriptors);
127127

128128
return Promise.all(commandPromises).then(commands => {
129129
return vscode.window.showQuickPick(commands);
@@ -135,40 +135,38 @@ export function dotnetRestoreAllProjects(server: OmniSharpServer) {
135135
});
136136
}
137137

138-
export function dotnetRestoreForProject(server: OmniSharpServer, fileName: string) {
138+
export function dotnetRestoreForProject(server: OmniSharpServer, filePath: string) {
139139

140140
if (!server.isRunning()) {
141141
return Promise.reject('OmniSharp server is not running.');
142142
}
143143

144144
return serverUtils.requestWorkspaceInformation(server).then(info => {
145145

146-
let projectDescriptors = protocol.getDotNetCoreProjectDescriptors(info);
146+
let descriptors = protocol.getDotNetCoreProjectDescriptors(info);
147147

148-
if (projectDescriptors.length === 0) {
148+
if (descriptors.length === 0) {
149149
return Promise.reject("No .NET Core projects found");
150150
}
151151

152-
let directory = path.dirname(fileName);
153-
154-
for (let projectDescriptor of projectDescriptors) {
155-
if (projectDescriptor.Path === fileName) {
156-
return dotnetRestore(directory, fileName);
152+
for (let descriptor of descriptors) {
153+
if (descriptor.FilePath === filePath) {
154+
return dotnetRestore(descriptor.Directory, filePath);
157155
}
158156
}
159157
});
160158
}
161159

162-
function dotnetRestore(cwd: string, fileName?: string) {
160+
function dotnetRestore(cwd: string, filePath?: string) {
163161
return new Promise<void>((resolve, reject) => {
164162
channel.clear();
165163
channel.show();
166164

167165
let cmd = 'dotnet';
168166
let args = ['restore'];
169167

170-
if (fileName) {
171-
args.push(fileName);
168+
if (filePath) {
169+
args.push(filePath);
172170
}
173171

174172
let dotnet = cp.spawn(cmd, args, { cwd: cwd, env: process.env });

src/omnisharp/protocol.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,23 +523,32 @@ export function findNetStandardTargetFramework(project: MSBuildProject): TargetF
523523

524524
export interface ProjectDescriptor {
525525
Name: string;
526-
Path: string;
526+
Directory: string;
527+
FilePath: string;
527528
}
528529

529530
export function getDotNetCoreProjectDescriptors(info: WorkspaceInformationResponse): ProjectDescriptor[] {
530531
let result = [];
531532

532533
if (info.DotNet && info.DotNet.Projects.length > 0) {
533534
for (let project of info.DotNet.Projects) {
534-
result.push({ Name: project.Name, Path: project.Path });
535+
result.push({
536+
Name: project.Name,
537+
Directory: project.Path,
538+
FilePath: path.join(project.Path, 'project.json')
539+
});
535540
}
536541
}
537542

538543
if (info.MsBuild && info.MsBuild.Projects.length > 0) {
539544
for (let project of info.MsBuild.Projects) {
540545
if (findNetCoreAppTargetFramework(project) !== undefined ||
541546
findNetStandardTargetFramework(project) !== undefined) {
542-
result.push({ Name: path.basename(project.Path), Path: project.Path });
547+
result.push({
548+
Name: path.basename(project.Path),
549+
Directory: path.dirname(project.Path),
550+
FilePath: project.Path
551+
});
543552
}
544553
}
545554
}

0 commit comments

Comments
 (0)