Add MSBuild and CMake build integration for WSLC container images#14551
Conversation
|
Hey @shuaiyuanxx 👋 — Following up on this draft PR. There are 11 unresolved review threads covering design and implementation feedback (flag file vs tar output, image save requirement, target naming, GLOB_RECURSE validation, VS design-time build interference, etc.). Is this still actively being developed? The PR is marked DO NOT MERGE — just checking in on the status and whether the approach is still the planned direction. |
Hi @benhillis, thanks for following up. Yes — this is still actively being developed. The design direction has been reviewed and approved via the spec. This PR is ready for review now. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Summary of the Pull Request
Add build-system integration to the
Microsoft.WSL.ContainersNuGet package so developers can declaratively build WSLC container images and export them to tar files as part of their MSBuild (C++/C#) and CMake workflows, with full incremental rebuild support.Developers declare
<WslcImage>items (MSBuild) or callwslc_add_image()(CMake) in their project, and the build system automatically runswslc image buildfollowed bywslc image saveafter the main build, skipping both when sources haven't changed.PR Checklist
Detailed Description of the Pull Request / Additional comments
New files
build/Microsoft.WSL.Containers.common.targetsWSLC0001), item metadata validation (WSLC0002–WSLC0004), incremental image build, image save to tar (atomic via.tmp+<Move>), optional prune, design-time build guard, and clean targetbuild/native/Microsoft.WSL.Containers.targets.tlogfiles (one path per line via ItemGroup) for VS Fast Up-to-Date Checkbuild/net/Microsoft.WSL.Containers.targetsUpToDateCheckInput/UpToDateCheckBuiltitems for VS up-to-date checkcmake/Microsoft.WSL.ContainersConfig.cmakewslc_add_image()CMake function: target-name validation, IMAGE parsing with auto-:latestappend, path normalization,CONFIGURE_DEPENDSglob, atomic save viacmake -E renameUsage — MSBuild (C++ / C#)
Imageis the full image reference (may include a tag);:latestis appended automatically when no tag is given.TarLocationis the output path for the saved tar (defaults to$(OutDir)<Identity>.tar).<WslcPruneAfterBuild>true</WslcPruneAfterBuild>to also runwslc image pruneafter save (off by default).Usage — CMake
IMAGEis the container image reference (required);:latestis appended automatically when no tag is given.TAR_LOCATIONdefaults to${CMAKE_CURRENT_BINARY_DIR}/<target>.tar. The tar is removed bycmake --build . --target clean(and Visual Studio's Clean).PRUNE_AFTER_BUILDto also runwslc image pruneafter save (off by default).Key design decisions
wslc image buildproduces the image;wslc image save -o <path>then exports it to a tar that downstream code can load viaWslcLoadSessionImageFromFileorwslc image load. Both steps run as part of the build, with full incremental tracking..tmp+ rename —wslc image saveusesCREATE_ALWAYS, which truncates the destination on entry. The integration writes to<tar>.tmpand renames on success (cmake -E rename/ MSBuild<Move>), so a failed save never leaves a partial tar that would mislead the incremental check.Inputs/Outputsand CMakeadd_custom_command(OUTPUT ...)track sources to skip both image build and save when nothing has changed. MSBuild uses a stamp file for the image build step (since image build doesn't produce a host file); CMake uses the tar directly as the output.wslc image pruneaffects the entire daemon image store, so it's off by default. Opt in via<WslcPruneAfterBuild>true</WslcPruneAfterBuild>(MSBuild) orPRUNE_AFTER_BUILDflag (CMake).Sourcesmetadata is escaped via$([MSBuild]::Escape())before being passed through MSBuild taskProperties=, and unescaped at the consumption site, so multi-directory paths work correctly.$(DesignTimeBuild) == 'true'to avoid slowing the VS IDE.wslc.exebeing on PATH (the WSL MSI puts it there) rather than guessing install locations. CMake usesfind_program(WSLC_CLI_PATH wslc); MSBuild defaultsWslcCliPathto barewslcso cmd.exe resolves it.DockerfileandContextare required;Identityis validated against invalid filename characters with actionable error codes (WSLC0001–WSLC0004).Validation Steps Performed
All tests run locally against wslc 2.7.3+ on Windows 11 (26200):
/p:WslcPruneAfterBuild=true)cmake --build . --target cleanremoves the tarPRUNE_AFTER_BUILDflag)WSLC0002/→ errorWSLC0004FATAL_ERRORWslcLoadSessionImageFromFile→ start container → capture stdout