Skip to content

Commit

Permalink
Fix ArgumentNullException when SDK resolver is given a null project p…
Browse files Browse the repository at this point in the history
…ath (#4335)

Fixes NuGet/Home#11376
  • Loading branch information
jeffkl authored Nov 22, 2021
1 parent 1fadba7 commit 3d44676
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ internal static class GlobalJsonReader
/// <returns>A <see cref="Dictionary{String,String}"/> of MSBuild SDK versions from a global.json if found, otherwise <code>null</code>.</returns>
public static Dictionary<string, string> GetMSBuildSdkVersions(SdkResolverContext context)
{
if (string.IsNullOrWhiteSpace(context?.ProjectFilePath))
{
// If the ProjectFilePath is not set, an in-memory project is being evaluated and there's no way to know which directory to start looking for a global.json
return null;
}

var projectDirectory = Directory.GetParent(context.ProjectFilePath);

if (projectDirectory == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ internal static bool TryGetNuGetVersionForSdk(string id, string version, SdkReso
return NuGetAbstraction.TryParseNuGetVersion(version, out parsedVersion);
}

parsedVersion = null;

// Don't try to find versions defined in global.json if the project full path isn't set because an in-memory project is being evaluated and there's no
// way to be sure where to look
if (string.IsNullOrWhiteSpace(context?.ProjectFilePath))
{
return false;
}

Dictionary<string, string> msbuildSdkVersions;

// Get the SDK versions from a previous state, otherwise find and load global.json to get them
Expand All @@ -100,8 +109,6 @@ internal static bool TryGetNuGetVersionForSdk(string id, string version, SdkReso
return NuGetAbstraction.TryParseNuGetVersion(globalJsonVersion, out parsedVersion);
}

parsedVersion = null;

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public void InvalidJsonLogsMessage()
}
}

[Fact]
public void NullProjectPath()
{
var context = new MockSdkResolverContext(projectPath: null);

GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();
}

[Fact]
public void SdkVersionsAreSuccessfullyLoaded()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ public void TryGetNuGetVersionNoVersionSpecified()
context: context);
}

[Fact]
public void TryGetNuGetVersionNullProjectPath()
{
var context = new MockSdkResolverContext(projectPath: null);

VerifyTryGetNuGetVersionForSdk(
version: null,
expectedVersion: null,
context: context);
}

[Fact]
public void TryGetNuGetVersionNullProjectPathWithVersion()
{
var context = new MockSdkResolverContext(projectPath: null);

VerifyTryGetNuGetVersionForSdk(
version: "1.0.0",
expectedVersion: NuGetVersion.Parse("1.0.0"),
context: context);
}

private void VerifyTryGetNuGetVersionForSdk(string version, NuGetVersion expectedVersion, SdkResolverContextBase context = null)
{
var result = NuGetSdkResolver.TryGetNuGetVersionForSdk("foo", version, context, out var parsedVersion);
Expand Down

0 comments on commit 3d44676

Please sign in to comment.