Skip to content

Add #:project directive #49311

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
merged 12 commits into from
Jun 13, 2025
Merged

Add #:project directive #49311

merged 12 commits into from
Jun 13, 2025

Conversation

jjonescz
Copy link
Member

Resolves #48746.

@jjonescz jjonescz requested a review from a team June 10, 2025 16:31
@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Jun 10, 2025
@jaredpar jaredpar added this to the 10.0.1xx milestone Jun 10, 2025
@jjonescz jjonescz requested review from jaredpar and MiYanni June 12, 2025 15:37
var resolvedProjectPath = Path.Combine(sourceDirectory, directiveText.Replace('\\', '/'));
if (Directory.Exists(resolvedProjectPath))
{
var fullFilePath = MsbuildProject.GetProjectFileFromDirectory(resolvedProjectPath).FullName;
Copy link
Member

Choose a reason for hiding this comment

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

A few lines above we normalize \\ to / but FullName on windows will still have \\ in the path. Should we re-normalize here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Consider user writing #:project ..\lib on Windows and another user executing that on Linux. The normalization above makes sure that's possible.

Normalization here isn't necessary for that to work, but I guess we could normalize for other reasons. But the current behavior (without normalization) should be consistent with what dotnet reference add is doing (since we use the same utility MsbuildProject.GetProjectFileFromDirectory as that command).

Copy link
Member

Choose a reason for hiding this comment

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

It's not obvious to me that #:project ..\lib should work on Linux. Do you happen to know the behavior of dotnet run --project ..\lib?

Copy link
Member Author

@jjonescz jjonescz Jun 12, 2025

Choose a reason for hiding this comment

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

dotnet run --project ..\lib probably wouldn't work. But <ProjectReference Include="..\lib\lib.csproj" /> would. A general rule I take from that is that using \ on command line won't work but in a source code (as #:project or <ProjectReference/>) works. I guess the reason it works in <ProjectReference/> is that msbuild started as Windows-only and they wanted it to be portable. We don't need to do that for #:project I guess. So I'm not against not normalizing at all and in effect forcing users to use forward slashes if they want to write cross-platform code. Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

Yep MSBuild "solves" this for us as paths there are normalized. We can decide to simply pass through what the user types in the directive directly I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can decide to simply pass through what the user types in the directive directly I think.

It's not so simple since we also want to support users specifying directory paths which <ProjectReference/> elements don't support, therefore we might need to search for the project file in the directory and for that to work on linux, we need to normalize slashes.

@jjonescz jjonescz requested a review from jaredpar June 13, 2025 11:19
@jaredpar jaredpar merged commit 9a1c6fa into dotnet:main Jun 13, 2025
30 checks passed
@jjonescz jjonescz deleted the sprint-project-ref branch June 13, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-run-file Items related to the "dotnet run <file>" effort
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dotnet run file.cs #: directive for project references
6 participants