Comments (5)
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.
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.
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.
I implement another solution is that: Create a new AssemblyCompileDateTimeAttribute class and a SourceGenerator project to inject AssemblyCompileDateTimeAttribute declaretion into each project.
from roslyn.
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)
- Missing nullability warning for spreads of maybe-null collections HOT 1
- Codegen for spreading a null array does not throw NullReferenceException HOT 9
- [Automated] PRs inserted in VS build main-35206.64
- Collection Expression Generated Types miss the [CompilerGenerated] attribute HOT 4
- Stack overflow when compiling incorrectly formed attribute argument string HOT 3
- Partial properties on struct warn about field ordering HOT 1
- [Automated] PRs inserted in VS build main-35207.120
- False positive IDE0001 when using extern alias
- The [InterceptsLocation] attribute generates a different "data" string for big-endian platforms HOT 7
- Synthesized collection types may result in empty unnecessary `<PrivateImplementationDetails>` class HOT 3
- Generator vs xaml generated files (the "*.g.i.cs") HOT 2
- [Automated] PRs inserted in VS build main-35208.78
- Default literals should not contribute their type from initial binding to best-type in nullable analysis
- CA1003 does not take the return value of an event handler into account
- Visual Studio did not auto fill complete after typing Property in VB.net HOT 4
- Fix formatting not using tabs HOT 2
- Refactor "Move type to file" should re-evaluate file header template
- Decompiled sources resolve wrong type.
- C# compiler integration with new System.Runtime.InteropServices.ExtendedLayoutAttribute
- [Automated] PRs inserted in VS build feature.debugger.main-35209.61
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roslyn.