Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create proper relative symbolic links #8251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

manfred-brands
Copy link
Contributor

Fixes #3730

Context

Symbolic link in the bin folder to obj/Assembly.dll were created just like that.

Changes Made

Converted relative paths to be relative to the target directory resulting in: ../obj/Assembly.dll
Modified NativeMethods.MakeSymbolicLink

Testing

Updated one unit test to test both absolute and relative paths. This failed before my changes, succeeds now.
As Path.GetRelativePath is not supported on netstandard2.0 and net472, I added a local version with appropriate unit tests.

Notes

@manfred-brands
Copy link
Contributor Author

The azure pipeline doesn't give a proper line number for the failing MacOS test and I don't see any artefacts I can look at.
As I don't have access to a Mac I cannot run those test my self. Any suggestions on how to resolve this?

@Forgind
Copy link
Member

Forgind commented Dec 21, 2022

Microsoft.Build.UnitTests.CopySymbolicLink_Tests.CopyToDestinationFolderWithSymbolicLinkCheck(useRelative: True)
System.IO.FileNotFoundException : Could not find file '/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/uiack5y3.3xu/2A333ED756AF4dc392E728D0F864A398/tmp5e9beda9b70849c6a8b53561d26ccb64.tmp'.
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, UnixFileMode openPermissions, Int64& fileLength, UnixFileMode& filePermissions, Func`4 createOpenException)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
   at System.IO.File.ReadAllText(String path, Encoding encoding)
   at Microsoft.Build.UnitTests.CopySymbolicLink_Tests.CopyToDestinationFolderWithSymbolicLinkCheck(Boolean useRelative) in /Users/runner/work/1/s/src/Tasks.UnitTests/Copy_Tests.cs:line 2441
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)\

@manfred-brands manfred-brands force-pushed the 3730-relativeSymbolicLinks branch 4 times, most recently from 5da1379 to 3c4f76c Compare January 4, 2023 07:24
errorMessage = symbolicLinkCreated ? null : "The link() library call failed with the following error code: " + Marshal.GetLastWin32Error();
}

return symbolicLinkCreated;
}

internal static string GetRelativePath(string relativeTo, string path)
Copy link
Member

Choose a reason for hiding this comment

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

Did you write this method yourself? It looks correct to me, as far as I can tell, but it's substantially longer than an answer using URIs:
https://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path

Might that work? My primary concern is that it'd be pretty easy to get this slightly wrong in an important way and have it fail. That is, I think it's right, but I'm not confident.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As the comment in the method says, it is based upon .NET 7 Path.cs
The unit test cases also come from .NET7.

I updated the code to using the Uri.MakeRelativeUri method as used in the article, but many of the unit test cases failed. Even after adding a trailing slash, still many tests failed. Seeing all the comments on the article, I'm not the only one. Uri also have a far more restricted character set than files.

I therefore think the original code is appropriate.

Why does msbuild need a build target for net472 and netstandard20? Do you update msbuild for older SDKs?

Copy link
Member

@JanKrivanek JanKrivanek left a comment

Choose a reason for hiding this comment

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

LGTM

src/Framework/NativeMethods.cs Outdated Show resolved Hide resolved
@JanKrivanek
Copy link
Member

@ladipro this PR will need RequiresSymbolicLinksTheory - can you please add it (reusing the RequiresSymbolicLinksFact)

@manfred-brands
Copy link
Contributor Author

@ladipro this PR will need RequiresSymbolicLinksTheory - can you please add it (reusing the RequiresSymbolicLinksFact)

Done.

@ladipro
Copy link
Member

ladipro commented Mar 13, 2023

@ladipro this PR will need RequiresSymbolicLinksTheory - can you please add it (reusing the RequiresSymbolicLinksFact)

Done.

Thank you for taking care of it! We may want to move the code from the constructor to a helper method so it's not duplicated in RequiresSymbolicLinksTheory and RequiresSymbolicLinksFact but that's not a blocking issue.

@ladipro
Copy link
Member

ladipro commented Mar 13, 2023

And apologies for missing Jan's mention.

@rainersigwald rainersigwald modified the milestones: VS 17.6, VS 17.7 Mar 28, 2023
@rainersigwald rainersigwald modified the milestones: VS 17.7, VS 17.8 Jun 29, 2023
@rainersigwald rainersigwald modified the milestones: VS 17.8, VS 17.9 Sep 21, 2023
@maridematte maridematte modified the milestones: VS 17.9, VS 17.13 Aug 27, 2024
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.

UseSymboliclinksIfPossible for local resources leads to unresolved path in out dir
6 participants