From a070976671e93ec902123ddda632b3d2350db15d Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Tue, 26 Apr 2016 16:04:33 -0500 Subject: [PATCH] "dotnet test project.json" fails in a directory with a test project.json in it When the current directory contains a valid project.json, and the user just says `dotnet test project.json`, normalizing the path fails becuase we end up calling Path.GetFullPath with an empty string. To fix this, if the directory of the file is empty, use the current directory. Fixing this for all "dotnet XXX" commands. Fix #2691 --- .../ProjectReader.cs | 37 +++++++++++++++++-- .../WorkspaceContext.cs | 24 ++---------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs b/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs index 50017451e..fe10cbae0 100644 --- a/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs +++ b/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs @@ -64,10 +64,7 @@ namespace Microsoft.DotNet.ProjectModel public static Project GetProject(string projectPath, ProjectReaderSettings settings = null) { - if (!projectPath.EndsWith(Project.FileName)) - { - projectPath = Path.Combine(projectPath, Project.FileName); - } + projectPath = NormalizeProjectFilePath(projectPath); var name = Path.GetFileName(Path.GetDirectoryName(projectPath)); @@ -236,6 +233,38 @@ namespace Microsoft.DotNet.ProjectModel return project; } + internal static string NormalizeProjectDirectoryPath(string path) + { + if (File.Exists(path) && + string.Equals(Path.GetFileName(path), Project.FileName, StringComparison.OrdinalIgnoreCase)) + { + string directoryName = Path.GetDirectoryName(path); + if (string.IsNullOrEmpty(directoryName)) + { + directoryName = Directory.GetCurrentDirectory(); + } + + return Path.GetFullPath(directoryName); + } + else if (Directory.Exists(path) && + File.Exists(Path.Combine(path, Project.FileName))) + { + return Path.GetFullPath(path); + } + + return null; + } + + internal static string NormalizeProjectFilePath(string path) + { + if (!path.EndsWith(Project.FileName)) + { + path = Path.Combine(path, Project.FileName); + } + + return Path.GetFullPath(path); + } + private static NuGetVersion SpecifySnapshot(string version, string snapshotValue) { if (version.EndsWith("-*")) diff --git a/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs b/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs index ac225eac6..e42c06b61 100644 --- a/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs +++ b/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs @@ -107,7 +107,7 @@ namespace Microsoft.DotNet.ProjectModel public void AddProject(string path) { - var projectPath = NormalizeProjectPath(path); + var projectPath = ProjectReader.NormalizeProjectDirectoryPath(path); if (projectPath != null) { @@ -228,7 +228,7 @@ namespace Microsoft.DotNet.ProjectModel public ProjectContextCollection GetProjectContextCollection(string projectPath) { - var normalizedPath = NormalizeProjectPath(projectPath); + var normalizedPath = ProjectReader.NormalizeProjectDirectoryPath(projectPath); if (normalizedPath == null) { return null; @@ -242,7 +242,7 @@ namespace Microsoft.DotNet.ProjectModel private FileModelEntry GetProjectCore(string projectDirectory) { - var normalizedPath = NormalizeProjectPath(projectDirectory); + var normalizedPath = ProjectReader.NormalizeProjectDirectoryPath(projectDirectory); if (normalizedPath == null) { return null; @@ -256,7 +256,7 @@ namespace Microsoft.DotNet.ProjectModel private LockFile GetLockFile(string projectDirectory) { - var normalizedPath = NormalizeProjectPath(projectDirectory); + var normalizedPath = ProjectReader.NormalizeProjectDirectoryPath(projectDirectory); if (normalizedPath == null) { return null; @@ -441,22 +441,6 @@ namespace Microsoft.DotNet.ProjectModel } } - private static string NormalizeProjectPath(string path) - { - if (File.Exists(path) && - string.Equals(Path.GetFileName(path), Project.FileName, StringComparison.OrdinalIgnoreCase)) - { - return Path.GetFullPath(Path.GetDirectoryName(path)); - } - else if (Directory.Exists(path) && - File.Exists(Path.Combine(path, Project.FileName))) - { - return Path.GetFullPath(path); - } - - return null; - } - private static List ResolveProjectPath(string projectPath) { if (File.Exists(projectPath))