Skip to content

Commit 8ab32be

Browse files
committed
feat: uses a vscode.Task for downloading
This is the right way of installing packages and inheriting variables from the default terminal
1 parent f2daba1 commit 8ab32be

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

src/lib/tools.ts

+40-33
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,22 @@ export async function promptForMissingTool(
112112
const items = ['Install'];
113113
return vscode.window.showInformationMessage(msg, ...opts).then(selected => {
114114
if (selected === 'Install') {
115-
switch (toolType) {
116-
case 'Python':
117-
installPythonTool(tool, logger);
118-
break;
119-
120-
case 'VSExt':
121-
logger.info(`Installing VS Marketplace Extension with id: ${tool}`);
122-
vscode.commands.executeCommand('extension.open', tool);
123-
vscode.commands.executeCommand('workbench.extensions.installExtension', tool);
124-
logger.info(`Extension ${tool} successfully installed`);
125-
break;
126-
127-
default:
128-
logger.error(`Failed to install tool: ${tool}`);
129-
vscode.window.showErrorMessage(`Failed to install tool: ${tool}`);
130-
break;
115+
if (toolType === 'Python') {
116+
pipInstall(tool)
117+
.then(msg => {
118+
vscode.window.showInformationMessage(msg);
119+
})
120+
.catch(msg => {
121+
vscode.window.showErrorMessage(msg);
122+
});
123+
} else if (toolType === 'VSExt') {
124+
if (logger) logger.info(`Installing VS Marketplace Extension with id: ${tool}`);
125+
vscode.commands.executeCommand('extension.open', tool);
126+
vscode.commands.executeCommand('workbench.extensions.installExtension', tool);
127+
if (logger) logger.info(`Extension ${tool} successfully installed`);
128+
} else {
129+
if (logger) logger.error(`Failed to install tool: ${tool}`);
130+
vscode.window.showErrorMessage(`Failed to install tool: ${tool}`);
131131
}
132132
} else if (selected === "Don't Show Again") {
133133
action();
@@ -140,25 +140,32 @@ export async function promptForMissingTool(
140140
* Does not explicitly check if `pip` is installed.
141141
*
142142
* @param pyPackage name of python package in PyPi
143-
* @param logger `optional` logging channel for output
144143
*/
145-
export function installPythonTool(pyPackage: string, logger?: Logger) {
146-
const installProcess = cp.spawnSync(
147-
'pip',
148-
'install --user --upgrade '.concat(pyPackage).split(' ')
144+
export async function pipInstall(pyPackage: string): Promise<string> {
145+
const py = 'python3'; // Fetches the top-most python in the Shell
146+
const args = ['-m', 'pip', 'install', '--user', '--upgrade', pyPackage];
147+
return await shellTask(py, args, `pip: ${pyPackage}`);
148+
}
149+
150+
export async function shellTask(command: string, args: string[], name: string): Promise<string> {
151+
const task = new vscode.Task(
152+
{ type: 'shell' },
153+
vscode.TaskScope.Workspace,
154+
name,
155+
'Modern Fortran',
156+
new vscode.ShellExecution(command, args)
149157
);
150-
if (installProcess.error) {
151-
logger.error(
152-
`Python package ${pyPackage} failed to install with code: ${installProcess.error}`
153-
);
154-
}
155-
if (installProcess.stdout) {
156-
const sep = '-'.repeat(80);
157-
logger.info(
158-
`pip install --user --upgrade ${pyPackage}:\n${sep}\n${installProcess.stdout}${sep}`
159-
);
160-
logger.info(`pip install was successful`);
161-
}
158+
// Temporay fix to https://github.com/microsoft/vscode/issues/157756
159+
(<vscode.Task>task).definition = { type: 'shell', command: command };
160+
const execution = await vscode.tasks.executeTask(task);
161+
return await new Promise<string>((resolve, reject) => {
162+
const disposable = vscode.tasks.onDidEndTask(e => {
163+
if (e.execution === execution) {
164+
disposable.dispose();
165+
resolve(`${name}: shell task completed successfully.`);
166+
} else reject(`${name}: shell task failed.`);
167+
});
168+
});
162169
}
163170

164171
/**

src/lsp/client.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
LS_NAME,
1212
isFortran,
1313
getOuterMostWorkspaceFolder,
14+
pipInstall,
1415
} from '../lib/tools';
1516
import { Logger } from '../services/logging';
1617
import { RestartLS } from '../features/commands';
@@ -313,18 +314,17 @@ export class FortlsClient {
313314
let fortlsDisabled = false;
314315
if (results.error) {
315316
const selection = window.showInformationMessage(msg, 'Install', 'Disable');
316-
selection.then(opt => {
317+
selection.then(async opt => {
317318
if (opt === 'Install') {
318-
const install = spawnSync('pip', ['install', '--user', '--upgrade', LS_NAME]);
319-
if (install.error) {
320-
this.logger.error(`[lsp.client] Unable to install fortls:`, install.error);
321-
window.showErrorMessage('Had trouble installing fortls, please install manually');
322-
fortlsDisabled = true;
323-
}
324-
if (install.stdout) {
325-
this.logger.info(`[lsp.client] ${install.stdout.toString()}`);
326-
fortlsDisabled = false;
327-
}
319+
await pipInstall(LS_NAME)
320+
.then(msg => {
321+
window.showInformationMessage(msg);
322+
fortlsDisabled = false;
323+
})
324+
.catch(msg => {
325+
window.showErrorMessage(msg);
326+
fortlsDisabled = true;
327+
});
328328
} else if (opt == 'Disable') {
329329
config.update('fortls.disabled', true);
330330
fortlsDisabled = true;

0 commit comments

Comments
 (0)