Search code examples
wixenvironment-variablesconditional-compilation

How can I check the existence of an environment variable?


The documentation for if/ifdef is slightly confusing. For <?if [expression] ?>, it states:

  • Variables can be used to check for existence
    ...
  • If the variable doesn't exist, evaluation will fail and an error will be raised.

It turns out if you just go: <?if $(env.MY_VAR) ?> and MY_VAR is not defined, compilation will fail. How do I check for existence?

Ordinarily, this is where one would use an ifdef, but these work strangely in Wix too. Instead of using $(var.Variable) syntax, they use <?ifdef Variable?>, meaning environment variables can't be checked this way.

What do I need to do to get the equivalent of normal c pre-processor:

#ifdef MY_ENVIRONMENT_VARIABLE

in Wix?


Solution

  • The correct way to reference environment variables in ifdef sections is:

    <?ifdef env.MY_VAR?>
      ...
    <?endif?>
    

    This works as expected.