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

Support supplying a watchFactory option that resolves the plugin and uses it for watching file or directory #51074

Closed
wants to merge 13 commits into from
Closed
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
192 changes: 148 additions & 44 deletions src/compiler/commandLineParser.ts

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4333,6 +4333,14 @@
"category": "Error",
"code": 5108
},
"'watchFactory' name can only be a package name.": {
"category": "Error",
"code": 5109
},
"Option 'watchFactory' cannot be specified without passing '--allowPlugins' on command line.": {
"category": "Error",
"code": 5110
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
Expand Down Expand Up @@ -6091,6 +6099,10 @@
"category": "Message",
"code": 6718
},
"Specify which factory to invoke 'watchFile' and 'watchDirectory' on.": {
"category": "Message",
"code": 6719
},
"Default catch clause variables as 'unknown' instead of 'any'.": {
"category": "Message",
"code": 6803
Expand All @@ -6099,6 +6111,10 @@
"category": "Message",
"code": 6804
},
"Allow running plugins.": {
"category": "Message",
"code": 6805
},

"one of:": {
"category": "Message",
Expand Down
113 changes: 107 additions & 6 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
endsWith,
enumerateInsertsAndDeletes,
FileSystemEntries,
forEach,
getDirectoryPath,
getFallbackOptions,
getNormalizedAbsolutePath,
Expand All @@ -28,20 +29,25 @@ import {
matchesExclude,
matchFiles,
memoize,
ModuleImportResult,
noop,
normalizePath,
normalizeSlashes,
orderedRemoveItem,
parsePackageName,
Path,
perfLogger,
PluginImport,
PollingWatchKind,
RequireResult,
resolveJSModule,
returnUndefined,
some,
startsWith,
stringContains,
timestamp,
unorderedRemoveItem,
UserWatchFactory,
UserWatchFactoryModule,
WatchDirectoryKind,
WatchFileKind,
WatchOptions,
Expand Down Expand Up @@ -469,7 +475,7 @@ function createFixedChunkSizePollingWatchFile(host: {
}
}

interface SingleFileWatcher<T extends FileWatcherCallback | FsWatchCallback>{
interface SingleFileWatcher<T extends FileWatcherCallback | FsWatchCallback> {
watcher: FileWatcher;
callbacks: T[];
}
Expand Down Expand Up @@ -827,6 +833,11 @@ export const enum FileSystemEntryKind {
Directory,
}

/** @internal */
export function setWatchOptionInternalProperty<K extends keyof WatchOptions>(options: WatchOptions, key: K, value: WatchOptions[K]) {
Object.defineProperty(options, key, { configurable: false, enumerable: false, value });
}

function createFileWatcherCallback(callback: FsWatchCallback): FileWatcherCallback {
return (_fileName, eventKind, modifiedTime) => callback(eventKind === FileWatcherEventKind.Changed ? "change" : "rename", "", modifiedTime);
}
Expand Down Expand Up @@ -882,6 +893,36 @@ function createFsWatchCallbackForDirectoryWatcherCallback(
};
}

/** @internal */
export interface ImportPluginResult<T> {
pluginConfigEntry: PluginImport;
resolvedModule: T | undefined;
errorLogs: string[] | undefined;
}
/** @internal */
export function resolveModule<T = {}>(
pluginConfigEntry: PluginImport,
searchPaths: readonly string[],
host: Pick<System, "require" | "resolvePath">,
log: (message: string) => void,
): ImportPluginResult<T> {
Debug.assertIsDefined(host.require);
let errorLogs: string[] | undefined;
let resolvedModule: T | undefined;
for (const initialDir of searchPaths) {
const resolvedPath = normalizeSlashes(host.resolvePath(combinePaths(initialDir, "node_modules")));
log(`Loading ${pluginConfigEntry.name} from ${initialDir} (resolved to ${resolvedPath})`);
const result = host.require(resolvedPath, pluginConfigEntry.name); // TODO: GH#18217
if (!result.error) {
resolvedModule = result.module as T;
break;
}
const err = result.error.stack || result.error.message || JSON.stringify(result.error);
(errorLogs ??= []).push(`Failed to load module '${pluginConfigEntry.name}' from ${resolvedPath}: ${err}`);
}
return { pluginConfigEntry, resolvedModule, errorLogs };
}

/** @internal */
export type FileSystemEntryExists = (fileorDirectrory: string, entryKind: FileSystemEntryKind) => boolean;

Expand All @@ -907,6 +948,7 @@ export interface CreateSystemWatchFunctions {
tscWatchDirectory: string | undefined;
inodeWatching: boolean;
sysLog: (s: string) => void;
getSystem: () => System;
}

/** @internal */
Expand All @@ -927,6 +969,7 @@ export function createSystemWatchFunctions({
tscWatchDirectory,
inodeWatching,
sysLog,
getSystem,
}: CreateSystemWatchFunctions): { watchFile: HostWatchFile; watchDirectory: HostWatchDirectory; } {
const pollingWatches = new Map<string, SingleFileWatcher<FileWatcherCallback>>();
const fsWatches = new Map<string, SingleFileWatcher<FsWatchCallback>>();
Expand All @@ -936,12 +979,67 @@ export function createSystemWatchFunctions({
let nonPollingWatchFile: HostWatchFile | undefined;
let hostRecursiveDirectoryWatcher: HostWatchDirectory | undefined;
let hitSystemWatcherLimit = false;
let reportErrorOnMissingRequire = true;

return {
watchFile,
watchDirectory
watchDirectory,
};

function getUserWatchFactory(options: WatchOptions | undefined): UserWatchFactory | undefined {
if (!options?.watchFactory) return undefined;
// Look if we alaready have this factory resolved
if (options.getResolvedWatchFactory) return options.getResolvedWatchFactory();
const system = getSystem();
if (!system.require) {
if (reportErrorOnMissingRequire) sysLog(`Custom watchFactory is ignored because of not running in environment that supports 'require'. Watches will defualt to builtin.`);
reportErrorOnMissingRequire = false;
return setUserWatchFactory(options, /*userWatchFactory*/ undefined);
}
const factoryName = isString(options.watchFactory) ? options.watchFactory : options.watchFactory.name;
if (!factoryName || parsePackageName(factoryName).rest) {
sysLog(`Skipped loading watchFactory ${isString(options.watchFactory) ? options.watchFactory : JSON.stringify(options.watchFactory)} because it can be named with only package name`);
return setUserWatchFactory(options, /*userWatchFactory*/ undefined);
}
const host = options.getHost?.();
const searchPaths = host ?
host.searchPaths :
[
combinePaths(system.getExecutingFilePath(), "../../..")
];
sysLog(`Enabling watchFactory ${isString(options.watchFactory) ? options.watchFactory : JSON.stringify(options.watchFactory)} from candidate paths: ${searchPaths.join(",")}`);
const { resolvedModule, errorLogs, pluginConfigEntry } = resolveModule<UserWatchFactoryModule>(
getWatchFactoryPlugin(options),
searchPaths,
getSystem(),
sysLog
);
if (typeof resolvedModule === "function") {
return setUserWatchFactory(options, resolvedModule({ typescript: { sys, FileWatcherEventKind }, options, config: pluginConfigEntry }));
}
else if (!resolvedModule) {
forEach(errorLogs, sysLog);
sysLog(`Couldn't find ${pluginConfigEntry.name}`);
}
else {
sysLog(`Skipped loading plugin ${pluginConfigEntry.name} because it did not expose a proper factory function`);
}
return setUserWatchFactory(options, /*userWatchFactory*/ undefined);
}

function getWatchFactoryPlugin(options: WatchOptions) {
const plugin = isString(options.watchFactory) ? { name: options.watchFactory } : options.watchFactory!;
return options.getHost?.().getPluginWithConfigOverride(plugin) || plugin;
}

function setUserWatchFactory(options: WatchOptions, userWatchFactory: UserWatchFactory | undefined) {
setWatchOptionInternalProperty(options, "getResolvedWatchFactory", userWatchFactory ? () => userWatchFactory : returnUndefined);
return userWatchFactory;
}

function watchFile(fileName: string, callback: FileWatcherCallback, pollingInterval: PollingInterval, options: WatchOptions | undefined): FileWatcher {
const userWatchFactory = getUserWatchFactory(options);
if (typeof userWatchFactory?.watchFile === "function") return userWatchFactory.watchFile(fileName, callback, pollingInterval, options);
options = updateOptionsForWatchFile(options, useNonPollingWatchers);
const watchFileKind = Debug.checkDefined(options.watchFile);
switch (watchFileKind) {
Expand Down Expand Up @@ -1022,6 +1120,8 @@ export function createSystemWatchFunctions({
}

function watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean, options: WatchOptions | undefined): FileWatcher {
const userWatchFactory = getUserWatchFactory(options);
if (typeof userWatchFactory?.watchDirectory === "function") return userWatchFactory.watchDirectory(directoryName, callback, recursive, options);
if (fsSupportsRecursiveFsWatch) {
return fsWatch(
directoryName,
Expand Down Expand Up @@ -1428,7 +1528,7 @@ export interface System {
base64decode?(input: string): string;
base64encode?(input: string): string;
/** @internal */ bufferFrom?(input: string, encoding?: string): Buffer;
/** @internal */ require?(baseDir: string, moduleName: string): RequireResult;
/** @internal */ require?(baseDir: string, moduleName: string): ModuleImportResult;

// For testing
/** @internal */ now?(): Date;
Expand Down Expand Up @@ -1467,7 +1567,7 @@ export let sys: System = (() => {
let profilePath = "./profile.cpuprofile";

const Buffer: {
new (input: string, encoding?: string): any;
new(input: string, encoding?: string): any;
from?(input: string, encoding?: string): any;
} = require("buffer").Buffer;

Expand Down Expand Up @@ -1505,6 +1605,7 @@ export let sys: System = (() => {
tscWatchDirectory: process.env.TSC_WATCHDIRECTORY,
inodeWatching: isLinuxOrMacOs,
sysLog,
getSystem: () => nodeSystem,
});
const nodeSystem: System = {
args: process.argv.slice(2),
Expand All @@ -1513,7 +1614,7 @@ export let sys: System = (() => {
write(s: string): void {
process.stdout.write(s);
},
getWidthOfTerminal(){
getWidthOfTerminal() {
return process.stdout.columns;
},
writeOutputIsTTY() {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface BuildOptions {
/** @internal */ locale?: string;
/** @internal */ generateCpuProfile?: string;
/** @internal */ generateTrace?: string;
/** @internal */ allowPlugins?: boolean;

[option: string]: CompilerOptionsValue | undefined;
}
Expand Down Expand Up @@ -567,7 +568,7 @@ function parseConfigFile<T extends BuilderProgram>(state: SolutionBuilderState<T
}
else {
parseConfigFileHost.onUnRecoverableConfigFileDiagnostic = d => diagnostic = d;
parsed = getParsedCommandLineOfConfigFile(configFileName, baseCompilerOptions, parseConfigFileHost, extendedConfigCache, baseWatchOptions);
parsed = getParsedCommandLineOfConfigFile(configFileName, baseCompilerOptions, parseConfigFileHost, extendedConfigCache, baseWatchOptions, /*extraFileExtensions*/ undefined, state.hostWithWatch.checkAllowPlugins);
parseConfigFileHost.onUnRecoverableConfigFileDiagnostic = noop;
}
configFileCache.set(configFilePath, parsed || diagnostic!);
Expand Down
Loading