octopack msbuild task fails to set version properly with web application that contains nuget packages, because it can’t load the types from the web assembly, and can’t find the gitversion type
I debuged the msbuild task, and it is failing here on line 27 https://github.com/OctopusDeploy/OctoPack/blob/3.6.1/source/OctoPack.Tasks/Util/AssemblyExtensions.cs
This is fairly easy to reproduce with simple console app. try loading the pirmary assembly from an external web project and it will work fine when calling Assembly.GetTypes().
However, do the same thing but add a nuget reference to the web project (like json.net) and it will fail, because Assembly.Load is looking for newtonsoft.json in the msbuild directory and not the web application directory.
Here is the output from the msbuild log
Using “GetAssemblyVersionInfo” task from assembly “C:\git\OctoPack\source\OctoPack.Tasks\bin\Debug\OctoPack.Tasks.dll”.
Task "GetAssemblyVersionInfo"
OctoPack: Get version info from assembly: C:\git\FooBar\Foo.Bar.Web\bin\Foo.Bar.Web.dll
OctoPack: Could load GitVersion information from the assembly at path C:\git\FooBar\Foo.Bar.Web\bin\Foo.Bar.Web.dll.
OctoPack: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at AssemblyExtensions.GetNugetVersionFromGitVersionInformation(Assembly assembly) in C:\git\OctoPack\source\OctoPack.Tasks\Util\AssemblyExtensions.cs:line 27
at OctoPack.Tasks.GetAssemblyVersionInfo.GetNuGetVersionFromGitVersionInformation(String path) in C:\git\OctoPack\source\OctoPack.Tasks\GetAssemblyVersionInfo.cs:line 141
at OctoPack.Tasks.GetAssemblyVersionInfo.UseNuGetVersionFromGitVersionInformation(String path, FileVersionInfo info) in C:\git\OctoPack\source\OctoPack.Tasks\GetAssemblyVersionInfo.cs:line 69
at OctoPack.Tasks.GetAssemblyVersionInfo.CreateTaskItemFromFileVersionInfo(String path) in C:\git\OctoPack\source\OctoPack.Tasks\GetAssemblyVersionInfo.cs:line 56
Done executing task “GetAssemblyVersionInfo”.
Here is the output from the fusion logger for one of the assemblies that fails to load
*** Assembly Binder Log Entry (7/20/2017 @ 12:57:44 PM) ***The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\MSBuild.exe
— A detailed error log follows.=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MSBuild.exe
Calling assembly : (Unknown).LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\MSBuild.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json/Newtonsoft.Json.EXE.
LOG: All probing URLs attempted and failed.
*** Assembly Binder Log Entry (7/20/2017 @ 12:57:44 PM) ***The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\MSBuild.exe
— A detailed error log follows.=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MSBuild.exe
Calling assembly : (Unknown).LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\MSBuild.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/bin/Newtonsoft.Json/Newtonsoft.Json.EXE.
LOG: All probing URLs attempted and failed.