Skip to content

Centralize fs usage. #8993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5b6955a
Use IFileSystem.fileExists() instead of fsextra.pathExists().
ericsnowcurrently Dec 9, 2019
740e0d7
Drop the lstatSync() call in EnvironmentVariablesService.
ericsnowcurrently Dec 9, 2019
b464494
Use IFileSystem instead of fs-extra in various places.
ericsnowcurrently Dec 10, 2019
2d59328
Add IFileSystem.readFileSync() and use it instead of fsextra.readFile…
ericsnowcurrently Dec 10, 2019
a1f1684
Add IFileSystem.createReadStream() and use it.
ericsnowcurrently Dec 10, 2019
e4d668a
Use IFileSystem in various datascience places instead of fs-extra.
ericsnowcurrently Dec 10, 2019
b65c5bf
Add IFileSystem.appendFile() and use it.
ericsnowcurrently Dec 10, 2019
a21558b
Export WriteStream from platform/types.
ericsnowcurrently Dec 10, 2019
28e7a0b
Use FileSystem in various places instead of node's fs.
ericsnowcurrently Dec 10, 2019
454b25e
Add IFileSystem.isDirReadonly() and use it.
ericsnowcurrently Dec 10, 2019
a929fcc
Add IFileSystem.move() and use it.
ericsnowcurrently Dec 10, 2019
acf6c2c
Drop utils/fs.fsExistsAsync().
ericsnowcurrently Dec 10, 2019
76d85ba
Drop utils/fs.fsReaddirAsync().
ericsnowcurrently Dec 10, 2019
a02b459
Drop utils/fs.getSubDirectories().
ericsnowcurrently Dec 10, 2019
fa2737c
Drop utils/fs.createTemporaryFile().
ericsnowcurrently Dec 10, 2019
24e58e4
Use IFileSystem.search() instead of glob().
ericsnowcurrently Dec 10, 2019
d7df432
Fix the Windows registry tests.
ericsnowcurrently Dec 10, 2019
913a278
Add IFileSystem.readData() and use it.
ericsnowcurrently Dec 12, 2019
48aef46
Drop an unused file.
ericsnowcurrently Dec 12, 2019
fb7bcb4
Pass IFileSystem in where needed.
ericsnowcurrently Dec 12, 2019
33377cb
Pass IFileSystem in where needed (in the datascience code).
ericsnowcurrently Dec 12, 2019
f207944
Use directoryExists() instead of fileExists().
ericsnowcurrently Dec 12, 2019
9e86f11
Be a little more efficient in readFile().
ericsnowcurrently Dec 13, 2019
a3ef492
Decrease a timeout if not CI.
ericsnowcurrently Dec 17, 2019
260c114
Refactor (for clarity) the venv creation testing helper.
ericsnowcurrently Dec 17, 2019
5080b06
Fix a test by using the old listdir behavior.
ericsnowcurrently Dec 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/client/common/application/webPanels/webPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
'use strict';
import '../../extensions';

import * as fs from 'fs-extra';
import * as uuid from 'uuid/v4';
import { Uri, Webview, WebviewPanel, window } from 'vscode';

