Skip to content

Quote manifest path when invoking cargo metadata - #1870

Open
RKS (rksharma-owg) wants to merge 1 commit into
microsoft:mainfrom
rksharma-owg:fix/rust-cargo-metadata-path-with-spaces
Open

RKS (rksharma-owg) wants to merge 1 commit into
microsoft:mainfrom
rksharma-owg:fix/rust-cargo-metadata-path-with-spaces

Conversation

@rksharma-owg

Copy link
Copy Markdown

Summary

Fixes an issue where invoking cargo metadata fails when the path to Cargo.toml contains spaces.

Fixes #1467

Problem

When scanning a Rust project whose path contains spaces (such as paths with spaces in folder names like AppControl Manager or workspace names), RustCliParser and RustMetadataContextBuilder pass the raw file path directly following the --manifest-path parameter in cliService.ExecuteCommandAsync.

Because the command line invocation service joins parameters with spaces (string.Join(" ", parameters)), the unquoted path is split by the process launcher/Cargo CLI argument parser into multiple arguments. As a result, Cargo aborts with an error such as:
error: unexpected argument '<PathSnippet>' found
causing the CLI scan to fail and fallback behavior to be triggered or fail.

Solution

  1. In RustCliParser.cs, wrap componentStream.Location in quotes if it contains spaces and is not already quoted.
  2. In RustMetadataContextBuilder.cs, wrap manifestPath in quotes if it contains spaces and is not already quoted.

Tests

  • Added ParseAsync_QuotesManifestPathWhenPathContainsSpaces in RustCliParserTests:
    • Verifies that cargo metadata is invoked with the manifest path wrapped in quotes when the path contains spaces.
  • Added BuildPackageOwnershipMapAsync_QuotesManifestPathWhenPathContainsSpaces in RustMetadataContextBuilderTests:
    • Verifies that RustMetadataContextBuilder correctly quotes the manifest path when calling cargo metadata for ownership map building.
  • All existing tests in RustCliParserTests (37 tests) and RustMetadataContextBuilderTests (14 tests) continue to pass.

When scanning Rust projects located in paths containing spaces, the
unquoted manifest path passed after --manifest-path is split by the
underlying command invocation mechanism, causing cargo metadata to fail with
'unexpected argument found'.

This change wraps the manifest path in quotes if it contains spaces and is
not already quoted, in both RustCliParser and RustMetadataContextBuilder.
Copilot AI lite review requested due to automatic review settings September 17, 2026 03:43
@rksharma-owg
RKS (rksharma-owg) requested a review from a team as a code owner September 17, 2026 03:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The changes directly address the reported argument-splitting failure and include targeted tests covering the new quoting behavior.

Pull request overview

This PR fixes Rust scanning failures when cargo metadata is invoked with a --manifest-path that contains spaces, which previously caused the path to be split into multiple arguments due to parameter joining in CommandLineInvocationService.

Changes:

  • Quote Cargo.toml manifest paths (when they contain spaces) before passing them to cliService.ExecuteCommandAsync in the Rust CLI parser and metadata context builder.
  • Add unit tests verifying the quoted manifest-path argument is used when the path contains spaces.
File summaries
File Description
src/Microsoft.ComponentDetection.Detectors/rust/Parsers/RustCliParser.cs Quotes the --manifest-path argument when componentStream.Location contains spaces.
src/Microsoft.ComponentDetection.Detectors/rust/RustMetadataContextBuilder.cs Quotes the --manifest-path argument when the provided manifest path contains spaces.
test/Microsoft.ComponentDetection.Detectors.Tests/RustCliParserTests.cs Adds a test asserting cargo metadata is invoked with a quoted manifest path when the path contains spaces.
test/Microsoft.ComponentDetection.Detectors.Tests/RustMetadataContextBuilderTests.cs Adds a test asserting ownership-map building invokes cargo metadata with a quoted manifest path when the path contains spaces.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

There is a bug when the tool calls Rust's Cargo Metadata command and there is a space in the path

2 participants