Search code examples
asp.net-mvcgulpsitecore8

Sitecore helix Apply-Xml-Transform replaces entire web.config instead of transforming


Experiencing an issue whereby when my Gulp task runs the XMLTransform task, it's replacing the entire contents of Sitecore application web.config file instead of transforming elements.. Can't see why?

I have 2x web.config.transform files in different projects in my solution, both of which have the XML-Document-Transform namespace that have very little in them so far.. example of one is here:

<?xml version="1.0" encoding="utf-8"?>

<configuration  xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <runtime xdt:Transform="InsertIfMissing">
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xdt:Transform="InsertIfMissing">
      <dependentAssembly xdt:Transform="InsertIfMissing">
        <assemblyIdentity xdt:Transform="InsertIfMissing" name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect xdt:Transform="InsertIfMissing" oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly xdt:Transform="InsertIfMissing">
        <assemblyIdentity xdt:Transform="InsertIfMissing" name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect xdt:Transform="InsertIfMissing" oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly xdt:Transform="InsertIfMissing">
        <assemblyIdentity xdt:Transform="InsertIfMissing" name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect xdt:Transform="InsertIfMissing" oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
      <dependentAssembly xdt:Transform="InsertIfMissing">
        <assemblyIdentity xdt:Transform="InsertIfMissing" name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect xdt:Transform="InsertIfMissing" oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
    </runtime>

<system.codedom xdt:Transform="InsertIfMissing">
<compilers xdt:Transform="InsertIfMissing">
  <compiler xdt:Transform="InsertIfMissing" xdt:Locator="Match(language)" language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
  <compiler xdt:Transform="InsertIfMissing" xdt:Locator="Match(language)" language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>

My gulpfile is pretty much the stock standard Helix one:

gulp.task("04-Apply-Xml-Transform", function () {
var layerPathFilters = ["./src/Foundation/**/*.transform", "./src/Feature/**/*.transform", "./src/Project/**/*.transform", "!./src/**/obj/**/*.transform", "!./src/**/bin/**/*.transform"];
return gulp.src(layerPathFilters)
    .pipe(foreach(function (stream, file) {
        var fileToTransform = file.path.replace(/.+code\\(.+)\.transform/, "$1");
        util.log("Applying configuration transform: " + file.path);
        return gulp.src("./scripts/applytransform.targets")
            .pipe(msbuild({
                targets: ["ApplyTransform"],
                configuration: config.buildConfiguration,
                logCommand: false,
                verbosity: "normal",
                stdout: true,
                errorOnFail: true,
                maxcpucount: 0,
                toolsVersion: config.buildToolsVersion,
                properties: {
                    Platform: config.buildPlatform,
                    WebConfigToTransform: config.websiteRoot,
                    TransformFile: file.path,
                    FileToTransform: fileToTransform
                }
            }));
    }));
});

applytransform.targets file is also the stock helix one:

<Project ToolsVersion="14.0" DefaultTargets="ApplyTransform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(MSBuildToolsVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="ApplyTransform">
    <ItemGroup>
      <Transform Include="$(TransformFile)" />

      <ConfigsToTransform Include="$(FileToTransform)" Condition="Exists(@(Transform))">
        <TransformPath>%(Transform.Identity)</TransformPath>
      </ConfigsToTransform>
    </ItemGroup>
    <Message Text="@(ConfigsToTransform)"></Message>
    <Message Text="@(Transform)"></Message>

    <TransformXml Source="$(WebConfigToTransform)\%(ConfigsToTransform.Identity)"
              Transform="%(ConfigsToTransform.TransformPath)"
              Destination="$(WebConfigToTransform)\%(ConfigsToTransform.Identity)"
              Condition="Exists(@(Transform))"/>
      </Target>
    </Project>

When I run the task the output all looks normal, however the resultant web.config file in my website root contains ONLY what is in the two transform files and I lose all the regular application configuration settings..

Anyone know what's up?


Solution

  • Turns out my issues were two-fold.

    1. I didn't look closely enough at the Sitecore.Foundation.Installer Project which I thought was being used only for Installing dummy XDB and Reporting data but in actual fact contains some of the logic for the transformation tooling in it.
    2. I had added some new projects to foundation/feature layers and it turns out that by default a web.config file when you right-click -> View Properties has it's "Build Action' set to "content" which results in this file being put in the output directory that is ultimately published to the web root directory. This should be set to 'None' and 'Copy to Output Directory' set to "Do not copy'.