Skip to content

Commit 3aa6eb3

Browse files
authored
Merge pull request #5221 from 50Wliu/users/winstonliu/use-msbuild-when-generating-launch-config
Use MSBuild info when computing program to launch
2 parents 764e5b5 + d938486 commit 3aa6eb3

File tree

6 files changed

+97
-89
lines changed

6 files changed

+97
-89
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"tslint": "5.12.1",
120120
"tslint-microsoft-contrib": "6.0.0",
121121
"tslint-no-unused-expression-chai": "0.1.4",
122-
"typescript": "4.2.4",
122+
"typescript": "4.6.3",
123123
"unzipper": "0.10.11",
124124
"vsce": "1.100.2",
125125
"webpack": "5.34.0",
@@ -4084,4 +4084,4 @@
40844084
}
40854085
]
40864086
}
4087-
}
4087+
}

src/assets.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as fs from 'fs-extra';
77
import * as jsonc from 'jsonc-parser';
8-
import { FormattingOptions, ModificationOptions } from 'jsonc-parser';
8+
import { FormattingOptions, ModificationOptions } from 'jsonc-parser';
99
import * as os from 'os';
1010
import * as path from 'path';
1111
import * as protocol from './omnisharp/protocol';
@@ -156,27 +156,29 @@ export class AssetGenerator {
156156
return ProgramLaunchType.Console;
157157
}
158158

