docs/DevelopmentTips.md
Open the .NET MAUI workspace in VS Code by simply opening the root of your locally cloned .NET MAUI repository folder. VS Code will detect the workspace automatically and suggest that you open it.
In VS Code, select the device that you will be testing on. Using the Command Palette (<kbd>CTRL</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> or on macOS <kbd>Command</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>) type pick device and you will be presented with a set of choices for your target device (Android, iOS, etc.). First select the option that describes the platform you want to run the project on, and then select the device that is available for that platform in the next menu.
There is a sample project in src/Controls/samples/Controls.Sample.Sandbox. This is an empty project, which directly references the .NET MAUI code. In this project you can add your code to reproduce an issue while allowing you to set breakpoints in .NET MAUI source code and debug through it easily.
To let VS Code know this is the project you want to run, select the Sandbox project by going to the Command Palette (<kbd>CTRL</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> or on macOS <kbd>Command</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>) and start typing pick startup and select ".NET MAUI: Pick Startup Project" and then select the Sandbox project.
Before using the Command Palette for the first time, you may have to wait a minute for IntelliSense and other tasks to initialize. If the project hasn't 'settled' yet, you will see an error "Pick Startup Project has resulted in an error."
Note: When you are committing your PR, do not include your changes to the Sandbox project.
The below parameters can be used with the dotnet cake command in the root of your locally cloned .NET MAUI repository folder.
Note: For provisioning .NET SDK and workloads, prefer using
./build.sh -restore(or./build.cmd -restoreon Windows) instead of Cake. The Cake targets below are utility commands.
--target=publicapi
dotnet cake --target=publicapi
--clean
--android
--ios
--windows
--catalyst
dotnet cake --target=VS --workloads=global --android --ios
Note you will have to git clean -xdf your project if you change or add platforms.
To build and run Blazor Desktop samples, check out the Blazor Desktop wiki topic.
.dotnet\dotnet via build.* scripts on the root folderThis is the recommended method for provisioning the .NET SDK and workloads. It uses the Arcade build infrastructure. For more information, you can look here
# Restore .NET SDK and workloads to .dotnet folder
./build.sh -restore
or on Windows:
.\build.cmd -restore
To also build the solution:
./build.sh -restore -build
or
.\build.cmd -restore -build
To pack:
./build.sh -restore -pack
or
.\build.cmd -restore -pack
You can run a Cake target to bootstrap .NET SDK in bin\dotnet and launch Visual Studio:
dotnet tool restore
dotnet cake --target=VS
There is also a VSCode target for launching Visual Studio Code.
dotnet tool restore
dotnet cake --target=VSCode
--sln=<Path to SLN>
--pack flag if you make changes and need to repack.dotnet tool restore
dotnet cake --sln="<download_directory>\MauiApp2\MauiApp2.sln" --target=VS
--pack
dotnet tool restore
dotnet cake --target=VS --pack --sln="<download_directory>\MauiApp2\MauiApp2.sln"
Create a new .NET MAUI app using your new packs
dotnet tool restore
dotnet cake --pack
mkdir MyMauiApp
cd MyMauiApp
..\bin\dotnet\dotnet new maui
..\bin\dotnet\dotnet build -t:Run -f net[current_sdk_version]-android
You can also run commands individually:
# install local tools required to build (cake, pwsh, etc..)
dotnet tool restore
# Provision .NET SDK in bin\dotnet
dotnet build src\DotNet\DotNet.csproj
# Builds Maui MSBuild tasks
.\bin\dotnet\dotnet build Microsoft.Maui.BuildTasks.slnf
# Builds the rest of Maui
.\bin\dotnet\dotnet build Microsoft.Maui.sln
# Launch Visual Studio
dotnet cake --target=VS
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.
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 your tasks.
You can start your test app with the dotnet-local script (so it uses your MAUI build).
MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.sh build -m:1
MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.sh build -m:1
set MSBUILDDEBUGONSTART=2
~/<some maui checkout>/dotnet-local.cmd build -m:1
Note: the -m:1 is important as it restricts MSBuild to 1 node.
Once MSBuild starts, it will print the following
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 Microsoft.Maui.sln solution open. For VSCode, open the workspace, then use the Attach to Process 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.
If you want to test in-tree in VSCode, the Build Platform Sample command will ask you if you want to debug MSBuild tasks and fill in the MSBUILDDEBUGONSTART for you. The PID text will appear in the Terminal window in VSCode. You can then use the Attach to Process Run and Debug option to attach to the process.
The Integration test project under src/TestUtils/src/Microsoft.Maui.IntegrationTests contains tests that build and/or run MAUI templates or other projects.
These tests can be run using the Test Explorer in VS, or from the command line with dotnet test. Here's how to run an individual test with parameters from command line:
dotnet test src/TestUtils/src/Microsoft.Maui.IntegrationTests --logger "console;verbosity=diagnostic" --filter "Name=Build\(%22maui%22,%22net7.0%22,%22Debug%22,False\)"
.NET MAUI now supports running device tests on .NET Engineering Services Helix using XHarness. Helix provides cloud-based device testing infrastructure that enables running tests across multiple platforms and devices in parallel.
Device tests can be run on the following platforms via Helix:
The device test projects include:
Controls.DeviceTests - UI control testsCore.DeviceTests - Core framework testsGraphics.DeviceTests - Graphics and drawing testsEssentials.DeviceTests - Platform API testsMauiBlazorWebView.DeviceTests - Blazor WebView testsCheck available queues at helix.dot.net. The current configuration uses:
osx.15.arm64.Openosx.15.arm64.Openubuntu.2204.amd64.android.33.openThe following commands assume you are on the root of the maui repository.
First, restore tools and build the required MSBuild tasks:
# Restore dotnet tools
dotnet tool restore
# Build the MSBuild tasks (required)
./build.sh -restore -build -configuration Release -projects $(PWD)/Microsoft.Maui.BuildTasks.slnf /bl:BuildBuildTasks.binlog -warnAsError false
Build the device test projects:
# Build device tests for all platforms
./build.sh -restore -build -configuration Release /p:BuildDeviceTests=true /bl:BuildDeviceTests.binlog -warnAsError false
Submit the tests to Helix for execution:
We need to set some variables. More info about Helix can be found here
export BUILD_REASON=pr
export BUILD_REPOSITORY_NAME=maui
export BUILD_SOURCEBRANCH=main
export SYSTEM_TEAMPROJECT=dnceng
export SYSTEM_ACCESSTOKEN=''
# Send to Helix for Android
./eng/common/msbuild.sh ./eng/helix_xharness.proj /restore /p:TreatWarningsAsErrors=false /t:Test /p:TargetOS=android /bl:sendhelix_android.binlog -verbosity:diag
# Send to Helix for iOS
./eng/common/msbuild.sh ./eng/helix_xharness.proj /restore /p:TreatWarningsAsErrors=false /t:Test /p:TargetOS=ios /bl:sendhelix_ios.binlog -verbosity:diag
# Send to Helix for Mac Catalyst
./eng/common/msbuild.sh ./eng/helix_xharness.proj /restore /p:TreatWarningsAsErrors=false /t:Test /p:TargetOS=maccatalyst /bl:sendhelix_catalyst.binlog -verbosity:diag
For Windows development, use the corresponding .cmd files:
set BUILD_REASON=pr
set BUILD_REPOSITORY_NAME=maui
set BUILD_SOURCEBRANCH=main
set SYSTEM_TEAMPROJECT=dnceng
set SYSTEM_ACCESSTOKEN=
REM Build MSBuild tasks
.\build.cmd -restore -build -configuration Release -projects ".\Microsoft.Maui.BuildTasks.slnf" /bl:BuildBuildTasks.binlog -warnAsError false
REM Build device tests
.\build.cmd -restore -build -configuration Release /p:BuildDeviceTests=true /bl:BuildDeviceTests.binlog -warnAsError false
REM Send to Helix (Android example)
.\eng\common\msbuild.cmd .\eng\helix_xharness.proj /restore /p:TreatWarningsAsErrors=false /t:Test /p:TargetOS=android /bl:sendhelix.binlog -verbosity:diag
The Helix configuration is defined in eng/helix_xharness.proj and includes:
/bl:filename.binlog for detailed MSBuild logs-verbosity:diag for maximum diagnostic outputThe device tests are integrated into the CI pipeline via:
eng/pipelines/common/stage-device-tests.yml - Pipeline templateeng/test-configuration.json - Test retry configuration