未验证 提交 d3bc63fb 编写于 作者: T Tomáš Matoušek 提交者: GitHub

Prefer more specific path map key (#49670)

上级 5faff982
......@@ -1482,6 +1482,8 @@ public new CSharpCommandLineArguments Parse(IEnumerable<string> args, string? ba
new CSharpRequiredLanguageVersion(MessageID.IDS_FeatureNullableReferenceTypes.RequiredVersion())), Location.None));
}
pathMap = SortPathMap(pathMap);
return new CSharpCommandLineArguments
{
IsScriptRunner = IsScriptCommandLineParser,
......
......@@ -10740,6 +10740,12 @@ public void PathMapParser()
parsedArgs.Errors.Verify();
Assert.Equal(KeyValuePairUtil.Create("a =,b" + s, "1,= 2" + s), parsedArgs.PathMap[0]);
Assert.Equal(KeyValuePairUtil.Create("x =,y" + s, "3 4" + s), parsedArgs.PathMap[1]);
parsedArgs = DefaultParse(new[] { @"/pathmap:C:\temp\=/_1/,C:\temp\a\=/_2/,C:\temp\a\b\=/_3/", "a.cs", @"a\b.cs", @"a\b\c.cs" }, WorkingDirectory);
parsedArgs.Errors.Verify();
Assert.Equal(KeyValuePairUtil.Create(@"C:\temp\a\b\", "/_3/"), parsedArgs.PathMap[0]);
Assert.Equal(KeyValuePairUtil.Create(@"C:\temp\a\", "/_2/"), parsedArgs.PathMap[1]);
Assert.Equal(KeyValuePairUtil.Create(@"C:\temp\", "/_1/"), parsedArgs.PathMap[2]);
}
[Theory]
......
......@@ -1195,5 +1195,13 @@ internal static bool TryParseUInt16(string? value, out ushort result)
CompilerOptionParseUtilities.ParseFeatures(builder, features);
return builder.ToImmutable();
}
/// <summary>
/// Sort so that more specific keys precede less specific.
/// When mapping a path we find the first key in the array that is a prefix of the path.
/// If multiple keys are prefixes of the path we want to use the longest (more specific) one for the mapping.
/// </summary>
internal static ImmutableArray<KeyValuePair<string, string>> SortPathMap(ImmutableArray<KeyValuePair<string, string>> pathMap)
=> pathMap.Sort((x, y) => -x.Key.Length.CompareTo(y.Key.Length));
}
}
......@@ -61,15 +61,13 @@ public SourceFileResolver(ImmutableArray<string> searchPaths, string? baseDirect
{
var pathMapBuilder = ArrayBuilder<KeyValuePair<string, string>>.GetInstance(pathMap.Length);
foreach (var kv in pathMap)
foreach (var (key, value) in pathMap)
{
var key = kv.Key;
if (key == null || key.Length == 0)
{
throw new ArgumentException(CodeAnalysisResources.EmptyKeyInPathMap, nameof(pathMap));
}
var value = kv.Value;
if (value == null)
{
throw new ArgumentException(CodeAnalysisResources.NullValueInPathMap, nameof(pathMap));
......
......@@ -1440,6 +1440,8 @@ lVbRuntimePlus:
' If the script is passed without the `\i` option simply execute the script (`vbi script.vbx`).
interactiveMode = interactiveMode Or (IsScriptCommandLineParser AndAlso sourceFiles.Count = 0)
pathMap = SortPathMap(pathMap)
Return New VisualBasicCommandLineArguments With
{
.IsScriptRunner = IsScriptCommandLineParser,
......
......@@ -3483,6 +3483,12 @@ print Goodbye, World"
parsedArgs.Errors.Verify()
Assert.Equal(KeyValuePairUtil.Create("a =,b" & s, "1,= 2" & s), parsedArgs.PathMap(0))
Assert.Equal(KeyValuePairUtil.Create("x =,y" & s, "3 4" & s), parsedArgs.PathMap(1))
parsedArgs = DefaultParse({"/pathmap:C:\temp\=/_1/,C:\temp\a\=/_2/,C:\temp\a\b\=/_3/", "a.cs", "a\b.cs", "a\b\c.cs"}, _baseDirectory)
parsedArgs.Errors.Verify()
Assert.Equal(KeyValuePairUtil.Create("C:\temp\a\b\", "/_3/"), parsedArgs.PathMap(0))
Assert.Equal(KeyValuePairUtil.Create("C:\temp\a\", "/_2/"), parsedArgs.PathMap(1))
Assert.Equal(KeyValuePairUtil.Create("C:\temp\", "/_1/"), parsedArgs.PathMap(2))
End Sub
' PathMapKeepsCrossPlatformRoot and PathMapInconsistentSlashes should be in an
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册