-
Notifications
You must be signed in to change notification settings - Fork 652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified the VB.NET Template so that GitVersionInformation is in the Global namespace #2313
Modified the VB.NET Template so that GitVersionInformation is in the Global namespace #2313
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minus the failing dotnet format
check and a potential ILMerge issue, I think this looks good.
@@ -84,44 +84,63 @@ appended to it. | |||
#### Other injected Variables | |||
|
|||
All other [variables](../more-info/variables) will be injected into an | |||
internal static class: | |||
internal static class part of the global namespace similar to this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just thinking, if GitversionInformation
is moved to the global namespace, won't this make ILMerging difficult? If several of the merged assemblies are versioned with GitVersion, we would potentially end up with several colliding GitversionInformation
classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That poses no problem because the generated classes are, and this is very important, internal. An external assembly cannot see these class unless you use reflection, and when using reflection they are resolved with their full name (which happens to be only the class name) against a specific assembly, therefore, no collision.
This trick, I often use with Visual Studio's "shared projects": they are just bunches of code that get "included" into the consuming project. I always make sure classes in these shared projects are internal so that they can be used from any assembly even if these assemblies reference one another.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if this were an issue, it would already have shown with C# or F# generated classes that are already part of the global namespace (and internal).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmh, maybe I spoke too quickly, I missed the bit about ILMerge... Everything being part of the same assembly in the end, it may be an issue. I'll give it a try, and sorry if you thought I was teaching you how visibility worked in .NET! However, my point that the issue already exists if any is still valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, after a quick googling, it appears this indeeds poses problems:
Suppose class A is internal to App, and also to one of the lib (with same namespace). Before merging there will not be any issue. After merging it will become issue to resolve ambiguous reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And with Fody + Costura, everything just works out of the box. This is because, the principle is radically different. The assembies' IL is not merged, instead the dependent assemblies are embedded as resources in the main assembly, then extracted and loaded at runtime (during module initialization). Thus, the assemblies never cease to exist as such and type resolution just works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are interested in witnessing the tests I did, everything is in this repository: https://github.com/odalet/GitVersionTests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And now for ideas on how this whole "code generation" thing could be tweaked:
-
The generated class could be put in a namespace or have a name that will guarantee non-collision. To guarantee this, the class name should be based on the assembly name (and not on some 'default' or root namespace). The class could be inside a namespace named as the assembly or be an inner class inside a class named like the assembly or be a concatenation of assembly name and
GitVersionInformation
... -
A property / command line argument could be provided to let the user choose the name of the generated class.
-
I recently stumbled upon the GitInfo. Its purpose is similar to GitVersion's with the following differences:
- Completely MSBuild-based, no C# code at all
- Seems to have far less feature. It seems to simply expose Git information and does not attempt clever inference of versions.
- There is however one feature that may be interesting here:
It, too, generates code containing version information. However:
- The version information is made available through constants, not properties or fields.
- The file containing this information is generated soon enough for it to be available at "design-time": it can be used without resorting to reflection.
For example, here is what I can write when using GitInfo:
Console.WriteLine($"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Minor}.{ThisAssembly.Git.SemVer.Patch}");
The advantage of this strategy is that because constants are inlined in the calling code, the original type that exposes them is useless at runtime and therefore, collisions on this type are not anymore an issue. And being able to use the version information without reflection also seems more user-friendly.
So, mixing some of these ideas may be the way to go. However, in any case, it is quite some work, and most probably breaking. In the meantime, my experiments with a selection of assembly merging tools show that the collision problem can be mitigated.
It's now up to you to reflect upon all this and decide what or what not to do :)
Hope this helped!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants are a great idea for how we can implement this in a new major version of GitVersion! Whether we'll do this for v6 or not is mainly dependent on timing; if someone submits a pull request implementing this before we go live with v6, it will be released with v6. If not, we would have to postpone it to v7.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generating constants should be rather straightforward. The MSBuild part that generates this soon so that the user can call into it may be a little trickier (I'm not very proficient at MSBuild ;)).
I'll probably come up with an issue that will serve as a discussion and specification of what can be done.
Yes I noticed this dotnet format issue (and same problem with PR #2314), but honestly I don't know what to do with this. It also seems to be related to files I didn't change... |
If you run |
Will do this (but not before the WE). Week days are dedicated to software I'm paid for ;) I'll also experiment with merging assemblies. By the way, I still have one question: do you want me to rebase/squash everything in a single commit that I'd force push or are several commits acceptable? |
👍
Several commits are fine, but if you can avoid merge commits and do |
Got it! |
…Global namespace Updated documentation
79747c5
to
4edfc3a
Compare
Rebasing atop master had the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can get away with calling this a bugfix, even though it may break something for someone.
Thank you so much for your contributions, @odalet! 🙏 |
Merge pull request #2313 from odalet/feature/put-vbnet-code_in-global-namespace Modified the VB.NET Template so that GitVersionInformation is in the Global namespace
Description
GitVersionInfoGenerator.cs
so that it uses different tab lengths for the VB case and the F#/C# cases (this is simply a cosmetic change so that VB output formatting is correct).msbuild-task.md
in the documentation so that:GitVersionInformation
in the assembly's default namespace but in the global namespaceRelated Issue
Fixes #2310
Motivation and Context
Now, the VB version of
GitVersionInformation
belongs to theGlobal
namespace in the same way as its F# and C# counterparts.How Has This Been Tested?
Checklist: