AppContext.BaseDirectory does not resolve through symlinks on OSX
I was hitting this issue this using a symlink to a CoreConsole Application on OSX 10.10.5. It is causing dotnet compile in http://github.com/dotnet/cli to fail to resolve file paths local to it's own location.
CoreCLR Version: 1.0.1-beta-23516 CoreConsole Version: 1.0.0-beta-23516
Here are the Repro Steps (dotnet cli required for publish step)
Create Program.cs
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(AppContext.BaseDirectory);
}
}
}
Create project.json
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.Runtime": "1.0.1-beta-*",
"Microsoft.NETCore.ConsoleHost":"1.0.0-*",
"System.IO": "4.0.10-beta-*",
"System.Console": "4.0.0-beta-*",
"System.Runtime": "4.0.21-beta-*",
"System.AppContext":"4.0.0-*"
},
"frameworks": {
"dnxcore50": { }
}
}
dotnet restore- Publish the app
dotnet publish --framework dnxcore50 --runtime osx.10.10-x64 mv ./bin/Debug/dnxcore50/osx.10.10-x64/coreconsole ./bin/Debug/dnxcore50/osx.10.10-x64/{project name}Where {project name} is the name of the parent folder- Try
./bin/Debug/dnxcore50/osx.10.10-x64/{project name}and you'll get the correct path - Create the symlink
ln -s $(pwd)/bin/Debug/dnxcore50/osx.10.10-x64/{project name} $(pwd)/link - Try
./linkand you should see a path to the symlink
We don't see this behavior on Ubuntu or Windows
@Sridhar-MS
I expect we can solve this, to some degree, by having corehost resolve symlinks completely before passing paths on to the CLR.
@anurse In corehost the host path is being resolved through the symlink. The app path is actually set correctly which makes me think something else is going on.
On Nov 21, 2015, at 11:33 AM, Andrew Stanton-Nurse <[email protected]mailto:[email protected]> wrote:
I expect we can solve this, to some degree, by having corehost resolve symlinks completely before passing paths on to the CLR.
Reply to this email directly or view it on GitHubhttps://github.com/dotnet/coreclr/issues/2128#issuecomment-158676388.
Ah. Also I missed that this was the coreclr repo and not dotnet/cli ;P.
@anurse nice 😂. I was able to repro with the CoreConsole host
BaseDirectory simply forwards the call to AppDomain.CurrentDomain.BaseDirectory.
Thanks for the info @AlexGhiondea !
What would the next step to pursue a fix then be?
@sergiy-k -- could you take a look?
/cc: @kouvel
@AlexGhiondea @weshaggard BaseDirectory is reading the value set by the host. While the host could fix this, the question is how do other File APIs deal with symlinks? Are they doing some sort of symlink->actual path resolution? If so, shouldnt that resolution be applied in this case as well (since fixing host only addresses one instance of this while FX could have other scenarios for the same)?
I am not sure if there are other places where we handle symlink (or how we do that) @ericstj might know.
In this particular case I am not sure what else we could do to resolve the symlink. We are not doing anything special other than forwarding the value from the BaseDirectory.
I believe @schellap can look into resolving symlinks but the question is if we are affected in a similar manner in other scenarios or not.
@JeremyKuhne Do you know if there are any places in the various path API where we resolve symlinks?
I don't think there is any place we explicitly do so. On Windows you have to use GetFinalPathNameByHandle() to resolve symlinks and afaik we've never done this. I ultimately intend to provide such an API in System.IO.Path, but I don't think there is an issue open for it.
I ultimately intend to provide such an API in System.IO.Path, but I don't think there is an issue open for it.
Having the FX implementation use such an API as part of the normalization process is the right thing to do so that it does not rely on external artifacts to do the right thing. @JeremyKuhne Please use this issue to track getting such an API implemented and fixing this. I am assigning this to you for the same.
For the short-term, @schellap will be fixing the host to resolve symlinks.
Took a look at this right now with @blackdwarf and @schellap
The APP_PATH as set by the host appears to be correct, yet AppContext.BaseDirectory is still resolving to the location of the symlink.
@schellap I take it that the host is passing the resolved symlink to the runtime, right?
This makes it appear so:
https://github.com/dotnet/cli/blob/master/src/corehost/cli/args.cpp#L36 https://github.com/dotnet/cli/blob/master/src/corehost/cli/hostpolicy.cpp#L165
I'm not sure if AppContext.Basedirectory derived from this "exe_path" or from the APP_PATHS property?
Yes, the host is passing resolved symlink to the runtime for the following properties:
"TRUSTED_PLATFORM_ASSEMBLIES",
"APP_PATHS",
"APP_NI_PATHS",
"NATIVE_DLL_SEARCH_DIRECTORIES",
"PLATFORM_RESOURCE_ROOTS",
For now, we can workaround the bug by overriding APP_CONTEXT_BASE_DIRECTORY also in the host, but this needs to be fixed properly in coreclr as well.
Just verified with @blackdwarf that overriding APP_CONTEXT_BASE_DIRECTORY with a fully resolved symlink in the host, the issue goes away.
If the host is doing the right thing, we should understand where it is going wrong before applying any workaround. @schellap is there a reason host does not pass APP_CONTEXT_BASE_DIRECTORY value to the runtime? I thought @AlexGhiondea mentioned that the property is used for AppDomain initialization.