Skip to content

Version.txt should use file version from assembly custom attributes not FileVersionInfo #4505

Closed

Description

@r0ss88

The changes made in issue #3132 have introduced a bug due to relying on a physical location for the assembly. If the Microsoft.ML.Core assembly is loaded from memory the assembly.location will be empty. Using FileVersionInfo.GetVersionInfo relies on a physical path - so an argument exception is thrown inside System.IO.Path since the supplied path is empty.

var versionInfo = FileVersionInfo.GetVersionInfo(typeof(RepositoryWriter).Assembly.Location);
using (var ent = rep.CreateEntry(DirTrainingInfo, "Version.txt"))
using (var writer = Utils.OpenWriter(ent.Stream))
writer.WriteLine(versionInfo.ProductVersion);

Instead of using FileVersionInfo.GetVersionInfo the assembly custom attributes should be used, for example:
var productVersion = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(AssemblyFileVersionAttribute)).ConstructorArguments.First();

This will return the same product string version as the FileVersionInfo.GetVersionInfo does.

Activity

  1. codemzs commented on Nov 30, 2019

    @codemzs
    Member

    thanks @r0ss88 . This seems like a good find but in what scenarios can we be loading this assembly from memory and it will not be present on disk?

  2. r0ss88 commented on Dec 2, 2019

    @r0ss88
    ContributorAuthor

    thanks @r0ss88 . This seems like a good find but in what scenarios can we be loading this assembly from memory and it will not be present on disk?

    Happy to contribute. In my scenario I am working with a system where assemblies can be loaded from disk or stored in a database. Though in this case they are loaded from disk the assembly is read as bytes then loaded - hence location is blank.

  3. r0ss88 commented on Jan 2, 2020

    @r0ss88
    ContributorAuthor

    @codemzs happy new year, any update?

  4. sharwell commented on Jan 2, 2020

    @sharwell
    Contributor

    @codemzs FileVersionInfo is not correct either way. This should be using the AssemblyInformationalVersion attribute (when present). If you use GitVersioning to generate the assembly attributes, it will produce an internal static class that you can reference directly for any information you need. Other version generation tools may do something similar. Otherwise, an approach like I used for ANTLR's code generator can be used:

    https://github.com/tunnelvisionlabs/antlr4cs/blob/12f3271157c2df1984eac7009d71c929fd070f20/runtime/CSharp/Antlr4.Tool/AntlrTool.cs#L39-L45

  5. removed their assignment
    on Jun 25, 2020
  6. ghost locked as resolved and limited conversation to collaborators on Mar 20, 2022
  7. added a commit that references this issue on Dec 4, 2025
    9b28b24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions