Creating environment variables in a Project with merge modules fails.

Dec 5, 2016 at 8:33 PM
I'm trying to create an environment inside of an msi that contains several merge modules. For some strange reason though, adding an environment variable to the msi causes the following error:
Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.First[TSource](IEnumerable1 source)
at WixSharp.Compiler.GetTopLevelDir(XElement product)
at WixSharp.Compiler.ProcessEnvVars(Project wProject, Dictionary
2 featureComponents, List`1 defaultFeatureComponents
, XElement product)
at WixSharp.Compiler.GenerateWixProj(Project project)
at WixSharp.Compiler.BuildWxs(Project project, String path, OutputType type)
at WixSharp.Compiler.BuildWxs(Project project, OutputType type)
at WixSharp.Compiler.Build(Project project, String path, OutputType type)
at WixSharp.Compiler.Build(Project project, OutputType type)
at WixSharp.Compiler.BuildMsi(Project project)
at WixSharp.Project.BuildMsi(String path)
at BlastZone.Program.Main(String[] args) in C:\Users\user\Documents\Visual Studio 2015\Projects\BlastZone\BlastZone
\Program.cs:line 25
I wrote a small program to test it. This one works just fine.
static void Main(string[] args)
        {
            var feature = new Feature("somename", "somedescription");
            var dir = new Dir(@"%ProgramFiles%\somedir",
                new Merge(feature, @"D:\path\mymsm.msm"));
            var project = new Project("testproject",
                dir); 

            project.Platform = Platform.x64;
            project.PreserveTempFiles = true;
            project.BuildMsi();
        }
However, if I do this instead:
var project = new Project("testproject",
                dir, 
                new EnvironmentVariable("foo", "bar"));
My program falls over. Any idea what's causing this, and how can I fix it?
Dec 7, 2016 at 5:09 AM
It's hard for me to comment your code as it is incomplete. I can only advise you to start with the working complete sample from the distro. My very simple test (below) showed no problems with combining Merge and EnvironmentVariables:
class Script
{
    static public void Main(string[] args)
    {
        var featureA = new Feature("Feature A", "Feature A description");
        var featureB = new Feature("Feature B", "Feature B description");
        var complete = new Feature("Complete");

        complete.Add(featureA)
                .Add(featureB);

        var project =
                new Project("MyMergeModuleSetup",
                    new Dir(@"%ProgramFiles%\My Company",
                        new File(featureA, @"Files\MainFile.txt"),
                        new Merge(featureB, @"Files\MyMergeModule.msm"),
                        new Merge(featureB, @"Files\MyMergeModule1.msm")),
                    new EnvironmentVariable("foo", "bar"));

        project.DefaultFeature = complete;
        project.UI = WUI.WixUI_FeatureTree;
        project.InstallerVersion = 200; //you may want to change it to match MSM module installer version

        project.PreserveTempFiles = true;

        project.BuildMsi();
    }
}
You may use GitHub (https://github.com/oleg-shilo/wixsharp) for further discussions.