diff --git a/src/Microsoft.Dnx.Host.Clr/DomainManager.cs b/src/Microsoft.Dnx.Host.Clr/DomainManager.cs index 5ca041df4..86c628a61 100644 --- a/src/Microsoft.Dnx.Host.Clr/DomainManager.cs +++ b/src/Microsoft.Dnx.Host.Clr/DomainManager.cs @@ -31,12 +31,6 @@ public override void InitializeNewDomain(AppDomainSetup appDomainInfo) _info.Main = Main; BindApplicationMain(ref _info); - - if (!string.IsNullOrEmpty(_info.ApplicationBase)) - { - Environment.SetEnvironmentVariable(EnvironmentNames.AppBase, _info.ApplicationBase); - } - appDomainInfo.ApplicationBase = _info.RuntimeDirectory; appDomainInfo.TargetFrameworkName = DetermineAppDomainTargetFramework(); appDomainInfo.ConfigurationFile = Path.Combine(_info.ApplicationBase, Constants.AppConfigurationFileName); diff --git a/src/Microsoft.Dnx.Host.Mono/EntryPoint.cs b/src/Microsoft.Dnx.Host.Mono/EntryPoint.cs index eab5fde78..4b9840999 100644 --- a/src/Microsoft.Dnx.Host.Mono/EntryPoint.cs +++ b/src/Microsoft.Dnx.Host.Mono/EntryPoint.cs @@ -55,25 +55,15 @@ public static int Main(string[] arguments) } } - // Set the default lib path to be next to the entry point location - Environment.SetEnvironmentVariable(EnvironmentNames.DefaultLib, Path.GetDirectoryName(typeof(EntryPoint).Assembly.Location)); - Environment.SetEnvironmentVariable(EnvironmentNames.ConsoleHost, "1"); - - var p = Environment.OSVersion.Platform; - if (p != PlatformID.MacOSX && p != PlatformID.Unix) - { - Environment.SetEnvironmentVariable(EnvironmentNames.DnxIsWindows, "1"); - } - arguments = ExpandCommandLineArguments(arguments); // Set application base dir var appbaseIndex = arguments.ToList().FindIndex(arg => string.Equals(arg, "--appbase", StringComparison.OrdinalIgnoreCase)); - if (appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1)) - { - Environment.SetEnvironmentVariable(EnvironmentNames.AppBase, arguments[appbaseIndex + 1]); - } + + var appBase = appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1) + ? arguments[appbaseIndex + 1] + : Directory.GetCurrentDirectory(); string operatingSystem, osVersion, architecture; GetOsDetails(out operatingSystem, out osVersion, out architecture); @@ -84,7 +74,7 @@ public static int Main(string[] arguments) OsVersion = osVersion, Architecture = architecture, RuntimeDirectory = Path.GetDirectoryName(typeof(EntryPoint).Assembly.Location), - ApplicationBase = appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1) ? arguments[appbaseIndex + 1] : Directory.GetCurrentDirectory(), + ApplicationBase = appBase, // NOTE(anurse): Mono is always "dnx451" (for now). TargetFramework = new FrameworkName("DNX", new Version(4, 5, 1)), HandleExceptions = true diff --git a/src/Microsoft.Dnx.Host/Bootstrapper.cs b/src/Microsoft.Dnx.Host/Bootstrapper.cs index 0580b57e1..e8e6f16aa 100644 --- a/src/Microsoft.Dnx.Host/Bootstrapper.cs +++ b/src/Microsoft.Dnx.Host/Bootstrapper.cs @@ -4,14 +4,11 @@ using System; using System.Collections.Generic; -using System.IO; -using System.Reflection; using System.Runtime.Versioning; using System.Threading.Tasks; using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime.Common; using Microsoft.Dnx.Runtime.Common.DependencyInjection; -using Microsoft.Dnx.Runtime.Common.Impl; using Microsoft.Dnx.Runtime.Infrastructure; using Microsoft.Dnx.Runtime.Loader; @@ -26,7 +23,7 @@ public Bootstrapper(IEnumerable searchPaths) _searchPaths = searchPaths; } - public Task RunAsync(List args, IRuntimeEnvironment env, FrameworkName targetFramework) + public Task RunAsync(List args, IRuntimeEnvironment env, string appBase, FrameworkName targetFramework) { var accessor = LoadContextAccessor.Instance; var container = new LoaderContainer(); @@ -48,17 +45,12 @@ public Task RunAsync(List args, IRuntimeEnvironment env, FrameworkN } #if DNX451 - string applicationBaseDirectory = Environment.GetEnvironmentVariable(EnvironmentNames.AppBase); - - if (string.IsNullOrEmpty(applicationBaseDirectory)) - { - applicationBaseDirectory = Directory.GetCurrentDirectory(); - } + string applicationBaseDirectory = appBase; // Set the app domain variable so that AppContext.BaseDirectory works on .NET Framework (and hopefully mono) AppDomain.CurrentDomain.SetData("APP_CONTEXT_BASE_DIRECTORY", applicationBaseDirectory); #else - string applicationBaseDirectory = AppContext.BaseDirectory; + var applicationBaseDirectory = AppContext.BaseDirectory; #endif var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug"; diff --git a/src/Microsoft.Dnx.Host/NativeMethods.cs b/src/Microsoft.Dnx.Host/NativeMethods.cs deleted file mode 100644 index 43b365053..000000000 --- a/src/Microsoft.Dnx.Host/NativeMethods.cs +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Runtime.InteropServices; - -namespace Microsoft.Dnx.Runtime -{ - internal static class NativeMethods - { - public unsafe static string Uname() - { - var buffer = stackalloc byte[8192]; - try - { - if (uname((IntPtr)buffer) == 0) - { - return Marshal.PtrToStringAnsi((IntPtr)buffer); - } - } - catch - { - } - - return null; - } - - // Linux and Mac import - [DllImport("libc")] - private static extern int uname(IntPtr buf); - -#if DNXCORE50 - public static Version OSVersion - { - get - { - uint dwVersion = GetVersion(); - - int major = (int)(dwVersion & 0xFF); - int minor = (int)((dwVersion >> 8) & 0xFF); - - return new Version(major, minor); - } - } - - private static uint GetVersion() - { - try - { - return GetVersion_ApiSet(); - } - catch - { - try - { - return GetVersion_Kernel32(); - } - catch - { - return 0; - } - } - - } - - // The API set is required by OneCore based systems - // and it is available only on Win 8 and newer - [DllImport("api-ms-win-core-sysinfo-l1-2-1", EntryPoint = "GetVersion")] - private static extern uint GetVersion_ApiSet(); - - // For Win 7 and Win 2008 compatibility - [DllImport("kernel32.dll", EntryPoint = "GetVersion")] - private static extern uint GetVersion_Kernel32(); -#endif - } -} diff --git a/src/Microsoft.Dnx.Host/RuntimeBootstrapper.cs b/src/Microsoft.Dnx.Host/RuntimeBootstrapper.cs index 9594ad908..9aa7d4454 100644 --- a/src/Microsoft.Dnx.Host/RuntimeBootstrapper.cs +++ b/src/Microsoft.Dnx.Host/RuntimeBootstrapper.cs @@ -18,16 +18,14 @@ public static class RuntimeBootstrapper public static int Execute(string[] args, BootstrapperContext bootstrapperContext) { - // If we're a console host then print exceptions to stderr - var printExceptionsToStdError = Environment.GetEnvironmentVariable(EnvironmentNames.ConsoleHost) == "1"; - try { return ExecuteAsync(args, bootstrapperContext).GetAwaiter().GetResult(); } catch (Exception ex) { - if (printExceptionsToStdError) + // If we're a console host then print exceptions to stderr + if (bootstrapperContext.HandleExceptions) { PrintErrors(ex); return 1; @@ -87,7 +85,7 @@ public static Task ExecuteAsync(string[] args, BootstrapperContext bootstra var optionFramework = app.Option("--framework ", "Set the framework version to use when running (i.e. dnx451, dnx452, dnx46, ...)", CommandOptionType.SingleValue); #endif - var env = new RuntimeEnvironment(); + var env = new RuntimeEnvironment(bootstrapperContext); app.HelpOption("-?|-h|--help"); app.VersionOption("--version", @@ -154,18 +152,18 @@ public static Task ExecuteAsync(string[] args, BootstrapperContext bootstra } // Resolve the lib paths - IEnumerable searchPaths = ResolveSearchPaths(optionLib.Values, app.RemainingArguments); + IEnumerable searchPaths = + ResolveSearchPaths(bootstrapperContext.RuntimeDirectory, optionLib.Values, app.RemainingArguments); var bootstrapper = new Bootstrapper(searchPaths); - return bootstrapper.RunAsync(app.RemainingArguments, env, bootstrapperContext.TargetFramework); + + return bootstrapper.RunAsync(app.RemainingArguments, env, bootstrapperContext.ApplicationBase, bootstrapperContext.TargetFramework); } - private static IEnumerable ResolveSearchPaths(List libPaths, List remainingArgs) + private static IEnumerable ResolveSearchPaths(string defaultLibPath, List libPaths, List remainingArgs) { var searchPaths = new List(); - var defaultLibPath = Environment.GetEnvironmentVariable(EnvironmentNames.DefaultLib); - if (!string.IsNullOrEmpty(defaultLibPath)) { // Add the default lib folder if specified diff --git a/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs b/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs index eb4e3be3b..2f081a1a8 100644 --- a/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs +++ b/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs @@ -3,6 +3,7 @@ using System; using System.Reflection; +using Microsoft.Dnx.Host; namespace Microsoft.Dnx.Runtime { @@ -14,33 +15,22 @@ public class RuntimeEnvironment : IRuntimeEnvironment private string _runtimeVersion; - public RuntimeEnvironment() + public RuntimeEnvironment(BootstrapperContext bootstrapperContext) { #if DNXCORE50 RuntimeType = RuntimeTypes.CoreCLR; - RuntimeArchitecture = IntPtr.Size == 8 ? RuntimeArchitectures.X64 : RuntimeArchitectures.X86; #else RuntimeType = Type.GetType("Mono.Runtime") == null ? RuntimeTypes.CLR : RuntimeTypes.Mono; - RuntimeArchitecture = Environment.Is64BitProcess ? RuntimeArchitectures.X64 : RuntimeArchitectures.X86; #endif - - // This is a temporary workaround until we pass a struct with OS information from native code - if (Environment.GetEnvironmentVariable(EnvironmentNames.DnxIsWindows) == "1") - { - _osName = RuntimeOperatingSystems.Windows; - } + RuntimeArchitecture = bootstrapperContext.Architecture; + _osName = bootstrapperContext.OperatingSystem; + _osVersion = bootstrapperContext.OsVersion; } public string OperatingSystem { get { - if (_osName == null) - { - string uname = NativeMethods.Uname(); - _osName = string.IsNullOrEmpty(uname) ? RuntimeOperatingSystems.Windows : uname; - } - return _osName; } } @@ -49,20 +39,6 @@ public string OperatingSystemVersion { get { - if (OperatingSystem != RuntimeOperatingSystems.Windows) - { - return null; - } - - if (_osVersion == null) - { -#if DNXCORE50 - _osVersion = NativeMethods.OSVersion.ToString(); -#else - _osVersion = Environment.OSVersion.Version.ToString(); -#endif - } - return _osVersion; } } diff --git a/src/Microsoft.Dnx.Runtime.Internals/Constants.cs b/src/Microsoft.Dnx.Runtime.Internals/Constants.cs index 2925c6019..a3328fa9a 100644 --- a/src/Microsoft.Dnx.Runtime.Internals/Constants.cs +++ b/src/Microsoft.Dnx.Runtime.Internals/Constants.cs @@ -10,12 +10,6 @@ internal static class RuntimeTypes public static readonly string Mono = nameof(Mono); } - internal static class RuntimeArchitectures - { - public static readonly string X86 = "x86"; - public static readonly string X64 = "x64"; - } - internal static class RuntimeOperatingSystems { public static readonly string Windows = nameof(Windows); diff --git a/src/Microsoft.Dnx.Runtime.Internals/RuntimeEnvironmentExtensions.cs b/src/Microsoft.Dnx.Runtime.Internals/RuntimeEnvironmentExtensions.cs index 5b4001512..9e27aa80e 100644 --- a/src/Microsoft.Dnx.Runtime.Internals/RuntimeEnvironmentExtensions.cs +++ b/src/Microsoft.Dnx.Runtime.Internals/RuntimeEnvironmentExtensions.cs @@ -98,12 +98,14 @@ private static string GetRuntimeOsName(this IRuntimeEnvironment env) { ver = "10"; } + + return os + ver; } else { os = os.ToLowerInvariant(); // Just use the lower-case full name of the OS as the RID OS } - return os + ver; + return os; } } } diff --git a/src/Microsoft.Dnx.Runtime.Sources/Impl/EnvironmentNames.cs b/src/Microsoft.Dnx.Runtime.Sources/Impl/EnvironmentNames.cs index 1ff0fd30b..c44b359db 100644 --- a/src/Microsoft.Dnx.Runtime.Sources/Impl/EnvironmentNames.cs +++ b/src/Microsoft.Dnx.Runtime.Sources/Impl/EnvironmentNames.cs @@ -16,12 +16,9 @@ internal static class EnvironmentNames public const string AppBase = "DNX_APPBASE"; public const string Framework = "DNX_FRAMEWORK"; public const string Configuration = "DNX_CONFIGURATION"; - public const string ConsoleHost = "DNX_CONSOLE_HOST"; - public const string DefaultLib = "DNX_DEFAULT_LIB"; public const string BuildKeyFile = "DNX_BUILD_KEY_FILE"; public const string BuildDelaySign = "DNX_BUILD_DELAY_SIGN"; public const string PortablePdb = "DNX_BUILD_PORTABLE_PDB"; - public const string DnxIsWindows = "DNX_IS_WINDOWS"; public const string AspNetLoaderPath = "DNX_ASPNET_LOADER_PATH"; public const string DnxDisableMinVersionCheck = "DNX_NO_MIN_VERSION_CHECK"; } diff --git a/src/dnx/dnx.cpp b/src/dnx/dnx.cpp index c3e0d4765..b183690a6 100644 --- a/src/dnx/dnx.cpp +++ b/src/dnx/dnx.cpp @@ -201,14 +201,8 @@ bool GetApplicationBase(const dnx::xstring_t& currentDirectory, int argc, dnx::c int CallApplicationProcessMain(int argc, dnx::char_t* argv[], dnx::trace_writer& trace_writer) { - // Set the DNX_CONOSLE_HOST flag which will print exceptions to stderr instead of throwing - SetConsoleHost(); - const auto currentDirectory = GetNativeBootstrapperDirectory(); - // Set the DEFAULT_LIB environment variable to be the same directory as the exe - SetEnvironmentVariable(_X("DNX_DEFAULT_LIB"), currentDirectory.c_str()); - // Set the FRAMEWORK environment variable to the value provided on the command line // (it needs to be available BEFORE the application main is called) auto frameworkName = GetOptionValue(argc, argv, _X("--framework")); @@ -247,7 +241,6 @@ int CallApplicationProcessMain(int argc, dnx::char_t* argv[], dnx::trace_writer& _X("dnx.coreclr.so"); #else _X("dnx.clr.dll"); - SetEnvironmentVariable(_X("DNX_IS_WINDOWS"), _X("1")); #endif // Note: need to keep as ASCII as GetProcAddress function takes ASCII params diff --git a/src/dnx/pal.h b/src/dnx/pal.h index a0f5fe9df..f16547626 100644 --- a/src/dnx/pal.h +++ b/src/dnx/pal.h @@ -10,7 +10,6 @@ dnx::xstring_t GetNativeBootstrapperDirectory(); void WaitForDebuggerToAttach(); bool IsTracingEnabled(); -void SetConsoleHost(); bool GetAppBasePathFromEnvironment(dnx::char_t* szPath); bool GetFullPath(const dnx::char_t* szPath, dnx::char_t* szFullPath); int CallApplicationMain(const dnx::char_t* moduleName, const char* functionName, CALL_APPLICATION_MAIN_DATA* data, dnx::trace_writer& trace_writer); diff --git a/src/dnx/pal.unix.cpp b/src/dnx/pal.unix.cpp index 5b605959c..ecd9dceff 100644 --- a/src/dnx/pal.unix.cpp +++ b/src/dnx/pal.unix.cpp @@ -18,16 +18,6 @@ bool IsTracingEnabled() return dnxTraceEnv != NULL && (strcmp(dnxTraceEnv, "1") == 0); } -void SetConsoleHost() -{ - char* dnxConsoleHostEnv = getenv("DNX_CONSOLE_HOST"); - - if (dnxConsoleHostEnv == NULL) - { - setenv("DNX_CONSOLE_HOST", "1", 1); - } -} - bool GetAppBasePathFromEnvironment(char* szPath) { char* appBaseEnv = getenv("DNX_APPBASE"); diff --git a/src/dnx/pal.win32.cpp b/src/dnx/pal.win32.cpp index 32e3281ee..372cc7d41 100644 --- a/src/dnx/pal.win32.cpp +++ b/src/dnx/pal.win32.cpp @@ -42,16 +42,6 @@ bool IsTracingEnabled() return GetEnvironmentVariable(L"DNX_TRACE", buff, 2) == 1 && buff[0] == L'1'; } -void SetConsoleHost() -{ - TCHAR szConsoleHost[2]; - DWORD nEnvConsoleHostSize = GetEnvironmentVariable(_T("DNX_CONSOLE_HOST"), szConsoleHost, 2); - if (nEnvConsoleHostSize == 0) - { - SetEnvironmentVariable(_T("DNX_CONSOLE_HOST"), _T("1")); - } -} - bool GetAppBasePathFromEnvironment(LPTSTR pszAppBase) { DWORD dwAppBase = GetEnvironmentVariable(_T("DNX_APPBASE"), pszAppBase, MAX_PATH); @@ -132,7 +122,6 @@ int CallApplicationMain(const wchar_t* moduleName, const char* functionName, CAL if (runtime_new_path.length() > 0) { trace_writer.write(std::wstring(L"Redirecting runtime to: ").append(runtime_new_path), true); - SetEnvironmentVariable(_T("DNX_DEFAULT_LIB"), runtime_new_path.c_str()); data->runtimeDirectory = runtime_new_path.c_str(); } diff --git a/test/Microsoft.Dnx.ApplicationHost.FunctionalTests/AppHostTests.cs b/test/Microsoft.Dnx.ApplicationHost.FunctionalTests/AppHostTests.cs index ebc11b2d8..1560ebe2f 100644 --- a/test/Microsoft.Dnx.ApplicationHost.FunctionalTests/AppHostTests.cs +++ b/test/Microsoft.Dnx.ApplicationHost.FunctionalTests/AppHostTests.cs @@ -127,7 +127,7 @@ public void AppHostShowsErrorWhenGivenSubcommandWasNotFoundInProjectJson(string arguments: "invalid", stdOut: out stdOut, stdErr: out stdErr, - environment: new Dictionary { { EnvironmentNames.AppBase, projectPath } }, + environment: new Dictionary { }, workingDir: projectPath); Assert.NotEqual(0, exitCode); diff --git a/test/Microsoft.Dnx.Host.Tests/RuntimeEnvironmentTests.cs b/test/Microsoft.Dnx.Host.Tests/RuntimeEnvironmentTests.cs index ca8929210..624e06f10 100644 --- a/test/Microsoft.Dnx.Host.Tests/RuntimeEnvironmentTests.cs +++ b/test/Microsoft.Dnx.Host.Tests/RuntimeEnvironmentTests.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using Microsoft.AspNet.Testing.xunit; +using Microsoft.Dnx.Host; using Microsoft.Dnx.Runtime; using Xunit; @@ -11,44 +12,10 @@ namespace dnx.hostTests { public class RuntimeEnvironmentTests { - [Fact] - public void RuntimeEnvironment_OS() - { - RuntimeEnvironment runtimeEnv = new RuntimeEnvironment(); - - var os = NativeMethods.Uname(); - if (os == null) - { - os = "Windows"; - Assert.NotNull(runtimeEnv.OperatingSystemVersion); - } - else - { - Assert.Null(runtimeEnv.OperatingSystemVersion); - } - - Assert.Equal(os, runtimeEnv.OperatingSystem); - } - - [Fact] - public void RuntimeEnvironment_RuntimeVersion() - { - RuntimeEnvironment runtimeEnv = new RuntimeEnvironment(); - Assert.NotNull(runtimeEnv.RuntimeVersion); - } - - [Fact] - public void RuntimeEnvironment_RuntimeArchitecture() - { - RuntimeEnvironment runtimeEnv = new RuntimeEnvironment(); - var runtimeArchitecture = IntPtr.Size == 8 ? "x64" : "x86"; - Assert.Equal(runtimeArchitecture, runtimeEnv.RuntimeArchitecture); - } - [Fact] public void RuntimeEnvironment_RuntimeType() { - RuntimeEnvironment runtimeEnv = new RuntimeEnvironment(); + RuntimeEnvironment runtimeEnv = new RuntimeEnvironment(new BootstrapperContext()); #if DNXCORE50 Assert.Equal("CoreCLR", runtimeEnv.RuntimeType); #else diff --git a/test/dnx.tests/pal.tests.cpp b/test/dnx.tests/pal.tests.cpp index 2bc7c0429..ae6646cf7 100644 --- a/test/dnx.tests/pal.tests.cpp +++ b/test/dnx.tests/pal.tests.cpp @@ -11,7 +11,6 @@ dnx::xstring_t GetNativeBootstrapperDirectory() { return L""; } void WaitForDebuggerToAttach() {} bool IsTracingEnabled() { return true; } -void SetConsoleHost() {} bool GetAppBasePathFromEnvironment(wchar_t* /*szPath*/) { return false; }; bool GetFullPath(const wchar_t* /*szPath*/, wchar_t* /*szFullPath*/) { return false; } int CallApplicationMain(const wchar_t* /*moduleName*/, const char* /*functionName*/, CALL_APPLICATION_MAIN_DATA* /*data*/, dnx::trace_writer& /*trace_writer*/) { return 3; }