@@ -4,6 +4,7 @@ import * as path from "path";
44import * as vscode from "vscode" ;
55import { Env } from "./client" ;
66import { log } from "./util" ;
7+ import { expectNotUndefined , unwrapUndefinable } from "./undefinable" ;
78
89export type RunnableEnvCfg =
910 | undefined
@@ -338,7 +339,7 @@ export function substituteVariablesInEnv(env: Env): Env {
338339 const depRe = new RegExp ( / \$ { (?< depName > .+ ?) } / g) ;
339340 let match = undefined ;
340341 while ( ( match = depRe . exec ( value ) ) ) {
341- const depName = match . groups ! . depName ;
342+ const depName = unwrapUndefinable ( match . groups ? .depName ) ;
342343 deps . add ( depName ) ;
343344 // `depName` at this point can have a form of `expression` or
344345 // `prefix:expression`
@@ -356,7 +357,7 @@ export function substituteVariablesInEnv(env: Env): Env {
356357 if ( match ) {
357358 const { prefix, body } = match . groups ! ;
358359 if ( prefix === "env" ) {
359- const envName = body ;
360+ const envName = unwrapUndefinable ( body ) ;
360361 envWithDeps [ dep ] = {
361362 value : process . env [ envName ] ?? "" ,
362363 deps : [ ] ,
@@ -384,13 +385,12 @@ export function substituteVariablesInEnv(env: Env): Env {
384385 do {
385386 leftToResolveSize = toResolve . size ;
386387 for ( const key of toResolve ) {
387- if ( envWithDeps [ key ] . deps . every ( ( dep ) => resolved . has ( dep ) ) ) {
388- envWithDeps [ key ] . value = envWithDeps [ key ] . value . replace (
389- / \$ { (?< depName > .+ ?) } / g,
390- ( _wholeMatch , depName ) => {
391- return envWithDeps [ depName ] . value ;
392- }
393- ) ;
388+ const item = unwrapUndefinable ( envWithDeps [ key ] ) ;
389+ if ( item . deps . every ( ( dep ) => resolved . has ( dep ) ) ) {
390+ item . value = item . value . replace ( / \$ { (?< depName > .+ ?) } / g, ( _wholeMatch , depName ) => {
391+ const item = unwrapUndefinable ( envWithDeps [ depName ] ) ;
392+ return item . value ;
393+ } ) ;
394394 resolved . add ( key ) ;
395395 toResolve . delete ( key ) ;
396396 }
@@ -399,7 +399,8 @@ export function substituteVariablesInEnv(env: Env): Env {
399399
400400 const resolvedEnv : Env = { } ;
401401 for ( const key of Object . keys ( env ) ) {
402- resolvedEnv [ key ] = envWithDeps [ `env:${ key } ` ] . value ;
402+ const item = unwrapUndefinable ( envWithDeps [ `env:${ key } ` ] ) ;
403+ resolvedEnv [ key ] = item . value ;
403404 }
404405 return resolvedEnv ;
405406}
@@ -418,20 +419,19 @@ function substituteVSCodeVariableInString(val: string): string {
418419function computeVscodeVar ( varName : string ) : string | null {
419420 const workspaceFolder = ( ) => {
420421 const folders = vscode . workspace . workspaceFolders ?? [ ] ;
421- if ( folders . length === 1 ) {
422- // TODO: support for remote workspaces?
423- return folders [ 0 ] . uri . fsPath ;
424- } else if ( folders . length > 1 ) {
425- // could use currently opened document to detect the correct
426- // workspace. However, that would be determined by the document
427- // user has opened on Editor startup. Could lead to
428- // unpredictable workspace selection in practice.
429- // It's better to pick the first one
430- return folders [ 0 ] . uri . fsPath ;
431- } else {
432- // no workspace opened
433- return "" ;
434- }
422+ const folder = folders [ 0 ] ;
423+ // TODO: support for remote workspaces?
424+ const fsPath : string =
425+ folder === undefined
426+ ? // no workspace opened
427+ ""
428+ : // could use currently opened document to detect the correct
429+ // workspace. However, that would be determined by the document
430+ // user has opened on Editor startup. Could lead to
431+ // unpredictable workspace selection in practice.
432+ // It's better to pick the first one
433+ folder . uri . fsPath ;
434+ return fsPath ;
435435 } ;
436436 // https://code.visualstudio.com/docs/editor/variables-reference
437437 const supportedVariables : { [ k : string ] : ( ) => string } = {
@@ -454,7 +454,11 @@ function computeVscodeVar(varName: string): string | null {
454454 } ;
455455
456456 if ( varName in supportedVariables ) {
457- return supportedVariables [ varName ] ( ) ;
457+ const fn = expectNotUndefined (
458+ supportedVariables [ varName ] ,
459+ `${ varName } should not be undefined here`
460+ ) ;
461+ return fn ( ) ;
458462 } else {
459463 // return "${" + varName + "}";
460464 return null ;
0 commit comments