How can I check the existence of an environment variable?
Asked Answered
M

2

11

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?

Modillion answered 17/4, 2012 at 9:44 Comment(1)
Note: <?ifdef MY_ENVIRONMENT_VARIABLE ?> does not work on its own.Modillion
M
23

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

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

This works as expected.

Modillion answered 17/4, 2012 at 10:44 Comment(2)
@Cymon #3460498Illaudable
Thanks, man, your question and your answer just saved me! I understand that WiX toolset source is open, but the documentation has its merits too.Endaendall
A
5
<Condition Message="Missing Environment Variable Message Goes Here"><![CDATA[%envvargoeshere]]></Condition>

Put the above element in the Package element of the wxs file. The installation will fail at runtime (install time) with a nice message if the environment variable does not exist.

Allgood answered 28/10, 2014 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.