Skip to content

CLI: Initialize cidfile option#40135

Open
AmelBawa-msft wants to merge 7 commits intofeature/wsl-for-appsfrom
user/amelbawa/cidfile
Open

CLI: Initialize cidfile option#40135
AmelBawa-msft wants to merge 7 commits intofeature/wsl-for-appsfrom
user/amelbawa/cidfile

Conversation

@AmelBawa-msft
Copy link
Copy Markdown

@AmelBawa-msft AmelBawa-msft commented Apr 8, 2026

Wires up --cidfile for wslc 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

  • Enables --cidfile argument for container run and container create
  • Validates the path before use (fails if file already exists)
  • Writes container ID atomically via CreateFileW(CREATE_NEW)
  • Adds e2e tests and CLI parsing test coverage; updates help-text expectations

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Error handling improvements:

  • ERROR_FILE_EXISTS / ERROR_ALREADY_EXISTS from CreateFileW maps to the dedicated WSLCCLI_CIDFileAlreadyExistsError message — covers TOCTOU races where the file appears between validation and write.
  • Write/partial-write failures now use MessageWslcFailedToWriteFile ("Failed to write to '{}': {}") instead of the misleading MessageWslcFailedToOpenFile.
  • New MessageWslcFailedToWriteFile localization string added to en-US/Resources.resw.

Argument validation:

  • ValidateCidFile throws HRESULT_FROM_WIN32(ec.value()) with path + reason on std::filesystem errors, rather than a generic E_INVALIDARG.

Validation Steps Performed

End-to-end tests added for container run and container create covering:

  • Successful CID file creation and content verification
  • Rejection when the CID file path already exists

@AmelBawa-msft AmelBawa-msft requested a review from a team as a code owner April 8, 2026 21:06
Copilot AI review requested due to automatic review settings April 8, 2026 21:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 --cidfile argument for container run and container create, store it on ContainerOptions, 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.

Copilot AI review requested due to automatic review settings April 8, 2026 22:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 8, 2026 22:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

…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>
@AmelBawa-msft AmelBawa-msft changed the title Initialize cidfile option CLI: Initialize cidfile option Apr 8, 2026
Copilot AI review requested due to automatic review settings April 9, 2026 17:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

void ValidateCidFile(const std::wstring& cidFile)
{
std::error_code ec;
if (std::filesystem::exists(std::filesystem::path{cidFile}, ec))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

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.

4 participants