Skip to content

Commit edef3b9

Browse files
github-actions[bot]tmdsericstj
authored
[release/7.0-staging] Microsoft.NETCore.Platforms: support adding rids with '-' in the base part. (#86282)
* Microsoft.NETCore.Platforms: support adding rids with '-' in the base part. Currently when trying to add a rid like 'linux-musl-x64' the rid is not understood to be base = 'linux-musl', arch = 'x64'. Instead the parser considers a potential optional qualifier. This causes the rid to be parsed as base = 'linux', arch = 'musl', and qualifier = 'x64'. We know the rids being added won't have a qualifier. If we take this into account while parsing, we can parse the rid correctly. * Update src/libraries/Microsoft.NETCore.Platforms/src/RuntimeGroupCollection.cs Co-authored-by: Eric StJohn <ericstj@microsoft.com> --------- Co-authored-by: Tom Deseyn <tom.deseyn@gmail.com> Co-authored-by: Eric StJohn <ericstj@microsoft.com>
1 parent 6f9d91c commit edef3b9

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/libraries/Microsoft.NETCore.Platforms/src/RID.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private enum RIDPart : int
5656
Max = Qualifier
5757
}
5858

59-
public static RID Parse(string runtimeIdentifier)
59+
public static RID Parse(string runtimeIdentifier, bool noQualifier)
6060
{
6161
string[] parts = new string[(int)RIDPart.Max + 1];
6262
bool omitVersionDelimiter = true;
@@ -90,6 +90,15 @@ public static RID Parse(string runtimeIdentifier)
9090
// version might be omitted
9191
else if (current == ArchitectureDelimiter)
9292
{
93+
// The qualifier delimiter and architecture delimiter are the same.
94+
// When there is no qualifier, there will be one delimiter past the base part
95+
// for the architecture.
96+
// So if we see another delimiter later in the string (for the architecture),
97+
// extend the base part instead of starting the architecture part.
98+
if (noQualifier && runtimeIdentifier.IndexOf(ArchitectureDelimiter, i + 1) != -1)
99+
{
100+
break;
101+
}
93102
// ensure there's no version later in the string
94103
if (runtimeIdentifier.IndexOf(VersionDelimiter, i) != -1)
95104
{

src/libraries/Microsoft.NETCore.Platforms/src/RuntimeGroupCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public RuntimeGroupCollection(ICollection<RuntimeGroup> runtimeGroups)
3434
/// <param name="parent"></param>
3535
public void AddRuntimeIdentifier(string runtimeIdentifier, string parent)
3636
{
37-
RID rid = RID.Parse(runtimeIdentifier);
37+
// don't parse qualifier since we don't use them and they are ambiguous with `-` in base RID
38+
RID rid = RID.Parse(runtimeIdentifier, noQualifier: true);
3839

3940
AddRuntimeIdentifier(rid, parent);
4041
}

src/libraries/Microsoft.NETCore.Platforms/tests/RidTests.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,57 @@ public class RidTests
1010
{
1111
public static IEnumerable<object[]> ValidRIDData()
1212
{
13-
yield return new object[] { "win10-x64", new RID() { BaseRID = "win", OmitVersionDelimiter = true, Version = new RuntimeVersion("10"), Architecture = "x64" } };
14-
yield return new object[] { "win10", new RID() { BaseRID = "win", OmitVersionDelimiter = true, Version = new RuntimeVersion("10")} };
15-
yield return new object[] { "linux", new RID() { BaseRID = "linux" } };
16-
yield return new object[] { "linux-x64", new RID() { BaseRID = "linux", Architecture = "x64" } };
17-
yield return new object[] { "linux-x64", new RID() { BaseRID = "linux", Architecture = "x64" } };
18-
yield return new object[] { "debian.10-x64", new RID() { BaseRID = "debian", Version = new RuntimeVersion("10"), Architecture = "x64" } };
19-
yield return new object[] { "linuxmint.19.2-x64", new RID() { BaseRID = "linuxmint", Version = new RuntimeVersion("19.2"), Architecture = "x64" } };
20-
yield return new object[] { "ubuntu.14.04-x64", new RID() { BaseRID = "ubuntu", Version = new RuntimeVersion("14.04"), Architecture = "x64" } };
21-
yield return new object[] { "foo-bar.42-arm", new RID() { BaseRID = "foo-bar", Version = new RuntimeVersion("42"), Architecture = "arm" } };
22-
yield return new object[] { "foo-bar-arm", new RID() { BaseRID = "foo", Architecture = "bar", Qualifier = "arm" } }; // demonstrates ambiguity, avoid using `-` in base
23-
yield return new object[] { "linux-musl-x64", new RID() { BaseRID = "linux", Architecture = "musl", Qualifier = "x64" } }; // yes, we already have ambiguous RIDs
13+
yield return new object[] { "win10-x64", new RID() { BaseRID = "win", OmitVersionDelimiter = true, Version = new RuntimeVersion("10"), Architecture = "x64" }, null };
14+
yield return new object[] { "win10", new RID() { BaseRID = "win", OmitVersionDelimiter = true, Version = new RuntimeVersion("10")}, null };
15+
yield return new object[] { "linux", new RID() { BaseRID = "linux" }, null };
16+
yield return new object[] { "linux-x64", new RID() { BaseRID = "linux", Architecture = "x64" }, null };
17+
yield return new object[] { "linux-x64", new RID() { BaseRID = "linux", Architecture = "x64" }, null };
18+
yield return new object[] { "debian.10-x64", new RID() { BaseRID = "debian", Version = new RuntimeVersion("10"), Architecture = "x64" }, null };
19+
yield return new object[] { "linuxmint.19.2-x64", new RID() { BaseRID = "linuxmint", Version = new RuntimeVersion("19.2"), Architecture = "x64" }, null };
20+
yield return new object[] { "ubuntu.14.04-x64", new RID() { BaseRID = "ubuntu", Version = new RuntimeVersion("14.04"), Architecture = "x64" }, null };
21+
yield return new object[] { "foo-bar.42-arm", new RID() { BaseRID = "foo-bar", Version = new RuntimeVersion("42"), Architecture = "arm" }, null };
22+
yield return new object[] { "foo-bar-arm", new RID() { BaseRID = "foo", Architecture = "bar", Qualifier = "arm" }, // demonstrates ambiguity, avoid using `-` in base
23+
new RID() { BaseRID = "foo-bar", Architecture = "arm" } };
24+
yield return new object[] { "linux-musl-x64", new RID() { BaseRID = "linux", Architecture = "musl", Qualifier = "x64" }, // yes, we already have ambiguous RIDs
25+
new RID() { BaseRID = "linux-musl", Architecture = "x64" } };
2426
}
2527

2628
[Theory]
2729
[MemberData(nameof(ValidRIDData))]
28-
internal void ParseCorrectly(string input, RID expected)
30+
internal void ParseCorrectly(string input, RID expected, RID? expectedNoQualifier)
2931
{
30-
RID actual = RID.Parse(input);
32+
_ = expectedNoQualifier; // unused
33+
34+
RID actual = RID.Parse(input, noQualifier: false);
3135

3236
Assert.Equal(expected, actual);
3337
}
3438

3539
[Theory]
3640
[MemberData(nameof(ValidRIDData))]
37-
internal void ToStringAsExpected(string expected, RID rid)
41+
internal void ParseCorrectlyNoQualifier(string input, RID expected, RID? expectedNoQualifier)
42+
{
43+
expectedNoQualifier ??= expected;
44+
45+
RID actual = RID.Parse(input, noQualifier: true);
46+
47+
Assert.Equal(expectedNoQualifier, actual);
48+
}
49+
50+
[Theory]
51+
[MemberData(nameof(ValidRIDData))]
52+
internal void ToStringAsExpected(string expected, RID rid, RID? expectedNoQualifierRid)
3853
{
3954
string actual = rid.ToString();
4055

4156
Assert.Equal(expected, actual);
57+
58+
if (expectedNoQualifierRid is not null)
59+
{
60+
actual = expectedNoQualifierRid.ToString();
61+
62+
Assert.Equal(expected, actual);
63+
}
4264
}
4365
}
4466
}

0 commit comments

Comments
 (0)