Skip to content
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

Add opt-in multi-root aware file watcher based on Axosoft/nsfw #28948

Merged
merged 16 commits into from
Jun 18, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"minimist": "1.2.0",
"native-keymap": "1.2.4",
"node-pty": "0.6.8",
"nsfw": "1.0.15",
"semver": "4.3.6",
"v8-profiler": "jrieken/v8-profiler#vscode",
"vscode-debugprotocol": "1.20.0",
Expand Down
40 changes: 40 additions & 0 deletions src/typings/nsfw.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

declare module 'nsfw' {
interface NsfwWatcher {
start(): void;
stop(): void;
}

interface NsfwWatchingPromise {
then(): void;
}

interface NsfwStartWatchingPromise {
then(fn: (watcher: NsfwWatcher) => void): NsfwWatchingPromise;
}

interface NsfwEvent {
action: number;
directory: string;
file?: string;
newFile?: string;
oldFile?: string;
}

interface NsfwFunction {
(dir: string, eventHandler: (events: NsfwEvent[]) => void, options?: any): NsfwStartWatchingPromise;
actions: {
CREATED: number;
DELETED: number;
MODIFIED: number;
RENAMED: number;
}
}

var nsfw: NsfwFunction;
export = nsfw;
}
1 change: 1 addition & 0 deletions src/vs/platform/files/common/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ export interface IFilesConfiguration {
autoSaveDelay: number;
eol: string;
hotExit: string;
useNsfwFileWatcher: boolean;
};
}

Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/buildfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ exports.collectModules = function (excludes) {
createModuleDescription('vs/workbench/services/search/node/searchApp', []),
createModuleDescription('vs/workbench/services/search/node/worker/searchWorkerApp', []),
createModuleDescription('vs/workbench/services/files/node/watcher/unix/watcherApp', []),
createModuleDescription('vs/workbench/services/files/node/watcher/nsfw/watcherApp', []),

createModuleDescription('vs/workbench/node/extensionHostProcess', []),

Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/parts/files/browser/files.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ configurationRegistry.registerConfiguration({
],
'description': nls.localize('hotExit', "Controls whether unsaved files are remembered between sessions, allowing the save prompt when exiting the editor to be skipped.", HotExitConfiguration.ON_EXIT, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE)
},
'files.useNsfwFileWatcher': {
'type': 'boolean',
'default': false,
'description': nls.localize('useNsfwFileWatcher', "Use the new experimental file watcher utilizing the nsfw library.")
},
'files.defaultLanguage': {
'type': 'string',
'description': nls.localize('defaultLanguage', "The default language mode that is assigned to new files.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FileService } from 'vs/workbench/services/files/node/fileService';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { parseArgs } from 'vs/platform/environment/node/argv';
import { RawTextSource } from 'vs/editor/common/model/textSource';
import { TestContextService } from 'vs/workbench/test/workbenchTestServices';

class TestEnvironmentService extends EnvironmentService {

Expand Down Expand Up @@ -47,7 +48,7 @@ const untitledBackupPath = path.join(workspaceBackupPath, 'untitled', crypto.cre

class TestBackupFileService extends BackupFileService {
constructor(workspace: Uri, backupHome: string, workspacesJsonPath: string) {
const fileService = new FileService(workspace.fsPath, { disableWatcher: true });
const fileService = new FileService(workspace.fsPath, { disableWatcher: true }, new TestContextService());

super(workspaceBackupPath, fileService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ suite('ConfigurationEditingService', () => {
instantiationService.stub(ITelemetryService, NullTelemetryService);
instantiationService.stub(IModeService, ModeServiceImpl);
instantiationService.stub(IModelService, instantiationService.createInstance(ModelServiceImpl));
instantiationService.stub(IFileService, new FileService(workspaceDir, { disableWatcher: true }));
instantiationService.stub(IFileService, instantiationService.createInstance(FileService, workspaceDir, { disableWatcher: true }));
instantiationService.stub(IUntitledEditorService, instantiationService.createInstance(UntitledEditorService));

instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ export class FileService implements IFileService {
encodingOverride,
watcherIgnoredPatterns,
verboseLogging: environmentService.verbose,
useNsfwFileWatcher: configuration.files.useNsfwFileWatcher
};

// create service
const workspace = this.contextService.getWorkspace();
this.raw = new NodeFileService(workspace ? workspace.resource.fsPath : void 0, fileServiceConfig);
this.raw = new NodeFileService(workspace ? workspace.resource.fsPath : void 0, fileServiceConfig, contextService);

// Listeners
this.registerListeners();
Expand Down
23 changes: 19 additions & 4 deletions src/vs/workbench/services/files/node/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import uri from 'vs/base/common/uri';
import nls = require('vs/nls');
import { isWindows, isLinux } from 'vs/base/common/platform';
import { dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';

import pfs = require('vs/base/node/pfs');
import encoding = require('vs/base/node/encoding');
Expand All @@ -35,6 +36,7 @@ import { FileWatcher as UnixWatcherService } from 'vs/workbench/services/files/n
import { FileWatcher as WindowsWatcherService } from 'vs/workbench/services/files/node/watcher/win32/watcherService';
import { toFileChangesEvent, normalize, IRawFileChange } from 'vs/workbench/services/files/node/watcher/common';
import Event, { Emitter } from 'vs/base/common/event';
import { FileWatcher as NsfwWatcherService } from 'vs/workbench/services/files/node/watcher/nsfw/watcherService';

export interface IEncodingOverride {
resource: uri;
Expand All @@ -51,6 +53,7 @@ export interface IFileServiceOptions {
watcherIgnoredPatterns?: string[];
disableWatcher?: boolean;
verboseLogging?: boolean;
useNsfwFileWatcher?: boolean;
}

function etag(stat: fs.Stats): string;
Expand Down Expand Up @@ -90,7 +93,11 @@ export class FileService implements IFileService {
private fileChangesWatchDelayer: ThrottledDelayer<void>;
private undeliveredRawFileChangesEvents: IRawFileChange[];

constructor(basePath: string, options: IFileServiceOptions) {
constructor(
basePath: string,
options: IFileServiceOptions,
private contextService: IWorkspaceContextService
) {
this.toDispose = [];
this.basePath = basePath ? paths.normalize(basePath) : void 0;

Expand Down Expand Up @@ -120,10 +127,14 @@ export class FileService implements IFileService {
}

if (this.basePath && !this.options.disableWatcher) {
if (isWindows) {
this.setupWin32WorkspaceWatching();
if (this.options.useNsfwFileWatcher) {
Copy link
Member Author

@Tyriar Tyriar Jun 18, 2017

Choose a reason for hiding this comment

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

@bpasero merging this now as the tests are passing and the new code should all be blocked off by this setting. Please send comments my way though 😃

Copy link
Member

Choose a reason for hiding this comment

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

@Tyriar sounds good. should we enable it automatically when we detect multiple folders are used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not just yet, I haven't tested at all on Windows. I may do so after playing with it on Monday.

Copy link
Member Author

Choose a reason for hiding this comment

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

@bpasero also getWorkspace2 seemed to be returning a single root at the time of initialization. Maybe I wasn't using it right but that may complicate things.

Copy link
Member

Choose a reason for hiding this comment

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

@Tyriar yes this is something I have to follow up with Sandeep, currently multi-folder comes in late.

this.setupNsfwWorkspceWatching();
} else {
this.setupUnixWorkspaceWatching();
if (isWindows) {
this.setupWin32WorkspaceWatching();
} else {
this.setupUnixWorkspaceWatching();
}
}
}

Expand Down Expand Up @@ -154,6 +165,10 @@ export class FileService implements IFileService {
this.toDispose.push(toDisposable(new UnixWatcherService(this.basePath, this.options.watcherIgnoredPatterns, e => this._onFileChanges.fire(e), this.options.errorLogger, this.options.verboseLogging).startWatching()));
}

private setupNsfwWorkspceWatching(): void {
this.toDispose.push(toDisposable(new NsfwWatcherService(this.basePath, this.options.watcherIgnoredPatterns, e => this._onFileChanges.fire(e), this.options.errorLogger, this.options.verboseLogging, this.contextService).startWatching()));
}

public resolveFile(resource: uri, options?: IResolveFileOptions): TPromise<IFileStat> {
return this.resolve(resource, options);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as glob from 'vs/base/common/glob';
import * as path from 'path';
import * as watcher from 'vs/workbench/services/files/node/watcher/common';
import * as nsfw from 'nsfw';
import { IWatcherService, IWatcherRequest } from 'vs/workbench/services/files/node/watcher/nsfw/watcher';
import { TPromise } from 'vs/base/common/winjs.base';
import { ThrottledDelayer } from 'vs/base/common/async';
import { FileChangeType } from 'vs/platform/files/common/files';

const nsfwActionToRawChangeType: { [key: number]: number } = [];
nsfwActionToRawChangeType[nsfw.actions.CREATED] = FileChangeType.ADDED;
nsfwActionToRawChangeType[nsfw.actions.MODIFIED] = FileChangeType.UPDATED;
nsfwActionToRawChangeType[nsfw.actions.DELETED] = FileChangeType.DELETED;


interface IPathWatcher {
watcher?: {
start(): void;
stop(): void;
};
}

export class NsfwWatcherService implements IWatcherService {
private static FS_EVENT_DELAY = 50; // aggregate and only emit events when changes have stopped for this duration (in ms)

private _pathWatchers: { [watchPath: string]: IPathWatcher } = {};

public watch(request: IWatcherRequest): TPromise<void> {
if (request.verboseLogging) {
console.log('request', request);
}

let undeliveredFileEvents: watcher.IRawFileChange[] = [];
const fileEventDelayer = new ThrottledDelayer(NsfwWatcherService.FS_EVENT_DELAY);

console.log('starting to watch ' + request.basePath);
this._pathWatchers[request.basePath] = {};

const promise = new TPromise<void>((c, e, p) => {
nsfw(request.basePath, events => {
for (let i = 0; i < events.length; i++) {
const e = events[i];

// Logging
if (request.verboseLogging) {
const logPath = e.action === nsfw.actions.RENAMED ? path.join(e.directory, e.oldFile) + ' -> ' + e.newFile : path.join(e.directory, e.file);
console.log(e.action === nsfw.actions.CREATED ? '[CREATED]' : e.action === nsfw.actions.DELETED ? '[DELETED]' : e.action === nsfw.actions.MODIFIED ? '[CHANGED]' : '[RENAMED]', logPath);
}

// Convert nsfw event to IRawFileChange and add to queue
let absolutePath: string;
if (e.action === nsfw.actions.RENAMED) {
// Rename fires when a file's name changes within a single directory
absolutePath = path.join(e.directory, e.oldFile);
if (!this._isPathIgnored(absolutePath, request.ignored)) {
undeliveredFileEvents.push({ type: FileChangeType.DELETED, path: absolutePath });
}
absolutePath = path.join(e.directory, e.newFile);
if (!this._isPathIgnored(absolutePath, request.ignored)) {
undeliveredFileEvents.push({ type: FileChangeType.ADDED, path: absolutePath });
}
} else {
absolutePath = path.join(e.directory, e.file);
if (!this._isPathIgnored(absolutePath, request.ignored)) {
undeliveredFileEvents.push({
type: nsfwActionToRawChangeType[e.action],
path: absolutePath
});
}
}
}

// Delay and send buffer
fileEventDelayer.trigger(() => {
const events = undeliveredFileEvents;
undeliveredFileEvents = [];

// Broadcast to clients normalized
const res = watcher.normalize(events);
p(res);

// Logging
if (request.verboseLogging) {
res.forEach(r => {
console.log(' >> normalized', r.type === FileChangeType.ADDED ? '[ADDED]' : r.type === FileChangeType.DELETED ? '[DELETED]' : '[CHANGED]', r.path);
});
}

return TPromise.as(null);
});
}).then(watcher => {
console.log('watcher ready ' + request.basePath);
this._pathWatchers[request.basePath].watcher = watcher;
return watcher.start();
});
});

return promise;
}

public setRoots(roots: string[]): TPromise<void> {
const rootsToStartWatching = roots.filter(r => !(r in this._pathWatchers));
const rootsToStopWatching = Object.keys(this._pathWatchers).filter(r => roots.indexOf(r) === -1);

// TODO: Don't watch inner folders
// TODO: Move verboseLogging to constructor
// Logging
if (true) {
console.log(`Set watch roots: start: [${rootsToStartWatching.join(',')}], stop: [${rootsToStopWatching.join(',')}]`);
}

const promises: TPromise<void>[] = [];
if (rootsToStartWatching.length) {
rootsToStartWatching.forEach(root => {
promises.push(this.watch({
basePath: root,
ignored: [],
// TODO: Inherit from initial request
verboseLogging: true
}));
});
}

if (rootsToStopWatching.length) {
rootsToStopWatching.forEach(root => {
this._pathWatchers[root].watcher.stop();
delete this._pathWatchers[root];
});
}

// TODO: Don't watch sub-folders of folders
return TPromise.join(promises).then(() => void 0);
}

private _isPathIgnored(absolutePath: string, ignored: string[]): boolean {
return ignored && ignored.some(ignore => glob.match(ignore, absolutePath));
}
}
24 changes: 24 additions & 0 deletions src/vs/workbench/services/files/node/watcher/nsfw/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

import { TPromise } from 'vs/base/common/winjs.base';

export interface IWatcherRequest {
basePath: string;
ignored: string[];
verboseLogging: boolean;
}

export interface IWatcherService {
setRoots(roots: string[]): TPromise<void>;
watch(request: IWatcherRequest): TPromise<void>;
}

export interface IFileWatcher {
startWatching(): () => void;
addFolder(folder: string): void;
}
14 changes: 14 additions & 0 deletions src/vs/workbench/services/files/node/watcher/nsfw/watcherApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';

import { Server } from 'vs/base/parts/ipc/node/ipc.cp';
import { WatcherChannel } from 'vs/workbench/services/files/node/watcher/nsfw/watcherIpc';
import { NsfwWatcherService } from 'vs/workbench/services/files/node/watcher/nsfw/nsfwWatcherService';

const server = new Server();
const service = new NsfwWatcherService();
const channel = new WatcherChannel(service);
server.registerChannel('watcher', channel);