Skip to content

Issue with dotnet build/restore of .NET project when a path to the project contains symbolic link #23430

Open
@echesakov

Description

@echesakov

Description

I am observing different behavior when restoring/building a .NET project that is referred to by the full path that contains "realpath" vs. the full path that contains a symbolic ink.

In the first case, the dotnet restore/dotnet build commands succeed. In the second case, they fail.

To Reproduce

  1. Create couple directories
mkdir -p realdir/App realdir/Lib

and a symbolic link to the root directory realdir

ln -s realdir symlink

stat -F symlink
lrwxr-xr-x 1 echesako staff 7 Jan 13 19:41:49 2022 symlink@ -> realdir
  1. Create a library project realdir/Lib/Lib.csproj that has a reference to some NuGet package (e.g. Newtonsoft.Json)
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  </ItemGroup>

</Project>
  1. Create another project realdir/App/App.csproj that references the first project
<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <ProjectReference Include="..\Lib\Lib.csproj" />
  </ItemGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

</Project>
  1. Create realdir/App/Program.cs that consumes a namespace/class/method from the package that was referenced in step 2.
using System;
using Newtonsoft;

Console.WriteLine("Hello, World!");
  1. Run the following commands where $RootDir contains a full path to the realdir. In my case, $RootDir is set to /Users/echesako/realdir
dotnet restore $RootDir/App/App.csproj --packages $RootDir/Packages
dotnet build $RootDir/App/App.csproj --no-restore /p:NuGetPackageRoot=$RootDir/Packages

The result will be successful build:

  Determining projects to restore...
  Restored /Users/echesako/realdir/Lib/Lib.csproj (in 300 ms).
  Restored /Users/echesako/realdir/App/App.csproj (in 300 ms).
Microsoft (R) Build Engine version 17.2.0-preview-22062-10+32e260b12 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  Lib -> /Users/echesako/realdir/Lib/bin/Debug/net7.0/Lib.dll
  App -> /Users/echesako/realdir/App/bin/Debug/net7.0/App.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.85
  1. Set $RootDir to a full path to the symlink. In my case, $RootDir is set to /Users/echesako/symlink and re-run the commands in the previous step (you would need to remove bin/ and obj/ directories in App and Lib before that)

The result will be failed build:

  Determining projects to restore...
  Restored /Users/echesako/symlink/App/App.csproj (in 40 ms).
  Restored /Users/echesako/realdir/Lib/Lib.csproj (in 300 ms).
Microsoft (R) Build Engine version 17.2.0-preview-22062-10+32e260b12 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  Lib -> /Users/echesako/symlink/Lib/bin/Debug/net7.0/Lib.dll
/Users/echesako/realdir/App/Program.cs(2,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [/Users/echesako/symlink/App/App.csproj]

Build FAILED.

/Users/echesako/realdir/App/Program.cs(2,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [/Users/echesako/symlink/App/App.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.70

You can see that during dotnet restore the Lib project was referred by its real name.
I suspect this might be an origin of the issue.

Further technical details

echesako@Egors-Mac-mini ~> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   7.0.100-alpha.1.22063.12
 Commit:    bbfca0cb1f

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  12.1
 OS Platform: Darwin
 RID:         osx.12-arm64
 Base Path:   /Users/echesako/Downloads/dotnet-sdk-osx-arm64/sdk/7.0.100-alpha.1.22063.12/

Host (useful for support):
  Version: 7.0.0-alpha.1.22061.11
  Commit:  ef7ff07109

.NET SDKs installed:
  7.0.100-alpha.1.22063.12 [/Users/echesako/Downloads/dotnet-sdk-osx-arm64/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.0-alpha.1.22062.12 [/Users/echesako/Downloads/dotnet-sdk-osx-arm64/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.0-alpha.1.22061.11 [/Users/echesako/Downloads/dotnet-sdk-osx-arm64/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions