Skip to content

Use InternalsVisibleTo instead of reflection in LocalAppContextSwitchesHelper - #4765

Open
paulmedynski wants to merge 1 commit into
mainfrom
dev/paul/local-app-switch-helper-ivt
Open

paulmedynski wants to merge 1 commit into
mainfrom
dev/paul/local-app-switch-helper-ivt

Conversation

@paulmedynski

@paulmedynski paulmedynski commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Replace reflection in LocalAppContextSwitchesHelper with typed access through InternalsVisibleTo. Renaming or removing a switch member now produces a compile-time error instead of a runtime reflection failure.

  • Make LocalAppContextSwitches.SwitchValue and its cached switch fields internal, and grant IVT to the existing Microsoft.Data.SqlClient.TestCommon assembly.
  • Preserve nullable switch state capture/restoration and the helper's existing serialization.
  • Preserve platform behavior: the managed networking cache is changed and restored only on Windows; on other platforms the helper leaves it untouched.
  • Support package-mode compilation by referencing the implementation DLL under lib/{tfm} rather than the public-only reference assembly. Map modern tests to the shipped net10.0 implementation.
  • Retain the existing signing conditions and add the public-key-qualified TestCommon IVT grant for signed package validation.

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

The package-mode implementation binding and signed friend-assembly configuration warrant final human validation.

Review effort: Balanced
Findings: None

What changed in this PR

Replaces reflection-based AppContext switch access with compile-time-checked internal access while isolating same-commit driver coupling from reusable test infrastructure.

Changes:

  • Adds InternalsVisibleTo access for directly manipulating cached switch values.
  • Moves internals-dependent helpers into a dedicated test assembly.
  • Updates test references and package-mode dependency resolution.
File Description
tests/​UnitTests/​.../​LocalAppContextSwitchesTest.cs Updates reflection-specific commentary.
tests/​UnitTests/​Microsoft.Data.SqlClient.UnitTests.csproj References internals helpers and Abstractions.
tests/​ManualTests/​Microsoft.Data.SqlClient.ManualTests.csproj References the internals helper project.
tests/​FunctionalTests/​Microsoft.Data.SqlClient.FunctionalTests.csproj References the internals helper project.
tests/​CommonInternals/​Microsoft.Data.SqlClient.TestCommon.Internals.csproj Defines the same-commit internals-bound assembly.
tests/​CommonInternals/​LocalAppContextSwitchesHelper.cs Implements direct cached-switch access.
tests/​CommonInternals/​ConnectionPoolVersionScope.cs Relocates pool-version isolation support.
tests/​Common/​Microsoft.Data.SqlClient.TestCommon.csproj Documents public-API-only version compatibility.
tests/​Common/​LocalAppContextSwitchesHelper.cs Removes the reflection-based implementation.
src/​.../​LocalAppContextSwitches.cs Exposes switch caches internally.
src/​Microsoft.Data.SqlClient.csproj Grants internals access to the helper assembly.
src/​Microsoft.Data.SqlClient.slnx Adds the new helper project.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@paulmedynski paulmedynski added the Area\Tests Issues that are targeted to tests or test projects label Sep 28, 2026
@paulmedynski paulmedynski added this to the 8.0.0-preview1 milestone Sep 28, 2026
Copilot AI review requested due to automatic review settings September 29, 2026 14:19

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

The managed-networking setter can leak cached state on non-Windows platforms because that state is not restored.

Review effort: Balanced
Findings: None

Previously missed (1)

In code that hasn't changed since last review

Medium severity Prevent non-Windows setter from leaking managed networking state

src/​Microsoft.Data.SqlClient/​tests/​CommonInternals/​LocalAppContextSwitchesHelper.cs:366

On non-Windows .NET, this setter now writes s_useManagedNetworking, while the constructor and Dispose deliberately skip capturing/restoring that field. Calling it there therefore leaks the altered cached value beyond the scope, contrary to the helper's restore contract. Guard the write on Windows (as the surrounding remarks describe), or capture and restore the field on every NET target.

paulmedynski added a commit that referenced this pull request Sep 29, 2026
Guard the managed networking cache setter on non-Windows platforms and add regression coverage for mutation and restoration behavior.

Refs #4765

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 29, 2026 18:34
@paulmedynski

Copy link
Copy Markdown
Contributor Author

Review body feedback

  • Source: review body, Copilot “Previously missed” finding — managed-networking cache state
    Fixed in e136d6fbc. UseManagedNetworking now writes s_useManagedNetworking only on Windows, matching the helper’s capture/restore contract and the production switch behavior. Added regression coverage verifying that the cache is unchanged on non-Windows and restored after disposal on Windows.

  • Source: review body, Copilot validation concern — package binding and friend assembly
    No additional code change was required. The internals helper builds successfully in Package mode against the implementation assembly with 0 warnings and 0 errors. The signed friend-assembly configuration was reviewed; the actual signing-key path remains exercised by CI because no CI signing key is available locally.

