Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider sdks from before the runtime #106

Merged
merged 23 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7d57e5c
Return all installed DotNetSDK instances
kurtgronbech Nov 29, 2019
36ca39d
Correctly check the index returned by FindIndex
kurtgronbech Nov 29, 2019
da34277
Resolved feedback from JoeRobich. Also added support for .NET 5.0.
kurtgronbech Nov 19, 2020
1eea571
Cleaned some whitespace
kurtgronbech Nov 19, 2020
3b1f0a0
Only consider sdks from before the runtime
Forgind Dec 7, 2020
5887126
Merge branch 'master' into get-all-installed-dotnetsdk-instances
Forgind Dec 8, 2020
b6353cb
use --list-sdks, fixed instance null-check, removed workingdirectory
kurtgronbech Dec 8, 2020
8069139
Go back to using --info and also get the base path
kurtgronbech Dec 9, 2020
9e91f9e
Indentation fix and clearer comment on why we insert basePath at the …
kurtgronbech Dec 9, 2020
47e602b
Removed a couple of test lines which had snuck in
kurtgronbech Dec 9, 2020
60cbc46
Parse versions differently
Forgind Dec 10, 2020
f7573bc
Updated pattern for SdkRegex
kurtgronbech Dec 10, 2020
afe8a1d
Changed GetDotNetBasePath to be an iterator
kurtgronbech Dec 10, 2020
32a6f0b
cleaned some whitespace
kurtgronbech Dec 10, 2020
af73626
Undo version parse change
Forgind Dec 14, 2020
ae983d3
Merge branch 'get-all-installed-dotnetsdk-instances' of https://githu…
Forgind Dec 14, 2020
ec16d05
Reverse order of access
Forgind Dec 14, 2020
cd09d13
Move check to RegisterDefaults
Forgind Dec 14, 2020
75c7f2c
Merge https://github.com/microsoft/MSBuildLocator into assemblies-old…
Forgind Dec 16, 2020
3d2ea08
Restrict filtering to core
Forgind Dec 16, 2020
a4af277
Filter in DotNetSdkLocationHelper
Forgind Dec 16, 2020
930ae79
Add comment and split conditions
Forgind Dec 17, 2020
aa38ab5
Update comment
Forgind Dec 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/MSBuildLocator/DotNetSdkLocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@ public static VisualStudioInstance GetInstance(string dotNetSdkPath)
return null;
}

string versionContent = File.ReadAllText(versionPath);
Match versionMatch = VersionRegex.Match(versionContent);
// Preview versions contain a hyphen after the numeric part of the version. Version.TryParse doesn't accept that.
Match versionMatch = VersionRegex.Match(File.ReadAllText(versionPath));

if (!versionMatch.Success)
{
return null;
}

if (!int.TryParse(versionMatch.Groups[1].Value, out int major) ||
if (!versionMatch.Success ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the original flow here because the failure conditions are much more obvious in separate ifs:

  1. The version regex didn't match.
  2. We can't parse a version out.
  3. the version doesn't match

Here all together it's hard to see that there are separate failure cases.

!int.TryParse(versionMatch.Groups[1].Value, out int major) ||
!int.TryParse(versionMatch.Groups[2].Value, out int minor) ||
!int.TryParse(versionMatch.Groups[3].Value, out int patch))
!int.TryParse(versionMatch.Groups[3].Value, out int patch) ||
major > Environment.Version.Major ||
(major == Environment.Version.Major && minor > Environment.Version.Minor))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely needs a comment! Describe all the things you've learned about why we care about the current runtime version relative to the SDK version, and the SDK policy as we currently understand it.

{
return null;
}
Expand Down Expand Up @@ -127,6 +125,7 @@ private static IEnumerable<string> GetDotNetBasePaths(string workingDirectory)

var lineSdkIndex = lines.FindIndex(line => line.Contains("SDKs installed"));

List<string> paths = new List<string>();
if (lineSdkIndex != -1)
{
lineSdkIndex++;
Expand All @@ -144,11 +143,20 @@ private static IEnumerable<string> GetDotNetBasePaths(string workingDirectory)
path = Path.Combine(path, version) + Path.DirectorySeparatorChar;

if (!path.Equals(basePath))
yield return path;
paths.Add(path);

lineSdkIndex++;
}
}

// The paths are sorted in increasing order. We want to return the newest SDKs first, however,
// so iterate over the list in reverse order. If basePath is disqualified because it was later
// than the runtime version, this ensures that RegisterDefaults will return the latest valid
// SDK instead of the earliest installed.
for (int i = paths.Count - 1; i >= 0; i--)
{
yield return paths[i];
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MSBuildLocator/MSBuildLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal static IEnumerable<VisualStudioInstance> QueryVisualStudioInstances(
/// <returns>Instance of Visual Studio found and registered.</returns>
public static VisualStudioInstance RegisterDefaults()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does RegisterDefaults need to change at all? I would expect the change to be entirely in the list returned by GetInstances. I can see from the commit history that you implemented it that way and then changed it, but not why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to implement #106 (comment)

Is that wrong?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Yes, I now think it's not the right design. We could potentially add another overload that takes a bool to "give me everything, I don't care" but I don't think we should filter in RegisterDefaults and I do think we should present only usable instances in the list by default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to filtering in DotNetSdkLocationHelper.cs.

Copy link
Member Author

@Forgind Forgind Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little resistant to add an overload for "give me everything" without anyone explicitly asking for it. I'm guessing most people who use MSBuildLocator use it for relatively straightforward purposes, and it would make it a little harder to pull in my unit testing PR when that comes along.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this affect a unit testing PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons: it would add something else I'd probably want to test, and since I'm already planning to add an overload to this, I'd have to split my overload into "give me everything" and "don't give me everything" cases. Not a huge deal, though.

{
var instance = GetInstances(VisualStudioInstanceQueryOptions.Default).FirstOrDefault();
VisualStudioInstance instance = GetInstances(VisualStudioInstanceQueryOptions.Default).FirstOrDefault();
if (instance == null)
{
var error = "No instances of MSBuild could be detected." +
Expand Down