Skip to content
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

Add basic support for 'dotnet run file.cs' #46915

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jjonescz
Copy link
Member

@jjonescz jjonescz commented Feb 18, 2025

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-CLI untriaged Request triage from a team member labels Feb 18, 2025
@jjonescz jjonescz marked this pull request as ready for review February 19, 2025 12:15
@Copilot Copilot bot review requested due to automatic review settings February 19, 2025 12:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/Cli/dotnet/commands/dotnet-run/RunCommand.cs:609

  • [nitpick] The method 'DiscoverProjectFilePath' now returns both a project file path and an entry point file via an out parameter; consider renaming it (for example, to 'DiscoverProjectAndEntryPointPaths') to better reflect its dual purpose.
private static string? DiscoverProjectFilePath(string? projectFileOrDirectoryPath, ref string[] args, out string? entryPointFilePath)

src/Cli/dotnet/commands/dotnet-run/RunCommand.cs:250

  • [nitpick] Consider renaming the 'projectFactory' out parameter to 'projectInstanceFactory' for improved clarity since it is intended to supply a ProjectInstance.
private void EnsureProjectIsBuilt(out Func<ProjectCollection, ProjectInstance>? projectFactory)

{
if (globalProperties.TryGetValue(key, out var existingValues))
{
globalProperties[key] = existingValues + ";" + value;
Copy link
Preview

Copilot AI Feb 19, 2025

Choose a reason for hiding this comment

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

[nitpick] Concatenating multiple property values with a semicolon might lead to formatting issues if the existing value already ends with one; consider aggregating values in a list and joining them to ensure proper formatting.

Suggested change
globalProperties[key] = existingValues + ";" + value;
var valuesList = existingValues.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
valuesList.Add(value);
globalProperties[key] = string.Join(";", valuesList);

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Member Author

Choose a reason for hiding this comment

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

If user is passing empty properties somehow, I think that's on them. Also, this should be equivalent to pre-existing code, just moved.

@@ -205,6 +199,16 @@ private static string GetDotnetPath()
return new Muxer().MuxerPath;
}

internal static Dictionary<string, string> GetMSBuildRequiredEnvironmentVariables()
{
return new()
Copy link
Member

Choose a reason for hiding this comment

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

I know this is code that is already there but surprised this is using an ordinal comparison for environment variable names.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, I guess I'm keeping this since it's a pre-existing behavior, but let me know if you think I should change this.

for (int i = args.Length - 1; i >= 0; i--)
{
var arg = args[i];
if (arg.StartsWith("/bl:") || arg.Equals("/bl")
Copy link
Member

Choose a reason for hiding this comment

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

Use explicit string comparison arguments

Copy link
Member Author

@jjonescz jjonescz Feb 20, 2025

Choose a reason for hiding this comment

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

Good idea. This code was copied from elsewhere, let me share it.

It looks like dotnet build is case insensitive (dotnet build -BL produces a binlog) whereas dotnet run is case sensitive (-BL is passed as an argument to the executed app), so I'm not sure if I should fix that inconsistency and use ignore-case comparison here to make dotnet run -BL produce binlog as well (but that's technically a breaking change)?

@@ -205,6 +199,16 @@ private static string GetDotnetPath()
return new Muxer().MuxerPath;
}

internal static Dictionary<string, string> GetMSBuildRequiredEnvironmentVariables()
Copy link
Member

Choose a reason for hiding this comment

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

This call is now being used in a few places, sometimes to create a new dictionary and sometimes to just iterate this data which is effectively static. Did you consider instead having a definition like this:

 internal static ImmutableDictionary<string, string> MSBuildRequiredEnvironmentVariables { get; } = ...

Then in the places where a new dictionary is needed use new (MSBuildRequiredEnvironmentVariables) and for iteration it can just use the property directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

The default env vars cannot be static and initialized only once, they can change.

}
}

public ProjectInstance CreateProjectInstance(ProjectCollection projectCollection)
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 reason this overload is needed? The below one with the optional should work for the call sites I see.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there is a delegate created from this one here:

projectFactory = command.CreateProjectInstance;

The one with the optional argument wouldn't work (would need a lambda wrapper which seems less efficient).

Also this one is public whereas the one with the optional arg isn't exposed, it's private.

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? The other files seem to implicitly have nullable analysis enabled

Copy link
Member Author

@jjonescz jjonescz Feb 20, 2025

Choose a reason for hiding this comment

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

They unfortunately don't, only nullable annotations are enabled, but you get no nullable warnings without this.

Which made me realize I could also nullable-enable the RunCommand.cs file since I did a lot of changes in there too.

@RikkiGibson RikkiGibson self-assigned this Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-CLI untriaged Request triage from a team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants