Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape brackets in Wix Script

Tags:

wix

I have a visual studio project named like MyProject(P2P) which works good all the time.

Now I'm using the Wix 3 to pack this project into a MSI, one of the steps is to write the Component node:

  <Component Id="MyProject(P2P).exe" Guid="34565d5d-07d6-495d-a184-bb3bdebe1fb8">
    <File Source="$(var.MyProject(P2P).TargetPath)" KeyPath="yes" />
  </Component>

Now I got the build error of Wix Project:

Product.wxs(807,0): error CNDL0234: Ill-formed preprocessor function '$var.MyProject(P2P).TargetPath'. Functions must have a prefix (like 'fun.'), a name at least 1 character long, and matching opening and closing parentheses.

Looks like need escape the ( and )? and how?

like image 655
Shawn Avatar asked Apr 15 '26 04:04

Shawn


1 Answers

As of WIX 3.11, there is no mechanism to escape parentheses in variable names. The WIX pre-processor should handle this without the need to escape parentheses, but in your case doesn't because of a bug and/or limitations in the pre-processor.

To understand what is going on we need to look at the relevant WIX source file src\tools\wix\PreProcessor.cs, which can be downloaded from https://github.com/wixtoolset/wix3/releases/tag/wix311rtm. In this file, the function PreprocessString() takes a string and attempts to replace pre-processor variables in the form of $(...) with the corresponding definition (I didn't include the source of this function because it is quite long).

Because your variable contains an open parenthesis character, the PreprocessString() function calls EvaluateFunction():

    /// <summary>
    /// Evaluate a function.
    /// </summary>
    /// <param name="sourceLineNumbers">The source line information for the function.</param>
    /// <param name="function">The function expression including the prefix and name.</param>
    /// <returns>The function value.</returns>
    public string EvaluateFunction(SourceLineNumberCollection sourceLineNumbers, string function)
    {
        string[] prefixParts = function.Split(variableSplitter, 2);
        // Check to make sure there are 2 parts and neither is an empty string.
        if (2 != prefixParts.Length || 0 >= prefixParts[0].Length || 0 >= prefixParts[1].Length)
        {
            throw new WixException(WixErrors.InvalidPreprocessorFunction(sourceLineNumbers, function));
        }
        string prefix = prefixParts[0];

        string[] functionParts = prefixParts[1].Split(new char[] { '(' }, 2);
        // Check to make sure there are 2 parts, neither is an empty string, and the second part ends with a closing paren.
        if (2 != functionParts.Length || 0 >= functionParts[0].Length || 0 >= functionParts[1].Length || !functionParts[1].EndsWith(")", StringComparison.Ordinal))
        {
            throw new WixException(WixErrors.InvalidPreprocessorFunction(sourceLineNumbers, function));
        }
        string functionName = functionParts[0];

        // Remove the trailing closing paren.
        string allArgs = functionParts[1].Substring(0, functionParts[1].Length - 1);

        // Parse the arguments and preprocess them.
        string[] args = allArgs.Split(argumentSplitter);
        for (int i = 0; i < args.Length; i++)
        {
            args[i] = this.PreprocessString(sourceLineNumbers, args[i].Trim());
        }

        string result = this.EvaluateFunction(sourceLineNumbers, prefix, functionName, args);

        // If the function didn't evaluate, try to evaluate the original value as a variable to support 
        // the use of open and closed parens inside variable names. Example: $(env.ProgramFiles(x86)) should resolve.
        if (null == result)
        {
            result = this.GetVariableValue(sourceLineNumbers, function, false);
        }

        return result;

Your string consists of three parts separated by the variableSplitter character '.', so the first test 2 != prefixParts.length fails, which results in the exception InvalidPreprocessorFunction thrown.

Further on the function attempts to evaluate the string as a function and if that fails, falls back to evaluating the string as a variable. This works for the example stated in the comment: $(env.ProgramFiles(x86)).

Suggest raising a bug report on the WIX issue bug tracker at https://github.com/wixtoolset/issues/issues.

As a work-around you could use a relative path from the setup project to your application project. For example:

<Component Id="MyProject(P2P).exe" Guid="34565d5d-07d6-495d-a184-bb3bdebe1fb8">
    <File Source="..\Release\MyProject(P2P).exe" KeyPath="yes" />
</Component
like image 75
bradfordrg Avatar answered Apr 19 '26 13:04

bradfordrg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!