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

Task name class name error #8921

Merged
merged 8 commits into from
Aug 4, 2023

Conversation

jrdodds
Copy link
Contributor

@jrdodds jrdodds commented Jun 20, 2023

Fixes #7842

Context

Both "MSBuild inline tasks" and "MSBuild inline tasks with RoslynCodeTaskFactory" documentation pages contain a note:

When defining the task class in the source file, the class name must agree with the TaskName attribute of the corresponding UsingTask element.

When this situation is detected, there is an error, but the error message is not helpful and doesn't explain the real issue.

PR #2363 added a throw of an exception when TaskType is not set. This is not a recent change but unfortunately, it appears to have impacted the messaging for the mismatched TaskName and class name error.

Changes Made

  • RoslynCodeTaskFactory.cs: In the Initialize() method when the factory fails to match the task name to a class name, a CodeTaskFactory.CouldNotFindTaskInAssembly error is logged (not thrown). This log message is rolled up to a MSB4036 error.
***: error : The task name "SayHello" could not be found.
***: error MSB4036: The "SayHello" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "***" directory.
  • TaskRegistry.cs: The exception from PR Display an error if an ITaskFactory does not set the TaskType property. #2363 was retained, because it covers all ITaskFactory instances, but the test was modified to include checking if ITaskFactory.Initialize() returned true. When Initialize() fails and returns false, the parent GetTaskFactory() method will return false. When Initialize() returns false, GetTaskFactory() should not perform other work. Within an else block where Initialize() is called, a check of TaskFactoryParameters is now qualified to only be performed when ITaskFactory.Initialize() returns true
  • .editorconfig: Added an .editorconfig to explicitly set RoslynCodeTaskFactory_Tests.cs with trim_trailing_whitespace = false. The unit test file contains expected source code output that contains trailing whitespace. The auto-formatting will break the unit tests.
  • RoslynCodeTaskFactory_Tests.cs:
    • modified CodeLanguageFromTaskBody() to test that Language will default to C#.
    • added CSharpClass() and CSharpClassSourceCodeFromFile() to provide coverage of Type="Class".
    • added MismatchedTaskNameAndTaskClassName() to test the error messaging change

Testing

Tested on Windows 11 and macOS 12.

Tested by running the full set of unit tests.

Tested by running a test solution + project.

Notes

In the original issue there is a further problem with the code where the output parameter is missing in the class source code. The error messaging for this situation could also be improved but is not addressed in this PR.

Copy link
Member

@JanKrivanek JanKrivanek left a comment

Choose a reason for hiding this comment

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

Looks good to me - thank you for the contribution!

src/Tasks.UnitTests/RoslynCodeTaskFactory_Tests.cs Outdated Show resolved Hide resolved
@YuliiaKovalova YuliiaKovalova merged commit 5e29ccb into dotnet:main Aug 4, 2023
@jrdodds jrdodds deleted the TaskNameClassNameError branch August 4, 2023 11:49
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.

Bad error when CodeTaskFactory has a mismatch between TaskName and class name
3 participants