Giter Site home page Giter Site logo

Comments (5)

jaredpar avatar jaredpar commented on August 18, 2024 1

This behavior is "By Design". The default for .NET SDK applications is to have deterministic builds. To achieve that the timestamp field is replaced with part of a deterministic hash of the compilation inputs. If you desire the old behavior you can set <Deterministic>false</Deterministic> in your project file. Doing so is generally not recommended.

from roslyn.

jjonescz avatar jjonescz commented on August 18, 2024 1

Note that a source generator might be an overkill, you can use MSBuild items to create attributes - you don't even need a custom attribute, there is AssemblyMetadataAttribute that can be used for this purpose - see example in the docs:

<ItemGroup>
  <!-- Include must be the fully qualified .NET type name of the Attribute to create. -->
  <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
    <!-- _Parameter1, _Parameter2, etc. correspond to the
        matching parameter of a constructor of that .NET attribute type -->
    <_Parameter1>BuildDate</_Parameter1>
    <_Parameter2>$(Date)</_Parameter2>
  </AssemblyAttribute>
</ItemGroup>

generates:

[assembly: System.Reflection.AssemblyMetadataAttribute("BuildDate", "01/19/2024")]

from roslyn.

CuteLeon avatar CuteLeon commented on August 18, 2024

I found below content in https://github.com/dotnet/runtime/blob/main/docs/design/specs/PE-COFF.md#deterministic-pecoff-file, think TimeDateStamp doesn't mean compile date time any more, what will be the substitute?

The value of field TimeDateStamp in COFF File Header of a deterministic PE/COFF file does not indicate the date and time when the file was produced and should not be interpreted that way. Instead the value of the field is derived from a hash of the file content.

from roslyn.

CuteLeon avatar CuteLeon commented on August 18, 2024

I implement another solution is that: Create a new AssemblyCompileDateTimeAttribute class and a SourceGenerator project to inject AssemblyCompileDateTimeAttribute declaretion into each project.

from roslyn.

CuteLeon avatar CuteLeon commented on August 18, 2024

Share my workaround:

public class AssemblyCompileDateTimeAttribute : Attribute
{
    public string CompileDateTime { get; set; }

    public AssemblyCompileDateTimeAttribute(string compileDateTime)
    {
        this.CompileDateTime = compileDateTime;
    }
}
    [Generator(LanguageNames.CSharp)]
    public class CompileDateTimeSourceGenerator : ISourceGenerator
    {
        public void Initialize(GeneratorInitializationContext context)
        {
        }

        public void Execute(GeneratorExecutionContext context)
        {
            var assemblyName = context.Compilation.Assembly.Name;
            var compileDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
            context.AddSource($"{assemblyName}.CompileDateTime.g.cs", $"[assembly: AssemblyTools.AssemblyCompileDateTimeAttribute(\"{compileDateTime}\")]");
        }
    }
    public static DateTime? GetCompileDateTimeFromAttribute(this Assembly assembly)
    {
        var buildDateTime = default(DateTime?);
        try
        {
            var attribute = assembly.GetCustomAttribute<AssemblyCompileDateTimeAttribute>();
            if (attribute is not null &&
                DateTime.TryParseExact(attribute.CompileDateTime, "yyyy-MM-dd HH:mm:ss.fffffff", CultureInfo.InvariantCulture, DateTimeStyles.None, out var compileDateTime))
            {
                buildDateTime = compileDateTime;
            }
        }
        finally
        {
        }
        return buildDateTime;
    }

from roslyn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.