import { Identifiers } from '../../../datascience/constants';
import { InteractiveWindowMessages } from '../../../datascience/interactive-common/interactiveWindowTypes';
import { SharedMessages } from '../../../datascience/messages';
import { IFileSystem } from '../../platform/types';
import { IDisposableRegistry } from '../../types';
import * as localize from '../../utils/localize';
import { IWebPanel, IWebPanelOptions, WebPanelMessage } from '../types';
Expand All @@ -26,10 +26,12 @@ export class WebPanel implements IWebPanel {
private id = uuid();

constructor(
private fs: IFileSystem,
private disposableRegistry: IDisposableRegistry,
private port: number | undefined,
private token: string | undefined,
private options: IWebPanelOptions) {
private options: IWebPanelOptions
) {
this.panel = window.createWebviewPanel(
options.title.toLowerCase().replace(' ', ''),
options.title,
Expand Down Expand Up @@ -86,7 +88,7 @@ export class WebPanel implements IWebPanel {
// tslint:disable-next-line:no-any
private async load() {
if (this.panel) {
const localFilesExist = await Promise.all(this.options.scripts.map(s => fs.pathExists(s)));
const localFilesExist = await Promise.all(this.options.scripts.map(s => this.fs.fileExists(s)));
if (localFilesExist.every(exists => exists === true)) {

// Call our special function that sticks this script inside of an html page
Expand Down
11 changes: 7 additions & 4 deletions src/client/common/application/webPanels/webPanelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { inject, injectable } from 'inversify';
import * as portfinder from 'portfinder';
import * as uuid from 'uuid/v4';

import { IFileSystem } from '../../platform/types';
import { IDisposableRegistry } from '../../types';
import { IWebPanel, IWebPanelOptions, IWebPanelProvider } from '../types';
import { WebPanel } from './webPanel';
Expand All @@ -15,13 +16,15 @@ export class WebPanelProvider implements IWebPanelProvider {
private port: number | undefined;
private token: string | undefined;

constructor(@inject(IDisposableRegistry) private disposableRegistry: IDisposableRegistry) {
}
constructor(
@inject(IDisposableRegistry) private disposableRegistry: IDisposableRegistry,
@inject(IFileSystem) private fs: IFileSystem
) { }

// tslint:disable-next-line:no-any
public async create(options: IWebPanelOptions): Promise<IWebPanel> {
const serverData = options.startHttpServer ? await this.ensureServerIsRunning() : { port: undefined, token: undefined };
return new WebPanel(this.disposableRegistry, serverData.port, serverData.token, options);
return new WebPanel(this.fs, this.disposableRegistry, serverData.port, serverData.token, options);
}

private async ensureServerIsRunning(): Promise<{ port: number; token: string }> {
Expand All @@ -36,7 +39,7 @@ export class WebPanelProvider implements IWebPanelProvider {
const webPanelServerModule = require('./webPanelServer') as typeof import('./webPanelServer');

// Start the server listening.
const webPanelServer = new webPanelServerModule.WebPanelServer(this.port, this.token);
const webPanelServer = new webPanelServerModule.WebPanelServer(this.port, this.token, this.fs);
webPanelServer.start();
this.disposableRegistry.push(webPanelServer);
}
Expand Down
6 changes: 3 additions & 3 deletions src/client/common/application/webPanels/webPanelServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as Cors from '@koa/cors';
import * as fs from 'fs-extra';
import * as http from 'http';
import * as Koa from 'koa';
import * as compress from 'koa-compress';
Expand All @@ -11,6 +10,7 @@ import * as path from 'path';
import { EXTENSION_ROOT_DIR } from '../../../constants';
import { Identifiers } from '../../../datascience/constants';
import { SharedMessages } from '../../../datascience/messages';
import { IFileSystem } from '../../platform/types';

interface IState {
cwd: string;
Expand All @@ -23,7 +23,7 @@ export class WebPanelServer {
private server: http.Server | undefined;
private state = new Map<string, IState>();

constructor(private port: number, private token: string) {
constructor(private port: number, private token: string, private fs: IFileSystem) {
this.app.use(Cors());
this.app.use(compress());
this.app.use(logger());
Expand Down Expand Up @@ -110,7 +110,7 @@ export class WebPanelServer {
break;

}
ctx.body = fs.createReadStream(filePath);
ctx.body = this.fs.createReadStream(filePath);
}

// Debugging tips:
Expand Down
40 changes: 19 additions & 21 deletions src/client/common/editor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Diff, diff_match_patch } from 'diff-match-patch';
import * as fs from 'fs-extra';
import { injectable } from 'inversify';
import * as md5 from 'md5';
import { EOL } from 'os';
import * as path from 'path';
import { Position, Range, TextDocument, TextEdit, Uri, WorkspaceEdit } from 'vscode';
import { IFileSystem } from '../common/platform/types';
import { IEditorUtils } from './types';

// Code borrowed from goFormat.ts (Go Extension for VS Code)
Expand Down Expand Up @@ -80,7 +80,7 @@ export function getTextEditsFromPatch(before: string, patch: string): TextEdit[]

return textEdits;
}
export function getWorkspaceEditsFromPatch(filePatches: string[], workspaceRoot?: string): WorkspaceEdit {
export function getWorkspaceEditsFromPatch(filePatches: string[], workspaceRoot: string | undefined, fs: IFileSystem): WorkspaceEdit {
const workspaceEdit = new WorkspaceEdit();
filePatches.forEach(patch => {
const indexOfAtAt = patch.indexOf('@@');
Expand All @@ -107,7 +107,7 @@ export function getWorkspaceEditsFromPatch(filePatches: string[], workspaceRoot?

let fileName = fileNameLines[0].substring(fileNameLines[0].indexOf(' a') + 3).trim();
fileName = workspaceRoot && !path.isAbsolute(fileName) ? path.resolve(workspaceRoot, fileName) : fileName;
if (!fs.existsSync(fileName)) {
if (!fs.fileExistsSync(fileName)) {
return;
}

Expand All @@ -123,7 +123,7 @@ export function getWorkspaceEditsFromPatch(filePatches: string[], workspaceRoot?
throw new Error('Unable to parse Patch string');
}

const fileSource = fs.readFileSync(fileName).toString('utf8');
const fileSource = fs.readFileSync(fileName);
const fileUri = Uri.file(fileName);

// Add line feeds and build the text edits
Expand Down Expand Up @@ -226,24 +226,22 @@ function getTextEditsInternal(before: string, diffs: [number, string][], startLi
return edits;
}

export function getTempFileWithDocumentContents(document: TextDocument): Promise<string> {
return new Promise<string>((resolve, reject) => {
const ext = path.extname(document.uri.fsPath);
// Don't create file in temp folder since external utilities
// look into configuration files in the workspace and are not able
// to find custom rules if file is saved in a random disk location.
// This means temp file has to be created in the same folder
// as the original one and then removed.
export async function getTempFileWithDocumentContents(document: TextDocument, fs: IFileSystem): Promise<string> {
const ext = path.extname(document.uri.fsPath);
// Don't create file in temp folder since external utilities
// look into configuration files in the workspace and are not
// to find custom rules if file is saved in a random disk location.
// This means temp file has to be created in the same folder
// as the original one and then removed.

// tslint:disable-next-line:no-require-imports
const fileName = `${document.uri.fsPath}.${md5(document.uri.fsPath)}${ext}`;
fs.writeFile(fileName, document.getText(), ex => {
if (ex) {
reject(`Failed to create a temporary file, ${ex.message}`);
}
resolve(fileName);
});
});
// tslint:disable-next-line:no-require-imports
const fileName = `${document.uri.fsPath}.${md5(document.uri.fsPath)}${ext}`;
try {
await fs.writeFile(fileName, document.getText());
} catch (ex) {
throw Error(`Failed to create a temporary file, ${ex.message}`);
}
return fileName;
}

/**
Expand Down
63 changes: 0 additions & 63 deletions src/client/common/envFileParser.ts

This file was deleted.

24 changes: 6 additions & 18 deletions src/client/common/installer/moduleInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as fs from 'fs';
import { injectable } from 'inversify';
import * as path from 'path';
import { CancellationToken, OutputChannel, ProgressLocation, ProgressOptions, window } from 'vscode';
Expand All @@ -12,10 +11,11 @@ import { EventName } from '../../telemetry/constants';
import { IApplicationShell } from '../application/types';
import { wrapCancellationTokens } from '../cancellation';
import { STANDARD_OUTPUT_CHANNEL } from '../constants';
import { IFileSystem } from '../platform/types';
import { ITerminalServiceFactory } from '../terminal/types';
import { ExecutionInfo, IConfigurationService, IOutputChannel } from '../types';
import { Products } from '../utils/localize';
import { isResource, noop } from '../utils/misc';
import { isResource } from '../utils/misc';
import { IModuleInstaller, InterpreterUri } from './types';

@injectable()
Expand All @@ -42,10 +42,11 @@ export abstract class ModuleInstaller implements IModuleInstaller {
if (!interpreter || interpreter.type !== InterpreterType.Unknown) {
await terminalService.sendCommand(pythonPath, args, token);
} else if (settings.globalModuleInstallation) {
if (await this.isPathWritableAsync(path.dirname(pythonPath))) {
await terminalService.sendCommand(pythonPath, args, token);
} else {
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
if (await fs.isDirReadonly(path.dirname(pythonPath))) {
this.elevatedInstall(pythonPath, args);
} else {
await terminalService.sendCommand(pythonPath, args, token);
}
} else {
await terminalService.sendCommand(pythonPath, args.concat(['--user']), token);
Expand Down Expand Up @@ -88,19 +89,6 @@ export abstract class ModuleInstaller implements IModuleInstaller {
}
return args;
}
private async isPathWritableAsync(directoryPath: string): Promise<boolean> {
const filePath = `${directoryPath}${path.sep}___vscpTest___`;
return new Promise<boolean>(resolve => {
fs.open(filePath, fs.constants.O_CREAT | fs.constants.O_RDWR, (error, fd) => {
if (!error) {
fs.close(fd, () => {
fs.unlink(filePath, noop);
});
}
return resolve(!error);
});
});
}

private elevatedInstall(execPath: string, args: string[]) {
const options = {
Expand Down
3 changes: 1 addition & 2 deletions src/client/common/net/fileDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

'use strict';

import { WriteStream } from 'fs';
import { inject, injectable } from 'inversify';
import * as requestTypes from 'request';
import { Progress, ProgressLocation } from 'vscode';
import { IApplicationShell } from '../application/types';
import { IFileSystem } from '../platform/types';
import { IFileSystem, WriteStream } from '../platform/types';
import { DownloadOptions, IFileDownloader, IHttpClient } from '../types';
import { Http } from '../utils/localize';
import { noop } from '../utils/misc';
Expand Down
Loading