-
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
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 |
---|---|---|
|
@@ -245,6 +245,16 @@ The directives are processed as follows: | |
(because `ProjectReference` items don't support directory paths). | ||
An error is reported if zero or more than one projects are found in the directory, just like `dotnet reference add` would do. | ||
|
||
Directive values support MSBuild variables (like `$(..)`) normally as they are translated literally and left to MSBuild engine to process. | ||
However, in `#:project` directives, variables might not be preserved during [grow up](#grow-up), | ||
MiYanni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
because there is additional processing of those directives that makes it technically challenging to preserve variables in all cases | ||
(project directive values need to be resolved to be relative to the target directory | ||
and also to point to a project file rather than a directory). | ||
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. I am wondering if it would be helpful to attempt to process the project path in two ways
Essentially, we first try to resolve as (1), as if it is the last argument to This would mean that the virtual project content can depend on the presence of the referenced projects on disk. I'm not sure if that introduces any major problems or not. It's possible this is the wrong approach and would just make things more confusing, not less, but, thought I would submit it in hopes of it helping to resolve the technical challenges. 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. That's interesting, at first glance this seems to definitely simplify some things - we wouldn't need to expand the paths against MSBuild project during We could skip searching for project file in the directory in case there are MSBuild variables, but we would still need to do that for non-variable paths, and that logic is actually the same, so that wouldn't simplify our lives much probably. So I'm not sure this would resolve any technical challenges after all? (If we want to support conversion of the variable paths.) |
||
Note that it is not expected that variables inside the path change their meaning during the conversion, | ||
so for example `#:project ../$(LibName)` is translated to `<ProjectReference Include="../../$(LibName)/Lib.csproj" />` (i.e., the variable is preserved). | ||
However, variables at the start can change, so for example `#:project $(ProjectDir)../Lib` is translated to `<ProjectReference Include="../../Lib/Lib.csproj" />` (i.e., the variable is expanded). | ||
In other directives, all variables are preserved during conversion. | ||
|
||
Because these directives are limited by the C# language to only appear before the first "C# token" and any `#if`, | ||
dotnet CLI can look for them via a regex or Roslyn lexer without any knowledge of defined conditional symbols | ||
and can do that efficiently by stopping the search when it sees the first "C# token". | ||
|
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) | ||
: d) | ||
.ToImmutableArray(); | ||
} | ||
|
||
return directives; | ||
} | ||
|
||
public static SourceText? RemoveDirectivesFromFile(ImmutableArray<CSharpDirective> directives, SourceText text) | ||
{ | ||
if (directives.Length == 0) | ||
|
@@ -1867,8 +1919,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] | ||
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. Is there a parameterless constructor somewhere I'm missing? If not, please remove 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. Good catch, thanks, |
||
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; } | ||
|
||
public static new Project? Parse(in ParseContext context) | ||
{ | ||
var directiveText = context.DirectiveText; | ||
|
@@ -1878,11 +1953,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 | ||
? 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)) | ||
{ | ||
|
@@ -1900,18 +1996,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.