55
66import * as fs from 'fs-extra' ;
77import * as jsonc from 'jsonc-parser' ;
8- import { FormattingOptions , ModificationOptions } from 'jsonc-parser' ;
8+ import { FormattingOptions , ModificationOptions } from 'jsonc-parser' ;
99import * as os from 'os' ;
1010import * as path from 'path' ;
1111import * 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 = / [ " ' ] O S - C O M M E N T \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}
0 commit comments