Skip to content

File.Copy copies through a symlink #80832

@Forgind

Description

@Forgind

Description

See dotnet/msbuild#8273

It appears that when passed a symlink, File.Copy overwrites the file at the other end of the symlink rather than overwriting the symlink. Is this intentional behavior? If so, perhaps there should be an overload that just overwrites the symlink?

Reproduction Steps

(From the issue above)

  1. Build an executable project with hard or symbolic links enabled
  2. Update the version of referenced NuGet package
  3. Build the application again, but this time, without using hard/symbolic links.
    MSBuild will try to update dependencies in the application's output folder. Sadly, instead of replacing existing links, it replaces actual files in NuGet cache (thus corrupting it).
  • hard-links
dotnet nuget locals --clear all
dotnet new console
dotnet add package newtonsoft.json -v 13.0.1
dotnet build /p:CreateHardLinksForCopyLocalIfPossible=true
dotnet add package newtonsoft.json -v 13.0.2
dotnet build
  • symbolic-links
dotnet nuget locals --clear all
dotnet new console
dotnet add package newtonsoft.json -v 13.0.1
dotnet build /p:CreateSymbolicLinksForCopyLocalIfPossible=true
dotnet add package newtonsoft.json -v 13.0.2
dotnet build

In both cases file newtonsoft.json\13.0.1\lib\netstandard2.0\Newtonsoft.Json.dll is silently replaced with newtonsoft.json\13.0.2\lib\net6.0\Newtonsoft.Json.dll:

Expected behavior

Files in the NuGet cache remain untouched.

Actual behavior

Files in a NuGet package are silently replaced with files from another version.

Regression?

No response

Known Workarounds

Delete the file before calling Copy

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions