Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/client/common/installer/moduleInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export abstract class ModuleInstaller implements IModuleInstaller {
await terminalService.sendCommand(pythonPath, args, token);
} else if (settings.globalModuleInstallation) {
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
if (await fs.isDirReadonly(path.dirname(pythonPath))) {
if (await fs.isDirReadonly(path.dirname(pythonPath)).catch(_err => true)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for this?

this.elevatedInstall(pythonPath, args);
} else {
await terminalService.sendCommand(pythonPath, args, token);
Expand Down
26 changes: 14 additions & 12 deletions src/client/common/platform/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { injectable } from 'inversify';
import { promisify } from 'util';
import * as vscode from 'vscode';
import { createDeferred } from '../utils/async';
import { noop } from '../utils/misc';
import { isFileNotFoundError } from './errors';
import { isFileNotFoundError, isNoPermissionsError } from './errors';
import { FileSystemPaths, FileSystemPathUtils } from './fs-paths';
import { TemporaryFileSystem } from './fs-temp';
// prettier-ignore
Expand Down Expand Up @@ -457,16 +456,19 @@ export class FileSystem implements IFileSystem {

public async isDirReadonly(dirname: string): Promise<boolean> {
const filePath = `${dirname}${this.paths.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);
});
});
const flags = fs.constants.O_CREAT | fs.constants.O_RDWR;
let fd: number;
try {
fd = await fs.open(filePath, flags);
} catch (err) {
if (isNoPermissionsError(err)) {
return true;
}
throw err; // re-throw
}
await fs.close(fd);
await fs.unlink(filePath);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two could probably, be done in the back ground rather than awaiting.
Basically previously there was no await on cleaning resources, now there is, hence it's unnecessarily slower.
Suggestion is to use fs.close().ignorewait

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eric Snow (@ericsnowcurrently) Do you think it would be good to create a seprate PR to clean up the resources in the background, as opposed to awaiting on them and slowing the exit from this fn?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can submit one if you think its ok.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great. Thanks!

return false;
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/common/installer/moduleInstaller.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,25 @@ suite('Module Installer', () => {
interpreterService.verifyAll();
terminalService.verifyAll();
});
test(`ignores failures in IFileSystem.isDirReadonly()`, async () => {
const info = TypeMoq.Mock.ofType<PythonInterpreter>();
info.setup((t: any) => t.then).returns(() => undefined);
info.setup(t => t.type).returns(() => InterpreterType.Unknown);
info.setup(t => t.version).returns(() => new SemVer('3.5.0-final'));
setActiveInterpreter(info.object);
pythonSettings.setup(p => p.globalModuleInstallation).returns(() => true);
const elevatedInstall = sinon.stub(TestModuleInstaller.prototype, 'elevatedInstall');
elevatedInstall.returns();
const err = new Error('oops!');
fs.setup(f => f.isDirReadonly(path.dirname(pythonPath))).returns(() => Promise.reject(err));
try {
await installer.installModule(product.name, resource);
} catch (ex) {
noop();
}
assert.ok(elevatedInstall.calledOnceWith(pythonPath, ['-m', 'executionInfo']));
interpreterService.verifyAll();
});
test('If cancellation token is provided, install while showing progress', async () => {
const options = {
location: ProgressLocation.Notification,
Expand Down
12 changes: 5 additions & 7 deletions src/test/common/platform/filesystem.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ suite('FileSystem', () => {

const isReadonly = await fileSystem.isDirReadonly(dirname);

expect(isReadonly).to.equal(false);
expect(isReadonly).to.equal(true);
});
});

Expand All @@ -1288,15 +1288,13 @@ suite('FileSystem', () => {

const isReadonly = await fileSystem.isDirReadonly(dirname);

expect(isReadonly).to.equal(true);
expect(isReadonly).to.equal(false);
});

// Failing may be more sensible, but for now we are sticking
// with the existing behavior.
test('false if the directory does not exist', async () => {
const isReadonly = await fileSystem.isDirReadonly(DOES_NOT_EXIST);
test('fail if the directory does not exist', async () => {
const promise = fileSystem.isDirReadonly(DOES_NOT_EXIST);

expect(isReadonly).to.equal(false);
await expect(promise).to.eventually.be.rejected;
});
});

Expand Down