Build/Test Issues

NCrunch and GitVersion

Started by Tim Long on 4,119 views

I'm using GitVersion to automatically version my code. Specifically, I use the GitVersionTask NuGet package that installs an MSBuild task to extract the version information during every build. One of the things this task does is to "inject" a static class called GitVersionInformation. I access this in my code for things like displaying the version string in the About box and emitting version strings to the log file. In fact, the first thing my code does is write out some version strings using NLog:


        [STAThread]
        private static void Main(string[] args)
            {
            // Manage unhandled exceptions
            Application.ThreadException += UnhandledThreadException;
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            Log.Info("Git Commit ID: {fullCommit}", GitVersionInformation.Sha);
            Log.Info("Git Short ID: {shortCommit}", GitVersionInformation.ShortSha);
            Log.Info("Commit Date: {commitDate}", GitVersionInformation.CommitDate);
            Log.Info("Semantic version: {semVer}", GitVersionInformation.SemVer);
            Log.Info("Full Semantic version: {fullSemVer}", GitVersionInformation.FullSemVer);
            Log.Info("Build metadata: {buildMetadata}", GitVersionInformation.FullBuildMetaData);
            Log.Info("Informational Version: {informationalVersion}", GitVersionInformation.InformationalVersion);
            // [other stuff elided for clarity]
            }


Visual Studio is happy with this, and the code builds without issues - but not in NCrunch. In NCrunch, I get this:


LocalServer.cs (99, 53): 'GitVersionInformation' is inaccessible due to its protection level
LocalServer.cs (100, 53): 'GitVersionInformation' is inaccessible due to its protection level
LocalServer.cs (101, 51): 'GitVersionInformation' is inaccessible due to its protection level
LocalServer.cs (102, 52): 'GitVersionInformation' is inaccessible due to its protection level
LocalServer.cs (103, 61): 'GitVersionInformation' is inaccessible due to its protection level
LocalServer.cs (104, 57): 'GitVersionInformation' is inaccessible due to its protection level
LocalServer.cs (105, 71): 'GitVersionInformation' is inaccessible due to its protection level


Protection level? Probably not correct. It's a static internal class and Visual Studio is perfectly happy to compile that code. I guess there's some subtlety I'm not seeing about the MSBuild task. Any advice?

Thanks,
Tim

Edited

Hi Tim,

Thanks for sharing this issue.

Does setting your 'Instrument Output Assembly' setting to 'False' for the assemblies involved make any difference here?

Are you making use of InternalsVisibleToAttribute anywhere in your code?
You can fix this with with disabling everything GitVersion related in your Project File.



  <PropertyGroup Condition="'$(NCrunch)' == '1'">
    <WriteVersionInfoToBuildLog>false</WriteVersionInfoToBuildLog>
    <UpdateAssemblyInfo>false</UpdateAssemblyInfo>
    <GenerateGitVersionInformation>false</GenerateGitVersionInformation>
    <GetVersion>false</GetVersion>
    <GenerateGitVersionWixDefines>false</GenerateGitVersionWixDefines>
    <UpdateVersionProperties>false</UpdateVersionProperties>
  </PropertyGroup>


I opened an issue on GitHub for GitVersion.Task https://github.com/GitTools/GitVersion/issues/1723

Edited

Since Version 5.0.0-beta5 you can use


  <PropertyGroup Condition="'$(NCrunch)' == '1'">
    <DisableGitVersionTask>true</DisableGitVersionTask>
  </PropertyGroup>


in the project file to disable the gitversiontask if needed.
I can't get this to work...

I'm using this:
<PropertyGroup Condition="'$(NCrunch)' == '1'">
    <DisableGitVersionTask>true</DisableGitVersionTask>
</PropertyGroup>


It's a .NET Standard 2.0 dll using the new project format (or actually, several) and none of them work in NCrunch. It's as if $(NCrunch) isn't defined in the build. If I set it unconditionally it works but that's no good.
Remco wrote:Could you try specifying this value using the Custom Build Properties configuration setting?


I tried to set NCrunch=1 in Custom build properties but that didn't work. I tried to add IsNCrunch = true and use that instead but that didn't work.

What *did work* however was to set DisableGitVersionTask = true in custom build properties. I'm not clear on what the difference is effectively.

Edit: that's a better solution anyway as I check in NCrunch settings anyway.

Edited

Remco wrote:
What *did work* however was to set DisableGitVersionTask = true in custom build properties. I'm not clear on what the difference is effectively.


Sorry, I should have been more specific ... this was what I was trying to suggest doing :)

It's a bit concerning that the NCrunch environment variable isn't available in your build conditions. Something must be blocking this from your build system. Regardless, the Custom Build Properties can be used for overriding anything of this sort, and is probably a cleaner option.

Post a reply

Log in to reply.