Skip to content

Escape MSBuild special characters in output paths - #52582

Closed
MiYanni with Copilot wants to merge 8 commits into
mainfrom
copilot/fix-dotnet-publish-semi-colon
Closed

MiYanni with Copilot wants to merge 8 commits into
mainfrom
copilot/fix-dotnet-publish-semi-colon

Conversation

Copilot AI commented Jan 20, 2026 •

Copy link
Copy Markdown
Contributor

dotnet publish -o fails when the output directory contains MSBuild special characters (;, %, $, etc.) because MSBuild interprets them as delimiters/references:

$ cd 't;e;s;t'
$ dotnet publish -o dist
MSBUILD : error MSB1006: Property is not valid.
Switch: e

Changes

  • Added EscapeMSBuildPropertyValue to percent-encode MSBuild special characters (; → %3B, % → %25, $ → %24, etc.)
  • Updated ForwardAsOutputPath to escape paths before forwarding to MSBuild
  • Escaping occurs before quote-wrapping to avoid interfering with quote logic

Impact

Applies to all commands using ForwardAsOutputPath: publish, build, clean, store.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • learn.microsoft.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>dotnet publish fails when publish directory has a semi-colon</issue_title>
<issue_description>Running dotnet publish fails when the publish directory is specified and has a semi-colon (either in a specified absolute path or computed from a specified relative path)

Related to this, dotnet-publish fails with semicolon as well.

$ dotnet new console -n 't;e;s;t'
$ cd 't;e;s;t'

$ dotnet publish -o dist
MSBUILD : error MSB1006: Property is not valid.
Switch: e

For switch syntax, type "MSBuild -help"

$ dotnet publish -p:PublishDir="$(pwd)/dist"                

The "HasTrailingSlash" function only accepts a scalar value, but its argument "$(ClickOncePublishDir)" evaluates to "/Users/adeel/projects/t;e;s;t/dist" which is not a scalar value.  /Users/adeel/.dotnet9/sdk/9.0.100-rc.1.24414.26/Microsoft.Common.CurrentVersion.targets
MSBUILD : error MSB1006: Property is not valid.
Switch: e

For switch syntax, type "MSBuild -help"

# without publishdir or -o / --output, it works
$ dotnet publish
Restore complete (0,3s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  t;e;s;t succeeded (0,3s) → bin/Release/net9.0/publish/

Build succeeded in 0,7s

Originally posted by @am11 in #104402</issue_description>

Comments on the Issue (you are @copilot in this section)

@baronfel Isn't this just needing to escape MSBuild special characters? https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-escape-special-characters-in-msbuild?view=vs-2022

It's possible that we could consider automatically escaping these special characters in the dotnet CLI layer, but that has historically proven to be an error-prone pathway.</comment_new>
<comment_new>@elinor-fung
For -p:PublishDir, I can see the user needing to escape since they are passing in the property directly. But for just doing -o <relative_dir> from a directory with a semi-colon, I think the expectation should be that any escaping is handled for them.</comment_new>
<comment_new>@marcpopMSFT
Triage: @MiYanni please assign to copilot and see if it can handle escaping certain CLI inputs before we pass to msbuild. Start with the -o flag but do in a generic way that we can apply to other CLI commands that forward to a -p option that's not a boolean or number (so a string).</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits January 20, 2026 23:40
Co-authored-by: MiYanni <17788297+MiYanni@users.noreply.github.com>
Co-authored-by: MiYanni <17788297+MiYanni@users.noreply.github.com>
Co-authored-by: MiYanni <17788297+MiYanni@users.noreply.github.com>
Co-authored-by: MiYanni <17788297+MiYanni@users.noreply.github.com>
Co-authored-by: MiYanni <17788297+MiYanni@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dotnet publish failure with semi-colon in directory Escape MSBuild special characters in output paths Jan 21, 2026
Copilot AI requested a review from MiYanni January 21, 2026 00:09
@MiYanni
MiYanni marked this pull request as ready for review March 23, 2026 20:26
Copilot AI review requested due to automatic review settings March 23, 2026 20:26
@MiYanni
MiYanni requested a review from a team March 23, 2026 20:31

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.

Pull request overview

Fixes dotnet <command> -o/--output failures when the computed output path contains MSBuild special characters (e.g., ;, %, $) by escaping them before forwarding the property value to MSBuild.

Changes:

  • Added an EscapeMSBuildPropertyValue helper to percent-encode MSBuild special characters in property values.
  • Updated ForwardAsOutputPath to escape the full output path before optional quote-wrapping and forwarding to MSBuild.
  • Added new MSBuild invocation tests for dotnet publish -o and dotnet build -o to validate escaping behavior.

Reviewed changes

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

File Description
src/Cli/dotnet/Extensions/OptionForwardingExtensions.cs Introduces MSBuild property escaping and applies it to all output-path forwarding.
test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetPublishInvocation.cs Adds tests asserting PublishDir is forwarded with escaped special characters.
test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetBuildInvocation.cs Adds tests asserting OutputPath is forwarded with escaped special characters.

Copilot AI requested a review from MiYanni March 23, 2026 20:48
@marcpopMSFT

Copy link
Copy Markdown
Member

@dotnet/kitten to review this as well since it is special casing msbuild chars.

@ViktorHofer

Copy link
Copy Markdown
Member

I assume you don't want to use MSBuild APIs in that code path?

@am11

am11 commented Mar 25, 2026

Copy link
Copy Markdown
Member

I assume you don't want to use MSBuild APIs in that code path?

Good point, and in case we can use them, MSBuildPropertyParser should be replaced by some appropriate MSBuild API to maintain a single source of truth.

@MiYanni

MiYanni commented Apr 24, 2026

Copy link
Copy Markdown
Member

I assume you don't want to use MSBuild APIs in that code path?

@ViktorHofer I don't know the specifics here. It was a "just throw it at Copilot" kind of task. After next week, I'll be out for a while. Not sure how to get this moved forward. If not, I can just close it.

@MiYanni

MiYanni commented May 1, 2026

Copy link
Copy Markdown
Member

Closing as I won't be able to get around to this before I'm on break (EoD).

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.

dotnet publish fails when publish directory has a semi-colon

6 participants