Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ Each side is a provider spec, and the two sides can mix:
| `-p`, `--prevent-change` | exit with `4` when the suggested change is at least `patch`, `minor` or `major`; `none` (the default) never fails |
| `-a`, `--allow-empty-previous-nuget` | succeed when `previous-nuget` finds no earlier release, for a first release |

The suggested change is `Patch` when the public API is unchanged and `Major` when something was deleted or changed.
Additions alone are currently reported as `Major` too: diffing Proc 0.12.0 against 0.13.0, which only adds
`NoWrapInThread`, suggests `Major`. Always name the `diff` subcommand: a provider spec as the first argument is read as
The suggested change follows semantic versioning. It is `None` when the public API is unchanged, `Patch` when only
assembly references changed, and `Minor` when API was only added or attributes changed: diffing Proc 0.12.0 against
0.13.0, which only adds `NoWrapInThread`, suggests `Minor`. It is `Major` when something was removed or changed, and
when a new interface member, abstract member or `required` member makes existing code change. Always name the `diff` subcommand: a provider spec as the first argument is read as
a command name.

More examples:
Expand Down
4 changes: 2 additions & 2 deletions build/scripts/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ let main argv =

// The framework-dependent build cannot catch what trimming breaks: #62 crashed only in the native-AOT
// binary, when NuGet deserialised registration metadata by reflection. So this publishes that binary and
// diffs real, immutable package versions with it. Proc 0.18.0's previous release is 0.17.0.
// diffs real, immutable package versions with it. Proc 0.18.0's previous release is 0.17.0, and 0.20.0 only adds to it.
| Smokeaot -> target [] {
let rid = RuntimeInformation.RuntimeIdentifier
let publishDirectory = Path.Combine(Paths.Output.FullName, "aot-smoke")
Expand All @@ -132,7 +132,7 @@ let main argv =
let binary = Paths.RootRelative <| Path.Combine(publishDirectory, executable)
let! result = text $"{binary} diff 'previous-nuget|Proc|0.18.0' 'nuget|Proc|0.20.0' --target Proc -f markdown"
printfn "%s" result
for expected in [ "Found previous nuget package found for Proc 0.18.0: 0.17.0"; "Suggested version change: Major" ] do
for expected in [ "Found previous nuget package found for Proc 0.18.0: 0.17.0"; "Suggested version change: Minor" ] do
if not (result.Contains expected) then failwithf "the native-AOT binary did not print '%s'" expected }

app.RunAsync(argv).GetAwaiter().GetResult()
52 changes: 35 additions & 17 deletions src/assembly-differ/AssemblyComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,45 @@ public AssemblyComparison(FileInfo first, FileInfo second)
public FileInfo Second { get; }
public IMetadataDiffItem Diff { get; set; }

public SuggestedVersionChange SuggestedVersionChange
/// <summary>
/// The version change the differences call for, by semantic versioning: <c>Major</c> when anything breaks, including
/// a new abstract or required member, <c>Minor</c>
/// when API is only added or changed compatibly, <c>Patch</c> when only assembly references changed, and <c>None</c>
/// when nothing changed.
/// </summary>
public SuggestedVersionChange SuggestedVersionChange => Diff == null ? SuggestedVersionChange.None : Classify(Diff);

private static SuggestedVersionChange Classify(IDiffItem item)
{
get
switch (item)
{
if (Diff == null)
// a reference the new assembly dropped is not part of its API
case AssemblyReferenceDiffItem:
return SuggestedVersionChange.Patch;

if (Diff.IsBreakingChange)
return SuggestedVersionChange.Major;

var differences = Diff.ChildrenDiffs.Concat(Diff.DeclarationDiffs).ToList();
if (differences.Count == 0)
// the compiler moves its own attributes, such as NullableContextAttribute, whenever members change
case CustomAttributeDiffItem attribute when attribute.HumanReadable.StartsWith("System.Runtime.CompilerServices.", StringComparison.Ordinal):
return SuggestedVersionChange.Patch;

var anyDeleted = differences.Any(diff => diff.DiffType == DiffType.Deleted);
var anyModified = differences.Any(diff => diff.DiffType == DiffType.Modified);
if (anyDeleted || anyModified)
// adding or removing an attribute, such as [Obsolete], does not break a caller
case CustomAttributeDiffItem:
return SuggestedVersionChange.Minor;
case IMetadataDiffItem metadata when metadata.DiffType == DiffType.Deleted:
return SuggestedVersionChange.Major;

var anyNew = differences.Any(diff => diff.DiffType == DiffType.New);
return anyNew ? SuggestedVersionChange.Minor : SuggestedVersionChange.Patch;
// an abstract or required member is new work for every implementer or caller
case MemberDiffItem { DiffType: DiffType.New, AddsObligation: true }:
return SuggestedVersionChange.Major;
case IMetadataDiffItem metadata when metadata.DiffType == DiffType.New:
return SuggestedVersionChange.Minor;
case IMetadataDiffItem metadata:
var change = SuggestedVersionChange.Patch;
foreach (var child in metadata.DeclarationDiffs.Concat(metadata.ChildrenDiffs))
{
var childChange = Classify(child);
if (childChange > change)
change = childChange;
}
return change;
default:
return item.IsBreakingChange ? SuggestedVersionChange.Major : SuggestedVersionChange.Minor;
}
}
}
Expand All @@ -60,5 +78,5 @@ public AllComparisonResults(List<AssemblyComparison> results, SuggestedVersionCh
(Comparisons, PreventVersionChange) = (results, preventChange);

public SuggestedVersionChange SuggestedVersionChange =>
Comparisons.Count == 0 ? SuggestedVersionChange.Patch : Comparisons.Select(c => c.SuggestedVersionChange).OrderByDescending(c => c).First();
Comparisons.Count == 0 ? SuggestedVersionChange.None : Comparisons.Select(c => c.SuggestedVersionChange).OrderByDescending(c => c).First();
}
12 changes: 8 additions & 4 deletions src/assembly-differ/Engine/Comparers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ internal sealed class MethodComparer : Comparer<MethodModel>

