-
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 3 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 |
|---|---|---|
|
|
@@ -30,7 +30,8 @@ public override int Execute() | |
|
|
||
| // Find directives (this can fail, so do this before creating the target directory). | ||
| var sourceFile = SourceFile.Load(file); | ||
| var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile, reportAllErrors: !_force, DiagnosticBag.ThrowOnFirst()); | ||
| var diagnostics = DiagnosticBag.ThrowOnFirst(); | ||
| var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile, reportAllErrors: !_force, diagnostics); | ||
|
|
||
| // Create a project instance for evaluation. | ||
| var projectCollection = new ProjectCollection(); | ||
|
|
@@ -42,6 +43,11 @@ public override int Execute() | |
| }; | ||
| var projectInstance = command.CreateProjectInstance(projectCollection); | ||
|
|
||
| // Evaluate directives. | ||
| directives = VirtualProjectBuildingCommand.EvaluateDirectives(projectInstance, directives, sourceFile, diagnostics); | ||
| command.Directives = directives; | ||
| projectInstance = command.CreateProjectInstance(projectCollection); | ||
|
|
||
| // Find other items to copy over, e.g., default Content items like JSON files in Web apps. | ||
| var includeItems = FindIncludedItems().ToList(); | ||
|
|
||
|
|
@@ -169,17 +175,42 @@ ImmutableArray<CSharpDirective> UpdateDirectives(ImmutableArray<CSharpDirective> | |
|
|
||
| foreach (var directive in directives) | ||
| { | ||
| // Fixup relative project reference paths (they need to be relative to the output directory instead of the source directory). | ||
| if (directive is CSharpDirective.Project project && | ||
| !Path.IsPathFullyQualified(project.Name)) | ||
| { | ||
| var modified = project.WithName(Path.GetRelativePath(relativeTo: targetDirectory, path: project.Name)); | ||
| result.Add(modified); | ||
| } | ||
| else | ||
| // Fixup relative project reference paths (they need to be relative to the output directory instead of the source directory, | ||
| // and preserve MSBuild interpolation variables like `$(..)` | ||
| // while also pointing to the project file rather than a directory). | ||
| if (directive is CSharpDirective.Project project) | ||
| { | ||
| result.Add(directive); | ||
| // If the path is absolute and it has some `$(..)` vars in it, | ||
| // turn it into a relative path (it might be in the form `$(ProjectDir)/../Lib` | ||
| // and we don't want that to be turned into an absolute path in the converted project). | ||
jjonescz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (Path.IsPathFullyQualified(project.Name)) | ||
| { | ||
| // If the path is absolute and has no `$(..)` vars, just keep it. | ||
| if (project.UnresolvedName == project.OriginalName) | ||
| { | ||
| result.Add(project); | ||
| continue; | ||
| } | ||
|
|
||
| project = project.WithName(Path.GetRelativePath(relativeTo: targetDirectory, path: project.Name)); | ||
| result.Add(project); | ||
| continue; | ||
| } | ||
|
|
||
| // If the original path is to a directory, just append the resolved file name | ||
| // but preserve the variables from the original, e.g., `../$(..)/Directory/Project.csproj`. | ||
| if (Directory.Exists(project.UnresolvedName)) | ||
|
||
| { | ||
| var projectFileName = Path.GetFileName(project.Name); | ||
| project = project.WithName(Path.Join(project.OriginalName, projectFileName)); | ||
| } | ||
|
|
||
| project = project.WithName(Path.GetRelativePath(relativeTo: targetDirectory, path: project.Name)); | ||
| result.Add(project); | ||
| continue; | ||
| } | ||
|
|
||
| result.Add(directive); | ||
| } | ||
|
|
||
| return result.DrainToImmutable(); | ||
|
|
||
| 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); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -1047,6 +1060,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); | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -1069,7 +1099,7 @@ ProjectRootElement CreateProjectRootElement(ProjectCollection projectCollection) | |||||||||||||
| var projectFileWriter = new StringWriter(); | ||||||||||||||
| WriteProjectFile( | ||||||||||||||
| projectFileWriter, | ||||||||||||||
| Directives, | ||||||||||||||
| directives, | ||||||||||||||
| isVirtualProject: true, | ||||||||||||||
| targetFilePath: EntryPointFileFullPath, | ||||||||||||||
| artifactsPath: ArtifactsPath, | ||||||||||||||
|
|
@@ -1589,6 +1619,28 @@ 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) | ||||||||||||||
|
||||||||||||||
| ? (project is null ? p : p.WithName(project.ExpandString(p.Name))) | |
| .ResolveProjectPath(sourceFile, diagnostics) | |
| ? (project is null | |
| ? p | |
| : p.WithName(project.ExpandString(p.Name))) | |
| .ResolveProjectPath(sourceFile, diagnostics) |
jjonescz marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like we would also want to create a new instance if !preserveUnresolvedName && UnresolvedName != name. If we don't expect this to occur, it would probably be good to add an assertion to that effect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, forgot to update this when adding the parameter.
Uh oh!
There was an error while loading. Please reload this page.