Validation:

  • Focused regression test: 1 passed.
  • LocalAppContextSwitchesTest: 2 passed.
  • Package-mode internals-helper build: succeeded with 0 warnings and 0 errors.

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

Package-mode implementation binding and signed friend-assembly behavior remain environment-sensitive and warrant final human validation.

Review effort: Balanced
Findings: None

@paulmedynski
paulmedynski marked this pull request as ready for review September 29, 2026 18:38
@paulmedynski
paulmedynski requested a review from a team as a code owner September 29, 2026 18:38
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.78%. Comparing base (f3cfc73) to head (c55aa24).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4765      +/-   ##
==========================================
- Coverage   66.95%   64.78%   -2.17%     
==========================================
  Files         292      286       -6     
  Lines       45438    68551   +23113     
==========================================
+ Hits        30422    44414   +13992     
- Misses      15016    24137    +9121     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.78% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@paulmedynski
paulmedynski enabled auto-merge (squash) September 30, 2026 14:06
Copilot AI balanced review requested due to automatic review settings October 1, 2026 13:54

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

The new helper project uses a nonexistent package implementation path, breaking Package-mode compilation.

Review effort: Balanced
Findings: 1 High severity

Open (1)

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

The new helper project references a nonexistent package path, breaking Package-mode compilation.

Review effort: Balanced
Findings: 1 High severity

Open (1)

paulmedynski added a commit that referenced this pull request Oct 1, 2026
Merging main brought in #4673 (Remove RID-specific SqlClient DLLs), which
deleted every runtimes/ entry from the SqlClient nuspec. The new
TestCommon.Internals project still pointed its Package-mode HintPath at
runtimes/win/lib/<tfm>, which no longer exists. Because ExcludeAssets="compile"
drops the ref assembly, this left no resolvable SqlClient reference and broke
Package-mode builds with MSB3245 followed by CS0234.

Point the HintPath at lib/<tfm>, matching the fix #4673 applied to
UnitTests.csproj, and drop the @todo hack comment that PR also removed, since
the RID-specific binaries it referred to no longer exist.

Verified by packing the driver from this commit (0 runtimes/ entries) and
building TestCommon.Internals in Package mode for net8.0 and net9.0, plus the
consuming UnitTests project. Reverting the HintPath reproduces MSB3245.

Addresses Copilot review feedback on PR #4765.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 15:27

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The assembly split, internal access grants, project references, isolation, and restoration coverage are consistent and complete.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@paulmedynski

Copy link
Copy Markdown
Contributor Author

Review body feedback

  • Copilot — "package-mode implementation binding ... warrants final human validation" (review, review)
    Correcting my previous answer. My last run reported that no code change was required and that Package mode built cleanly. That was validated against a locally cached package built before the main merge, so it was wrong. Re-validating against a package packed from the current commit shows the binding genuinely was broken: Remove RID-specific SqlClient DLLs #4673 removed the runtimes/ payload the HintPath relied on. Fixed in 89ed5a5.

  • Copilot — "Fix invalid SqlClient HintPath in Package mode" (review, review)
    Duplicate of inline thread r4156204815; fixed in 89ed5a5 and answered in that thread.

Discussion comments

  • No actionable discussion comments. The only human comment is my own prior summary; the Codecov post is operational noise and was set aside.

@benrr101 benrr101 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.

I don't really understand why there's a new project. Feel free to push back and explain, but for now I'd request that the common internals project get rolled into the common project.

@@ -0,0 +1,63 @@
<Project Sdk="Microsoft.NET.Sdk">

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.

Sorry ... why do we need a new project for this? Can we just leave this stuff the TestCommon project? It seems to me we have three "common" projects for tests with this change. I'd prefer to reduce that number than increase it :)

Replace reflection with typed access to cached AppContext switches. Grant internals access to TestCommon, retain signed package-mode support, and preserve managed networking behavior across platforms.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 8, 2026 13:34
@paulmedynski
paulmedynski force-pushed the dev/paul/local-app-switch-helper-ivt branch from 89ed5a5 to c55aa24 Compare October 8, 2026 13:34

Copilot AI 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.

🟡 Changes recommended

The pinned performance baseline still compiles internals-bound helpers against an older driver package, causing CS0122 errors.

1 open finding

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

@paulmedynski
paulmedynski requested a review from benrr101 October 8, 2026 13:42

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area\Tests Issues that are targeted to tests or test projects

Projects

Status: Waiting for customer

Development

Successfully merging this pull request may close these issues.

4 participants