Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
MSBuild wpp.targets file to fix the long path in web packages from Visual Studio
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PackagePath Condition=" '$(PackagePath)'=='' ">website</PackagePath>
<EnableAddReplaceToUpdatePacakgePath Condition=" '$(EnableAddReplaceToUpdatePacakgePath)'=='' ">true</EnableAddReplaceToUpdatePacakgePath>
<PackageDependsOn>
$(PackageDependsOn);
AddReplaceRuleForAppPath;
</PackageDependsOn>
</PropertyGroup>
<Target Name="AddReplaceRuleForAppPath" Condition=" '$(EnableAddReplaceToUpdatePacakgePath)'=='true' ">
<PropertyGroup>
<_PkgPathFull Condition=" '$(WPPAllFilesInSingleFolder)'!='' ">$([System.IO.Path]::GetFullPath($(WPPAllFilesInSingleFolder)))</_PkgPathFull>
<_PkgPathFull Condition=" '$(_PkgPathFull)' == '' ">$([System.IO.Path]::GetFullPath($(_PackageTempDir)))</_PkgPathFull>
</PropertyGroup>
<!-- escape the text into a regex -->
<EscapeTextForRegularExpressions Text="$(_PkgPathFull)">
<Output TaskParameter="Result" PropertyName="_PkgPathRegex" />
</EscapeTextForRegularExpressions>
<ItemGroup>
<MsDeployReplaceRules Include="replaceFullPath">
<Match>$(_PkgPathRegex)</Match>
<Replace>$(PackagePath)</Replace>
</MsDeployReplaceRules>
</ItemGroup>
</Target>
</Project>

While this does substantially shorten the paths in the web package file, it currently breaks connection string substitution in the Web.config file.

When running msdeploy.exe with the "-verbose" switch and a web package created with shortened paths (i.e. using the additional MSBuild file listed above), the following message is emitted:

Verbose: Parameter entry 'AdventureWorks-Web.config Connection String/1' could not be applied anywhere.

Consequently, the Web.config file contains something like:

<connectionStrings>
  <add name="AdventureWorks"
    connectionString="$(ReplacableToken_AdventureWorks-Web.config Connection String_0)" />
</connectionStrings>

When running msdeploy.exe with the "-verbose" switch and a web package without shortened paths, the following message is emitted:

Verbose: Parameter entry 'AdventureWorks-Web.config Connection String/1' is applicable to 'C:\NotBackedUp\AdventureWorks\Portal\Main\src\Web\obj\Debug\Package\PackageTmp\Web.config' because of its scope.

...and the connectionString attribute in the Web.config file is replaced as expected.

Owner

sayedihashimi commented Dec 16, 2015

@jeremy-jameson thanks for letting me know. I don't see why this would cause that behavior. Do you have any ideas?

Can you make the sample packages you created available so I van download them? That will simplify my investigation.

kenlyon commented Aug 4, 2016

The cause of the problem is that in the parameters.xml file, the scope attribute of the parameterEntry element still contains the long path to the original location, so it expects the web.config file to still be there. I have not tried it yet, but I believe changing the scope attribute to website\\Web\.config$ would fix it.

Is it intentional that the EnableAddReplaceToUpdatePacakgePath has Pacakge instead of Package..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment