Skip to content

Commit

Permalink
Add searchPaths option so we know where to look for watchFactory from…
Browse files Browse the repository at this point in the history
… server which follows same locations as other Plugins
  • Loading branch information
sheetalkamat committed Oct 10, 2022
1 parent 060eff8 commit 67d6ee5
Show file tree
Hide file tree
Showing 28 changed files with 640 additions and 604 deletions.
22 changes: 13 additions & 9 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ namespace ts {
/*@internal*/
export function resolveModule<T = {}>(
pluginConfigEntry: PluginImport,
searchPaths: string[],
searchPaths: readonly string[],
host: Pick<System, "require" | "resolvePath">,
log: (message: string) => void,
): ImportPluginResult<T> {
Expand All @@ -865,7 +865,7 @@ namespace ts {
/*@internal*/
export async function importPlugin<T = {}>(
pluginConfigEntry: PluginImport,
searchPaths: string[],
searchPaths: readonly string[],
host: Pick<System, "importPlugin">,
log: (message: string) => void,
): Promise<ImportPluginResult<T>> {
Expand Down Expand Up @@ -985,12 +985,16 @@ namespace ts {
options.getResolvedWatchFactory = returnUndefined;
}
else {
const searchPaths = options.configFile ? [
getDirectoryPath(getNormalizedAbsolutePath(options.configFile.fileName, system.getCurrentDirectory())),
combinePaths(system.getExecutingFilePath(), "../../..")
] : [
combinePaths(system.getExecutingFilePath(), "../../..")
];
const searchPaths = options.watchFactorySearchPaths ?
options.watchFactorySearchPaths() :
// Do we allow configFile
// Do we need allowLocalPlugins and pluginProbeLocations for compile time as well?
options.configFile ? [
getDirectoryPath(getNormalizedAbsolutePath(options.configFile.fileName, system.getCurrentDirectory())),
combinePaths(system.getExecutingFilePath(), "../../..")
] : [
combinePaths(system.getExecutingFilePath(), "../../..")
];
sysLog(`Enabling watchFactory ${isString(options.watchFactory) ? options.watchFactory : options.watchFactory.name} from candidate paths: ${searchPaths.join(",")}`);
watchFactory = system.importPlugin ?
importFactory(options, searchPaths, system) :
Expand All @@ -1015,7 +1019,7 @@ namespace ts {
return resolvedWatchFactory;
}

function importFactory(options: WatchOptions, searchPaths: string[], system: System) {
function importFactory(options: WatchOptions, searchPaths: readonly string[], system: System) {
return setGetResolvedWatchFactory(options, {
pending: importPlugin<UserWatchFactory>(
isString(options.watchFactory) ? { name: options.watchFactory } : options.watchFactory!,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6695,6 +6695,7 @@ namespace ts {
/** configFile is set as non enumerable property so as to avoid checking of json source files */
/* @internal */ readonly configFile?: TsConfigSourceFile;
/* @internal */ getResolvedWatchFactory?(): ResolvedWatchFactory | undefined;
/* @internal */ watchFactorySearchPaths?(): readonly string[];

[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
Expand Down
26 changes: 26 additions & 0 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,7 @@ namespace ts.server {
configFileExistenceInfo.config.watchedDirectoriesStale = true;
configFileExistenceInfo.config.reloadLevel = undefined;
}
if (parsedCommandLine.watchOptions) parsedCommandLine.watchOptions.watchFactorySearchPaths = () => this.getProjectPluginSearchPaths(canonicalConfigFilePath);

// If watch options different than older options when setting for the first time, update the config file watcher
if (!oldCommandLine && !isJsonEqual(
Expand Down Expand Up @@ -3049,6 +3050,10 @@ namespace ts.server {
if (args.watchOptions) {
const result = convertWatchOptions(args.watchOptions);
this.hostConfiguration.watchOptions = result?.watchOptions;
if (this.hostConfiguration.watchOptions?.watchFactory) {
// Set the watchFactory searchPaths same as global paths
this.hostConfiguration.watchOptions.watchFactorySearchPaths = () => this.getGlobalPluginSearchPaths();
}
this.projectWatchOptions.clear();
this.logger.info(`Host watch options changed to ${JSON.stringify(this.hostConfiguration.watchOptions)}, it will be take effect for next watches.`);
if (result?.errors?.length) {
Expand Down Expand Up @@ -4106,6 +4111,27 @@ namespace ts.server {
return false;
}

/*@internal */
getGlobalPluginSearchPaths() {
// Search any globally-specified probe paths, then our peer node_modules
return [
...this.pluginProbeLocations,
// ../../.. to walk from X/node_modules/typescript/lib/tsserver.js to X/node_modules/
combinePaths(this.getExecutingFilePath(), "../../.."),
];
}

/*@internal*/
getProjectPluginSearchPaths(canonicalConfigFilePath: string | undefined) {
const searchPaths = this.getGlobalPluginSearchPaths();
if (canonicalConfigFilePath && this.allowLocalPluginLoads) {
const local = getDirectoryPath(canonicalConfigFilePath);
this.logger.info(`Local plugin loading enabled; adding ${local} to search paths`);
searchPaths.unshift(local);
}
return searchPaths;
}

/*@internal*/
requestEnablePlugin(project: Project, pluginConfigEntry: PluginImport, searchPaths: string[]) {
if (!this.host.importPlugin && !this.host.require) {
Expand Down
27 changes: 12 additions & 15 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,9 @@ namespace ts.server {
/*@internal*/
setWatchOptions(watchOptions: WatchOptions | undefined) {
this.watchOptions = watchOptions;
if (this.watchOptions?.watchFactory) {
this.watchOptions.watchFactorySearchPaths = () => this.getProjectPluginSearchPaths();
}
}

/*@internal*/
Expand Down Expand Up @@ -1574,13 +1577,8 @@ namespace ts.server {
}

/*@internal*/
protected getGlobalPluginSearchPaths() {
// Search any globally-specified probe paths, then our peer node_modules
return [
...this.projectService.pluginProbeLocations,
// ../../.. to walk from X/node_modules/typescript/lib/tsserver.js to X/node_modules/
combinePaths(this.projectService.getExecutingFilePath(), "../../.."),
];
protected getProjectPluginSearchPaths() {
return this.projectService.getProjectPluginSearchPaths(/*canonicalConfigFilePath*/ undefined);
}

protected enableGlobalPlugins(options: CompilerOptions): void {
Expand All @@ -1593,7 +1591,7 @@ namespace ts.server {
}

// Enable global plugins with synthetic configuration entries
const searchPaths = this.getGlobalPluginSearchPaths();
const searchPaths = this.projectService.getGlobalPluginSearchPaths();
for (const globalPluginName of this.projectService.globalPlugins) {
// Skip empty names from odd commandline parses
if (!globalPluginName) continue;
Expand Down Expand Up @@ -2451,6 +2449,11 @@ namespace ts.server {
return this.getCurrentProgram()?.forEachResolvedProjectReference(cb);
}

/*@internal*/
protected getProjectPluginSearchPaths() {
return this.projectService.getProjectPluginSearchPaths(this.canonicalConfigFilePath);
}

/*@internal*/
enablePluginsWithOptions(options: CompilerOptions): void {
this.plugins.length = 0;
Expand All @@ -2461,14 +2464,8 @@ namespace ts.server {
return;
}

const searchPaths = this.getGlobalPluginSearchPaths();
if (this.projectService.allowLocalPluginLoads) {
const local = getDirectoryPath(this.canonicalConfigFilePath);
this.projectService.logger.info(`Local plugin loading enabled; adding ${local} to search paths`);
searchPaths.unshift(local);
}

// Enable tsconfig-specified plugins
const searchPaths = this.getProjectPluginSearchPaths();
if (options.plugins) {
for (const pluginConfigEntry of options.plugins) {
this.enablePlugin(pluginConfigEntry, searchPaths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Info 6 [00:00:29.000] Search path: /user/username/projects/myproject
Info 7 [00:00:30.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 8 [00:00:31.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 9 [00:00:32.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}}
Info 10 [00:00:33.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Info 6 [00:00:29.000] Search path: /user/username/projects/myproject
Info 7 [00:00:30.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 8 [00:00:31.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 9 [00:00:32.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"}
Info 10 [00:00:33.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Info 6 [00:00:29.000] Search path: /user/username/projects/myproject
Info 7 [00:00:30.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 8 [00:00:31.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 9 [00:00:32.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}}
Info 10 [00:00:33.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Info 9 [00:00:32.000] Search path: /user/username/projects/myproject
Info 10 [00:00:33.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 11 [00:00:34.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 12 [00:00:35.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}}
Info 13 [00:00:36.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Info 9 [00:00:32.000] Search path: /user/username/projects/myproject
Info 10 [00:00:33.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 11 [00:00:34.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 12 [00:00:35.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"}
Info 13 [00:00:36.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Info 9 [00:00:32.000] Search path: /user/username/projects/myproject
Info 10 [00:00:33.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 11 [00:00:34.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 12 [00:00:35.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":{"name":"myplugin","myconfig":"somethingelse"}}
Info 13 [00:00:36.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Info 9 [00:00:32.000] Search path: /user/username/projects/myproject
Info 10 [00:00:33.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 11 [00:00:34.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 12 [00:00:35.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"}
Info 13 [00:00:36.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Info 6 [00:00:29.000] Search path: /user/username/projects/myproject
Info 7 [00:00:30.000] For info: /user/username/projects/myproject/a.ts :: Config file name: /user/username/projects/myproject/tsconfig.json
Info 8 [00:00:31.000] Creating configuration project /user/username/projects/myproject/tsconfig.json
Info 9 [00:00:32.000] FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"} Project: /user/username/projects/myproject/tsconfig.json WatchType: Config file
CustomRequire:: Resolving myplugin from /a/lib/tsc.js/../../../node_modules
CustomRequire:: Resolving myplugin from /a/pluginprobe1/node_modules
Custom watchFile: /user/username/projects/myproject/tsconfig.json 2000 {"watchFactory":"myplugin"}
Info 10 [00:00:33.000] Config: /user/username/projects/myproject/tsconfig.json : {
"rootNames": [
Expand Down
Loading

0 comments on commit 67d6ee5

Please sign in to comment.