7

I'm trying to start an instance of grunt watch whenever a particular project is run from VisualStudio. I have a single page-app written in ember, and compiled with grunt, which connects to a backend written with WebAPI. I'm trying to reduce friction so it's possible to Start Debugging (F5) in VS and have it get everything up and going.

If I add a post-build event to compile the app with grunt, it works fine:

node node_modules\grunt-cli\bin\grunt dev --no-color

grunt watch never terminates, so the VisualStudio build appears to hang until you terminate the node.exe process (which is mostly expected, other than not being able to use Ctrl+Break to stop in VS):

node ../node_modules\grunt-cli\bin\grunt watch --no-color

I've tried starting with the start command, but VisualStudio still waits for it to exit (just saying "Build started..."):

start node ../node_modules\grunt-cli\bin\grunt dev --no-color

I've also tried with start's /i parameter, but that doesn't help. It opens a new window running grunt watch, but the build doesn't continue (and the app doesn't start) until I close the console window.

enter image description here

Presumably this has something to do with the process being a child of the build process? Is there an actual way to start a background task without VisualStudio waiting on it?

gregmac
  • 24,276
  • 10
  • 87
  • 118

1 Answers1

6

Not sure why exactly start doesn't do the trick (works perfectly from the command line), but afaik msbuild spawns a seperate cmd process for it's build events so it will have something to do with that.

A working alternative is to use Powershell to start the process instead. No idea about the builtin powershell syntax, but invoking C#'s Process.Start works just as fine:

powershell -Command "[System.Diagnostics.Process]::Start( '/path/to/node', 'args' )"

That answers your question, however I think it's not what you really want, and you're asking the wrong question.. You say you want to 'start an instance whenever a particular project is run from VisualStudio', but then you go on asking about build events which occur when a project is built. That is different and seems unhandy since every single build will start a new instance. Instead, I think what you actually want/need is to start an instance everytime you start debugging your project. That's also possible as laid out here:

  • add an empty project to your solution
  • enter your node command under the projects' Properties->Debugging
  • right-click solution, select Set Startup Project
  • select Multiple startup projects
  • set Action to Start for your main project
  • set Action to Start without debugging for the empty project

Now hit F5 and VS will start node, plus start debugging your project.

Community
  • 1
  • 1
stijn
  • 34,664
  • 13
  • 111
  • 163
  • Can you elaborate what you mean by "enter your node command under the projects' `properties->Debugging`? – Jared Beach Apr 21 '16 at 16:02
  • 1
    @JaredBeach the command which starts node, like in the question, something like `node ../node_modules\grunt-cli\bin\grunt watch --no-color` – stijn Apr 21 '16 at 16:05
  • Is there a way to get it to close when you stop debugging? Under "Start external program, I have "C:\Windows\System32\java.exe" and under Command line arguments, I have "-jar fakeSMTP-2.0.jar -s". It doesn't close when I stop debugging though – Jared Beach Apr 21 '16 at 16:10
  • Nevermind. I switched from a console app project to a winform and that fixed it.... Don't have a clue why – Jared Beach Apr 21 '16 at 16:21