Can extra options be specified via an environment variable?
-
Monday, April 23, 2012 7:15 PM
I want to be able to pass cache settings via an environment variable.
Given
CodeContractsExtraAnalysisOptions=-cache -cacheServer localhost
I've tried using
- %CodeContractsExtraAnalysisOptions%, this generates an error
- $CodeContractsExtraAnalysisOptions, this doesn't seem to do anything.
Is this possible? If so, how?
All Replies
-
Monday, April 23, 2012 9:04 PMOwner
Hello,
no, at the moment we do not have this feature.
thanks
f
- Proposed As Answer by Francesco LogozzoMicrosoft Employee, Owner Monday, April 23, 2012 9:04 PM
-
Tuesday, April 24, 2012 12:51 AM
Hi,
MSBuild supports environment variables with the following syntax:
$(EnvVarName)
I don't know whether you'll be able to set this option in the properties window, but you should be able to edit the project file manually. It should look something like this:
<CodeContractsExtraAnalysisOptions>$(CCAnalysisOptionsEnvVar)</CodeContractsExtraAnalysisOptions>
- Dave
-
Tuesday, April 24, 2012 9:50 AM
As Francesco said it doesn't have this feature.
When I try I get this
CodeContracts: MyClass: Cannot load assembly '$(CCAnalysisOptionsEnvVar)'
-
Tuesday, April 24, 2012 3:09 PM
Hi,
I just tested it myself and it works fine as long as you edit the project file manually, as mentioned previously.
Editing the Extra Static Checker Options field in the properties window doesn't work if you do it directly because it XML-encodes the value in the project file.
Here's what I did to get it working:
- Add a new environment variable with the name CCAnalysisOptionsEnvVar
and specify an appropriate value; e.g., I used the following value:
-show progress -outputwarnmasks - Open your project file in a text editor; e.g., right-mouse click the project in Solution Explorer and select Unload, then right-mouse click again and select Edit.
- Find the CodeContractsExtraAnalysisOptions element within the PropertyGroup that contains the desired
Configuration and Platform settings and then update it as follows:
<CodeContractsExtraAnalysisOptions>$(CCAnalysisOptionsEnvVar)</CodeContractsExtraAnalysisOptions>
- Restart Visual Studio. This is required to make sure that the new environment variable is read into the process.
Now if you open the properties window you'll see that the value from the environment variable is shown in the Extra Static Checker Options field instead of the variable name. This is because MSBuild evaluates the variable before passing it to the CC extension.
Warning: Editing other CC settings in the properties window may cause you to lose all manual changes you've made within the project file.
It'd be great if CC preserved manual settings instead of overwriting them when saving changes to other settings.
- Dave
- Proposed As Answer by Dave Sexton Tuesday, April 24, 2012 3:09 PM
- Edited by Dave Sexton Tuesday, April 24, 2012 3:12 PM Small clarification
- Add a new environment variable with the name CCAnalysisOptionsEnvVar
and specify an appropriate value; e.g., I used the following value:
-
Thursday, April 26, 2012 10:44 AM
Hi Dave,
thanks for that explaination, very informative. I didn't know that VS would evaluate the env var in that way.
Much appreciated.

