Skip to content

Commit 17b3342

Browse files
committed
Manually pull in changes from release/10.0.2xx
1 parent 1a86c64 commit 17b3342

19 files changed

+116
-18
lines changed

src/Cli/Microsoft.DotNet.FileBasedPrograms/AppDirectiveHelpers.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@ namespace Microsoft.DotNet.FileBasedPrograms;
2727

2828
internal static class AppDirectiveHelpers
2929
{
30-
31-
#pragma warning disable RSEXPERIMENTAL003 // 'SyntaxTokenParser' is experimental
3230
public static SyntaxTokenParser CreateTokenizer(SourceText text)
3331
{
3432
return SyntaxFactory.CreateTokenParser(text,
3533
CSharpParseOptions.Default.WithFeatures([new("FileBasedProgram", "true")]));
3634
}
37-
#pragma warning restore RSEXPERIMENTAL003 // 'SyntaxTokenParser' is experimental
3835

3936
/// <param name="reportAllErrors">
4037
/// If <see langword="true"/>, the whole <paramref name="sourceFile"/> is parsed to find diagnostics about every app directive.
@@ -140,8 +137,8 @@ public static void FindLeadingDirectives(
140137
// TODO: original impl was using more recent span-oriented APIs. Optimal sharing may be tricky.
141138
// var parts = Patterns.Whitespace.EnumerateSplits(message, 2);
142139
var parts = Patterns.Whitespace.Split(message.ToString(), 2);
143-
var name = parts.Length > 0 ? parts[0] : null;
144-
var value = parts.Length > 1 ? parts[1] : null;
140+
var name = parts.Length > 0 ? parts[0] : "";
141+
var value = parts.Length > 1 ? parts[1] : "";
145142
Debug.Assert(!(parts.Length > 2));
146143

147144
var whiteSpace = GetWhiteSpaceInfo(triviaList, index);
@@ -155,9 +152,16 @@ public static void FindLeadingDirectives(
155152
},
156153
Diagnostics = diagnostics,
157154
SourceFile = sourceFile,
158-
DirectiveKind = name ?? "",
159-
DirectiveText = value ?? ""
155+
DirectiveKind = name,
156+
DirectiveText = value,
160157
};
158+
159+
// Block quotes now so we can later support quoted values without a breaking change. https://github.com/dotnet/sdk/issues/49367
160+
if (value.Contains('"'))
161+
{
162+
diagnostics.AddError(sourceFile, context.Info.Span, FileBasedProgramsResources.QuoteInDirective);
163+
}
164+
161165
if (CSharpDirective.Parse(context) is { } directive)
162166
{
163167
// If the directive is already present, report an error.
@@ -477,7 +481,11 @@ public sealed class Project(in ParseInfo info) : Named(info)
477481
if (Directory.Exists(resolvedProjectPath))
478482
{
479483
var fullFilePath = GetProjectFileFromDirectory(resolvedProjectPath).FullName;
480-
directiveText = ExternalHelpers.GetRelativePath(sourceDirectory, fullFilePath);
484+
485+
// Keep a relative path only if the original directive was a relative path.
486+
directiveText = ExternalHelpers.IsPathFullyQualified(directiveText)
487+
? fullFilePath
488+
: ExternalHelpers.GetRelativePath(relativeTo: sourceDirectory, fullFilePath);
481489
}
482490
else if (!File.Exists(resolvedProjectPath))
483491
{
@@ -495,6 +503,11 @@ public sealed class Project(in ParseInfo info) : Named(info)
495503
};
496504
}
497505

506+
public Project WithName(string name)
507+
{
508+
return new Project(Info) { Name = name };
509+
}
510+
498511
public static FileInfo GetProjectFileFromDirectory(string projectDirectory)
499512
{
500513
DirectoryInfo dir;
@@ -571,10 +584,14 @@ internal sealed class SimpleDiagnostic
571584
/// </summary>
572585
public readonly struct Position
573586
{
574-
public required SourceFile SourceFile { get; init; }
575-
public required TextSpan TextSpan { get; init; }
576-
public string Path => SourceFile.Path;
577-
public LinePositionSpan Span => SourceFile.GetFileLinePositionSpan(TextSpan).Span;
587+
public string Path { get; init; }
588+
public LinePositionSpan Span { get; init; }
589+
590+
public static implicit operator Position(FileLinePositionSpan fileLinePositionSpan) => new()
591+
{
592+
Path = fileLinePositionSpan.Path,
593+
Span = fileLinePositionSpan.Span,
594+
};
578595
}
579596
}
580597

@@ -596,7 +613,7 @@ public void AddError(SourceFile sourceFile, TextSpan span, string message, Excep
596613
if (Builder != null)
597614
{
598615
Debug.Assert(!IgnoreDiagnostics);
599-
Builder.Add(new SimpleDiagnostic { Location = new SimpleDiagnostic.Position { SourceFile = sourceFile, TextSpan = span }, Message = message });
616+
Builder.Add(new SimpleDiagnostic { Location = sourceFile.GetFileLinePositionSpan(span), Message = message });
600617
}
601618
else if (!IgnoreDiagnostics)
602619
{

src/Cli/Microsoft.DotNet.FileBasedPrograms/ExternalHelpers.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,34 @@ namespace Microsoft.DotNet.FileBasedPrograms;
1010

1111
/// <summary>
1212
/// When targeting netstandard2.0, the user of the source package must "implement" certain methods by declaring members in this type.
13+
/// </summary>
1314
partial class ExternalHelpers
1415
{
1516
public static partial int CombineHashCodes(int value1, int value2);
1617
public static partial string GetRelativePath(string relativeTo, string path);
1718

19+
public static partial bool IsPathFullyQualified(string path);
20+
1821
#if NET
1922
public static partial int CombineHashCodes(int value1, int value2)
2023
=> HashCode.Combine(value1, value2);
2124

2225
public static partial string GetRelativePath(string relativeTo, string path)
2326
=> Path.GetRelativePath(relativeTo, path);
2427

28+
public static partial bool IsPathFullyQualified(string path)
29+
=> Path.IsPathFullyQualified(path);
30+
2531
#elif FILE_BASED_PROGRAMS_SOURCE_PACKAGE_BUILD
32+
// This path should only be used when we are verifying that the source package itself builds under netstandard2.0.
2633
public static partial int CombineHashCodes(int value1, int value2)
27-
=> throw new NotImplementedException("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.");
34+
=> throw new NotImplementedException();
2835

2936
public static partial string GetRelativePath(string relativeTo, string path)
30-
=> throw new NotImplementedException("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.");
37+
=> throw new NotImplementedException();
38+
39+
public static partial bool IsPathFullyQualified(string path)
40+
=> throw new NotImplementedException();
3141

3242
#endif
3343
}
@@ -43,7 +53,7 @@ public GracefulException(string? message) : base(message)
4353
{
4454
}
4555

46-
public GracefulException(string? message, string? todo2) : base(message)
56+
public GracefulException(string? message, string? arg) : base(message)
4757
{
4858
}
4959

src/Cli/Microsoft.DotNet.FileBasedPrograms/FileBasedProgramsResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@
154154
<value>Duplicate directives are not supported: {0}</value>
155155
<comment>{0} is the directive type and name.</comment>
156156
</data>
157+
<data name="QuoteInDirective" xml:space="preserve">
158+
<value>Directives currently cannot contain double quotes (").</value>
159+
</data>
157160
<data name="InvalidProjectDirective" xml:space="preserve">
158161
<value>The '#:project' directive is invalid: {0}</value>
159162
<comment>{0} is the inner error message.</comment>

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.DotNet.FileBasedPrograms/xlf/FileBasedProgramsResources.ko.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)