Typescript: Implement '--watch' for tsc.exe

Created on 16 Mar 2015  路  24Comments  路  Source: microsoft/TypeScript

error TS5001: The current host does not support the '--watch' option.

I got the really cool watch option working using the typescript spirce cde. but wouldn it be cool to have it working with tsc?

Unactionable Visual Studio

Most helpful comment

I'm not sure why this was closed. Supporting --watch for our tsc.exe host would both be possible and desirable. Right now the limiting factor is that our tsc.exe host is a bit of ugly C++, that uses some ancient COM interfaces for Chakra that we haven't spent much effort on. Our options are:

  1. Implement --watch functionality in that C++ code. Given an appropriate C++ file watcher, this shouldn't be too difficult.
  2. Move to a managed code host and use the rich .Net functionality here. This would be a lot of work, but it would be nice to have a cleaner and more welcoming host for people to work in.
  3. Move to a more modern, native, Chakra host API (i.e. jsrt, and then expose file watching through that as well.

Ideally, we would open source these efforts as well.

All 24 comments

So watch does not work on windows?

  • for making it work on .win too.

Just to be clear, --watch does work on Windows if you're using node.js/io.js, but the tsc.exe program distributed with VS does not support it; you still have Compile on Save for similar functionality anyhow.

As Daniel says, and as I wrote: "The current _host_" does not support the watch. I use node now as a host and come along well. So for me this is not an issue anymore and I think others will be fine as well with node as a host.

I realy do not get what means - "node as a host"

javascript code needs some program that executes it. That is either a browser or node.js or something else, like tsc.exe.

The typescript compiler is tsc.js and you can use it for example by running

tsc.exe
node tsc.js

IN the first case, tsc.exe is a javascript runner, or in other words a "host" for tsc.js . In the second case, node.js is the runner, or "host". Besides executing the js code, the host provides some services like file-watching to the js code. And here is the missing piece: tsc.exe does not implement file watching, while node.js does. So using

node tsc.js -w  mycode.ts

you get file watching while

tsc.exe -w  mycode.ts

will throw

error TS5001: The current host does not support the '--watch' option.

I'm not sure why this was closed. Supporting --watch for our tsc.exe host would both be possible and desirable. Right now the limiting factor is that our tsc.exe host is a bit of ugly C++, that uses some ancient COM interfaces for Chakra that we haven't spent much effort on. Our options are:

  1. Implement --watch functionality in that C++ code. Given an appropriate C++ file watcher, this shouldn't be too difficult.
  2. Move to a managed code host and use the rich .Net functionality here. This would be a lot of work, but it would be nice to have a cleaner and more welcoming host for people to work in.
  3. Move to a more modern, native, Chakra host API (i.e. jsrt, and then expose file watching through that as well.

Ideally, we would open source these efforts as well.

i closed it, because i opened it and found a workaround.
but appreciate your efforts for a watching tsc.exe.

So workaround for winodws is to search tsc.js and make cmd file to start it with node. :)

So workaround for winodws is to search tsc.js and make cmd file to start it with node. :)

No, if you're going to use node, we'd recommend you install TypeScript via npm and make sure that node's tsc comes before Visual Studio's tsc in your PATH.

@DanielRosenwasser : right, this is clean and portable
@gintsgints; yes, this is what I ended up with. since I have several tsc verisons, i brutally hard coded the path getting this ts.cmd:

rem usage: ts app
node X:\typescript\bin\tsc.js -w --sourcemap %1.ts --out js\%1.js

@urbanhop hope you won't mind, I modified your script to be in a code block so it's a little more readable.

thx!

Please add to the "tsc --watch" doc that it is not supported on straight windows hosts but works on node on windows. That would save everyone's time.
Thanks

Would it be possible to get a cmd file that watches the .ts files and recompiles them?
Thanks

@fjanon not really, we'd be better off adapting the host to take on this functionality.

How about this approach around this line:
https://github.com/Microsoft/TypeScript/blob/master/src/compiler/sys.ts#L324

        declare hostSystemSupport: ts.System;

        if (typeof hostSystemSupport !== 'undefined') {
          return hostSystemSupport;
        }
        else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
            return getWScriptSystem();
        }
        else if (typeof module !== "undefined" && module.exports) {
            return getNodeSystem();
        }
        else {
            return undefined; // Unsupported host
        }

So rather than faking WSH or node (both rich and complex apis), your Chakra host will implement exactly what tsc needs in the first place.

And as a side effect it will open the door for easy hosting of tsc in other weird and wonderful scenarios.

Correct me if I'm wrong but if I create a new .ts file into the folder been watched by the --watch option of the tsc compiler, I need to execute again the tsc --watch command in the bash or terminal? It is this behavior by design? This occurs on Mac and also on Windows.

Will be possible to add into the --watch options the functionality to not be necessary to re execute the command each time a new file is added to the project (or folder)?

currently the compiler only adds watches to files. the problem is node does not provide a native directory watcher capabilities. we would have to either do it based on file watchers, which would not scale, or include a timestamp based implementation. we are open to suggestions here if any one has better ideas on how to get this to work.

I do not think this is related to this issue. this issue is to enable --watch for tsc.exe. we should file a different issue for the directory watcher in --w.

I agree with you @mhegazy, that was my impression when I start writing the comment. I will continue the discussion in other issue. Thanks for the feedback and the information about Node.js.

This has been implemented, I think this issue can be closed?

This has been implemented, I think this issue can be closed?

no. this has not been implemented.

This is the solution which works for me. see

Can someone explain to me if tsc.exe does not support --watch, why does Visual Studio Code have a tsc --watch -p build task? Also, tsc doesn't run code. I have no idea what you guys are talking about there. It compiles typescript into javascript. Personally, I'm writing typescript to run in the browser. I don't need node. I just want VS Code to build javascript from my tsconfig.json when I save a typescript file. I'm new to VS Code, but this is a really simple thing for a program to solve. Building it manually works just fine.

Sorry, but I'm really frustrated and finding this command line stuff to be a bit unnecessary.

Since tsc.exe is being discontinued (new versions of Visual Studio and NuGet will be shipping with tsc.js and expect Node to be installed), I think it's appropriate to close this issue. Users will now be able to install Node and get the expected --watch functionality.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  路  3Comments

uber5001 picture uber5001  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments

blendsdk picture blendsdk  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments