Skip to content

Commit

Permalink
fix: When UNKNOWN errors are encountered, track the filenames and ski…
Browse files Browse the repository at this point in the history
…p them when watching
  • Loading branch information
symwell committed Dec 16, 2022
1 parent c9eaf83 commit d020e6e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
34 changes: 32 additions & 2 deletions packages/cli/src/fingerprint/fingerprintWatchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { verbose } from '../utils';
import { FingerprintEvent } from './fingerprinter';
import FingerprintQueue from './fingerprintQueue';
import Globber from './globber';
import path from 'path';

export default class FingerprintWatchCommand {
private pidfilePath: string | undefined;
public fpQueue: FingerprintQueue;
public watcher?: FSWatcher;
private poller?: Globber;
private _numProcessed = 0;
public unreadableFiles = new Set();

public get numProcessed() {
return this._numProcessed;
Expand Down Expand Up @@ -45,6 +47,15 @@ export default class FingerprintWatchCommand {
}
}

getFilenameFromErrorMessage(errorMessage: string): string {
// The errorMessage must contain the filename within single quotes
// and looks like this:
// Error: UNKNOWN: unknown error, lstat 'c:\Users\Test\Programming\MyProject'
let quoteStartIndex = errorMessage.indexOf(`'`);
let quoteEndIndex = errorMessage.indexOf(`'`, quoteStartIndex + 1);
return errorMessage.substring(quoteStartIndex + 1, quoteEndIndex);
}

async watcherErrorFunction (error: Error) {
if (this.watcher && error.message.includes("ENOSPC: System limit for number of file watchers reached")) {
console.warn(error.stack);
Expand All @@ -67,7 +78,10 @@ export default class FingerprintWatchCommand {
errorMessage: error.message,
errorStack: error.stack,
},
});
});
const filename = this.getFilenameFromErrorMessage(error.message);
this.unreadableFiles.add(filename);
console.warn("Will not read this file again.");
} else {
// let it crash if it's some other error, to learn what the error is
throw error;
Expand All @@ -90,9 +104,25 @@ export default class FingerprintWatchCommand {
this.poller.start();
await pollReady;

// Custom ignore function needed to skip unreadableFiles because
// attempting to read them can block reading all files. It also
// cuts down the watch tree to just what we need.
const ignored = (targetPath: string) => {
['/node_modules/', '/.git/'].forEach((pattern) => {
if (this.unreadableFiles.has(targetPath) || targetPath.includes(pattern)) {
return true;
}
});

// Also make sure to not try to recurse down node_modules or .git
const basename = path.basename(targetPath);
return basename === 'node_modules' || basename === '.git';
};


this.watcher = watch(glob, {
ignoreInitial: true,
ignored: ['**/node_modules/**', '**/.git/**'],
ignored: ignored,
ignorePermissionErrors: true,
})
.on('add', this.added.bind(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,26 @@ describe(FingerprintWatchCommand, () => {
expect(cmd.watcher).not.toBeUndefined();
await cmd.watcherErrorFunction(new Error("ENOSPC: System limit for number of file watchers reached"));
expect(cmd.watcher).toBeUndefined();
});
});

it('gets filenames from error messages', async () => {
cmd = new FingerprintWatchCommand(appMapDir);
expect(cmd.getFilenameFromErrorMessage(`Error: UNKNOWN: unknown error, lstat 'c:\\Users\\Test\\Programming\\MyProject'`)).toBe('c:\\Users\\Test\\Programming\\MyProject');
});

it('does not raise on unknown error lstat', async () => {
it('does not raise on unknown error lstat', async () => {
cmd = new FingerprintWatchCommand(appMapDir);
cmd.watcher = new FSWatcher();
expect(cmd.watcher).not.toBeUndefined();
expect(cmd.unreadableFiles.size).toBe(0);
console.warn = jest.fn();
const errorMessage = "UNKNOWN: unknown error, lstat";
const errorMessage = `Error: UNKNOWN: unknown error, lstat 'c:\\Users\\Test\\Programming\\MyProject'`;
await cmd.watcherErrorFunction(new Error(errorMessage));
expect(cmd.watcher).not.toBeUndefined();
expect(console.warn).toBeCalledTimes(1);
expect(console.warn).toBeCalledTimes(2);
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining(errorMessage));
expect(cmd.unreadableFiles.size).toBe(1);
expect(cmd.unreadableFiles.has('c:\\Users\\Test\\Programming\\MyProject')).toBe(true);
});

describe('telemetry', () => {
Expand Down

0 comments on commit d020e6e

Please sign in to comment.