Skip to content

"tsc -w" will compile and watch, then how to run tsc in "watch-only" mode without compiling? #12996

Closed

Description

@yaooluu

TypeScript Version: 2.0.3

Hi, I'm finding it confusing when using "tsc -w". From angular quickstart guide and almost every tutorial, I see similar things in package.json as the line below:

"start": "tsc && concurrently \"tsc -w\" \"lite-server\" "

Expected behavior:
The command should compile only once, and then "watch" and "start lite-server" at the same time.

Actual behavior:
The command compiles once first. Then "compile and watch" and "start lite-server" at the same time. The actual result is it compiled twice.

I'm finding this annoying especially when running with karma (which watches for JS file changes).

"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\""

From the above code, when karma start karma.conf.js runs, tests start running. At the same time (due to concurrently), "tsc -w" starts running and fires another round of TS compilation, which changed the output JS files. Then Karma detected these JS file changes, stopped the currently running (partially or finished) tests, and started re-run the tests again.

I've only find the tsc documentation explaining "tsc -w" as "Run the compiler in watch mode. Watch input files and trigger recompilation on changes.".
https://www.typescriptlang.org/docs/handbook/compiler-options.html

From my understanding, what we wanted from "tsc && concurrently 'tsc -w' 'do-something' " is compile && (watch and do-something concurrently). But we actually got "compile && (compile-then-watch and do-something concurrently. Do we have any options to make "tsc -w" only watch, but don't compile?

