Skip to content

Commit 09cfa4b

Browse files
committed
Use untildify for ~ in cwd and additionalPowerShellExes
This makes them way less annoying. I'm ok taking a dependency on untildify as it's a very simple package, and the Python extension for VS Code also uses it. However, it must remain at v4.0.0, as the latest version, v5.0.0, is pure ESM and therefore cannot be loaded by VS Code. https://github.com/sindresorhus/untildify/releases/tag/v5.0.0
1 parent 350eaff commit 09cfa4b

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@vscode/extension-telemetry": "0.8.2",
7878
"node-fetch": "2.6.12",
7979
"semver": "7.5.4",
80+
"untildify": "4.0.0",
8081
"uuid": "9.0.0",
8182
"vscode-languageclient": "8.1.0",
8283
"vscode-languageserver-protocol": "3.17.3"

src/platform.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { integer } from "vscode-languageserver-protocol";
99
import { ILogger } from "./logging";
1010
import { changeSetting, getSettings, PowerShellAdditionalExePathSettings } from "./settings";
1111

12+
// This is an ESM module and so cannot be imported because of VS Code
13+
// eslint-disable-next-line @typescript-eslint/no-var-requires
14+
const untildify: (value: string) => string = require("untildify");
15+
1216
// This uses require so we can rewire it in unit tests!
1317
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-var-requires
1418
const utils = require("./utils");
@@ -232,11 +236,13 @@ export class PowerShellExeFinder {
232236
private *enumerateAdditionalPowerShellInstallations(): Iterable<IPossiblePowerShellExe> {
233237
for (const versionName in this.additionalPowerShellExes) {
234238
if (Object.prototype.hasOwnProperty.call(this.additionalPowerShellExes, versionName)) {
235-
const exePath = utils.stripQuotePair(this.additionalPowerShellExes[versionName]);
239+
let exePath = utils.stripQuotePair(this.additionalPowerShellExes[versionName]);
236240
if (!exePath) {
237241
continue;
238242
}
239243

244+
exePath = untildify(exePath);
245+
240246
// Always search for what the user gave us first
241247
yield new PossiblePowerShellExe(exePath, versionName);
242248

src/settings.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import utils = require("./utils");
66
import os = require("os");
77
import { ILogger } from "./logging";
88

9+
// This is an ESM module and so cannot be imported because of VS Code
10+
// eslint-disable-next-line @typescript-eslint/no-var-requires
11+
const untildify: (value: string) => string = require("untildify");
12+
913
// TODO: Quite a few of these settings are unused in the client and instead
1014
// exist just for the server. Those settings do not need to be represented in
1115
// this class, as the LSP layers take care of communicating them. Frankly, this
@@ -221,8 +225,11 @@ export async function validateCwdSetting(logger: ILogger): Promise<string> {
221225
vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd"));
222226

223227
// Only use the cwd setting if it exists.
224-
if (cwd !== undefined && await utils.checkIfDirectoryExists(cwd)) {
225-
return cwd;
228+
if (cwd !== undefined) {
229+
cwd = untildify(cwd);
230+
if (await utils.checkIfDirectoryExists(cwd)) {
231+
return cwd;
232+
}
226233
}
227234

228235
// If there is no workspace, or there is but it has no folders, fallback.

test/core/platform.test.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ if (process.platform === "win32") {
451451

452452
additionalPowerShellExes = {
453453
"pwsh": "C:\\Users\\test\\pwsh\\pwsh.exe",
454+
"pwsh-tilde": "~\\pwsh\\pwsh.exe",
454455
"pwsh-no-exe": "C:\\Users\\test\\pwsh\\pwsh",
455456
"pwsh-folder": "C:\\Users\\test\\pwsh\\",
456457
"pwsh-folder-no-slash": "C:\\Users\\test\\pwsh",
@@ -466,15 +467,18 @@ if (process.platform === "win32") {
466467
isOS64Bit: true,
467468
isProcess64Bit: true,
468469
},
469-
environmentVars: {
470-
"USERPROFILE": "C:\\Users\\test",
471-
},
470+
environmentVars: {},
472471
expectedPowerShellSequence: [
473472
{
474473
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
475474
displayName: "pwsh",
476475
supportsProperArguments: true
477476
},
477+
{
478+
exePath: path.join(os.homedir(), "pwsh", "pwsh.exe"),
479+
displayName: "pwsh-tilde",
480+
supportsProperArguments: true
481+
},
478482
{
479483
exePath: "C:\\Users\\test\\pwsh\\pwsh",
480484
displayName: "pwsh-no-exe",
@@ -747,6 +751,7 @@ if (process.platform === "win32") {
747751

748752
additionalPowerShellExes = {
749753
"pwsh": "/home/bin/pwsh",
754+
"pwsh-tilde": "~/bin/pwsh",
750755
"pwsh-folder": "/home/bin/",
751756
"pwsh-folder-no-slash": "/home/bin",
752757
"pwsh-single-quotes": "'/home/bin/pwsh'",
@@ -761,15 +766,18 @@ if (process.platform === "win32") {
761766
isOS64Bit: true,
762767
isProcess64Bit: true,
763768
},
764-
environmentVars: {
765-
"HOME": "/home/test",
766-
},
769+
environmentVars: {},
767770
expectedPowerShellSequence: [
768771
{
769772
exePath: "/home/bin/pwsh",
770773
displayName: "pwsh",
771774
supportsProperArguments: true
772775
},
776+
{
777+
exePath: path.join(os.homedir(), "bin", "pwsh"),
778+
displayName: "pwsh-tilde",
779+
supportsProperArguments: true
780+
},
773781
{
774782
exePath: "/home/bin/",
775783
displayName: "pwsh-folder",

0 commit comments

Comments
 (0)