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

Debugging MSBuild Tasks #8730

Merged
merged 6 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"port": 10000,
"preLaunchTask": "run-sample-under-dotnet",
},
{
"name": "Debug MSBuild Task",
"type": "coreclr",
"request": "attach",
"processId": "${input:processid}"
}
],
"inputs": [
{
Expand All @@ -52,6 +58,12 @@
"default": "Debug",
"description": "The Build Configuration",
"options": [ "Debug", "Release"]
},
{
"id": "processid",
"type": "promptString",
"default": "0",
"description": "Enter dotnet build process id reported when setting the env var MSBUILDDEBUGONSTART=2",
}
]
}
49 changes: 49 additions & 0 deletions Documentation/guides/MSBuildBestPractices.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@ This guide is a work-in-progress, but really has two main goals:
- What are good MSBuild practice in relation to what we already have
going on in Xamarin.Android MSBuild targets?

## Debugging MSBuild Tasks

One thing that is very useful is the ability to debug your Tasks while
they are being run on a build process. This is possible thanks to the
`MSBUILDDEBUGONSTART` environment variable. When set to `2` this will
force MSBuild to wait for a debugger connection before continuing.
You will see the following prompt.

```dotnetcli
Waiting for debugger to attach (dotnet PID 13001). Press enter to continue...
```

You can then use VS or VSCode to attach to this process and debug you tasks.

In the case of .NET Android we need to do a couple of thing first though. Firstly
we need to disable the use of `ILRepacker` on the `Xamarin.Android.Build.Tasks`
assembly. This is because `ILRepacker` does NOT handle debug symbols very well.
Assemblies it generates seem to be JIT optimized so the debugger will not load
the symbols. A new MSBuild property has been introduced to disable this feature
while debugging. `_ILRepackEnabled` can be set as an environment variable which
MSBuild will pickup. You will also need to build the `Debug` Configuration.

```dotnetcli
export CONFIGURATION=Debug
make prepare && _ILRepackEnabled=false make jenkins
```

This will disable the `ILRepacker` for the build.

You can then start your test app with the `dotnet-local` script (so it uses your build)

```dotnetcli
MSBUILDDEBUGONSTART=2 ~/<some xamarin.android checkout>/dotnet-local.sh build -m:1
```
dellis1972 marked this conversation as resolved.
Show resolved Hide resolved

Note: the `-m:1` is important as it restricts MSBuild to 1 node.

Once MSBuild starts it will print the following

```dotnetcli
Waiting for debugger to attach (dotnet PID xxxx). Press enter to continue...
```

You need to copy the PID value so we can use this in the IDE. For Visual Studio you can use the `Attach to Process` menu option, while you have the Xamarin.Android.sln solution open. For VSCode open the workspace then use the `Debug MSBuild Task` Run and Debug option. You will be prompted for the PID and it will then connect.

Once connected go back to your command prompt and press ENTER so that the MSBuild process can continue.

You will be able to set breakpoints in Tasks (but not Targets) and step through code from this point on.

## Naming

MSBuild targets, properties, and item groups are prefixed with an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<_MultiDexAarInAndroidSdk>extras\android\m2repository\com\android\support\multidex\1.0.1\multidex-1.0.1.aar</_MultiDexAarInAndroidSdk>
<_SupportLicense Condition="Exists('$(_AndroidSdkLocation)\extras\android\m2repository\NOTICE.txt')">$(_AndroidSdkLocation)\extras\android\m2repository\NOTICE.txt</_SupportLicense>
<_SupportLicense Condition="Exists('$(_AndroidSdkLocation)\extras\android\m2repository\m2repository\NOTICE.txt')">$(_AndroidSdkLocation)\extras\android\m2repository\m2repository\NOTICE.txt</_SupportLicense>
<_ILRepackEnabled Condition=" '$(_ILRepackEnabled)' == '' ">true</_ILRepackEnabled>
</PropertyGroup>
<ItemGroup>
<None
Expand Down Expand Up @@ -271,6 +272,7 @@
</ItemGroup>

<Target Name="ILRepacker"
Condition=" '$(_ILRepackEnabled)' == 'true' "
Copy link
Member

Choose a reason for hiding this comment

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

It looks like we only have 3 assemblies left?
https://github.com/xamarin/xamarin-android/blob/e987ac458536e59a8329a06d5c5d5f4d4ea2c6b6/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets#L268-L270

Can we remove these and not pack them?

We could also consider System.Text.Json over Newtonsoft.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving from Newtonsoft might help. As the others I'm not sure.
We should do that in a new PR

BeforeTargets="CopyFilesToOutputDirectory"
Inputs="$(MSBuildAllProjects);@(IntermediateAssembly);@(InputAssemblies)"
Outputs="$(IntermediateOutputPath)ILRepacker.stamp" >
Expand Down