CLI: Initialize cidfile option#40135
CLI: Initialize cidfile option#40135AmelBawa-msft wants to merge 7 commits intofeature/wsl-for-appsfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enables the --cidfile option for wslc container run / wslc container create, wiring it from CLI parsing through to container execution so the container ID can be written to a user-specified file, and adds end-to-end coverage for the new behavior.
Changes:
- Enable
--cidfileargument forcontainer runandcontainer create, store it onContainerOptions, and propagate it into service execution. - Implement CID file validation (fails when the path already exists) and write container IDs to the specified file.
- Add e2e tests + command-line parsing test cases, and update help-text expectations to include
--cidfile.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/windows/wslc/e2e/WSLCE2EContainerRunTests.cpp | Adds e2e tests for container run --cidfile (success + already-exists) and updates help expectations. |
| test/windows/wslc/e2e/WSLCE2EContainerCreateTests.cpp | Adds e2e tests for container create --cidfile (success + already-exists) and updates help expectations. |
| test/windows/wslc/CommandLineTestCases.h | Adds CLI parsing test cases covering --cidfile for run/create. |
| src/windows/wslc/tasks/ContainerTasks.cpp | Plumbs parsed --cidfile into ContainerOptions. |
| src/windows/wslc/services/ContainerService.cpp | Implements writing the container ID to the cidfile for run/create flows. |
| src/windows/wslc/services/ContainerModel.h | Adds CidFile to ContainerOptions. |
| src/windows/wslc/commands/ContainerRunCommand.cpp | Enables ArgType::CIDFile for container run. |
| src/windows/wslc/commands/ContainerCreateCommand.cpp | Enables ArgType::CIDFile for container create. |
| src/windows/wslc/arguments/ArgumentValidation.h | Declares ValidateCidFile. |
| src/windows/wslc/arguments/ArgumentValidation.cpp | Adds CID file validation + hooks it into argument validation switch. |
| src/windows/wslc/arguments/ArgumentDefinitions.h | Uncomments / enables the CIDFile argument definition in the global argument list. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…l-case file-exists error Agent-Logs-Url: https://github.com/microsoft/WSL/sessions/8f4c0514-1fa5-45a7-aed2-11d0a878188b Co-authored-by: AmelBawa-msft <104940545+AmelBawa-msft@users.noreply.github.com>
| void ValidateCidFile(const std::wstring& cidFile) | ||
| { | ||
| std::error_code ec; | ||
| if (std::filesystem::exists(std::filesystem::path{cidFile}, ec)) |
There was a problem hiding this comment.
I don't recommend manually checking if it the file exists since that creates a potential race condition with the CreateFile() call later.
| } | ||
|
|
||
| const auto path = std::filesystem::path(cidFilePath.value()); | ||
| HANDLE file = ::CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); |
There was a problem hiding this comment.
I recommend using:
auto file = wil::try_create_new_file(path.c_str(), GENERIC_WRITE);
That way we don't manually have to close the handle
| } | ||
|
|
||
| DWORD bytesWritten{}; | ||
| const bool writeSuccess = ::WriteFile(file, containerId.data(), static_cast<DWORD>(containerId.size()), &bytesWritten, nullptr) != FALSE; |
There was a problem hiding this comment.
nit: We can simplify this to:
THROW_IF_WIN32_BOOL_FALSE(WriteFile(file, containerId.data(), static_cast<DWORD>(containerId.size()), &bytesWritten, nullptr);
I think it would be OK not to have specialized errors for write errors here, since they're very unlikely since we successfully opened the handle at this point
Wires up
--cidfileforwslc container run/wslc container create, writing the container ID to a user-specified file with atomic exclusive-create semantics (no clobbering existing files).Summary of the Pull Request
--cidfileargument forcontainer runandcontainer createCreateFileW(CREATE_NEW)PR Checklist
Detailed Description of the Pull Request / Additional comments
Error handling improvements:
ERROR_FILE_EXISTS/ERROR_ALREADY_EXISTSfromCreateFileWmaps to the dedicatedWSLCCLI_CIDFileAlreadyExistsErrormessage — covers TOCTOU races where the file appears between validation and write.MessageWslcFailedToWriteFile("Failed to write to '{}': {}") instead of the misleadingMessageWslcFailedToOpenFile.MessageWslcFailedToWriteFilelocalization string added toen-US/Resources.resw.Argument validation:
ValidateCidFilethrowsHRESULT_FROM_WIN32(ec.value())with path + reason onstd::filesystemerrors, rather than a genericE_INVALIDARG.Validation Steps Performed
End-to-end tests added for
container runandcontainer createcovering: