Skip to content

Fix IL3000 trimming warning in WindowsLibraryLoader - #1884

Merged
shimat merged 1 commit into
mainfrom
fix/1766-aot-assembly-location
May 30, 2026
Merged

shimat merged 1 commit into
mainfrom
fix/1766-aot-assembly-location

Conversation

@shimat

@shimat shimat commented May 30, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1766

Problem

WindowsLibraryLoader.LoadLibrary used Assembly.Location to determine the base directory for native DLL lookup. In single-file and AoT published apps, Assembly.Location always returns an empty string and causes the IL3000 trimming warning at publish time.

Changes

  • Replace Assembly.Location with AppContext.BaseDirectory, which is the correct alternative for all publish modes including single-file and AoT.
  • Remove the now-redundant second lookup: the previous code already fell back to AppContext.BaseDirectory on the very next attempt, making the Assembly.Location path both broken and unnecessary.
  • Remove the dead #if !NET40 / #else guard. NET40 is never defined in this project. The minimum consumable framework via netstandard2.0 is .NET 4.6.1, where AppContext.BaseDirectory is unconditionally available.
  • Remove the now-unused using System.Reflection import.

AppContext.BaseDirectory is available across all current target frameworks (netstandard2.0, netstandard2.1, net8.0), so there is no regression for any supported target.

Summary by CodeRabbit

Release Notes

  • Documentation

    • Added comprehensive issue backlog documentation with structured categorization of issues by effort level (quick wins, medium effort, large effort, on hold) and investigation status with tracking information.
  • Chores

    • Updated version control configuration to exclude additional project files.
    • Simplified Windows library loading by consolidating implementation approach.

Review Change Stack

Replace Assembly.Location with AppContext.BaseDirectory. Assembly.Location
returns an empty string in single-file and AoT published apps, triggering
IL3000 trimming warnings. AppContext.BaseDirectory is the correct
alternative and is available across all current target frameworks
(netstandard2.0, netstandard2.1, net8.0).

The previous code already fell back to AppContext.BaseDirectory on the
very next attempt, so the Assembly.Location lookup was also redundant.
Remove the now-dead #if !NET40 guard (NET40 is never defined in this
project; the minimum consumable framework via netstandard2.0 is .NET 4.6.1,
where AppContext.BaseDirectory is available unconditionally).
Remove the no-longer-needed `using System.Reflection` import.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@shimat shimat self-assigned this May 30, 2026
@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR removes reflection-based library path detection from WindowsLibraryLoader in favor of AppContext.BaseDirectory, eliminating AOT/Trimming compatibility warnings. Supporting changes include a new issue backlog document and gitignore updates.

Changes

WindowsLibraryLoader and documentation

Layer / File(s) Summary
WindowsLibraryLoader AppContext.BaseDirectory migration
src/OpenCvSharp/Internal/PInvoke/WindowsLibraryLoader.cs
Removed System.Reflection import and refactored LoadLibrary method to unconditionally use AppContext.BaseDirectory instead of attempting to derive base directory from the executing assembly, eliminating IL3000 warnings under PublishAot and PublishTrimmed builds.
Issue backlog and gitignore updates
.gitignore, docs/issue-backlog.md
Added structured issue backlog document categorizing tracked issues by effort and status, and updated .gitignore to exclude personal notes and the Japanese issue backlog variant.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and specifically describes the main change: fixing the IL3000 trimming warning in WindowsLibraryLoader, which directly matches the primary objective of the pull request.
Linked Issues check ✅ Passed The PR successfully addresses the requirement in #1766 by replacing Assembly.Location with AppContext.BaseDirectory in WindowsLibraryLoader.LoadLibrary to eliminate the IL3000 warning for single-file and AoT published apps.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the IL3000 warning: modifying WindowsLibraryLoader.cs to use AppContext.BaseDirectory, updating .gitignore appropriately, and documenting the issue backlog.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/1766-aot-assembly-location

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/issue-backlog.md (1)

1-121: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Save this Markdown file as UTF-8 with BOM.

This new .md file appears to be checked in without the required BOM marker. Please resave it as UTF-8 with BOM to match repo policy.

As per coding guidelines, **/*.{cs,csproj,yml,md,json}: All source files must be saved as UTF-8 with BOM (EF BB BF)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/issue-backlog.md` around lines 1 - 121, The file docs/issue-backlog.md
was committed without the required UTF-8 BOM; reopen docs/issue-backlog.md,
resave it encoded as "UTF-8 with BOM" (EF BB BF) using your editor/IDE or
iconv/PowerShell, verify the BOM is present at the top of the file, then stage
and commit the change (or amend the existing commit) so the repo policy for
UTF-8-with-BOM on markdown and other source files is satisfied.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@docs/issue-backlog.md`:
- Around line 1-121: The file docs/issue-backlog.md was committed without the
required UTF-8 BOM; reopen docs/issue-backlog.md, resave it encoded as "UTF-8
with BOM" (EF BB BF) using your editor/IDE or iconv/PowerShell, verify the BOM
is present at the top of the file, then stage and commit the change (or amend
the existing commit) so the repo policy for UTF-8-with-BOM on markdown and other
source files is satisfied.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: eaa569af-943b-4a17-9169-7e01ef89541a

📥 Commits

Reviewing files that changed from the base of the PR and between 9a49884 and 66bd807.

📒 Files selected for processing (3)
  • .gitignore
  • docs/issue-backlog.md
  • src/OpenCvSharp/Internal/PInvoke/WindowsLibraryLoader.cs

@shimat
shimat merged commit aee67d6 into main May 30, 2026
10 checks passed
@shimat
shimat deleted the fix/1766-aot-assembly-location branch May 30, 2026 09:40
@shimat shimat added the bug Confirmed defect in OpenCvSharp label Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Confirmed defect in OpenCvSharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warning when using PublishTrimmed and PublishAoT

1 participant