2023-01-24 06:43:56 -06:00
|
|
|
import * as Is from "vscode-languageclient/lib/common/utils/is";
|
2022-10-16 10:11:24 -05:00
|
|
|
import * as os from "os";
|
2023-01-24 06:43:56 -06:00
|
|
|
import * as path from "path";
|
2018-10-07 15:59:02 -05:00
|
|
|
import * as vscode from "vscode";
|
2023-07-10 16:10:00 -05:00
|
|
|
import type { Env } from "./client";
|
2020-03-17 06:44:31 -05:00
|
|
|
import { log } from "./util";
|
2023-06-27 14:03:53 -05:00
|
|
|
import { expectNotUndefined, unwrapUndefinable } from "./undefinable";
|
2019-01-18 04:59:08 -06:00
|
|
|
|
2020-07-02 14:08:33 -05:00
|
|
|
export type RunnableEnvCfg =
|
|
|
|
| undefined
|
|
|
|
| Record<string, string>
|
|
|
|
| { mask?: string; env: Record<string, string> }[];
|
2020-07-02 11:47:40 -05:00
|
|
|
|
2018-10-07 15:59:02 -05:00
|
|
|
export class Config {
|
2022-05-13 06:21:52 -05:00
|
|
|
readonly extensionId = "rust-lang.rust-analyzer";
|
2022-10-20 14:12:58 -05:00
|
|
|
configureLang: vscode.Disposable | undefined;
|
2020-03-09 12:57:13 -05:00
|
|
|
|
2020-05-24 19:47:33 -05:00
|
|
|
readonly rootSection = "rust-analyzer";
|
2020-03-09 12:57:13 -05:00
|
|
|
private readonly requiresReloadOpts = [
|
2020-04-02 05:47:58 -05:00
|
|
|
"cargo",
|
2020-04-12 11:05:33 -05:00
|
|
|
"procMacro",
|
2022-10-17 07:53:46 -05:00
|
|
|
"serverPath",
|
|
|
|
"server",
|
2020-04-02 05:47:58 -05:00
|
|
|
"files",
|
2022-10-20 14:12:58 -05:00
|
|
|
].map((opt) => `${this.rootSection}.${opt}`);
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2020-03-24 03:31:42 -05:00
|
|
|
readonly package: {
|
|
|
|
version: string;
|
2020-03-25 13:56:48 -05:00
|
|
|
releaseTag: string | null;
|
2020-03-24 03:31:42 -05:00
|
|
|
enableProposedApi: boolean | undefined;
|
|
|
|
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
|
2020-02-15 19:08:36 -06:00
|
|
|
|
2021-05-23 15:33:32 -05:00
|
|
|
readonly globalStorageUri: vscode.Uri;
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
constructor(ctx: vscode.ExtensionContext) {
|
2021-05-23 15:33:32 -05:00
|
|
|
this.globalStorageUri = ctx.globalStorageUri;
|
2023-03-10 18:35:05 -06:00
|
|
|
this.discoveredWorkspaces = [];
|
2020-03-23 18:00:57 -05:00
|
|
|
vscode.workspace.onDidChangeConfiguration(
|
|
|
|
this.onDidChangeConfiguration,
|
|
|
|
this,
|
|
|
|
ctx.subscriptions
|
|
|
|
);
|
|
|
|
this.refreshLogging();
|
2022-10-20 14:12:58 -05:00
|
|
|
this.configureLanguage();
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this.configureLang?.dispose();
|
2020-02-13 15:05:32 -06:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
private refreshLogging() {
|
2023-01-24 06:43:56 -06:00
|
|
|
log.setEnabled(this.traceExtension ?? false);
|
2020-07-05 09:42:52 -05:00
|
|
|
log.info("Extension version:", this.package.version);
|
|
|
|
|
|
|
|
const cfg = Object.entries(this.cfg).filter(([_, val]) => !(val instanceof Function));
|
|
|
|
log.info("Using configuration", Object.fromEntries(cfg));
|
2020-02-13 14:48:20 -06:00
|
|
|
}
|
2020-02-07 20:22:44 -06:00
|
|
|
|
2023-03-10 18:35:05 -06:00
|
|
|
public discoveredWorkspaces: JsonProject[];
|
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
|
|
|
|
this.refreshLogging();
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2022-10-20 14:12:58 -05:00
|
|
|
this.configureLanguage();
|
|
|
|
|
2020-03-09 12:57:13 -05:00
|
|
|
const requiresReloadOpt = this.requiresReloadOpts.find((opt) =>
|
2020-02-13 14:48:20 -06:00
|
|
|
event.affectsConfiguration(opt)
|
|
|
|
);
|
2018-10-07 15:59:02 -05:00
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
if (!requiresReloadOpt) return;
|
2019-02-07 04:37:36 -06:00
|
|
|
|
2022-10-20 14:12:58 -05:00
|
|
|
if (this.restartServerOnConfigChange) {
|
2023-04-28 14:34:31 -05:00
|
|
|
await vscode.commands.executeCommand("rust-analyzer.restartServer");
|
2022-06-14 04:11:08 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-20 14:12:58 -05:00
|
|
|
const message = `Changing "${requiresReloadOpt}" requires a server restart`;
|
|
|
|
const userResponse = await vscode.window.showInformationMessage(message, "Restart now");
|
|
|
|
|
|
|
|
if (userResponse) {
|
2023-04-28 14:34:31 -05:00
|
|
|
const command = "rust-analyzer.restartServer";
|
2022-10-20 14:12:58 -05:00
|
|
|
await vscode.commands.executeCommand(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up additional language configuration that's impossible to do via a
|
|
|
|
* separate language-configuration.json file. See [1] for more information.
|
|
|
|
*
|
|
|
|
* [1]: https://github.com/Microsoft/vscode/issues/11514#issuecomment-244707076
|
|
|
|
*/
|
|
|
|
private configureLanguage() {
|
2023-01-17 15:25:46 -06:00
|
|
|
// Only need to dispose of the config if there's a change
|
2023-02-10 13:55:17 -06:00
|
|
|
if (this.configureLang) {
|
|
|
|
this.configureLang.dispose();
|
2023-01-17 15:25:46 -06:00
|
|
|
this.configureLang = undefined;
|
|
|
|
}
|
2023-01-17 14:29:52 -06:00
|
|
|
|
|
|
|
let onEnterRules: vscode.OnEnterRule[] = [
|
|
|
|
{
|
|
|
|
// Carry indentation from the previous line
|
2023-07-06 22:25:02 -05:00
|
|
|
// if it's only whitespace
|
|
|
|
beforeText: /^\s+$/,
|
2023-01-17 14:29:52 -06:00
|
|
|
action: { indentAction: vscode.IndentAction.None },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// After the end of a function/field chain,
|
|
|
|
// with the semicolon on the same line
|
|
|
|
beforeText: /^\s+\..*;/,
|
|
|
|
action: { indentAction: vscode.IndentAction.Outdent },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// After the end of a function/field chain,
|
|
|
|
// with semicolon detached from the rest
|
|
|
|
beforeText: /^\s+;/,
|
|
|
|
previousLineText: /^\s+\..*/,
|
|
|
|
action: { indentAction: vscode.IndentAction.Outdent },
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (this.typingContinueCommentsOnNewline) {
|
2022-10-20 14:12:58 -05:00
|
|
|
const indentAction = vscode.IndentAction.None;
|
|
|
|
|
2023-01-17 14:29:52 -06:00
|
|
|
onEnterRules = [
|
|
|
|
...onEnterRules,
|
|
|
|
{
|
|
|
|
// Doc single-line comment
|
|
|
|
// e.g. ///|
|
|
|
|
beforeText: /^\s*\/{3}.*$/,
|
|
|
|
action: { indentAction, appendText: "/// " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Parent doc single-line comment
|
|
|
|
// e.g. //!|
|
|
|
|
beforeText: /^\s*\/{2}\!.*$/,
|
|
|
|
action: { indentAction, appendText: "//! " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Begins an auto-closed multi-line comment (standard or parent doc)
|
|
|
|
// e.g. /** | */ or /*! | */
|
|
|
|
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
|
|
|
|
afterText: /^\s*\*\/$/,
|
|
|
|
action: {
|
|
|
|
indentAction: vscode.IndentAction.IndentOutdent,
|
|
|
|
appendText: " * ",
|
2022-10-20 14:12:58 -05:00
|
|
|
},
|
2023-01-17 14:29:52 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Begins a multi-line comment (standard or parent doc)
|
|
|
|
// e.g. /** ...| or /*! ...|
|
|
|
|
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
|
|
|
|
action: { indentAction, appendText: " * " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Continues a multi-line comment
|
|
|
|
// e.g. * ...|
|
|
|
|
beforeText: /^(\ \ )*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
|
|
|
|
action: { indentAction, appendText: "* " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Dedents after closing a multi-line comment
|
|
|
|
// e.g. */|
|
|
|
|
beforeText: /^(\ \ )*\ \*\/\s*$/,
|
|
|
|
action: { indentAction, removeText: 1 },
|
|
|
|
},
|
|
|
|
];
|
2022-06-06 02:51:50 -05:00
|
|
|
}
|
2023-01-17 14:29:52 -06:00
|
|
|
|
2023-02-10 13:55:17 -06:00
|
|
|
this.configureLang = vscode.languages.setLanguageConfiguration("rust", {
|
|
|
|
onEnterRules,
|
|
|
|
});
|
2020-02-08 16:27:04 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
// We don't do runtime config validation here for simplicity. More on stackoverflow:
|
|
|
|
// https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
|
2020-02-08 16:27:04 -06:00
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
private get cfg(): vscode.WorkspaceConfiguration {
|
|
|
|
return vscode.workspace.getConfiguration(this.rootSection);
|
|
|
|
}
|
|
|
|
|
2020-04-11 07:23:07 -05:00
|
|
|
/**
|
|
|
|
* Beware that postfix `!` operator erases both `null` and `undefined`.
|
|
|
|
* This is why the following doesn't work as expected:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const nullableNum = vscode
|
|
|
|
* .workspace
|
|
|
|
* .getConfiguration
|
2022-04-29 06:34:03 -05:00
|
|
|
* .getConfiguration("rust-analyzer")
|
2020-04-11 07:23:07 -05:00
|
|
|
* .get<number | null>(path)!;
|
|
|
|
*
|
|
|
|
* // What happens is that type of `nullableNum` is `number` but not `null | number`:
|
|
|
|
* const fullFledgedNum: number = nullableNum;
|
|
|
|
* ```
|
|
|
|
* So this getter handles this quirk by not requiring the caller to use postfix `!`
|
|
|
|
*/
|
2023-01-24 06:43:56 -06:00
|
|
|
private get<T>(path: string): T | undefined {
|
2023-03-10 18:35:05 -06:00
|
|
|
return prepareVSCodeConfig(this.cfg.get<T>(path));
|
2020-04-11 07:23:07 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 09:39:15 -06:00
|
|
|
get serverPath() {
|
|
|
|
return this.get<null | string>("server.path") ?? this.get<null | string>("serverPath");
|
|
|
|
}
|
2023-01-24 06:43:56 -06:00
|
|
|
|
2022-05-23 21:59:19 -05:00
|
|
|
get serverExtraEnv(): Env {
|
2022-05-23 22:08:59 -05:00
|
|
|
const extraEnv =
|
|
|
|
this.get<{ [key: string]: string | number } | null>("server.extraEnv") ?? {};
|
2023-01-24 06:43:56 -06:00
|
|
|
return substituteVariablesInEnv(
|
|
|
|
Object.fromEntries(
|
|
|
|
Object.entries(extraEnv).map(([k, v]) => [
|
|
|
|
k,
|
|
|
|
typeof v !== "string" ? v.toString() : v,
|
|
|
|
])
|
|
|
|
)
|
2022-05-23 22:08:59 -05:00
|
|
|
);
|
2020-12-30 03:17:25 -06:00
|
|
|
}
|
2020-04-11 07:23:07 -05:00
|
|
|
get traceExtension() {
|
|
|
|
return this.get<boolean>("trace.extension");
|
|
|
|
}
|
|
|
|
|
2023-03-09 14:06:26 -06:00
|
|
|
get discoverProjectCommand() {
|
2023-03-09 14:27:24 -06:00
|
|
|
return this.get<string[] | undefined>("discoverProjectCommand");
|
2023-03-09 14:06:26 -06:00
|
|
|
}
|
|
|
|
|
2023-06-30 16:18:52 -05:00
|
|
|
get problemMatcher(): string[] {
|
|
|
|
return this.get<string[]>("runnables.problemMatcher") || [];
|
|
|
|
}
|
|
|
|
|
2020-06-18 14:20:13 -05:00
|
|
|
get cargoRunner() {
|
|
|
|
return this.get<string | undefined>("cargoRunner");
|
|
|
|
}
|
|
|
|
|
2023-06-25 19:26:04 -05:00
|
|
|
get runnablesExtraEnv() {
|
2023-06-28 23:24:28 -05:00
|
|
|
const item = this.get<any>("runnables.extraEnv") ?? this.get<any>("runnableEnv");
|
2022-10-12 12:10:49 -05:00
|
|
|
if (!item) return item;
|
|
|
|
const fixRecord = (r: Record<string, any>) => {
|
|
|
|
for (const key in r) {
|
2022-10-12 16:14:59 -05:00
|
|
|
if (typeof r[key] !== "string") {
|
2022-10-12 12:10:49 -05:00
|
|
|
r[key] = String(r[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (item instanceof Array) {
|
|
|
|
item.forEach((x) => fixRecord(x.env));
|
|
|
|
} else {
|
|
|
|
fixRecord(item);
|
|
|
|
}
|
|
|
|
return item;
|
2020-07-02 11:47:40 -05:00
|
|
|
}
|
|
|
|
|
2022-06-06 02:51:50 -05:00
|
|
|
get restartServerOnConfigChange() {
|
|
|
|
return this.get<boolean>("restartServerOnConfigChange");
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:22:45 -05:00
|
|
|
get typingContinueCommentsOnNewline() {
|
|
|
|
return this.get<boolean>("typing.continueCommentsOnNewline");
|
|
|
|
}
|
|
|
|
|
2020-04-29 05:10:42 -05:00
|
|
|
get debug() {
|
2021-04-22 08:09:46 -05:00
|
|
|
let sourceFileMap = this.get<Record<string, string> | "auto">("debug.sourceFileMap");
|
|
|
|
if (sourceFileMap !== "auto") {
|
|
|
|
// "/rustc/<id>" used by suggestions only.
|
|
|
|
const { ["/rustc/<id>"]: _, ...trimmed } =
|
2023-01-24 06:43:56 -06:00
|
|
|
this.get<Record<string, string>>("debug.sourceFileMap") ?? {};
|
2021-04-22 08:09:46 -05:00
|
|
|
sourceFileMap = trimmed;
|
|
|
|
}
|
2020-05-06 08:01:35 -05:00
|
|
|
|
2020-04-29 05:10:42 -05:00
|
|
|
return {
|
2020-04-29 06:13:57 -05:00
|
|
|
engine: this.get<string>("debug.engine"),
|
2023-01-24 06:43:56 -06:00
|
|
|
engineSettings: this.get<object>("debug.engineSettings") ?? {},
|
2020-06-10 08:15:28 -05:00
|
|
|
openDebugPane: this.get<boolean>("debug.openDebugPane"),
|
2020-05-14 03:12:10 -05:00
|
|
|
sourceFileMap: sourceFileMap,
|
2020-04-29 05:10:42 -05:00
|
|
|
};
|
|
|
|
}
|
2020-05-17 11:51:44 -05:00
|
|
|
|
2020-06-03 06:15:54 -05:00
|
|
|
get hoverActions() {
|
|
|
|
return {
|
2022-05-13 14:17:03 -05:00
|
|
|
enable: this.get<boolean>("hover.actions.enable"),
|
|
|
|
implementations: this.get<boolean>("hover.actions.implementations.enable"),
|
|
|
|
references: this.get<boolean>("hover.actions.references.enable"),
|
|
|
|
run: this.get<boolean>("hover.actions.run.enable"),
|
|
|
|
debug: this.get<boolean>("hover.actions.debug.enable"),
|
|
|
|
gotoTypeDef: this.get<boolean>("hover.actions.gotoTypeDef.enable"),
|
2020-06-03 06:15:54 -05:00
|
|
|
};
|
|
|
|
}
|
2022-11-18 12:47:45 -06:00
|
|
|
get previewRustcOutput() {
|
|
|
|
return this.get<boolean>("diagnostics.previewRustcOutput");
|
|
|
|
}
|
2022-12-27 00:13:38 -06:00
|
|
|
|
|
|
|
get useRustcErrorCode() {
|
|
|
|
return this.get<boolean>("diagnostics.useRustcErrorCode");
|
|
|
|
}
|
2023-05-26 10:50:07 -05:00
|
|
|
|
|
|
|
get showDependenciesExplorer() {
|
|
|
|
return this.get<boolean>("showDependenciesExplorer");
|
|
|
|
}
|
2018-10-07 15:59:02 -05:00
|
|
|
}
|
2022-04-29 06:34:03 -05:00
|
|
|
|
2023-03-14 11:49:35 -05:00
|
|
|
// the optional `cb?` parameter is meant to be used to add additional
|
|
|
|
// key/value pairs to the VS Code configuration. This needed for, e.g.,
|
|
|
|
// including a `rust-project.json` into the `linkedProjects` key as part
|
|
|
|
// of the configuration/InitializationParams _without_ causing VS Code
|
|
|
|
// configuration to be written out to workspace-level settings. This is
|
|
|
|
// undesirable behavior because rust-project.json files can be tens of
|
|
|
|
// thousands of lines of JSON, most of which is not meant for humans
|
|
|
|
// to interact with.
|
2023-03-10 18:35:05 -06:00
|
|
|
export function prepareVSCodeConfig<T>(
|
|
|
|
resp: T,
|
|
|
|
cb?: (key: Extract<keyof T, string>, res: { [key: string]: any }) => void
|
|
|
|
): T {
|
2023-01-24 06:43:56 -06:00
|
|
|
if (Is.string(resp)) {
|
|
|
|
return substituteVSCodeVariableInString(resp) as T;
|
|
|
|
} else if (resp && Is.array<any>(resp)) {
|
2022-10-16 10:11:24 -05:00
|
|
|
return resp.map((val) => {
|
2023-03-10 18:35:05 -06:00
|
|
|
return prepareVSCodeConfig(val);
|
2023-01-24 06:43:56 -06:00
|
|
|
}) as T;
|
2022-10-16 10:11:24 -05:00
|
|
|
} else if (resp && typeof resp === "object") {
|
|
|
|
const res: { [key: string]: any } = {};
|
|
|
|
for (const key in resp) {
|
|
|
|
const val = resp[key];
|
2023-03-10 18:35:05 -06:00
|
|
|
res[key] = prepareVSCodeConfig(val);
|
|
|
|
if (cb) {
|
|
|
|
cb(key, res);
|
|
|
|
}
|
2022-10-16 10:11:24 -05:00
|
|
|
}
|
2023-01-24 06:43:56 -06:00
|
|
|
return res as T;
|
2022-10-16 10:11:24 -05:00
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
}
|
2023-01-24 06:43:56 -06:00
|
|
|
|
|
|
|
// FIXME: Merge this with `substituteVSCodeVariables` above
|
2022-05-11 07:22:58 -05:00
|
|
|
export function substituteVariablesInEnv(env: Env): Env {
|
|
|
|
const missingDeps = new Set<string>();
|
|
|
|
// vscode uses `env:ENV_NAME` for env vars resolution, and it's easier
|
|
|
|
// to follow the same convention for our dependency tracking
|
|
|
|
const definedEnvKeys = new Set(Object.keys(env).map((key) => `env:${key}`));
|
|
|
|
const envWithDeps = Object.fromEntries(
|
|
|
|
Object.entries(env).map(([key, value]) => {
|
|
|
|
const deps = new Set<string>();
|
|
|
|
const depRe = new RegExp(/\${(?<depName>.+?)}/g);
|
|
|
|
let match = undefined;
|
|
|
|
while ((match = depRe.exec(value))) {
|
2023-06-28 04:15:30 -05:00
|
|
|
const depName = unwrapUndefinable(match.groups?.["depName"]);
|
2022-05-11 07:22:58 -05:00
|
|
|
deps.add(depName);
|
|
|
|
// `depName` at this point can have a form of `expression` or
|
|
|
|
// `prefix:expression`
|
|
|
|
if (!definedEnvKeys.has(depName)) {
|
|
|
|
missingDeps.add(depName);
|
2022-05-17 12:15:06 -05:00
|
|
|
}
|
2022-05-11 07:22:58 -05:00
|
|
|
}
|
|
|
|
return [`env:${key}`, { deps: [...deps], value }];
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const resolved = new Set<string>();
|
2022-05-11 07:28:08 -05:00
|
|
|
for (const dep of missingDeps) {
|
|
|
|
const match = /(?<prefix>.*?):(?<body>.+)/.exec(dep);
|
|
|
|
if (match) {
|
|
|
|
const { prefix, body } = match.groups!;
|
|
|
|
if (prefix === "env") {
|
2023-06-27 14:03:53 -05:00
|
|
|
const envName = unwrapUndefinable(body);
|
2022-05-11 07:28:08 -05:00
|
|
|
envWithDeps[dep] = {
|
|
|
|
value: process.env[envName] ?? "",
|
|
|
|
deps: [],
|
|
|
|
};
|
|
|
|
resolved.add(dep);
|
|
|
|
} else {
|
|
|
|
// we can't handle other prefixes at the moment
|
|
|
|
// leave values as is, but still mark them as resolved
|
|
|
|
envWithDeps[dep] = {
|
|
|
|
value: "${" + dep + "}",
|
|
|
|
deps: [],
|
|
|
|
};
|
|
|
|
resolved.add(dep);
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-11 07:34:43 -05:00
|
|
|
envWithDeps[dep] = {
|
2022-10-16 10:11:24 -05:00
|
|
|
value: computeVscodeVar(dep) || "${" + dep + "}",
|
2022-05-11 07:34:43 -05:00
|
|
|
deps: [],
|
|
|
|
};
|
2022-05-11 07:28:08 -05:00
|
|
|
}
|
|
|
|
}
|
2022-05-11 07:22:58 -05:00
|
|
|
const toResolve = new Set(Object.keys(envWithDeps));
|
|
|
|
|
|
|
|
let leftToResolveSize;
|
|
|
|
do {
|
|
|
|
leftToResolveSize = toResolve.size;
|
|
|
|
for (const key of toResolve) {
|
2023-06-27 14:03:53 -05:00
|
|
|
const item = unwrapUndefinable(envWithDeps[key]);
|
|
|
|
if (item.deps.every((dep) => resolved.has(dep))) {
|
|
|
|
item.value = item.value.replace(/\${(?<depName>.+?)}/g, (_wholeMatch, depName) => {
|
|
|
|
const item = unwrapUndefinable(envWithDeps[depName]);
|
|
|
|
return item.value;
|
|
|
|
});
|
2022-05-11 07:22:58 -05:00
|
|
|
resolved.add(key);
|
|
|
|
toResolve.delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (toResolve.size > 0 && toResolve.size < leftToResolveSize);
|
|
|
|
|
|
|
|
const resolvedEnv: Env = {};
|
|
|
|
for (const key of Object.keys(env)) {
|
2023-06-27 14:03:53 -05:00
|
|
|
const item = unwrapUndefinable(envWithDeps[`env:${key}`]);
|
|
|
|
resolvedEnv[key] = item.value;
|
2022-05-11 07:22:58 -05:00
|
|
|
}
|
|
|
|
return resolvedEnv;
|
|
|
|
}
|
2022-05-11 07:34:43 -05:00
|
|
|
|
2023-01-24 06:43:56 -06:00
|
|
|
const VarRegex = new RegExp(/\$\{(.+?)\}/g);
|
|
|
|
function substituteVSCodeVariableInString(val: string): string {
|
|
|
|
return val.replace(VarRegex, (substring: string, varName) => {
|
|
|
|
if (Is.string(varName)) {
|
|
|
|
return computeVscodeVar(varName) || substring;
|
|
|
|
} else {
|
|
|
|
return substring;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-16 10:11:24 -05:00
|
|
|
function computeVscodeVar(varName: string): string | null {
|
|
|
|
const workspaceFolder = () => {
|
|
|
|
const folders = vscode.workspace.workspaceFolders ?? [];
|
2023-06-27 14:03:53 -05:00
|
|
|
const folder = folders[0];
|
|
|
|
// TODO: support for remote workspaces?
|
|
|
|
const fsPath: string =
|
|
|
|
folder === undefined
|
|
|
|
? // no workspace opened
|
|
|
|
""
|
|
|
|
: // could use currently opened document to detect the correct
|
|
|
|
// workspace. However, that would be determined by the document
|
|
|
|
// user has opened on Editor startup. Could lead to
|
|
|
|
// unpredictable workspace selection in practice.
|
|
|
|
// It's better to pick the first one
|
|
|
|
folder.uri.fsPath;
|
|
|
|
return fsPath;
|
2022-10-16 10:11:24 -05:00
|
|
|
};
|
2022-05-11 07:34:43 -05:00
|
|
|
// https://code.visualstudio.com/docs/editor/variables-reference
|
|
|
|
const supportedVariables: { [k: string]: () => string } = {
|
2022-10-16 10:11:24 -05:00
|
|
|
workspaceFolder,
|
2022-05-11 07:34:43 -05:00
|
|
|
|
|
|
|
workspaceFolderBasename: () => {
|
2022-10-16 10:11:24 -05:00
|
|
|
return path.basename(workspaceFolder());
|
2022-05-11 07:34:43 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
cwd: () => process.cwd(),
|
2022-10-16 10:11:24 -05:00
|
|
|
userHome: () => os.homedir(),
|
2022-05-11 07:34:43 -05:00
|
|
|
|
|
|
|
// see
|
|
|
|
// https://github.com/microsoft/vscode/blob/08ac1bb67ca2459496b272d8f4a908757f24f56f/src/vs/workbench/api/common/extHostVariableResolverService.ts#L81
|
|
|
|
// or
|
|
|
|
// https://github.com/microsoft/vscode/blob/29eb316bb9f154b7870eb5204ec7f2e7cf649bec/src/vs/server/node/remoteTerminalChannel.ts#L56
|
2023-06-28 04:15:30 -05:00
|
|
|
execPath: () => process.env["VSCODE_EXEC_PATH"] ?? process.execPath,
|
2022-05-11 07:34:43 -05:00
|
|
|
|
|
|
|
pathSeparator: () => path.sep,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (varName in supportedVariables) {
|
2023-06-27 14:03:53 -05:00
|
|
|
const fn = expectNotUndefined(
|
|
|
|
supportedVariables[varName],
|
|
|
|
`${varName} should not be undefined here`
|
|
|
|
);
|
|
|
|
return fn();
|
2022-05-11 07:34:43 -05:00
|
|
|
} else {
|
2022-10-16 10:11:24 -05:00
|
|
|
// return "${" + varName + "}";
|
|
|
|
return null;
|
2022-05-11 07:34:43 -05:00
|
|
|
}
|
|
|
|
}
|