Skip to content

Fix dotnet reference add/remove failing when no --project is specified#53594

Merged
baronfel merged 4 commits intomainfrom
copilot/fix-dotnet-reference-add-issue
Mar 25, 2026
Merged

Fix dotnet reference add/remove failing when no --project is specified#53594
baronfel merged 4 commits intomainfrom
copilot/fix-dotnet-reference-add-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

dotnet reference add <path> and dotnet reference remove <path> would fail with Could 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 --project option was provided.

ReferenceListCommand already had the correct behavior; ReferenceAddCommand and ReferenceRemoveCommand were simply missing the ?? Directory.GetCurrentDirectory() fallback.

Changes

  • ReferenceAddCommand / ReferenceRemoveCommand: apply ?? Directory.GetCurrentDirectory() when GetFileOrDirectory() returns null, consistent with ReferenceListCommand
  • Tests: add WhenNoProjectIsSpecifiedItUsesCurrentDirectory for both add and remove covering the exact repro from the issue report
cd ClassLib2
dotnet reference add ../ClassLib1/ClassLib1.csproj  # previously failed, now works
dotnet reference remove ../ClassLib1/ClassLib1.csproj  # same fix
Original prompt

This section details on the original issue you should resolve

<issue_title>dotnet reference add can'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 directory

To Reproduce

dotnet new classlib -n ClassLib1
dotnet new classlib -n ClassLib2
cd ClassLib2
dotnet reference add ../ClassLib1/ClassLib1.csproj

Exceptions (if any)

Could not find project or directory ``.

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

- The IDE (VS / VS Code/ VS4Mac) you're running on, and its version

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

💡 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.

Copilot AI changed the title [WIP] Fix 'dotnet reference add' can't find project in current directory Fix dotnet reference add/remove failing when no --project is specified Mar 25, 2026
Copilot AI requested a review from baronfel March 25, 2026 15:32
@baronfel baronfel marked this pull request as ready for review March 25, 2026 15:37
Copilot AI review requested due to automatic review settings March 25, 2026 15:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 for dotnet reference add project selection when --project isn’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.

@baronfel baronfel enabled auto-merge (squash) March 25, 2026 20:56
@baronfel
Copy link
Copy Markdown
Member

/ba-g netanalyzers macos known issue

@baronfel baronfel merged commit a13e02d into main Mar 25, 2026
27 of 29 checks passed
@baronfel baronfel deleted the copilot/fix-dotnet-reference-add-issue branch March 25, 2026 23:07
@baronfel
Copy link
Copy Markdown
Member

/backport to release/10.0.3xx

@github-actions
Copy link
Copy Markdown
Contributor

Started backporting to release/10.0.3xx (link to workflow run)

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.

dotnet reference add can't find project in current directory

4 participants