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

Allow --watchFactory option that uses user specified plugin for watching files and directories #54012

Closed
wants to merge 6 commits into from

Conversation

sheetalkamat
Copy link
Member

@sheetalkamat sheetalkamat commented Apr 25, 2023

With this change we allow users to use custom watchFactory plugin either on command line or in editor
For plugin writers the plugin is expected to be module that returns the factory with methods to watchFile and watchDirectory

    type UserWatchFactoryModule = (mod: {
        typescript: typeof ts;
    }) => UserWatchFactory;
    interface UserWatchFactoryCreateInfo {
        options: WatchOptions;
        config: any;
        host: WatchHost;
        solution?: SolutionBuilder<BuilderProgram>;
        watch?: WatchOfConfigFile<BuilderProgram> | WatchOfFilesAndCompilerOptions<BuilderProgram>;
    }
    interface UserWatchFactoryCreateInfo {
        session?: ts.server.Session<unknown>;
    }
    interface UserWatchFactory {
        create(createInfo: UserWatchFactoryCreateInfo): void;
        watchFile?(fileName: string, callback: FileWatcherCallback, pollingInterval: number, options: WatchOptions | undefined): FileWatcher;
        watchDirectory?(fileName: string, callback: DirectoryWatcherCallback, recursive: boolean, options: WatchOptions | undefined): FileWatcher;
    }
    interface UserWatchFactory {
        onConfigurationChanged?(config: any): void; // Called if running with vscode as plugin
    }

This behaves similar to language service plugin as in user can have this set in tsconfig.json in watchOptions as watchFactory, pass it on command line during batch compilation or set in vscode's global watchOptions

From commandline the watchFactory options is honored only if running with passing --allowPlugins flag.

watchFactory option can either be module name to look for or it can be PluginImport which is object literal with property name that is resolved as name and the object literal is passed on to the factory.

The changes in PR include:

  • Adding option watchFactory in watchOptions that takes in either string or object literal. The name of the factory needs to be package name to resolve (just like our LS plugins`
  • During watchFile or watchDirectory we look for this resolved module and use the specific function if available.
  • On command line we look for the module next to configFile, currentDirectory or typescript we are running from
  • In editor it looks for paths just like it would find LS plugins from (eg. depending on options, custom locations, executing typescript peer module or next to tsconfig.json)
  • In editor we also support calling onConfigurationChanged similar to we call for LS plugins when editor uses protocol to do so.

Here are some of the prototypes for watchFactory plugins:

  1. Watching using parcel watcher: https:/sheetalkamat/typescript-parcel-watcher
    With this you can run tsc --watchFactory typescript-parcel-watcher or set typescript-parcel-watcher as option in the vscode's watchOptions preferences

  2. If using session events and custom command in vscode Changes to vscode to support watchFactory sheetalkamat/vscode#1
    You would need the plugin and updated vscode version per PR so that plugin registers custom commands and uses events to communicate through tsserver. You would also need to set watchOptions in vscode preferences to typescript-vscode-watcher
    image

  3. Watching using vscode extension and plugin https:/sheetalkamat/vscode-tsserver-watcher-plugin
    Here the watching happens by creating a separate server connection between plugin and extension to do the vscode native watching. For this you would need extension, plugin and to set vscode-tsserver-watcher-plugin as watchOptions preferences in vscode. Note that this would also set the plugin as global plugin (which would in turn fail as global plugin since it does not return the LS in as part of create call. Vscode could make watchFactory extension take a flag in contributes settings that doesnt pass the plugin as global plugin as well. But this is just prototype so.
    image

@typescript-bot
Copy link
Collaborator

Thanks for the PR! It looks like you've changed the TSServer protocol in some way. Please ensure that any changes here don't break consumers of the current TSServer API. For some extra review, we'll ping @sheetalkamat, @mjbvz, @zkat, and @joj for you. Feel free to loop in other consumers/maintainers if necessary

@sheetalkamat sheetalkamat force-pushed the watchFactory branch 7 times, most recently from 8bfc8c4 to 4f05278 Compare April 26, 2023 21:08
@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels May 1, 2023
@sheetalkamat sheetalkamat changed the title Watch factory Allow --watchFactory option that uses user specified plugin for watching files and directories May 1, 2023
@sheetalkamat sheetalkamat marked this pull request as ready for review May 1, 2023 21:03
@sheetalkamat
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 2, 2023

Heya @sheetalkamat, I've started to run the tarball bundle task on this PR at 4ca063f. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 2, 2023

Hey @sheetalkamat, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/153693/artifacts?artifactName=tgz&fileId=26EF50313EF6DC11AE5939709DA47D6DF2E2C014E54C4C193726E4789CA04AC702&fileName=/typescript-5.1.0-insiders.20230502.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@sheetalkamat
Copy link
Member Author

Since #54662 is not merged, closing this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants