runtime icon indicating copy to clipboard operation
runtime copied to clipboard

AppContext.BaseDirectory does not resolve through symlinks on OSX

Open brthor opened this issue 11 years ago • 22 comments

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": { }
        }
    }
  1. dotnet restore
  2. Publish the app dotnet publish --framework dnxcore50 --runtime osx.10.10-x64
  3. 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
  4. Try ./bin/Debug/dnxcore50/osx.10.10-x64/{project name} and you'll get the correct path
  5. Create the symlink ln -s $(pwd)/bin/Debug/dnxcore50/osx.10.10-x64/{project name} $(pwd)/link
  6. Try ./link and you should see a path to the symlink

We don't see this behavior on Ubuntu or Windows

@Sridhar-MS

brthor avatar Nov 21 '15 03:11 brthor

I expect we can solve this, to some degree, by having corehost resolve symlinks completely before passing paths on to the CLR.

analogrelay avatar Nov 21 '15 19:11 analogrelay

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

brthor avatar Nov 21 '15 20:11 brthor

Ah. Also I missed that this was the coreclr repo and not dotnet/cli ;P.

analogrelay avatar Nov 21 '15 21:11 analogrelay

@anurse nice 😂. I was able to repro with the CoreConsole host

brthor avatar Nov 21 '15 21:11 brthor

BaseDirectory simply forwards the call to AppDomain.CurrentDomain.BaseDirectory.

AlexGhiondea avatar Nov 24 '15 08:11 AlexGhiondea

Thanks for the info @AlexGhiondea !

What would the next step to pursue a fix then be?

brthor avatar Nov 24 '15 20:11 brthor

@sergiy-k -- could you take a look?

AlexGhiondea avatar Jan 08 '16 23:01 AlexGhiondea

/cc: @kouvel

sergiy-k avatar Jan 09 '16 01:01 sergiy-k

@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)?

gkhanna79 avatar Jan 15 '16 00:01 gkhanna79

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.

AlexGhiondea avatar Jan 15 '16 01:01 AlexGhiondea

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.

gkhanna79 avatar Jan 15 '16 01:01 gkhanna79

@JeremyKuhne Do you know if there are any places in the various path API where we resolve symlinks?

weshaggard avatar Jan 15 '16 05:01 weshaggard

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.

JeremyKuhne avatar Jan 15 '16 05:01 JeremyKuhne

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.

gkhanna79 avatar Jan 15 '16 20:01 gkhanna79

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.

brthor avatar Jan 26 '16 20:01 brthor

@schellap I take it that the host is passing the resolved symlink to the runtime, right?

gkhanna79 avatar Jan 26 '16 20:01 gkhanna79

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?

brthor avatar Jan 26 '16 20:01 brthor

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.

schellap avatar Jan 26 '16 20:01 schellap

Just verified with @blackdwarf that overriding APP_CONTEXT_BASE_DIRECTORY with a fully resolved symlink in the host, the issue goes away.

schellap avatar Jan 26 '16 20:01 schellap

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.

gkhanna79 avatar Jan 26 '16 20:01 gkhanna79