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

Log NU1105 error for badly specified framework #6291

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from

Conversation

Nigusu-Allehu
Copy link
Contributor

@Nigusu-Allehu Nigusu-Allehu commented Feb 27, 2025

Bug

Related: NuGet/Home#12943

Description

Ensure misconfigured projects (specifically projects with badly specified framework) fail with a coded error (NU1105), improving error clarity and debugging

Test project

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFrameworks>net.6.0;net.8.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>
</Project>

Before

image

After

image

PR Checklist

@Nigusu-Allehu Nigusu-Allehu changed the title Log coded error for unsupported frameworks Log NU error for unsupported frameworks Feb 27, 2025
@Nigusu-Allehu Nigusu-Allehu changed the title Log NU error for unsupported frameworks Log NU1105 error for unsupported frameworks Feb 27, 2025
@Nigusu-Allehu Nigusu-Allehu changed the title Log NU1105 error for unsupported frameworks Log NU1105 error for badly specified framework Feb 27, 2025
@Nigusu-Allehu Nigusu-Allehu self-assigned this Feb 27, 2025
@Nigusu-Allehu Nigusu-Allehu marked this pull request as ready for review February 27, 2025 22:07
@Nigusu-Allehu Nigusu-Allehu requested a review from a team as a code owner February 27, 2025 22:07
jgonz120
jgonz120 previously approved these changes Feb 27, 2025
Copy link
Contributor

@jgonz120 jgonz120 left a comment

Choose a reason for hiding this comment

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

lgtm, not my area of expertise. Could you provide a screenshot of what the command prompt showed before your change?


if (invalidFrameworks.Count > 0)
{
throw RestoreSpecException.Create("", files);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming the "" will be replaced with some new message that says in a generic way that one or more frameworks were invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was on purpose. The logging of the error is done using an ilogger in a previous line.
The exception would look like

error : Invalid restore input. Input files: N:\trash\p7\p7\p7.csproj.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we're throwing, there's still reason to put a message that makes sense into that exception, regardless of logging that may have happened.

An empty string here can result in an odd message with 2 periods like "Invalid restore input. ."
See:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that would happen

<data name="InvalidRestoreInput" xml:space="preserve">
. The string does not have a period at the end.

If we were to put a message in the exception, it would look something like

NU1105: Invalid framework: ".9"
NU1105: Invalid framework: "unsupp1orted"
NU1105: Invalid framework: "1.222"
error : Invalid restore input. One or more invalid frameworks were detected. Input files: N:\trash\p7\p7\p7.csproj.

If we leave it as an empty message, it will look like

NU1105: Invalid framework: ".9"
NU1105: Invalid framework: "unsupp1orted"
NU1105: Invalid framework: "1.222"
error : Invalid restore input.  Input files: N:\trash\p7\p7\p7.csproj.

Personally, I think adding the message again is redundent.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're referencing 2 strings here. I'm only talking about InvalidRestoreInput

/// Looks up a localized string similar to Invalid restore input. {0}.

When this message is an empty string, I expect "Invalid restore input.__" where __ represents 2 unnecessary spaces.
This does not have "Input files:" in the string.

That would be InvalidRestoreInputWithFiles. Again, I expect the empty string message to create this message: "Invalid restore input.___Input files: {1}." where ___ represents 3 unnecessary spaces and {1} is filled in with filenames.

I'm not following why it's good to omit the message in the exception. I don't think typically we say that logging a message means an exception should be generalized or with empty messages. It may be redundant but is there an issue with such redundancy?

@Nigusu-Allehu Nigusu-Allehu requested a review from nkolev92 March 5, 2025 17:41
@Nigusu-Allehu Nigusu-Allehu requested a review from nkolev92 March 5, 2025 19:18
@Nigusu-Allehu Nigusu-Allehu requested a review from nkolev92 March 5, 2025 21:18
nkolev92
nkolev92 previously approved these changes Mar 5, 2025
Copy link
Contributor

@donnie-msft donnie-msft left a comment

Choose a reason for hiding this comment

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

Thx for addressing my earlier comments!

Comment on lines +182 to +184
List<NuGetFramework> frameworkNames = new List<NuGetFramework>(spec.TargetFrameworks.Count);

foreach (var f in spec.TargetFrameworks)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would combine the foreach and only loop through the list once. Its okay to create a list with more items than you end up using. This that case it might make a list with 3 items but only add 2, then after the foreach its just going to throw. But .NET would know that a list with a capacity of 3 really only has 2 items in it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can you rename f here please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would combine the foreach and only loop through the list once. Its okay to create a list with more items than you end up using. This that case it might make a list with 3 items but only add 2, then after the foreach its just going to throw. But .NET would know that a list with a capacity of 3 really only has 2 items in it.

The second loop is there in hopes of avoiding allocation since we might throw an exception if an invalid framework is found. That way, we don’t allocate a list unless we know we need it. However, given that the list is small, the extra allocation is negligible, and I don’t mind switching to one loop either.

@nkolev92
Copy link
Member

nkolev92 commented Mar 6, 2025

Out of curiosity, have you tested how Visual Studio behaves here?
I just wanted to make sure we're not double validating and posting these errors twice in VS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants