Fix dotnet reference add/remove failing when no --project is specified#53594
Merged
Fix dotnet reference add/remove failing when no --project is specified#53594
dotnet reference add/remove failing when no --project is specified#53594Conversation
…ect specified Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/ad068b1e-b490-481a-8d68-0865680dcfc2
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/ad068b1e-b490-481a-8d68-0865680dcfc2
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/ad068b1e-b490-481a-8d68-0865680dcfc2
Copilot
AI
changed the title
[WIP] Fix 'dotnet reference add' can't find project in current directory
Fix Mar 25, 2026
dotnet reference add/remove failing when no --project is specified
baronfel
approved these changes
Mar 25, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes dotnet reference add/remove <path> failing when invoked from a directory that already contains a project file and no --project is specified, by aligning add/remove behavior with existing list behavior (fallback to current directory).
Changes:
- Add
?? Directory.GetCurrentDirectory()fallback fordotnet reference addproject selection when--projectisn’t provided. - Add the same fallback for
dotnet reference remove. - Add regression tests covering “no
--project, project is in current directory” for both add and remove.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Cli/dotnet/Commands/Reference/Add/ReferenceAddCommand.cs | Falls back to current directory when --project is omitted. |
| src/Cli/dotnet/Commands/Reference/Remove/ReferenceRemoveCommand.cs | Falls back to current directory when --project is omitted. |
| test/dotnet.Tests/CommandTests/Reference/Add/GivenDotnetAddReference.cs | Adds regression test for dotnet reference add with no --project. |
| test/dotnet.Tests/CommandTests/Reference/Remove/GivenDotnetRemoveP2P.cs | Adds regression test for dotnet reference remove with no --project. |
MiYanni
approved these changes
Mar 25, 2026
Member
|
/ba-g netanalyzers macos known issue |
Member
|
/backport to release/10.0.3xx |
Contributor
|
Started backporting to |
This was referenced Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
dotnet reference add <path>anddotnet reference remove <path>would fail withCould not find project or directory ''when run from a directory containing a project file, because neither command fell back to the current directory when no--projectoption was provided.ReferenceListCommandalready had the correct behavior;ReferenceAddCommandandReferenceRemoveCommandwere simply missing the?? Directory.GetCurrentDirectory()fallback.Changes
ReferenceAddCommand/ReferenceRemoveCommand: apply?? Directory.GetCurrentDirectory()whenGetFileOrDirectory()returns null, consistent withReferenceListCommandWhenNoProjectIsSpecifiedItUsesCurrentDirectoryfor bothaddandremovecovering the exact repro from the issue reportOriginal prompt
This section details on the original issue you should resolve
<issue_title>
dotnet reference addcan't find project in current directory</issue_title><issue_description><!--
Please keep in mind that the GitHub issue tracker is not intended as a general support forum, but for reporting non-security bugs and feature requests.
If you believe you have an issue that affects the SECURITY of the platform, please do NOT create an issue and instead email your issue details to secure@microsoft.com. Your report may be eligible for our bug bounty but ONLY if it is reported through email.
For other types of questions, consider using StackOverflow.
-->
Describe the bug
When I use
dotnet reference add ../ClassLib1/ClassLib1.csproj, I get an error message, even though there's a csproj in the current directoryTo Reproduce
Exceptions (if any)
Further technical details
details of dotnet --info
.NET SDK: Version: 10.0.100 Commit: b0f34d51fc Workload version: 10.0.100-manifests.5fb86115 MSBuild version: 18.0.2+b0f34d51f
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.100\
.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 10.0.0
Architecture: x64
Commit: b0f34d51fc
.NET SDKs installed:
8.0.416 [C:\Program Files\dotnet\sdk]
9.0.308 [C:\Program Files\dotnet\sdk]
10.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
DOTNET_ROLL_FORWARD_TO_PRERELEASE [1]
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
<agent_instructions>investigate and fix this report</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@zivkan I found that using `--project .` works, and is a little bit faster for me to type than `--project RealName.csproj`. Anyway, the point is that the command doesn't appear to use the current directory when no value is passed. @zivkan FWIW, `dotnet package add` has the same issue.dotnet reference addcan't find project in current directory #51897💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.