diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 635e187d..db2a881e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,6 +34,8 @@ env: # renovate: datasource=dotnet-version depName=dotnet-sdk DOTNET_VERSION: '8.0.428' PLUGIN_NAME: 'Sleezer' + # Lidarr installs only a zip named for its own runtime; the build ships a copy under this name once it passes against Lidarr's nightly. + LIDARR_NIGHTLY_FRAMEWORK: 'net10.0' jobs: build: @@ -50,7 +52,8 @@ jobs: branch_repo_url: ${{ steps.git_info.outputs.branch_repo_url }} plugin_input_format: ${{ steps.git_info.outputs.plugin_input_format }} commit_messages: ${{ steps.git_info.outputs.commit_messages }} - package_name: ${{ steps.package.outputs.package_name }} + package_names: ${{ steps.nightly.outputs.package_names }} + nightly_framework: ${{ steps.nightly.outputs.nightly_framework }} steps: - name: Checkout with submodules uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 @@ -404,11 +407,42 @@ jobs: dotnet run --project tools/SmokeLoad/SmokeLoad.csproj \ -c Release --no-build -- "$PLUGIN_DLL" "$HOST_DIR" + - name: Check against Lidarr nightly and package for its runtime + # Compiling against ext/Lidarr cannot catch a member Lidarr 4.0 removed (v1.39.11); PluginCheck lists such refs. + # A failed check still ships the $FRAMEWORK zip, so a servarr outage or a nightly break never blocks the plugins branch. + id: nightly + env: + PACKAGE_NAME: ${{ steps.package.outputs.package_name }} + VERSION: ${{ steps.meta.outputs.package_version }} + FRAMEWORK: ${{ steps.meta.outputs.framework }} + run: | + set -euo pipefail + if [ "$LIDARR_NIGHTLY_FRAMEWORK" = "$FRAMEWORK" ]; then + echo "::error::The plugin now builds for $FRAMEWORK itself; drop the nightly copy from this workflow" + exit 1 + fi + + STAGE=$(mktemp -d) + unzip -q "$GITHUB_WORKSPACE/$PACKAGE_NAME" -d "$STAGE" + NAMES=("$PACKAGE_NAME") + SHIPPED="" + if bash tools/PluginCheck/check-nightly.sh "$STAGE/Lidarr.Plugin.$PLUGIN_NAME.dll" "$LIDARR_NIGHTLY_FRAMEWORK"; then + NIGHTLY_NAME="${PLUGIN_NAME}-v${VERSION}.${LIDARR_NIGHTLY_FRAMEWORK}.zip" + cp "$GITHUB_WORKSPACE/$PACKAGE_NAME" "$GITHUB_WORKSPACE/$NIGHTLY_NAME" + NAMES+=("$NIGHTLY_NAME") + SHIPPED=$LIDARR_NIGHTLY_FRAMEWORK + else + echo "::warning::The Lidarr nightly check failed (see above), so this release ships only the $FRAMEWORK zip: Lidarr on $LIDARR_NIGHTLY_FRAMEWORK cannot install or update to it" + fi + echo "nightly_framework=$SHIPPED" >> "$GITHUB_OUTPUT" + { echo "package_names<> "$GITHUB_OUTPUT" + - name: Upload plugin package uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: name: plugin-package - path: ${{ steps.package.outputs.package_name }} + path: ${{ steps.nightly.outputs.package_names }} + if-no-files-found: error retention-days: 7 - name: Upload build logs @@ -438,16 +472,26 @@ jobs: - name: Verify artifact id: verify env: - PACKAGE_NAME: ${{ needs.build.outputs.package_name }} + PACKAGE_NAMES: ${{ needs.build.outputs.package_names }} run: | - ARTIFACT_PATH="./artifacts/$PACKAGE_NAME" - if [ ! -s "$ARTIFACT_PATH" ]; then - echo "::error::Artifact missing or empty at $ARTIFACT_PATH" - ls -la ./artifacts/ + set -euo pipefail + PATHS=() + while IFS= read -r NAME; do + [ -n "$NAME" ] || continue + ARTIFACT_PATH="./artifacts/$NAME" + if [ ! -s "$ARTIFACT_PATH" ]; then + echo "::error::Artifact missing or empty at $ARTIFACT_PATH" + ls -la ./artifacts/ + exit 1 + fi + echo "$NAME: $(stat -c%s "$ARTIFACT_PATH") bytes" + PATHS+=("$ARTIFACT_PATH") + done <<< "$PACKAGE_NAMES" + if [ "${#PATHS[@]}" -eq 0 ]; then + echo "::error::The build job named no packages" exit 1 fi - echo "Size: $(stat -c%s "$ARTIFACT_PATH") bytes" - echo "artifact_path=$ARTIFACT_PATH" >> "$GITHUB_OUTPUT" + { echo "artifact_paths<> "$GITHUB_OUTPUT" - name: Generate release notes markdown id: notes @@ -458,7 +502,13 @@ jobs: COMMIT: ${{ needs.build.outputs.git_commit }} PLUGIN_INPUT: ${{ needs.build.outputs.plugin_input_format }} COMMITS: ${{ needs.build.outputs.commit_messages }} + NIGHTLY: ${{ needs.build.outputs.nightly_framework }} run: | + DOTNET_LINE=$FRAMEWORK + if [ -n "$NIGHTLY" ]; then + DOTNET_LINE="$FRAMEWORK, also as $NIGHTLY (checked against Lidarr's nightly)" + fi + cat > release_notes.md << EOL ## 📝 What's New: @@ -474,7 +524,7 @@ jobs: ## 📦 Package Information **Version:** $VERSION - **.NET Version:** $FRAMEWORK + **.NET Version:** $DOTNET_LINE **Minimum Lidarr Version:** $MIN_LIDARR **Commit:** $COMMIT EOL @@ -489,7 +539,7 @@ jobs: prerelease: ${{ needs.build.outputs.is_prerelease == 'true' }} make_latest: ${{ needs.build.outputs.is_latestrelease == 'true' }} target_commitish: ${{ needs.build.outputs.git_branch }} - files: ${{ steps.verify.outputs.artifact_path }} + files: ${{ steps.verify.outputs.artifact_paths }} fail_on_unmatched_files: true env: GITHUB_TOKEN: ${{ github.token }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d41ac2e5..3e822bc0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,6 +83,10 @@ jobs: dotnet test tests/${PLUGIN_NAME}.Tests/${PLUGIN_NAME}.Tests.csproj \ -c Release -p:NuGetAudit=false + # Built without -f, so the plugin lands in _plugins//, not _plugins/// as in build.yml + - name: Check against Lidarr nightly + run: bash tools/PluginCheck/check-nightly.sh "_plugins/${PLUGIN_NAME}/Lidarr.Plugin.${PLUGIN_NAME}.dll" + dependency-audit: runs-on: ubuntu-latest steps: diff --git a/tools/PluginCheck/Checker.cs b/tools/PluginCheck/Checker.cs new file mode 100644 index 00000000..caf163a8 --- /dev/null +++ b/tools/PluginCheck/Checker.cs @@ -0,0 +1,235 @@ +using Mono.Cecil; + +namespace Sleezer.PluginCheck +{ + public sealed class Checker + { + private readonly ModuleDefinition _module; + private readonly string _self; + private readonly string _lib; + private readonly SortedSet _problems = new(StringComparer.Ordinal); + + // The plugin's folder is not searched: a CI build keeps net8 Lidarr copies there that would hide an assembly the build lacks + public Checker(string plugin, string lib) + { + _lib = lib; + var resolver = new DefaultAssemblyResolver(); + resolver.RemoveSearchDirectory("."); + resolver.RemoveSearchDirectory("bin"); + resolver.AddSearchDirectory(lib); + _module = ModuleDefinition.ReadModule(plugin, new ReaderParameters { AssemblyResolver = resolver }); + _self = _module.Assembly.Name.Name; + } + + public int TypeRefs { get; private set; } + public int MemberRefs { get; private set; } + public int RequiredMembers { get; private set; } + public int Overrides { get; private set; } + + public IReadOnlyCollection Run() + { + CheckTypeRefs(); + CheckMemberRefs(); + CheckImplementations(); + CheckOverrides(); + return _problems; + } + + private void CheckTypeRefs() + { + foreach (var tr in _module.GetTypeReferences().Where(t => IsHost(t.Scope))) + { + TypeRefs++; + if (ResolveType(tr) == null) + { + _problems.Add($"missing type {tr.FullName} ({tr.Scope.Name})"); + } + } + } + + // Array members are made by the runtime, so they never resolve + private void CheckMemberRefs() + { + foreach (var mr in _module.GetMemberReferences().Where(m => m.DeclaringType is not ArrayType && IsHost(m.DeclaringType.Scope))) + { + MemberRefs++; + if (ResolveMember(mr) == null) + { + _problems.Add($"missing member {mr.FullName}"); + } + } + } + + private void CheckImplementations() + { + foreach (var type in AllTypes(_module.Types).Where(t => !t.IsInterface && !t.IsAbstract)) + { + var interfaces = new List(); + CollectInterfaces(type, interfaces); + foreach (var method in RequiredMethods(type, interfaces)) + { + RequiredMembers++; + if (!Implements(type, interfaces, method)) + { + _problems.Add($"unimplemented {type.FullName} lacks {method.FullName}"); + } + } + } + } + + // An override whose base member changed signature becomes a new member the host never calls; explicit overrides are member refs + private void CheckOverrides() + { + foreach (var type in AllTypes(_module.Types).Where(t => !t.IsInterface)) + { + foreach (var method in type.Methods.Where(m => m.IsVirtual && !m.IsNewSlot && !m.HasOverrides)) + { + Overrides++; + if (Overridden(type, method) == false) + { + _problems.Add($"not overriding {method.FullName}"); + } + } + } + } + + private IEnumerable RequiredMethods(TypeDefinition type, List interfaces) + { + var required = interfaces.Where(i => IsHost(i)).ToList(); + for (var b = ResolveType(type.BaseType); b != null; b = ResolveType(b.BaseType)) + { + if (IsHost(b)) + { + required.Add(b); + } + } + + return required.SelectMany(t => t.Methods).Where(m => m.IsAbstract && !m.IsStatic).Distinct(); + } + + private static void CollectInterfaces(TypeDefinition t, List into) + { + for (; t != null; t = ResolveType(t.BaseType)) + { + foreach (var d in t.Interfaces.Select(i => ResolveType(i.InterfaceType)).Where(d => d != null && !into.Contains(d))) + { + into.Add(d); + CollectInterfaces(d, into); + } + } + } + + // Host: Lidarr's own assemblies, even ones the build dropped, and every library in its bin dir + private bool IsHost(string assembly) => + assembly != _self && (assembly.StartsWith("Lidarr.", StringComparison.Ordinal) || File.Exists(Path.Combine(_lib, assembly + ".dll"))); + + private bool IsHost(IMetadataScope scope) => scope is AssemblyNameReference a && IsHost(a.Name); + + private bool IsHost(TypeDefinition t) => IsHost(t.Module.Assembly.Name.Name); + + // An interface member is met by any virtual method of its name and signature or by an interface's default; a class member only by an override + private static bool Implements(TypeDefinition type, List interfaces, MethodDefinition required) + { + var isInterface = required.DeclaringType.IsInterface; + for (var t = type; t != null; t = ResolveType(t.BaseType)) + { + if (t.Methods.Any(m => m.IsVirtual && !m.IsAbstract && (isInterface || !m.IsNewSlot) && m.Name == required.Name && SameSignature(m, required))) + { + return true; + } + + if (t.Methods.Any(m => ExplicitlyOverrides(m, required))) + { + return true; + } + } + + return isInterface && interfaces.Any(i => i.Methods.Any(m => !m.IsAbstract && ExplicitlyOverrides(m, required))); + } + + private static bool ExplicitlyOverrides(MethodDefinition m, MethodDefinition required) => + m.Overrides.Any(o => o.Name == required.Name && o.DeclaringType.GetElementType().FullName == required.DeclaringType.FullName && SameSignature(o, required)); + + // null when a base can't be read; a plugin type's own base the build lacks is reported by CheckTypeRefs + private static bool? Overridden(TypeDefinition type, MethodDefinition method) + { + for (var b = type.BaseType; b != null;) + { + var bd = ResolveType(b); + if (bd == null) + { + return null; + } + + if (bd.Methods.Any(m => m.IsVirtual && m.Name == method.Name && SameSignature(m, method))) + { + return true; + } + + b = bd.BaseType; + } + + return false; + } + + private static bool SameSignature(MethodReference m, MethodReference required) => + m.Parameters.Count == required.Parameters.Count + && SameType(m.ReturnType, required.ReturnType) + && m.Parameters.Zip(required.Parameters).All(p => SameType(p.First.ParameterType, p.Second.ParameterType)); + + // A generic parameter on either side matches any type: substituting them needs every base's arguments + private static bool SameType(TypeReference a, TypeReference b) + { + a = StripModifiers(a); + b = StripModifiers(b); + + if (a is GenericParameter || b is GenericParameter) + { + return true; + } + + if (a is GenericInstanceType ga && b is GenericInstanceType gb) + { + return SameType(ga.ElementType, gb.ElementType) + && ga.GenericArguments.Count == gb.GenericArguments.Count + && ga.GenericArguments.Zip(gb.GenericArguments).All(p => SameType(p.First, p.Second)); + } + + if (a is TypeSpecification sa && b is TypeSpecification sb) + { + return sa.GetType() == sb.GetType() && (sa as ArrayType)?.Rank == (sb as ArrayType)?.Rank && SameType(sa.ElementType, sb.ElementType); + } + + return a.FullName == b.FullName; + } + + private static TypeReference StripModifiers(TypeReference t) => t is IModifierType m ? StripModifiers(m.ElementType) : t; + + private static IEnumerable AllTypes(IEnumerable ts) => ts.SelectMany(t => new[] { t }.Concat(AllTypes(t.NestedTypes))); + + // Cecil throws, rather than returning null, when the assembly itself is missing + private static TypeDefinition ResolveType(TypeReference t) + { + try + { + return t?.GetElementType().Resolve(); + } + catch (AssemblyResolutionException) + { + return null; + } + } + + private static IMemberDefinition ResolveMember(MemberReference m) + { + try + { + return m switch { MethodReference r => r.Resolve(), FieldReference f => f.Resolve(), _ => null }; + } + catch (AssemblyResolutionException) + { + return null; + } + } + } +} diff --git a/tools/PluginCheck/PluginCheck.csproj b/tools/PluginCheck/PluginCheck.csproj new file mode 100644 index 00000000..cacc11b3 --- /dev/null +++ b/tools/PluginCheck/PluginCheck.csproj @@ -0,0 +1,20 @@ + + + + + Exe + net8.0 + disable + enable + Sleezer.PluginCheck + Sleezer.PluginCheck + false + bin/$(Configuration)/ + true + + + + + + + diff --git a/tools/PluginCheck/Program.cs b/tools/PluginCheck/Program.cs new file mode 100644 index 00000000..e72c6ece --- /dev/null +++ b/tools/PluginCheck/Program.cs @@ -0,0 +1,21 @@ +using Sleezer.PluginCheck; + +// Usage: Sleezer.PluginCheck . Lists each type or member the plugin uses from Lidarr's bin dir that the build lacks, +// each host abstract member a plugin type no longer implements, and each override left with no base member (see Checker.SameType). +if (args.Length < 2) +{ + Console.Error.WriteLine("Usage: Sleezer.PluginCheck "); + return 2; +} + +var checker = new Checker(args[0], args[1]); +var problems = checker.Run(); + +Console.WriteLine($"checked {checker.TypeRefs} host type refs, {checker.MemberRefs} member refs, {checker.RequiredMembers} required members, {checker.Overrides} overrides against {args[1]}"); +foreach (var p in problems) +{ + Console.WriteLine(p); +} + +Console.WriteLine(problems.Count == 0 ? "RESULT: compatible" : $"RESULT: {problems.Count} problem(s)"); +return problems.Count == 0 ? 0 : 1; diff --git a/tools/PluginCheck/check-nightly.sh b/tools/PluginCheck/check-nightly.sh new file mode 100644 index 00000000..c757641d --- /dev/null +++ b/tools/PluginCheck/check-nightly.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# Usage: check-nightly.sh [framework]. Runs PluginCheck against Lidarr's nightly; with a framework, also fails unless the nightly runs it. +set -euo pipefail + +plugin=$1 +framework=${2:-} +if [ ! -f "$plugin" ]; then + echo "::error::No plugin at $plugin to check" + exit 1 +fi +host=$(mktemp -d) +trap 'rm -rf "$host"' EXIT + +if ! curl -fsSL --retry 3 -o "$host/lidarr.tar.gz" \ + "https://lidarr.servarr.com/v1/update/nightly/updatefile?os=linux&runtime=netcore&arch=x64"; then + echo "::error::Could not download Lidarr's nightly build" + exit 1 +fi +if ! tar -xzf "$host/lidarr.tar.gz" -C "$host"; then + echo "::error::Lidarr's nightly download is not a readable tarball" + exit 1 +fi +if [ ! -f "$host/Lidarr/Lidarr.runtimeconfig.json" ]; then + echo "::error::Lidarr's nightly download has no Lidarr/Lidarr.runtimeconfig.json" + exit 1 +fi +if ! nightly=$(jq -er .runtimeOptions.tfm "$host/Lidarr/Lidarr.runtimeconfig.json"); then + echo "::error::Lidarr's nightly names no runtime in Lidarr/Lidarr.runtimeconfig.json" + exit 1 +fi +if [ -n "$framework" ] && [ "$nightly" != "$framework" ]; then + echo "::error::Lidarr's nightly runs $nightly, not $framework, so the $framework zip cannot be checked" + exit 1 +fi + +project="$(dirname "$0")/PluginCheck.csproj" +dotnet build "$project" -c Release --nologo +if ! dotnet run --project "$project" -c Release --no-build -- "$plugin" "$host/Lidarr"; then + echo "::error::PluginCheck failed against Lidarr's $nightly nightly (see above)" + exit 1 +fi