Activity

  1. RyanCavanaugh commented on Dec 21, 2016

    @RyanCavanaugh
    Member

    I suppose we could add this, but it'd need to be well-hidden enough that people wouldn't accidently use it and think TS was broken for not compiling the first time around.

    I'm assuming they wrote their script that way so that lite-server didn't launch and not see the .js file and panic. It seems like the "right" fix here is to only invoke the first tsc conditionally on the target output file not existing. Adding more switches to tsc to support a scenario like this is perhaps fixing the problem in the wrong place.

  2. mhegazy commented on Dec 21, 2016

    @mhegazy
    Contributor

    currentlly tsc will update the file if it has changed since the last time it wrote it. we can make it so that it reads the output file the first time if it exists.

  3. yaooluu commented on Dec 24, 2016

    @yaooluu
    Author

    I think the fact that "tsc && concurrently \"tsc -w\" \"lite-server\" " actually compiles the same code twice is already inefficient due to how tsc -w behaves (compile and watch).

    All I'm looking for is something like tsc -w --no-initial-compille.

  4. emmanuelbuah commented on Apr 17, 2017

    @emmanuelbuah

    I run into this as well. It's useful to be able to compile or watch or compile + watch. tsc -w --no-initial-compile should solve the issue or breakout -w to only setup watch (no compilation) and only compile on future changes.

  5. pushfactory commented on Jul 16, 2017

    @pushfactory

    Couldn't we just remove the first 'tsc' from that line of code?
    "start": "concurrently \"tsc -w\" \"lite-server\" "

  6. avbentem commented on Jul 17, 2017

    @avbentem

    But then, mhvj, it might still be compiling when the server is already started?

  7. amir-arad commented on Jul 22, 2017

    @amir-arad

    IMO the essential features here are:

    • start another process when the initial transpilation is done and watch mode begins.
    • not touch the target files more than once during that process (unless the source actually changed)

    I'm affected by this usually when chaining watchers for dev-mode over a complex multy-project workspace. for instance several typescript watchers that produce dependencies for each other, and a webpack-dev-server that bundles all their target files whenever any of them change.

  8. vyorkin commented on Sep 17, 2017

    @vyorkin

    TL;DR: This is exactly the same problem Yao Lu (@yaooluu) described above

    It'll be great to have this, could be useful for monorepos (e.g. managed by lerna), where you have multiple packages that you want to test (I mean cross-package / integration testing).
    In my case to accomplish this and do TDD I have to watch and rebuild (tsc -w) my packages constantly, at the same time I run jest in a watch-mode. The problem is that when I ran these two tasks in parallel jest is usually starts faster so I have to run just tsc to build dependency sub-packages first, and only then I can run tsc -w and jest in parallel. Thats why I have to run tsc two times.

  9. vyorkin commented on Sep 18, 2017

    @vyorkin

    if understand correctly, to start working on this and have a chance that my PR will be accepted this feature needs to

    be approved (marked as "Milestone == Community" by a TypeScript coordinator with the message "Approved") in the suggestion issue

    according to CONTRIBUTING

    what should be done to make it approved? does it need just more feedback or I can start working on it, submit a PR and only then it could be approved?

  10. RyanCavanaugh commented on Sep 19, 2017

    @RyanCavanaugh
    Member

    Mohamed Hegazy (@mhegazy) thoughts on taking a PR for a flag to skip the initial watch cycle?

  11. itsUndefined commented on Nov 17, 2017

    @itsUndefined

    This needs to be done.

  12. mhegazy commented on Nov 17, 2017

    @mhegazy
    Contributor

    Sure, we would consider a PR for it.

  13. avbentem commented on Dec 5, 2017

    @avbentem

    Just as an aside, I'm using https://github.com/gilamran/tsc-watch which extends the built-in compiler with an --onSuccess option, which can then be used like:

    "start": "tsc-watch --project . --outDir ./dist --onSuccess \"nodemon ./dist/bin/www.js\""

    (Another aside: it seems the above does not work with projects in subfolders, such as "tsc-watch --project ./server --outDir ./server/dist --onSuccess \"nodemon ./server/dist/bin/www.js\"", for which the first compilation is fine, but compilation results of changes do somehow not end up in ./server/dist.)

  14. 17 remaining items

  15. ericbf commented on Mar 22, 2019

    @ericbf
    Contributor

    Any update here?

  16. dziegelbein commented on Apr 26, 2019

    @dziegelbein

    I created a package called runmon to work around this very issue.

  17. sheetalkamat commented on Apr 26, 2019

    @sheetalkamat
    Member

    --incremental should allow you to build incrementally all the time.

  18. pronebird commented on Apr 26, 2019

    @pronebird

    Sheetal Nandi (@sheetalkamat) can you elaborate how that enables us to use tsc -w directly without using tsc-watch?

  19. sheetalkamat commented on Apr 30, 2019

    @sheetalkamat
    Member
  20. kirill-konshin commented on May 4, 2019

    @kirill-konshin

    Sheetal Nandi (@sheetalkamat) perfecto, seems to be working! tsc && tsc -w runs much faster.

  21. pronebird commented on May 4, 2019

    @pronebird

    Sheetal Nandi (@sheetalkamat) now I got it. Thank you!

  22. lukywong commented on May 31, 2019

    @lukywong

    +1 any update on this?

  23. RyanCavanaugh commented on Aug 19, 2019

    @RyanCavanaugh
    Member

    I think --incremental actually addresses this to a high enough degree - with that flag turned on, the next watch invocation after a full build will be effectively free. Please do comment (with specifics) if you disagree and we can re-evaluate

  24. michaelfarrell76 commented on Aug 23, 2019

    @michaelfarrell76

    I disagree with the above statement. here is the workflow I have setup:

    I am running a node server inside of a docker container with the folder structure

    /src (ts files)
    /build (js files)
    

    I want to run a single start command inside of the docker container, i.e.

    npm run start
    

    with the following package.json scripts:

    "build": "tsc && copy ./src/{**/*.hbs,**/*.json,**/*.png} ./build",
    "start": "npm run build && concurrently 'npm run build:watch' 'npm run server:start'",
    "server:start": "nodemon --delay 2 -e '*' --watch build 'node build/server.js'",
    "build:watch": "tsc --watch-only"
    
    

    with this setup, i have a tsc watching for any files changes in src and rebuilding them into build, simultaneously i have nodemon restarting the node server whenever the build folder changes.

    On start, I have to perform a fresh build and copy since tsc watch does not allow for post-compile scripts to run (such as copying over non-ts files), and then I have tsc:watch setup to rebuild, and thus restart the server anytime ts files change.

    What tends to happen here is that my server will startup right away, but then since build:watch always performs a rebuild, this causes my server to autoreload everytime I start it up. This is quite annoying since the app will work, and then suddenly a network error is thrown after the ~3 second tsc rebuild. Since the server startup takes about 3-5 seconds, this makes the server start time go from 3 seconds to about 10-15 seconds everytime the docker image is booted.

    This was so annoying that I had to start my server with two commands and i just run build:watch outside of the docker container. it;s quite easy to forget to run both commands and really tricky bugs have come out of just not having the build:watch command running in another window.

    This workflow could be fixed by either allowing:

    • a command to be run every time tsc rebuilds
    • allowing watch-only mode so there is optionally no initial re-compilation
  25. pronebird commented on Aug 23, 2019

    @pronebird

    Michael Farrell (@michaelfarrell76)

    watch-only mode so there is optionally no initial re-compilation

    Try --incremental, it does exactly that, i.e it won't rebuild the same stuff all over again. You should use task automation if you need to copy files or do any other kind of pre-processing, expecting tsc to do this makes no sense.

    a command to be run every time tsc rebuilds

    Some way of knowing when tsc rebuilds files is a good idea. I use tsc-watch for that, but it parses the CLI output which is not optimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions