Skip to content

Preserve MSBuild variable references when upgrading packages - #714

Merged
slang25 merged 9 commits into
dotnet-outdated:masterfrom
hwoodiwiss:support-version-variables
Feb 22, 2026
Merged

slang25 merged 9 commits into
dotnet-outdated:masterfrom
hwoodiwiss:support-version-variables

Conversation

@hwoodiwiss

@hwoodiwiss hwoodiwiss commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

AI Usage

This PR was mostly created using AI, specifically Claude Sonnet 4.5/4.6, using OpenCode. The design was my idea, the implementation came from the friendly robot.

Problem

When a project uses MSBuild properties to manage package versions (e.g. Version="$(NewtonsoftJsonVersion)"), running dotnet outdated --upgrade would overwrite the variable reference with a literal version string. This broke the project's version management strategy and required manual cleanup after every upgrade.

Solution

Introduces VariableTrackingService, a new Core service that detects MSBuild variable references in package versions before an upgrade and restores them afterwards. The service performs a two-step operation around the dotnet add package call:

  1. Pre-upgrade: Scans the project file and all .props files in the directory hierarchy to build a map of which packages use variable references and where those variables are defined.
  2. Post-upgrade: After dotnet add package overwrites the version with a literal string, updates the property definition to the new version value and restores the $(VariableName) reference in the PackageReference (or PackageVersion) element.

Supported scenarios

All three common variable reference patterns are supported:

  • Direct reference — variable defined and used in the same .csproj
  • Central Package Version Management (CPVM) — PackageVersion elements with variable references in Directory.Packages.props
  • Cross-file — variable defined in Directory.Build.props (or any .props file in a parent directory), used in a .csproj

Not supported

  • Second-order variables — if a property's value is itself a variable reference (e.g. <FooVersion>$(GroupVersion)</FooVersion>), the service will not trace through the chain. The upgrade will proceed but the variable reference will be replaced with a literal version string.
  • Conditional properties — Condition="..." attributes on <PropertyGroup> or individual property elements are ignored. The first unconditional definition of a property wins.
  • MSBuild string expressions — property values using MSBuild functions or concatenation (e.g. $(MajorVersion).$(MinorVersion).0) are not recognised as version values and will not be tracked.
  • Properties defined outside .props files — only .props files found by walking up the directory tree from the project are scanned. Properties imported from SDK targets, NuGet packages, or other non-standard locations are not resolved.

In all unsupported cases the upgrade proceeds normally — the package version is updated successfully, but the variable reference is not preserved.

Changes

File Change
DotNetOutdated.Core/Services/VariableTrackingService.cs New service + interface + PackageVariableInfo data class
DotNetOutdated.Core/Services/DotNetPackageService.cs Inject IVariableTrackingService; call discover/update around dotnet add package
DotNetOutdated/Program.cs Register VariableTrackingService in the DI container
test-projects/direct-reference-variables/ New test fixture
test-projects/cpvm-variables/ New test fixture
test-projects/cross-file-variables/ New test fixture
test/DotNetOutdated.Tests/EndToEndTests.cs Three new end-to-end tests, one per supported scenario

Notes

  • XML formatting is preserved — the service uses targeted regex replacement rather than re-serialising the XML document.
  • The IFileSystem abstraction is respected throughout, keeping the service fully unit-testable.
  • No new production dependencies are introduced.

Implements variable tracking and preservation for both CPVM and direct PackageReference scenarios. When packages use MSBuild properties like Version="$(PropertyName)", the tool now:
- Discovers and tracks variable references before upgrading packages
- Updates the property value instead of replacing with literal versions
- Preserves the variable reference syntax in package declarations
- Handles shared properties used by multiple packages
- Includes caching to optimize performance when processing multiple packages

Changes:
- Add VariableTrackingService to discover and track property-based versions
- Update CentralPackageVersionManagementService to preserve CPVM variable references
- Update DotNetPackageService to restore variable references after dotnet add package
- Register VariableTrackingService in DI container
- Add integration tests for both CPVM and direct reference scenarios
- Add test projects demonstrating variable usage patterns

All 155 tests passing.
- Move RestoreVariableReference method from DotNetPackageService to VariableTrackingService
- Rename to UpdatePackageVariable for better clarity
- Simplify method signature to use PackageVariableInfo directly instead of redundant parameters
- Add cache invalidation after file updates to ensure data consistency
- Remove unused System.Xml.Linq and System.Linq imports from DotNetPackageService
- Encapsulate all variable management logic in a single service
Minor cleanup, removal of unused things
- Create test-projects/cpvm-variables with Directory.Packages.props
- Add test case Can_Upgrade_CPVM_With_Variables_Preserves_Variable_References
- Verify that variable references like $(NewtonsoftJsonVersion) are preserved in Directory.Packages.props
- Test confirms that VariableTrackingService works correctly with Central Package Management
- Add PackageReferenceFilePath to track where package reference is used separately from where property is defined
- Refactor DiscoverPackageVariables to collect all property definitions first, then match with package references
- Update UpdatePackageVariable to update both property file and package reference file
- Add test for variables defined in Directory.Build.props and used in project file
- Fixes scenario where variable is defined in one file but used in another
@hwoodiwiss
hwoodiwiss force-pushed the support-version-variables branch from 4d90f36 to 3f8906f Compare February 18, 2026 07:54

