Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions documentation/general/dotnet-run-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
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).
Copy link
Member

@RikkiGibson RikkiGibson Oct 8, 2025

Choose a reason for hiding this comment

The 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

  1. CLI-like. dotnet add MyProject.csproj reference OtherProject.csproj, etc. MSBuild variables are not respected, a relative path to a directory or project is expected. This is probably the 90% case.
  2. MSBuild-like. A project file path is expected. Variables are expanded. Full paths are expected (e.g. $(MSBuildThisFileDirectory)/path/to/project.csproj).

Essentially, we first try to resolve as (1), as if it is the last argument to dotnet add MyProject.csproj reference OtherProject.csproj, and, if we don't find it, just pass the path as written into the virtual project as case (2). I am speculating there is not a need to include both the convenience features of (1) and the expressiveness of (2). It's not something that either ordinary projects or the CLI can do today.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 dotnet run. But consider that during dotnet project convert, if we get something like #:project $(MSBuildThisFileDirectory)/path/to/project.csproj, we still need to evaluate it ourselves so we can rewrite it to make it correct relative to the conversion output directory. So the logic for expanding the paths probably needs to remain.

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".
Expand Down
59 changes: 49 additions & 10 deletions src/Cli/dotnet/Commands/Project/Convert/ProjectConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -169,17 +175,50 @@ 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 (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;
}

// If the path is absolute and it *starts* with some `$(..)` vars,
// 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).
//
// If the path is absolute but the `$(..)` vars are *inside* of it (like `C:\$(..)\Lib`),
// instead of at the start, we can keep those vars, i.e., skip this `if` block.
//
// The `OriginalName` is absolute if there are no `$(..)` vars at the start.
if (!Path.IsPathFullyQualified(project.OriginalName))
{
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();
Expand Down
120 changes: 104 additions & 16 deletions src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

return project;
}

private ProjectInstance CreateProjectInstance(
ProjectCollection projectCollection,
ImmutableArray<CSharpDirective> directives,
Action<IDictionary<string, string>>? addGlobalProperties)
{
var projectRoot = CreateProjectRootElement(projectCollection);

Expand All @@ -1069,7 +1099,7 @@ ProjectRootElement CreateProjectRootElement(ProjectCollection projectCollection)
var projectFileWriter = new StringWriter();
WriteProjectFile(
projectFileWriter,
Directives,
directives,
isVirtualProject: true,
targetFilePath: EntryPointFileFullPath,
artifactsPath: ArtifactsPath,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The 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 required members from the type, this isn't serving any purpose.

Copy link
Member Author

@jjonescz jjonescz Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks, this is a leftover from some earlier state of the code edit: I though could remove the SetsRequiredMembers too but looks like that has to stay due to the required base property Name - but I removed the required modifiers, those seemed unnecessary.

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;
Expand All @@ -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))
{
Expand All @@ -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}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ public void SameAsTemplate()
}

[Theory] // https://github.com/dotnet/sdk/issues/50832
[InlineData("File", "Lib", "../Lib", "Project", "../Lib/lib.csproj")]
[InlineData(".", "Lib", "./Lib", "Project", "../Lib/lib.csproj")]
[InlineData(".", "Lib", "Lib/../Lib", "Project", "../Lib/lib.csproj")]
[InlineData("File", "Lib", "../Lib", "File/Project", "../../Lib/lib.csproj")]
[InlineData("File", "Lib", "..\\Lib", "File/Project", "../../Lib/lib.csproj")]
[InlineData("File", "Lib", "../Lib", "Project", "..{/}Lib{/}lib.csproj")]
[InlineData(".", "Lib", "./Lib", "Project", "..{/}Lib{/}lib.csproj")]
[InlineData(".", "Lib", "Lib/../Lib", "Project", "..{/}Lib{/}lib.csproj")]
[InlineData("File", "Lib", "../Lib", "File/Project", "..{/}..{/}Lib{/}lib.csproj")]
[InlineData("File", "Lib", @"..\Lib", "File/Project", @"..{/}..\Lib{/}lib.csproj")]
[InlineData("File", "Lib", "../$(LibProjectName)", "File/Project", "..{/}..{/}$(LibProjectName){/}lib.csproj")]
[InlineData("File", "Lib", @"..\$(LibProjectName)", "File/Project", @"..{/}..\$(LibProjectName){/}lib.csproj")]
[InlineData("File", "Lib", "$(MSBuildProjectDirectory)/../$(LibProjectName)", "File/Project", "..{/}..{/}Lib{/}lib.csproj")]
public void ProjectReference_RelativePaths(string fileDir, string libraryDir, string reference, string outputDir, string convertedReference)
{
var testInstance = _testAssetsManager.CreateTestDirectory();
Expand Down Expand Up @@ -105,6 +108,7 @@ public static void M()
Directory.CreateDirectory(fileDirFullPath);
File.WriteAllText(Path.Join(fileDirFullPath, "app.cs"), $"""
#:project {reference}
#:property LibProjectName=Lib
C.M();
""");

Expand All @@ -130,7 +134,7 @@ public static void M()

File.ReadAllText(Path.Join(outputDirFullPath, "app.csproj"))
.Should().Contain($"""
<ProjectReference Include="{convertedReference.Replace('/', Path.DirectorySeparatorChar)}" />
<ProjectReference Include="{convertedReference.Replace("{/}", Path.DirectorySeparatorChar.ToString())}" />
""");
}

