-
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
Merged
jjonescz
merged 16 commits into
dotnet:release/10.0.2xx
from
jjonescz:sprint-project-vars
Nov 14, 2025
+397
−72
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bc17aea
Support variables in `#:project` directives
jjonescz 7a28231
Fix slashes
jjonescz bfba2dc
Fix typo in a comment
jjonescz b3dc5e4
Remove unnecessary `required`s
jjonescz 2a0f51a
Preserve variables inside full paths
jjonescz ffa9443
Merge branch 'release/10.0.2xx' into sprint-project-vars
jjonescz 4194017
Improve code
jjonescz f829602
Fix and test dependency on working directory
jjonescz 9fd4539
Fix file name case in a test
jjonescz 9f33382
Merge branch 'release/10.0.2xx' into sprint-project-vars
jjonescz dd92781
Move directive evaluation to the shared package
jjonescz be97a18
Test duplicate project directives
jjonescz 8987eb1
Clarify project directive name properties
jjonescz 5c7334a
Merge branch 'release/10.0.2xx' into sprint-project-vars
jjonescz 422beed
Fix source package errors
jjonescz fdd1d0e
Improve code
jjonescz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,14 @@ | |
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.Json.Serialization; | ||
| using System.Text.RegularExpressions; | ||
| using System.Xml; | ||
| using Microsoft.Build.Execution; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
@@ -225,6 +227,30 @@ static bool Fill(ref WhiteSpaceInfo info, in SyntaxTriviaList triviaList, int in | |
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If there are any <c>#:project</c> <paramref name="directives"/>, expands <c>$()</c> in them and ensures they point to project files (not directories). | ||
| /// </summary> | ||
| public static ImmutableArray<CSharpDirective> EvaluateDirectives( | ||
|
Member
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. When this change is consumed on the Roslyn side, I think the Roslyn analyzer should call this method also, to avoid a change in behavior. See also dotnet/roslyn#80575 (comment) |
||
| 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), CSharpDirective.Project.NameKind.Expanded)) | ||
| .EnsureProjectFilePath(sourceFile, diagnostics) | ||
| : d) | ||
| .ToImmutableArray(); | ||
| } | ||
|
|
||
| return directives; | ||
| } | ||
| } | ||
|
|
||
| internal readonly record struct SourceFile(string Path, SourceText Text) | ||
|
|
@@ -457,8 +483,32 @@ 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] | ||
| public Project(in ParseInfo info, string name) : base(info) | ||
| { | ||
| Name = name; | ||
| OriginalName = name; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Preserved across <see cref="WithName"/> calls, i.e., | ||
| /// this is the original directive text as entered by the user. | ||
| /// </summary> | ||
| public string OriginalName { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// This is the <see cref="OriginalName"/> with MSBuild <c>$(..)</c> vars expanded (via <see cref="ProjectInstance.ExpandString"/>). | ||
| /// </summary> | ||
| public string? ExpandedName { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// This is the <see cref="ExpandedName"/> resolved via <see cref="EnsureProjectFilePath"/> | ||
| /// (i.e., this is a file path if the original text pointed to a directory). | ||
| /// </summary> | ||
| public string? ProjectFilePath { get; init; } | ||
|
|
||
| public static new Project? Parse(in ParseContext context) | ||
| { | ||
| var directiveText = context.DirectiveText; | ||
|
|
@@ -468,19 +518,57 @@ public sealed class Project(in ParseInfo info) : Named(info) | |
| return context.Diagnostics.AddError<Project?>(context.SourceFile, context.Info.Span, string.Format(FileBasedProgramsResources.MissingDirectiveName, directiveKind)); | ||
| } | ||
|
|
||
| return new Project(context.Info, directiveText); | ||
| } | ||
|
|
||
| public enum NameKind | ||
| { | ||
| /// <summary> | ||
| /// Change <see cref="Named.Name"/> and <see cref="ExpandedName"/>. | ||
| /// </summary> | ||
| Expanded = 1, | ||
|
|
||
| /// <summary> | ||
| /// Change <see cref="Named.Name"/> and <see cref="Project.ProjectFilePath"/>. | ||
| /// </summary> | ||
| ProjectFilePath = 2, | ||
|
|
||
| /// <summary> | ||
| /// Change only <see cref="Named.Name"/>. | ||
| /// </summary> | ||
| Final = 3, | ||
| } | ||
|
|
||
| public Project WithName(string name, NameKind kind) | ||
| { | ||
| return new Project(Info, name) | ||
| { | ||
| OriginalName = OriginalName, | ||
| ExpandedName = kind == NameKind.Expanded ? name : ExpandedName, | ||
| ProjectFilePath = kind == NameKind.ProjectFilePath ? name : ProjectFilePath, | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If the directive points to a directory, returns a new directive pointing to the corresponding project file. | ||
RikkiGibson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public Project EnsureProjectFilePath(SourceFile sourceFile, DiagnosticBag diagnostics) | ||
| { | ||
| var resolvedName = 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. | ||
| // https://github.com/dotnet/sdk/issues/51487: Behavior should not depend on process current directory | ||
| var sourceDirectory = Path.GetDirectoryName(context.SourceFile.Path) ?? "."; | ||
| var resolvedProjectPath = Path.Combine(sourceDirectory, directiveText.Replace('\\', '/')); | ||
| // Also normalize backslashes to forward slashes to ensure the directive works on all platforms. | ||
| var sourceDirectory = Path.GetDirectoryName(sourceFile.Path) | ||
| ?? throw new InvalidOperationException($"Source file path '{sourceFile.Path}' does not have a containing directory."); | ||
| var resolvedProjectPath = Path.Combine(sourceDirectory, resolvedName.Replace('\\', '/')); | ||
| if (Directory.Exists(resolvedProjectPath)) | ||
| { | ||
| var fullFilePath = GetProjectFileFromDirectory(resolvedProjectPath).FullName; | ||
|
|
||
| // Keep a relative path only if the original directive was a relative path. | ||
| directiveText = ExternalHelpers.IsPathFullyQualified(directiveText) | ||
| resolvedName = ExternalHelpers.IsPathFullyQualified(resolvedName) | ||
| ? fullFilePath | ||
| : ExternalHelpers.GetRelativePath(relativeTo: sourceDirectory, fullFilePath); | ||
| } | ||
|
|
@@ -491,22 +579,14 @@ public sealed class Project(in ParseInfo info) : Named(info) | |
| } | ||
| catch (GracefulException e) | ||
| { | ||
| context.Diagnostics.AddError(context.SourceFile, context.Info.Span, string.Format(FileBasedProgramsResources.InvalidProjectDirective, e.Message), e); | ||
| diagnostics.AddError(sourceFile, Info.Span, string.Format(FileBasedProgramsResources.InvalidProjectDirective, e.Message), e); | ||
| } | ||
|
|
||
| return new Project(context.Info) | ||
| { | ||
| Name = directiveText, | ||
| }; | ||
| } | ||
|
|
||
| public Project WithName(string name) | ||
| { | ||
| return new Project(Info) { Name = name }; | ||
| return WithName(resolvedName, NameKind.ProjectFilePath); | ||
| } | ||
|
|
||
| // https://github.com/dotnet/sdk/issues/51487: Delete copies of methods from MsbuildProject and MSBuildUtilities from the source package, sharing the original method(s) under src/Cli instead. | ||
| public static FileInfo GetProjectFileFromDirectory(string projectDirectory) | ||
| private static FileInfo GetProjectFileFromDirectory(string projectDirectory) | ||
| { | ||
| DirectoryInfo dir; | ||
| try | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.