From 34d24459161e9dfe6fd8fa50e268c7eefcc83d50 Mon Sep 17 00:00:00 2001 From: Bryan Thornbury Date: Thu, 7 Apr 2016 16:20:51 -0700 Subject: [PATCH] Add --runtimeconfig to the hostfxr, and use this option for the xunit scenario. --- .../IPackagedCommandSpecFactory.cs | 3 +- .../PackagedCommandSpecFactory.cs | 32 +++++++++++++------ .../ProjectDependenciesCommandResolver.cs | 23 +++---------- .../ProjectToolsCommandResolver.cs | 3 +- src/corehost/cli/fxr/fx_muxer.cpp | 12 +++++-- src/corehost/cli/hostpolicy.cpp | 8 +++-- src/corehost/cli/libhost.cpp | 7 ++-- src/corehost/cli/libhost.h | 2 +- 8 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IPackagedCommandSpecFactory.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IPackagedCommandSpecFactory.cs index 61990262f..3c5316456 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IPackagedCommandSpecFactory.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IPackagedCommandSpecFactory.cs @@ -17,7 +17,8 @@ namespace Microsoft.DotNet.Cli.Utils IEnumerable allowedExtensions, string nugetPackagesRoot, CommandResolutionStrategy commandResolutionStrategy, - string depsFilePath); + string depsFilePath, + string runtimeConfigPath); } } diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/PackagedCommandSpecFactory.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/PackagedCommandSpecFactory.cs index 93268b293..ae10b4812 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/PackagedCommandSpecFactory.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/PackagedCommandSpecFactory.cs @@ -17,7 +17,8 @@ namespace Microsoft.DotNet.Cli.Utils IEnumerable allowedExtensions, string nugetPackagesRoot, CommandResolutionStrategy commandResolutionStrategy, - string depsFilePath) + string depsFilePath, + string runtimeConfigPath) { var toolAssembly = toolLibrary?.RuntimeAssemblies @@ -35,7 +36,7 @@ namespace Microsoft.DotNet.Cli.Utils return null; } - var isPortable = IsPortableApp(commandPath); + var isPortable = IsPortableApp(commandPath, runtimeConfigPath); return CreateCommandSpecWrappingWithCorehostIfDll( commandPath, @@ -43,7 +44,8 @@ namespace Microsoft.DotNet.Cli.Utils depsFilePath, commandResolutionStrategy, nugetPackagesRoot, - isPortable); + isPortable, + runtimeConfigPath); } private string GetCommandFilePath(string nugetPackagesRoot, LockFileTargetLibrary toolLibrary, LockFileItem runtimeAssembly) @@ -62,7 +64,8 @@ namespace Microsoft.DotNet.Cli.Utils string depsFilePath, CommandResolutionStrategy commandResolutionStrategy, string nugetPackagesRoot, - bool isPortable) + bool isPortable, + string runtimeConfigPath) { var commandExtension = Path.GetExtension(commandPath); @@ -74,7 +77,8 @@ namespace Microsoft.DotNet.Cli.Utils depsFilePath, commandResolutionStrategy, nugetPackagesRoot, - isPortable); + isPortable, + runtimeConfigPath); } return CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy); @@ -86,7 +90,8 @@ namespace Microsoft.DotNet.Cli.Utils string depsFilePath, CommandResolutionStrategy commandResolutionStrategy, string nugetPackagesRoot, - bool isPortable) + bool isPortable, + string runtimeConfigPath) { var host = string.Empty; var arguments = new List(); @@ -110,6 +115,12 @@ namespace Microsoft.DotNet.Cli.Utils arguments.Add(commandPath); + if (runtimeConfigPath != null) + { + arguments.Add("--runtimeconfig"); + arguments.Add(runtimeConfigPath); + } + if (depsFilePath != null) { arguments.Add("--depsfile"); @@ -134,13 +145,14 @@ namespace Microsoft.DotNet.Cli.Utils return new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy); } - private bool IsPortableApp(string commandPath) + private bool IsPortableApp(string commandPath, string runtimeConfigPath) { var commandDir = Path.GetDirectoryName(commandPath); - var runtimeConfigPath = Directory.EnumerateFiles(commandDir) - .FirstOrDefault(x => x.EndsWith("runtimeconfig.json")); - + runtimeConfigPath = string.IsNullOrEmpty(runtimeConfigPath) + ? Directory.EnumerateFiles(commandDir).FirstOrDefault(x => x.EndsWith("runtimeconfig.json")) + : runtimeConfigPath; + if (runtimeConfigPath == null) { return false; diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs index 232fba1b1..b1c356d26 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs @@ -78,25 +78,11 @@ namespace Microsoft.DotNet.Cli.Utils var depsFilePath = projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.DepsJson; + var runtimeConfigPath = + projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.RuntimeConfigJson; + var toolLibrary = GetToolLibraryForContext(projectContext, commandName); - return ResolveFromDependencyLibrary( - toolLibrary, - depsFilePath, - commandName, - allowedExtensions, - commandArguments, - projectContext); - } - - private CommandSpec ResolveFromDependencyLibrary( - LockFileTargetLibrary toolLibrary, - string depsFilePath, - string commandName, - IEnumerable allowedExtensions, - IEnumerable commandArguments, - ProjectContext projectContext) - { return _packagedCommandSpecFactory.CreateCommandSpecFromLibrary( toolLibrary, commandName, @@ -104,7 +90,8 @@ namespace Microsoft.DotNet.Cli.Utils allowedExtensions, projectContext.PackagesDirectory, s_commandResolutionStrategy, - depsFilePath); + depsFilePath, + runtimeConfigPath); } private LockFileTargetLibrary GetToolLibraryForContext( diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs index a510aa9fa..57ffb8290 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs @@ -123,7 +123,8 @@ namespace Microsoft.DotNet.Cli.Utils _allowedCommandExtensions, projectContext.PackagesDirectory, s_commandResolutionStrategy, - depsFilePath); + depsFilePath, + null); } private LockFile GetToolLockFile( diff --git a/src/corehost/cli/fxr/fx_muxer.cpp b/src/corehost/cli/fxr/fx_muxer.cpp index 4164d32d6..1ddb5ef13 100644 --- a/src/corehost/cli/fxr/fx_muxer.cpp +++ b/src/corehost/cli/fxr/fx_muxer.cpp @@ -304,15 +304,21 @@ int fx_muxer_t::parse_args_and_execute(const pal::string_t& own_dir, int argoff, pal::string_t opts_deps_file = _X("--depsfile"); pal::string_t opts_probe_path = _X("--additionalprobingpath"); + pal::string_t opts_runtime_config = _X("--runtimeconfig"); + pal::string_t deps_file = get_last_known_arg(opts, opts_deps_file, _X("")); + pal::string_t runtime_config = get_last_known_arg(opts, opts_runtime_config, _X("")); std::vector probe_paths = opts.count(opts_probe_path) ? opts[opts_probe_path] : std::vector(); trace::verbose(_X("Current argv is %s"), app_candidate.c_str()); pal::string_t app_or_deps = deps_file.empty() ? app_candidate : deps_file; - pal::string_t no_json = app_candidate; - pal::string_t dev_config_file; - auto config_file = get_runtime_config_from_file(no_json, &dev_config_file); + pal::string_t app_or_runtime_config = runtime_config.empty() ? app_candidate : runtime_config; + pal::string_t config_file, dev_config_file; + + trace::error(_X("Finding runtimeconfig.json from [%s]"), app_or_runtime_config.c_str()); + get_runtime_config_paths_from_file(app_or_runtime_config, &config_file, &dev_config_file); + runtime_config_t config(config_file, dev_config_file); for (const auto& path : config.get_probe_paths()) { diff --git a/src/corehost/cli/hostpolicy.cpp b/src/corehost/cli/hostpolicy.cpp index 795439f75..6349e7ce3 100644 --- a/src/corehost/cli/hostpolicy.cpp +++ b/src/corehost/cli/hostpolicy.cpp @@ -246,9 +246,11 @@ SHARED_API int corehost_main(const int argc, const pal::char_t* argv[]) } else { - pal::string_t dev_config_file; - auto config_path = get_runtime_config_from_file(args.managed_application, &dev_config_file); - runtime_config_t config(config_path, dev_config_file); + pal::string_t config_file, dev_config_file; + + get_runtime_config_paths_from_file(args.managed_application, &config_file, &dev_config_file); + runtime_config_t config(config_file, dev_config_file); + if (!config.is_valid()) { trace::error(_X("Invalid runtimeconfig.json [%s] [%s]"), config.get_path().c_str(), config.get_dev_path().c_str()); diff --git a/src/corehost/cli/libhost.cpp b/src/corehost/cli/libhost.cpp index fa398b825..a97c8ff19 100644 --- a/src/corehost/cli/libhost.cpp +++ b/src/corehost/cli/libhost.cpp @@ -6,20 +6,23 @@ #include "trace.h" #include "libhost.h" -pal::string_t get_runtime_config_from_file(const pal::string_t& file, pal::string_t* dev_cfg) +void get_runtime_config_paths_from_file(const pal::string_t& file, pal::string_t* cfg, pal::string_t* dev_cfg) { auto name = get_filename_without_ext(file); + auto json_name = name + _X(".runtimeconfig.json"); auto dev_json_name = name + _X(".runtimeconfig.dev.json"); + auto json_path = get_directory(file); auto dev_json_path = json_path; append_path(&json_path, json_name.c_str()); append_path(&dev_json_path, dev_json_name.c_str()); + trace::verbose(_X("Runtime config is cfg=%s dev=%s"), json_path.c_str(), dev_json_path.c_str()); dev_cfg->assign(dev_json_path); - return json_path; + cfg -> assign(json_path); } host_mode_t detect_operating_mode(const int argc, const pal::char_t* argv[], pal::string_t* p_own_dir) diff --git a/src/corehost/cli/libhost.h b/src/corehost/cli/libhost.h index 220901372..27b29acf7 100644 --- a/src/corehost/cli/libhost.h +++ b/src/corehost/cli/libhost.h @@ -80,7 +80,7 @@ public: } }; -pal::string_t get_runtime_config_from_file(const pal::string_t& file, pal::string_t* dev_config_file); +void get_runtime_config_paths_from_file(const pal::string_t& file, pal::string_t* config_file, pal::string_t* dev_config_file); host_mode_t detect_operating_mode(const int argc, const pal::char_t* argv[], pal::string_t* own_dir = nullptr); void try_patch_roll_forward_in_dir(const pal::string_t& cur_dir, const fx_ver_t& start_ver, pal::string_t* max_str);