Expand Down Expand Up @@ -191,6 +195,64 @@ public static void M()
""");
}

[Fact]
public void ProjectReference_FullPath_WithVars()
{
var testInstance = _testAssetsManager.CreateTestDirectory();

var libraryDirFullPath = Path.Join(testInstance.Path, "Lib");
Directory.CreateDirectory(libraryDirFullPath);
File.WriteAllText(Path.Join(libraryDirFullPath, "lib.cs"), """
public static class C
{
public static void M()
{
System.Console.WriteLine("Hello from library");
}
}
""");
File.WriteAllText(Path.Join(libraryDirFullPath, "lib.csproj"), $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
</PropertyGroup>
</Project>
""");

var fileDirFullPath = Path.Join(testInstance.Path, "File");
Directory.CreateDirectory(fileDirFullPath);
File.WriteAllText(Path.Join(fileDirFullPath, "app.cs"), $"""
#:project {fileDirFullPath}/../$(LibProjectName)
#:property LibProjectName=Lib
C.M();
""");

var expectedOutput = "Hello from library";

new DotnetCommand(Log, "run", "app.cs")
.WithWorkingDirectory(fileDirFullPath)
.Execute()
.Should().Pass()
.And.HaveStdOut(expectedOutput);

var outputDirFullPath = Path.Join(testInstance.Path, "File/Project");
new DotnetCommand(Log, "project", "convert", "app.cs", "-o", outputDirFullPath)
.WithWorkingDirectory(fileDirFullPath)
.Execute()
.Should().Pass();

new DotnetCommand(Log, "run")
.WithWorkingDirectory(outputDirFullPath)
.Execute()
.Should().Pass()
.And.HaveStdOut(expectedOutput);

File.ReadAllText(Path.Join(outputDirFullPath, "app.csproj"))
.Should().Contain($"""
<ProjectReference Include="{"../../$(LibProjectName)/lib.csproj".Replace('/', Path.DirectorySeparatorChar)}" />
""");
}

[Fact]
public void DirectoryAlreadyExists()
{
Expand Down Expand Up @@ -1551,7 +1613,9 @@ public void Directives_VersionedSdkFirst()
private static void Convert(string inputCSharp, out string actualProject, out string? actualCSharp, bool force, string? filePath)
{
var sourceFile = new SourceFile(filePath ?? "/app/Program.cs", SourceText.From(inputCSharp, Encoding.UTF8));
var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile, reportAllErrors: !force, DiagnosticBag.ThrowOnFirst());
var diagnostics = DiagnosticBag.ThrowOnFirst();
var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile, reportAllErrors: !force, diagnostics);
directives = VirtualProjectBuildingCommand.EvaluateDirectives(project: null, directives, sourceFile, diagnostics);
var projectWriter = new StringWriter();
VirtualProjectBuildingCommand.WriteProjectFile(projectWriter, directives, isVirtualProject: false);
actualProject = projectWriter.ToString();
Expand Down
Loading
Loading