protected override IDiffItem Missing(MethodModel element) => new MemberDiffItem(MetadataType.Method, true, false, element.Display.Name, null, null);

protected override IDiffItem Added(MethodModel element) => new MemberDiffItem(MetadataType.Method, false, true, element.Display.Name, null, null);
protected override IDiffItem Added(MethodModel element) =>
new MemberDiffItem(MetadataType.Method, false, true, element.Display.Name, null, null) { AddsObligation = element.IsAbstract };

protected override IDiffItem? Changed(MethodModel oldElement, MethodModel newElement)
{
Expand Down Expand Up @@ -298,7 +299,8 @@ internal sealed class FieldComparer : Comparer<FieldModel>

protected override IDiffItem Missing(FieldModel element) => new MemberDiffItem(MetadataType.Field, true, false, element.Display.Name, null, null);

protected override IDiffItem Added(FieldModel element) => new MemberDiffItem(MetadataType.Field, false, true, element.Display.Name, null, null);
protected override IDiffItem Added(FieldModel element) =>
new MemberDiffItem(MetadataType.Field, false, true, element.Display.Name, null, null) { AddsObligation = element.AddsObligation };

protected override IDiffItem? Changed(FieldModel oldElement, FieldModel newElement)
{
Expand Down Expand Up @@ -326,7 +328,8 @@ internal sealed class PropertyComparer : Comparer<PropertyModel>

protected override IDiffItem Missing(PropertyModel element) => new MemberDiffItem(MetadataType.Property, true, false, element.Display.Name, null, null);

protected override IDiffItem Added(PropertyModel element) => new MemberDiffItem(MetadataType.Property, false, true, element.Display.Name, null, null);
protected override IDiffItem Added(PropertyModel element) =>
new MemberDiffItem(MetadataType.Property, false, true, element.Display.Name, null, null) { AddsObligation = element.AddsObligation };

protected override IDiffItem? Changed(PropertyModel oldElement, PropertyModel newElement)
{
Expand Down Expand Up @@ -354,7 +357,8 @@ internal sealed class EventComparer : Comparer<EventModel>

protected override IDiffItem Missing(EventModel element) => new MemberDiffItem(MetadataType.Event, true, false, element.Display.Name, null, null);

protected override IDiffItem Added(EventModel element) => new MemberDiffItem(MetadataType.Event, false, true, element.Display.Name, null, null);
protected override IDiffItem Added(EventModel element) =>
new MemberDiffItem(MetadataType.Event, false, true, element.Display.Name, null, null) { AddsObligation = element.AddsObligation };

protected override IDiffItem? Changed(EventModel oldElement, EventModel newElement)
{
Expand Down
6 changes: 6 additions & 0 deletions src/assembly-differ/Engine/DiffItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ public sealed class MemberDiffItem(
) : MetadataDiffItem(hasOld, hasNew, name, declarationDiffs, childrenDiffs)
{
public override MetadataType MetadataType { get; } = metadataType;

/// <summary>
/// A new member that existing code has to act on: an abstract one, such as an interface member without a default
/// implementation, which every implementer must add, or a required one, which every object initializer must set.
/// </summary>
public bool AddsObligation { get; init; }
}

/// <summary>An assembly reference the new module no longer has. Reports do not count these as API changes.</summary>
Expand Down
19 changes: 17 additions & 2 deletions src/assembly-differ/Engine/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ IReadOnlyList<AttributeModel> Attributes
public bool IsVirtual => (Flags & MethodAttributes.Virtual) != 0;

public bool IsStatic => (Flags & MethodAttributes.Static) != 0;

public bool IsAbstract => (Flags & MethodAttributes.Abstract) != 0;
}

internal sealed record FieldModel(string Name, FieldAttributes Flags, string TypeFullName, Display Display, IReadOnlyList<AttributeModel> Attributes)
Expand All @@ -68,6 +70,8 @@ internal sealed record FieldModel(string Name, FieldAttributes Flags, string Typ
public bool IsApi => Access is FieldAttributes.Public or FieldAttributes.FamORAssem or FieldAttributes.Family;

public bool IsStatic => (Flags & FieldAttributes.Static) != 0;

public bool AddsObligation => Attributes.Any(Obligations.IsRequired);
}

internal sealed record PropertyModel(
Expand All @@ -77,9 +81,20 @@ internal sealed record PropertyModel(
IReadOnlyList<AttributeModel> Attributes,
MethodModel? Get,
MethodModel? Set
);
)
{
public bool AddsObligation => Get is { IsAbstract: true } || Set is { IsAbstract: true } || Attributes.Any(Obligations.IsRequired);
}

internal sealed record EventModel(string Name, Display Display, IReadOnlyList<AttributeModel> Attributes, MethodModel? Add, MethodModel? Remove);
internal static class Obligations
{
public static bool IsRequired(AttributeModel attribute) => attribute.Signature == "System.Runtime.CompilerServices.RequiredMemberAttribute::.ctor()";
}

internal sealed record EventModel(string Name, Display Display, IReadOnlyList<AttributeModel> Attributes, MethodModel? Add, MethodModel? Remove)
{
public bool AddsObligation => Add is { IsAbstract: true } || Remove is { IsAbstract: true };
}

/// <summary>Reads the parts of an assembly the comparison looks at, in the order the metadata tables hold them.</summary>
internal sealed class ModelReader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
exit code: 0
--- stdout
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr


=== markdown
exit code: 0
--- stdout
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr


=== asciidoc
exit code: 0
--- stdout
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr


Expand All @@ -29,10 +29,10 @@ exit code: 0
### API Changes: `Lib`

```diff
Scanned: 📑 1 assemblies(s), Suggested change in version: Patch
Scanned: 📑 1 assemblies(s), Suggested change in version: None
+ 0 breaking change(s) detected.

🌟 0 new additions, 🔴 0 removals, 🔷 0 modifications
```
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
exit code: 0
--- stdout
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr


=== markdown
exit code: 0
--- stdout
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr


=== asciidoc
exit code: 0
--- stdout
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr


Expand All @@ -29,10 +29,10 @@ exit code: 0
### API Changes: `Lib`

```diff
Scanned: 📑 1 assemblies(s), Suggested change in version: Patch
Scanned: 📑 1 assemblies(s), Suggested change in version: None
+ 0 breaking change(s) detected.

🌟 0 new additions, 🔴 0 removals, 🔷 0 modifications
```
[diff] Suggested version change: Patch
[diff] Suggested version change: None
--- stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exit code: 0
<Module Name="Lib.dll" DiffType="Modified">
<Type Name="Lib.D1&lt;T&gt;" DiffType="New" />
</Module>
</Assembly>[diff] Suggested version change: Major
</Assembly>[diff] Suggested version change: Minor
--- stderr


Expand All @@ -17,7 +17,7 @@ exit code: 0
## API Changes: `Lib`

## `Lib.D1<T>` is new
[diff] Suggested version change: Major
[diff] Suggested version change: Minor
--- stderr


Expand All @@ -32,7 +32,7 @@ exit code: 0

[horizontal]
type:: added
[diff] Suggested version change: Major
[diff] Suggested version change: Minor
--- stderr


Expand All @@ -43,7 +43,7 @@ exit code: 0
### API Changes: `Lib`

```diff
Scanned: 📑 1 assemblies(s), Suggested change in version: Major
Scanned: 📑 1 assemblies(s), Suggested change in version: Minor
+ 0 breaking change(s) detected.

+ 🌟 1 new additions
Expand All @@ -65,5 +65,5 @@ Lib.dll
+ 🌟 Lib.D1<T>
```
</details>
[diff] Suggested version change: Major
[diff] Suggested version change: Minor
--- stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exit code: 0
<Method Name="M6(Boolean&amp;)" DiffType="New" />
</Type>
</Module>
</Assembly>[diff] Suggested version change: Major
</Assembly>[diff] Suggested version change: Minor
--- stderr


Expand All @@ -22,7 +22,7 @@ exit code: 0
## `Lib.C2` is new
## `Lib.C3<T>`
### `M6(Boolean&)` is added
[diff] Suggested version change: Major
[diff] Suggested version change: Minor
--- stderr


Expand All @@ -43,7 +43,7 @@ type:: added

[horizontal]
`M6(Boolean&)` method:: added
[diff] Suggested version change: Major
[diff] Suggested version change: Minor
--- stderr


Expand All @@ -54,7 +54,7 @@ exit code: 0
### API Changes: `Lib`

```diff
Scanned: 📑 1 assemblies(s), Suggested change in version: Major
Scanned: 📑 1 assemblies(s), Suggested change in version: Minor
+ 0 breaking change(s) detected.

+ 🌟 2 new additions
Expand All @@ -80,5 +80,5 @@ Lib.C3<T>
+ 🌟 M6(Boolean&)
```
</details>
[diff] Suggested version change: Major
[diff] Suggested version change: Minor
--- stderr
Loading
Loading