1

When we are running our build through MSBuild, what context does the MSBuild run in/under?

The reason I am asking is because I have pre-build event that calls a command line program (ajax minification) that works perfectly if the project is build through VS2010 (on the same machine), but does not work when our build system (CCNet) is building it. The error i get is 9009, which means that its cant find the file. The command line program has a environment path set for its location (environment path is set through setx, maybe its only set for current user?), so calling only its name works perfectly in all cases other then when CCNet calls MSBuild.

BlueChameleon
  • 924
  • 1
  • 10
  • 36
  • The prebuild event is executed by the MSBuild Exec class. Which runs the commands in a hidden instance of cmd.exe. The working directory is set to the project's output directory, the initial environment is inherited from the parent process. Do *not* use SETX, that will not modify the environment. Use SET. Use PATH to alter the path. – Hans Passant Feb 12 '13 at 22:08
  • Can you explain where should i use SET PATH? I used SETX once after I installed the ajax minification program. How would i use SET PATH in the pre-build event? – BlueChameleon Feb 13 '13 at 14:41
  • 1
    Just put it in your pre-build event: set path=c:\foo\bar;%path%. – Hans Passant Feb 13 '13 at 14:45
  • Just tried this and it works. The only problem is that im hardcoding the path in my project files. Is there any way I can set path and take path from the global environment path variables that are already there (the ones that have been set by SETX before) – BlueChameleon Feb 13 '13 at 15:40
  • I'm running out of ideas on how to tell you to stop using SETX. It updates the registry, not the environment. – Hans Passant Feb 13 '13 at 15:52
  • Then how can i set the environment path once after i install the JS minifer? I dont want to hardcode this path in my build events, i just want to call jsmin.exe from build event and it should know (from environment) where to find it. As i understand SET PATH is only for current process. Once the process finished the environment path disapaers. – BlueChameleon Feb 13 '13 at 16:07

1 Answers1

1

MSBuild is just like any other development tool, it runs under whatever user context launched it.

When you run msbuild via Visual Studio, it runs as you, because your login context ran Visual Studio, which in turn ran the build.

If you run it through some build service, the answer will depend on how that build service operates. TFS Build, for example, launches MSBuild as whatever user you have configured for the login credentials for the TFS Build service. If you have configured CC.NET to run as a service, then you've also had to specify which user it's running as. That's who MSBuild will also be running as.

If you need a specific environment set up for the build, you have a few options. The easiest one, though very much a "giant hammer" approach, is to globally configure the environment on the build machine for all users. Other options include running some kind of pre-configuration step in your build, or write a startup batch file to launch the service manually after setting up the environment.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117