@hwoodiwiss hwoodiwiss Feb 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Based on the .gittattributes, LF seems to be correct, but the repo currently has CRLF. Is it okay to review this with &w=1? If not I can update the gitattributes to follow what's currently in the repo.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I'll fix up the line endings after

@dotnet-outdated dotnet-outdated deleted a comment from claude Bot Feb 22, 2026
@dotnet-outdated dotnet-outdated deleted a comment from claude Bot Feb 22, 2026
…ilure

- Add VariableTrackingServiceTests covering Discover and Update paths,
  including caching, cross-file, CPVM, and GlobalPackageReference scenarios
- Add optional Action<string> onWarning callback to VariableTrackingService
  so failures in UpdatePackageVariable are reported rather than silently dropped
- Wire the callback to IReporter.Warn in Program.cs DI registration
- Fix class comment style (remove malformed // <summary> XML tags)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@slang25
slang25 merged commit f14301a into dotnet-outdated:master Feb 22, 2026
7 checks passed
@hwoodiwiss
hwoodiwiss deleted the support-version-variables branch February 25, 2026 09:09
github-actions Bot pushed a commit to EelcoLos/nx-tinkering that referenced this pull request Mar 2, 2026
Updated
[dotnet-outdated-tool](https://github.com/dotnet-outdated/dotnet-outdated)
from 4.7.0 to 4.7.1.

<details>
<summary>Release notes</summary>

_Sourced from [dotnet-outdated-tool's
releases](https://github.com/dotnet-outdated/dotnet-outdated/releases)._

## 4.7.1

## What's Changed
* Fix --no-restore corrupting .csproj in CPM projects by @​slang25 in
dotnet-outdated/dotnet-outdated#713
* Add Claude Code GitHub Workflow by @​slang25 in
dotnet-outdated/dotnet-outdated#716
* Preserve MSBuild variable references when upgrading packages by
@​hwoodiwiss in
dotnet-outdated/dotnet-outdated#714

## New Contributors
* @​hwoodiwiss made their first contribution in
dotnet-outdated/dotnet-outdated#714

**Full Changelog**:
dotnet-outdated/dotnet-outdated@v4.7.0...v4.7.1

Commits viewable in [compare
view](dotnet-outdated/dotnet-outdated@v4.7.0...v4.7.1).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=dotnet-outdated-tool&package-manager=nuget&previous-version=4.7.0&new-version=4.7.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
github-actions Bot pushed a commit to ptr727/LanguageTags that referenced this pull request Mar 3, 2026
Updated
[dotnet-outdated-tool](https://github.com/dotnet-outdated/dotnet-outdated)
from 4.7.0 to 4.7.1.

<details>
<summary>Release notes</summary>

_Sourced from [dotnet-outdated-tool's
releases](https://github.com/dotnet-outdated/dotnet-outdated/releases)._

## 4.7.1

## What's Changed
* Fix --no-restore corrupting .csproj in CPM projects by @​slang25 in
dotnet-outdated/dotnet-outdated#713
* Add Claude Code GitHub Workflow by @​slang25 in
dotnet-outdated/dotnet-outdated#716
* Preserve MSBuild variable references when upgrading packages by
@​hwoodiwiss in
dotnet-outdated/dotnet-outdated#714

## New Contributors
* @​hwoodiwiss made their first contribution in
dotnet-outdated/dotnet-outdated#714

**Full Changelog**:
dotnet-outdated/dotnet-outdated@v4.7.0...v4.7.1

Commits viewable in [compare
view](dotnet-outdated/dotnet-outdated@v4.7.0...v4.7.1).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=dotnet-outdated-tool&package-manager=nuget&previous-version=4.7.0&new-version=4.7.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
slang25 added a commit to GREsau/dotnet-outdated that referenced this pull request Nov 8, 2026
Resolves conflicts with variable tracking (dotnet-outdated#714), the CPM --no-restore
direct write (dotnet-outdated#713), file-based apps (dotnet-outdated#752/dotnet-outdated#755) and short TFM names (dotnet-outdated#750).
AddPackage now takes the VersionRange through all of master's new paths;
single-version paths use the range's lower bound for now (refined in
follow-up commits). Restores the System.Text.Json using needed by master's
tests and adapts DotNetPackageServiceTests to the new signature.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants