Skip to content

Commit d95b320

Browse files
authored
fix: enable paths in the global npm cache (#1277)
1 parent 521e761 commit d95b320

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

client/src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as util from "util";
1717

1818
import * as vscode from "vscode";
1919
import { registerSidebar } from "./tasks_sidebar";
20+
import { getDenoInfoJson } from "./util";
2021

2122
function handleConfigurationChange(event: vscode.ConfigurationChangeEvent) {
2223
if (
@@ -79,6 +80,9 @@ export async function activate(
7980
): Promise<void> {
8081
extensionContext.outputChannel = extensionContext.outputChannel ??
8182
vscode.window.createOutputChannel(LANGUAGE_CLIENT_NAME);
83+
extensionContext.denoInfoJson = await getDenoInfoJson(
84+
extensionContext.outputChannel,
85+
);
8286
const p2cMap = new Map<string, string>();
8387
extensionContext.clientOptions = {
8488
documentSelector: [
@@ -228,6 +232,7 @@ export async function activate(
228232
enableSettingsUnscoped: extensionContext.enableSettingsUnscoped,
229233
enableSettingsByFolder: extensionContext.enableSettingsByFolder,
230234
scopesWithDenoJson: Array.from(extensionContext.scopesWithDenoJson ?? []),
235+
npmCache: extensionContext.denoInfoJson?.npmCache ?? null,
231236
};
232237
});
233238

client/src/shared_types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export interface PluginSettings {
1212
enableSettingsUnscoped: EnableSettings;
1313
enableSettingsByFolder: [string, EnableSettings][];
1414
scopesWithDenoJson: string[];
15+
npmCache: string | null;
1516
}

client/src/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface DenoExtensionContext {
3535
| ServerCapabilities<DenoExperimental>
3636
| undefined;
3737
scopesWithDenoJson: Set<string> | undefined;
38+
denoInfoJson: DenoInfoJson | null;
3839
statusBar: DenoStatusBar;
3940
tsApi: TsApi;
4041
outputChannel: vscode.OutputChannel;
@@ -48,6 +49,10 @@ export interface TestCommandOptions {
4849
inspect: boolean;
4950
}
5051

52+
export interface DenoInfoJson {
53+
npmCache?: string;
54+
}
55+
5156
export interface UpgradeAvailable {
5257
latestVersion: string;
5358
isCanary: boolean;

client/src/util.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
} from "vscode";
1616
import * as jsoncParser from "jsonc-parser/lib/esm/main.js";
1717
import { semver } from "./semver";
18+
import type { DenoInfoJson } from "./types";
19+
import { spawnSync } from "child_process";
20+
import type * as vscode from "vscode";
1821

1922
/** Assert that the condition is "truthy", otherwise throw. */
2023
export function assert(cond: unknown, msg = "Assertion failed."): asserts cond {
@@ -112,6 +115,37 @@ async function getDefaultDenoCommand() {
112115
}
113116
}
114117

118+
export async function getDenoInfoJson(
119+
outputChannel: vscode.OutputChannel,
120+
): Promise<DenoInfoJson | null> {
121+
try {
122+
const command = await getDenoCommandName();
123+
const { stdout, stderr, status, error } = spawnSync(command, [
124+
"info",
125+
"--json",
126+
], {
127+
encoding: "utf-8",
128+
stdio: ["ignore", "pipe", "pipe"],
129+
env: {
130+
...process.env,
131+
"NO_COLOR": "1",
132+
},
133+
});
134+
if (error) {
135+
throw error;
136+
}
137+
if (status != 0) {
138+
throw `Command failed: ${stderr}`;
139+
}
140+
return JSON.parse(stdout);
141+
} catch (error) {
142+
outputChannel.appendLine(
143+
`Couldn't get 'deno info --json' output: ${error}`,
144+
);
145+
return null;
146+
}
147+
}
148+
115149
function fileExists(executableFilePath: string): Promise<boolean> {
116150
return new Promise<boolean>((resolve) => {
117151
fs.stat(executableFilePath, (err, stat) => {

typescript-deno-plugin/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class Plugin implements ts.server.PluginModule {
6060
if (!pluginSettings) {
6161
return false;
6262
}
63+
if (pluginSettings.npmCache) {
64+
if (pathStartsWith(fileName, pluginSettings.npmCache)) {
65+
return true;
66+
}
67+
}
6368
const enableSettings =
6469
pluginSettings.enableSettingsByFolder?.find(([workspace, _]) =>
6570
pathStartsWith(fileName, workspace)

0 commit comments

Comments
 (0)