-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support variables in #:project directives
#51108
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
base: release/10.0.2xx
Are you sure you want to change the base?
Changes from all commits
bc17aea
7a28231
bfba2dc
b3dc5e4
2a0f51a
ffa9443
4194017
f829602
9fd4539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| using System.Collections.Immutable; | ||
| using System.Collections.ObjectModel; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Security; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
@@ -164,14 +165,26 @@ public VirtualProjectBuildingCommand( | |
| /// </summary> | ||
| public bool NoWriteBuildMarkers { get; init; } | ||
|
|
||
| private SourceFile EntryPointSourceFile | ||
| { | ||
| get | ||
| { | ||
| if (field == default) | ||
| { | ||
| field = SourceFile.Load(EntryPointFileFullPath); | ||
| } | ||
|
|
||
| return field; | ||
| } | ||
| } | ||
|
|
||
| public ImmutableArray<CSharpDirective> Directives | ||
| { | ||
| get | ||
| { | ||
| if (field.IsDefault) | ||
| { | ||
| var sourceFile = SourceFile.Load(EntryPointFileFullPath); | ||
| field = FindDirectives(sourceFile, reportAllErrors: false, DiagnosticBag.ThrowOnFirst()); | ||
| field = FindDirectives(EntryPointSourceFile, reportAllErrors: false, DiagnosticBag.ThrowOnFirst()); | ||
| Debug.Assert(!field.IsDefault); | ||
| } | ||
|
|
||
|
|
@@ -1049,6 +1062,23 @@ public ProjectInstance CreateProjectInstance(ProjectCollection projectCollection | |
| private ProjectInstance CreateProjectInstance( | ||
| ProjectCollection projectCollection, | ||
| Action<IDictionary<string, string>>? addGlobalProperties) | ||
| { | ||
| var project = CreateProjectInstance(projectCollection, Directives, addGlobalProperties); | ||
|
|
||
| var directives = EvaluateDirectives(project, Directives, EntryPointSourceFile, DiagnosticBag.ThrowOnFirst()); | ||
| if (directives != Directives) | ||
| { | ||
| Directives = directives; | ||
| project = CreateProjectInstance(projectCollection, directives, addGlobalProperties); | ||
| } | ||
jjonescz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return project; | ||
| } | ||
|
|
||
| private ProjectInstance CreateProjectInstance( | ||
| ProjectCollection projectCollection, | ||
| ImmutableArray<CSharpDirective> directives, | ||
| Action<IDictionary<string, string>>? addGlobalProperties) | ||
| { | ||
| var projectRoot = CreateProjectRootElement(projectCollection); | ||
|
|
||
|
|
@@ -1071,7 +1101,7 @@ ProjectRootElement CreateProjectRootElement(ProjectCollection projectCollection) | |
| var projectFileWriter = new StringWriter(); | ||
| WriteProjectFile( | ||
| projectFileWriter, | ||
| Directives, | ||
| directives, | ||
| isVirtualProject: true, | ||
| targetFilePath: EntryPointFileFullPath, | ||
| artifactsPath: ArtifactsPath, | ||
|
|
@@ -1611,6 +1641,30 @@ static bool Fill(ref WhiteSpaceInfo info, in SyntaxTriviaList triviaList, int in | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If there are any <c>#:project</c> <paramref name="directives"/>, expand <c>$()</c> in them and then resolve the project paths. | ||
| /// </summary> | ||
| public static ImmutableArray<CSharpDirective> EvaluateDirectives( | ||
| ProjectInstance? project, | ||
| ImmutableArray<CSharpDirective> directives, | ||
| SourceFile sourceFile, | ||
| DiagnosticBag diagnostics) | ||
| { | ||
| if (directives.OfType<CSharpDirective.Project>().Any()) | ||
| { | ||
| return directives | ||
| .Select(d => d is CSharpDirective.Project p | ||
| ? (project is null | ||
| ? p | ||
| : p.WithName(project.ExpandString(p.Name))) | ||
| .ResolveProjectPath(sourceFile, diagnostics) | ||
| : d) | ||
| .ToImmutableArray(); | ||
| } | ||
|
|
||
| return directives; | ||
| } | ||
|
|
||
| public static SourceText? RemoveDirectivesFromFile(ImmutableArray<CSharpDirective> directives, SourceText text) | ||
| { | ||
| if (directives.Length == 0) | ||
|
|
@@ -1897,8 +1951,31 @@ public sealed class Package(in ParseInfo info) : Named(info) | |
| /// <summary> | ||
| /// <c>#:project</c> directive. | ||
| /// </summary> | ||
| public sealed class Project(in ParseInfo info) : Named(info) | ||
| public sealed class Project : Named | ||
| { | ||
| [SetsRequiredMembers] | ||
jjonescz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public Project(in ParseInfo info, string name) : base(info) | ||
| { | ||
| Name = name; | ||
| OriginalName = name; | ||
| UnresolvedName = name; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Preserved across <see cref="WithName"/> calls. | ||
| /// </summary> | ||
| public string OriginalName { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Preserved across <see cref="ResolveProjectPath"/> calls. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When MSBuild <c>$(..)</c> vars are expanded via <see cref="WithName"/>, | ||
| /// the <see cref="UnresolvedName"/> will be expanded, but not resolved | ||
| /// (i.e., can be pointing to project directory or file). | ||
| /// </remarks> | ||
| public string UnresolvedName { get; init; } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like it's not straightforward to determine whether certain steps have been performed or not on a given Is there anything we can do about that? For example, maybe there should be Curious if you think this situation is worth doing anything about. It also looks like properties which refer to the same project, using different original paths, are supposed to be treated as duplicates. I think I filed a bug on that for when |
||
|
|
||
| public static new Project? Parse(in ParseContext context) | ||
| { | ||
| var directiveText = context.DirectiveText; | ||
|
|
@@ -1908,11 +1985,32 @@ public sealed class Project(in ParseInfo info) : Named(info) | |
| return context.Diagnostics.AddError<Project?>(context.SourceFile, context.Info.Span, string.Format(CliCommandStrings.MissingDirectiveName, directiveKind)); | ||
| } | ||
|
|
||
| return new Project(context.Info, directiveText); | ||
| } | ||
|
|
||
| public Project WithName(string name, bool preserveUnresolvedName = false) | ||
| { | ||
| return name == Name && (preserveUnresolvedName || UnresolvedName == name) | ||
| ? this | ||
| : new Project(Info, name) | ||
| { | ||
| OriginalName = OriginalName, | ||
| UnresolvedName = preserveUnresolvedName ? UnresolvedName : name, | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If the directive points to a directory, returns a new directive pointing to the corresponding project file. | ||
| /// </summary> | ||
| public Project ResolveProjectPath(SourceFile sourceFile, DiagnosticBag diagnostics) | ||
| { | ||
| var directiveText = Name; | ||
|
|
||
| try | ||
| { | ||
| // If the path is a directory like '../lib', transform it to a project file path like '../lib/lib.csproj'. | ||
| // Also normalize blackslashes to forward slashes to ensure the directive works on all platforms. | ||
| var sourceDirectory = Path.GetDirectoryName(context.SourceFile.Path) ?? "."; | ||
| // Also normalize backslashes to forward slashes to ensure the directive works on all platforms. | ||
| var sourceDirectory = Path.GetDirectoryName(sourceFile.Path) ?? "."; | ||
| var resolvedProjectPath = Path.Combine(sourceDirectory, directiveText.Replace('\\', '/')); | ||
| if (Directory.Exists(resolvedProjectPath)) | ||
| { | ||
|
|
@@ -1930,18 +2028,10 @@ public sealed class Project(in ParseInfo info) : Named(info) | |
| } | ||
| catch (GracefulException e) | ||
| { | ||
| context.Diagnostics.AddError(context.SourceFile, context.Info.Span, string.Format(CliCommandStrings.InvalidProjectDirective, e.Message), e); | ||
| diagnostics.AddError(sourceFile, Info.Span, string.Format(CliCommandStrings.InvalidProjectDirective, e.Message), e); | ||
| } | ||
|
|
||
| return new Project(context.Info) | ||
| { | ||
| Name = directiveText, | ||
| }; | ||
| } | ||
|
|
||
| public Project WithName(string name) | ||
| { | ||
| return new Project(Info) { Name = name }; | ||
| return WithName(directiveText, preserveUnresolvedName: true); | ||
| } | ||
|
|
||
| public override string ToString() => $"#:project {Name}"; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.