159-
private computeProgramPath() {
159+
private computeProgramPath(): string {
160160
if (!this.startupProject) {
161161
throw new Error("Startup project not set");
162162
}
163163

164-
const startupProjectDir = path.dirname(this.startupProject.Path);
165-
const relativeProjectDir = path.join('${workspaceFolder}', path.relative(this.workspaceFolder.uri.fsPath, startupProjectDir));
166-
const configurationName = 'Debug';
167-
const targetFramework = protocol.findNetCoreTargetFramework(this.startupProject);
168-
const result = path.join(relativeProjectDir, `bin/${configurationName}/${targetFramework.ShortName}/${this.startupProject.AssemblyName}.dll`);
169-
return result;
164+
const relativeTargetPath = path.relative(this.workspaceFolder.uri.fsPath, this.startupProject.TargetPath);
165+
if (relativeTargetPath === this.startupProject.TargetPath) {
166+
// This can happen if, for example, the workspace folder and the target path
167+
// are on completely different drives.
168+
return this.startupProject.TargetPath;
169+
}
170+
return path.join('${workspaceFolder}', relativeTargetPath);
170171
}
171172

172173
private computeWorkingDirectory(): string {
173174
if (!this.startupProject) {
174175
throw new Error("Startup project not set");
175176
}
176177

177-
const startupProjectDir = path.dirname(this.startupProject.Path);
178-
179-
return path.join('${workspaceFolder}', path.relative(this.workspaceFolder.uri.fsPath, startupProjectDir));
178+
// Startup project will always be a child of the workspace folder,
179+
// so the special check above isn't necessary.
180+
const relativeProjectPath = path.relative(this.workspaceFolder.uri.fsPath, this.startupProject.Path);
181+
return path.join('${workspaceFolder}', path.dirname(relativeProjectPath));
180182
}
181183

182184
public createLaunchJsonConfigurationsArray(programLaunchType: ProgramLaunchType): vscode.DebugConfiguration[] {
@@ -583,8 +585,8 @@ function getBuildAssetsNotificationSetting() {
583585
return csharpConfig.get<boolean>('supressBuildAssetsNotification');
584586
}
585587

586-
export function getFormattingOptions(): FormattingOptions {
587-
const editorConfig = vscode.workspace.getConfiguration('editor');
588+
export function getFormattingOptions(): FormattingOptions {
589+
const editorConfig = vscode.workspace.getConfiguration('editor');
588590

589591
const tabSize = editorConfig.get<number>('tabSize') ?? 4;
590592
const insertSpaces = editorConfig.get<boolean>('insertSpaces') ?? true;
@@ -609,7 +611,7 @@ export async function addTasksJsonIfNecessary(generator: AssetGenerator, operati
609611
}
610612

611613
const formattingOptions = getFormattingOptions();
612-
614+
613615
const tasksJson = generator.createTasksConfiguration();
614616

615617
let text: string;
@@ -622,7 +624,7 @@ export async function addTasksJsonIfNecessary(generator: AssetGenerator, operati
622624
else {
623625
// when tasks.json exists just update the tasks node
624626
const ourConfigs = tasksJson.tasks;
625-
const content = fs.readFileSync(generator.tasksJsonPath).toString();
627+
const content = fs.readFileSync(generator.tasksJsonPath).toString();
626628
const updatedJson = updateJsonWithComments(content, ourConfigs, 'tasks', 'label', formattingOptions);
627629
text = updatedJson;
628630
}
@@ -659,7 +661,7 @@ async function addLaunchJsonIfNecessary(generator: AssetGenerator, operations: A
659661
}`;
660662

661663
text = jsonc.applyEdits(launchJsonText, jsonc.format(launchJsonText, null, formattingOptions));
662-
}
664+
}
663665
else {
664666
// when launch.json exists replace or append our configurations
665667
const ourConfigs = jsonc.parse(launchJsonConfigurations);
@@ -751,18 +753,18 @@ async function getExistingAssets(generator: AssetGenerator) {
751753
assets = assets.concat(tasks);
752754
}
753755

754-
if(fs.pathExistsSync(generator.launchJsonPath)) {
756+
if(fs.pathExistsSync(generator.launchJsonPath)) {
755757
const content = fs.readFileSync(generator.launchJsonPath).toString();
756758
let configurationNames = [
757-
".NET Core Launch (console)",
759+
".NET Core Launch (console)",
758760
".NET Core Launch (web)",
759761
".NET Core Attach",
760-
"Launch and Debug Standalone Blazor WebAssembly App",
762+
"Launch and Debug Standalone Blazor WebAssembly App",
761763
];
762764
const configurations = jsonc.parse(content)?.configurations?.
763765
map((t: { name: string; }) => t.name).
764766
filter((n: string) => configurationNames.includes(n));
765-
767+
766768
assets = assets.concat(configurations);
767769
}
768770

@@ -833,22 +835,22 @@ export function replaceCommentPropertiesWithComments(text: string) {
833835
// replacing dummy properties OS-COMMENT with the normal comment syntax
834836
let regex = /["']OS-COMMENT\d*["']\s*\:\s*["'](.*)["']\s*?,/gi;
835837
let withComments = text.replace(regex, '// $1');
836-
838+
837839
return withComments;
838840
}
839841

840-
export function updateJsonWithComments(text: string, replacements: any[], nodeName: string, keyName: string, formattingOptions: FormattingOptions) : string {
842+
export function updateJsonWithComments(text: string, replacements: any[], nodeName: string, keyName: string, formattingOptions: FormattingOptions) : string {
841843
let modificationOptions : ModificationOptions = {
842844
formattingOptions
843845
};
844-
846+
845847
// parse using jsonc because there are comments
846848
// only use this to determine what to change
847849
// we will modify it as text to keep existing comments
848850
let parsed = jsonc.parse(text);
849851
let items = parsed[nodeName];
850852
let itemKeys : string[] = items.map((i: { [x: string]: string; }) => i[keyName]);
851-
853+
852854
let modified = text;
853855
// count how many items we inserted to ensure we are putting items at the end
854856
// in the same order as they are in the replacements array
@@ -866,6 +868,6 @@ export function updateJsonWithComments(text: string, replacements: any[], nodeNa
866868
// changes one by one
867869
modified = updated;
868870
});
869-
871+
870872
return replaceCommentPropertiesWithComments(modified);
871873
}

src/omnisharp/protocol.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,15 @@ export interface CakeContext {
320320

321321
export interface MSBuildProject {
322322
ProjectGuid: string;
323+
/** Absolute path to the csproj file. */
323324
Path: string;
324325
AssemblyName: string;
326+
/** Absolute path to the output assembly DLL. */
325327
TargetPath: string;
326328
TargetFramework: string;
327329
SourceFiles: string[];
328330
TargetFrameworks: TargetFramework[];
331+
/** Absolute path to the output directory. */
329332
OutputPath: string;
330333
IsExe: boolean;
331334
IsUnityProject: boolean;

0 commit comments

Comments
 (0)