Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 83e9a39

Browse files
authored
Change Environment.Version to return product version (#22664)
* Change Environment.Version to return product version - Contributes to https://github.com/dotnet/corefx/issues/31099 - Use AssemblyInformationalVersion attribute as fallback * Add sanity test for Environment.Version * Disable CodeDom tests * Fix test assembly name
1 parent 3e8cd50 commit 83e9a39

File tree

5 files changed

+149
-4
lines changed

5 files changed

+149
-4
lines changed

Diff for: src/System.Private.CoreLib/System.Private.CoreLib.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737
<SkipCommonResourcesIncludes>true</SkipCommonResourcesIncludes>
3838
<DocumentationFile>$(OutputPath)$(MSBuildProjectName).xml</DocumentationFile>
3939
</PropertyGroup>
40+
41+
<!-- Approximate Arcade version string https://github.com/dotnet/arcade/blob/master/Documentation/CorePackages/Versioning.md -->
42+
<!-- This workaround should be removed once CoreCLR repo is updated to use Arcade -->
43+
<PropertyGroup>
44+
<InformationalVersion>$(VersionPrefix)</InformationalVersion>
45+
</PropertyGroup>
46+
<PropertyGroup Condition="'$(PreReleaseLabel)' != ''">
47+
<InformationalVersion>$(InformationalVersion)-$(PreReleaseLabel).$(BuildNumberMajor).$(BuildNumberMinor)</InformationalVersion>
48+
</PropertyGroup>
49+
<PropertyGroup Condition="'$(LatestCommit)' != ''">
50+
<InformationalVersion>$(InformationalVersion)+$(LatestCommit)</InformationalVersion>
51+
</PropertyGroup>
52+
4053
<!-- Add Serviceable attribute to the project's metadata -->
4154
<ItemGroup>
4255
<AssemblyMetadata Include="Serviceable">

Diff for: src/System.Private.CoreLib/shared/System/Environment.cs

+24-4
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,30 @@ public static OperatingSystem OSVersion
129129

130130
public static bool UserInteractive => true;
131131

132-
// Previously this represented the File version of mscorlib.dll. Many other libraries in the framework and outside took dependencies on the first three parts of this version
133-
// remaining constant throughout 4.x. From 4.0 to 4.5.2 this was fine since the file version only incremented the last part. Starting with 4.6 we switched to a file versioning
134-
// scheme that matched the product version. In order to preserve compatibility with existing libraries, this needs to be hard-coded.
135-
public static Version Version => new Version(4, 0, 30319, 42000);
132+
public static Version Version
133+
{
134+
get
135+
{
136+
// FX_PRODUCT_VERSION is expected to be set by the host
137+
string versionString = (string)AppContext.GetData("FX_PRODUCT_VERSION");
138+
139+
if (versionString == null)
140+
{
141+
// Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host
142+
versionString = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
143+
}
144+
145+
ReadOnlySpan<char> versionSpan = versionString.AsSpan();
146+
147+
// Strip optional suffixes
148+
int separatorIndex = versionSpan.IndexOfAny("-+ ");
149+
if (separatorIndex != -1)
150+
versionSpan = versionSpan.Slice(0, separatorIndex);
151+
152+
// Return zeros rather then failing if the version string fails to parse
153+
return Version.TryParse(versionSpan, out Version version) ? version : new Version();
154+
}
155+
}
136156

137157
public static long WorkingSet
138158
{

Diff for: tests/CoreFX/CoreFX.issues.json

+38
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@
883883
"name": "System.Tests.MathFTests.Min",
884884
"reason": "https://github.com/dotnet/coreclr/pull/20912"
885885
},
886+
{
887+
"name": "System.Tests.EnvironmentTests.Version_MatchesFixedVersion",
888+
"reason": "outdated"
889+
},
886890
]
887891
}
888892
},
@@ -1557,4 +1561,38 @@
15571561
]
15581562
}
15591563
},
1564+
{
1565+
"name": "System.CodeDom.Tests",
1566+
"enabled": true,
1567+
"exclusions": {
1568+
"namespaces": null,
1569+
"classes": null,
1570+
"methods": [
1571+
{
1572+
"name": "System.CodeDom.Compiler.Tests.CSharpCodeGenerationTests.ProviderSupports",
1573+
"reason": "outdated"
1574+
},
1575+
{
1576+
"name": "System.CodeDom.Compiler.Tests.CSharpCodeGenerationTests.MetadataAttributes",
1577+
"reason": "outdated"
1578+
},
1579+
{
1580+
"name": "System.CodeDom.Compiler.Tests.CSharpCodeGenerationTests.RegionsSnippetsAndLinePragmas",
1581+
"reason": "outdated"
1582+
},
1583+
{
1584+
"name": "System.CodeDom.Compiler.Tests.VBCodeGenerationTests.RegionsSnippetsAndLinePragmas",
1585+
"reason": "outdated"
1586+
},
1587+
{
1588+
"name": "System.CodeDom.Compiler.Tests.VBCodeGenerationTests.MetadataAttributes",
1589+
"reason": "outdated"
1590+
},
1591+
{
1592+
"name": "System.CodeDom.Compiler.Tests.VBCodeGenerationTests.ProviderSupports",
1593+
"reason": "outdated"
1594+
}
1595+
]
1596+
}
1597+
}
15601598
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
class enviroment_version
8+
{
9+
static int Main()
10+
{
11+
Version ver = Environment.Version;
12+
Console.WriteLine($"Environment.Version = {ver}");
13+
14+
if (ver < new Version("3.0"))
15+
{
16+
Console.WriteLine("ERROR: Version less than 3.0.");
17+
return -1;
18+
}
19+
20+
// Verify that we are not returning hardcoded version from .NET Framework.
21+
if (ver == new Version("4.0.30319.42000"))
22+
{
23+
Console.WriteLine("ERROR: Version is hardcoded .NET Framework version.");
24+
return -1;
25+
}
26+
27+
// .NET Core assemblies use 4.6+ as file version. Verify that we have not used
28+
// the file version as product version by accident.
29+
if (ver.Major == 4 && (ver.Minor >= 6))
30+
{
31+
Console.WriteLine("ERROR: Version is 4.6+.");
32+
return -1;
33+
}
34+
35+
Console.WriteLine("PASSED");
36+
return 100;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
13+
<CLRTestKind>BuildAndRun</CLRTestKind>
14+
<CLRTestPriority>0</CLRTestPriority>
15+
</PropertyGroup>
16+
<!-- Default configurations to help VS understand the configurations -->
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
20+
</PropertyGroup>
21+
<ItemGroup>
22+
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
23+
<Visible>False</Visible>
24+
</CodeAnalysisDependentAssemblyPaths>
25+
</ItemGroup>
26+
<ItemGroup>
27+
<!-- Add Compile Object Here -->
28+
<Compile Include="environment_version.cs" />
29+
</ItemGroup>
30+
<ItemGroup>
31+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
32+
</ItemGroup>
33+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
34+
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
35+
</PropertyGroup>
36+
</Project>

0 commit comments

Comments
 (0)