Skip to content

Commit

Permalink
Merge pull request #492 from rasmus/fix-assembly-version
Browse files Browse the repository at this point in the history
Fix: AssemblyVersion should be properly set
  • Loading branch information
rasmus authored Jan 23, 2025
2 parents b453956 + bbd9fc8 commit a464aaa
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.36

* *Nothing yet...*
* New: Dropped the `-beta` on Bake version, version `0.x` says as much
* Fix: `AssemblyVersion` must always be `[MAJOR].0.0.0` to limit assembly load errors

# 0.35-beta

Expand Down
11 changes: 10 additions & 1 deletion Source/Bake/Cooking/Composers/DotNetComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private IEnumerable<Recipe> CreateRecipe(
Platform.Any,
path,
ingredients.Version,
properties,
new DirectoryArtifact(
Path.Combine(visualStudioProject.Directory, path)));

Expand Down Expand Up @@ -293,6 +294,7 @@ private IEnumerable<Recipe> CreateRecipe(
targetPlatform,
path,
ingredients.Version,
properties,
new ExecutableArtifact(
visualStudioProject.CsProj.ToolCommandName,
Path.Combine(
Expand Down Expand Up @@ -367,8 +369,15 @@ private Dictionary<string, string> CreateProperties(
}

var legacyVersion = ingredients.Version.LegacyVersion.ToString();

// CRITICAL for NuGet packages!
// Setting the AssemblyVersion to [MAJOR].0.0.0 ensures that the assembly version remains stable
// across minor and patch updates, which is important for binding redirects and compatibility.
properties["AssemblyVersion"] = $"{ingredients.Version.Major}.0.0.0";

properties["Version"] = legacyVersion;
properties["AssemblyVersion"] = legacyVersion;
properties["ApplicationVersion"] = legacyVersion;
properties["FileVersion"] = legacyVersion;
properties["AssemblyFileVersion"] = legacyVersion;
properties["Description"] = BuildDescription(visualStudioSolution, ingredients);

Expand Down
5 changes: 2 additions & 3 deletions Source/Bake/Cooking/Cooks/DotNet/DotNetPublishCook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Threading;
using System.Threading.Tasks;
using Bake.Services.Tools;
using Bake.Services.Tools.DotNetArguments;
using Bake.ValueObjects.Recipes.DotNet;
Expand Down Expand Up @@ -51,7 +49,8 @@ protected override async Task<bool> CookAsync(
recipe.Configuration,
recipe.Platform,
recipe.Output,
recipe.Version);
recipe.Version,
recipe.Properties);

using var toolResult = await _dotNet.PublishAsync(
argument,
Expand Down
33 changes: 12 additions & 21 deletions Source/Bake/Services/Tools/DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,11 @@ public async Task<IToolResult> BuildAsync(
"--configuration", argument.Configuration,
};

foreach (var (property, value) in argument.Properties)
{
arguments.Add($"-p:{property}={value.ToMsBuildEscaped()}");
}
arguments.AddRange(AsPropertyArguments(argument.Properties));

AddIf(!argument.Incremental, arguments, "--no-incremental");
AddIf(!argument.Restore, arguments, "--no-restore");

arguments.AddRange(VersionArguments(argument.Version));

var buildRunner = _runnerFactory.CreateRunner(
"dotnet",
argument.WorkingDirectory,
Expand All @@ -156,6 +151,14 @@ public async Task<IToolResult> BuildAsync(
return new ToolResult(runnerResult);
}

private static IEnumerable<string> AsPropertyArguments(IReadOnlyDictionary<string, string> properties)
{
foreach (var (property, value) in properties)
{
yield return $"-p:{property}={value.ToMsBuildEscaped()}";
}
}

public async Task<IToolResult> TestAsync(
DotNetTestArgument argument,
CancellationToken cancellationToken)
Expand Down Expand Up @@ -262,23 +265,19 @@ public async Task<IToolResult> PublishAsync(
"--configuration", argument.Configuration,
"--nologo",
"--output", argument.Output,
"-p:CheckEolTargetFramework=false"
};

if (argument.Platform.Os != ExecutableOperatingSystem.Any)
{
arguments.AddRange(new []
{
"--runtime", argument.Platform.GetDotNetRuntimeIdentifier()
});
arguments.AddRange(["--runtime", argument.Platform.GetDotNetRuntimeIdentifier()]);
}

arguments.AddRange(AsPropertyArguments(argument.Properties));

AddIf(!argument.Build, arguments, "--no-build");
AddIf(argument.PublishSingleFile, arguments, "-p:PublishSingleFile=true");
AddIf(argument.SelfContained, arguments, "--self-contained", "true");

arguments.AddRange(VersionArguments(argument.Version));

var buildRunner = _runnerFactory.CreateRunner(
"dotnet",
argument.WorkingDirectory,
Expand All @@ -289,13 +288,5 @@ public async Task<IToolResult> PublishAsync(

return new ToolResult(runnerResult);
}

private static IEnumerable<string> VersionArguments(SemVer version)
{
yield return $"-p:Version={version}";
yield return $"-p:ApplicationVersion={version}";
yield return $"-p:AssemblyVersion={version.LegacyVersion}";
yield return $"-p:FileVersion={version.LegacyVersion}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class DotNetPublishArgument : DotNetArgument
public Platform Platform { get; }
public string Output { get; }
public SemVer Version { get; }
public IReadOnlyDictionary<string, string> Properties { get; }

public DotNetPublishArgument(
string filePath,
Expand All @@ -43,7 +44,8 @@ public DotNetPublishArgument(
string configuration,
Platform platform,
string output,
SemVer version)
SemVer version,
IReadOnlyDictionary<string, string> properties)
: base(filePath)
{
PublishSingleFile = publishSingleFile;
Expand All @@ -53,6 +55,7 @@ public DotNetPublishArgument(
Platform = platform;
Output = output;
Version = version;
Properties = properties;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class DotNetPublishRecipe : Recipe
[YamlMember]
public SemVer Version { get; [Obsolete] set; } = null!;

[YamlMember]
public Dictionary<string, string> Properties { get; [Obsolete] set; } = null!;

[Obsolete]
public DotNetPublishRecipe() { }

Expand All @@ -65,6 +68,7 @@ public DotNetPublishRecipe(
Platform platform,
string output,
SemVer version,
Dictionary<string, string> properties,
params Artifact[] artifacts)
: base(artifacts)
{
Expand All @@ -77,6 +81,7 @@ public DotNetPublishRecipe(
Platform = platform;
Output = output;
Version = version;
Properties = properties;
#pragma warning restore CS0612 // Type or member is obsolete
}
}
Expand Down

0 comments on commit a464aaa

Please sign in to comment.