Skip to content

Some cmdlets don't respect -LiteralPath when it comes to backslashes #8158

Closed

Description

Path normalization (fixing of slashes) is happening regardless of whether -literalpath is used. Seen in Set-Content, Get-Content, Out-File (maybe others, search for NormalizePath()

Steps to reproduce

# on Unix based system
Set-Content -LiteralPath "\foo.txt" -Value "bar"

Expected behavior

A file called "\foo.txt" is created with content "bar"

Actual behavior

Path normalization (fixing slashes) doesn't take into account that this is a literal path.

Set-Content : Access to the path '/foo.txt' is denied.
At line:1 char:1
+ Set-Content -LiteralPath "\foo.txt" -Value "bar"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (/foo.txt:String) [Set-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.SetContentCommand

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0-preview.1
PSEdition                      Core
GitCommitId                    6.2.0-preview.1
OS                             Darwin 18.0.0 Darwin Kernel Version 18.0.0: Wed Aug 22 20:13:40 PDT 2018; root:xnu-4903.201.2~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. mklement0 commented on Oct 31, 2018

    @mklement0
    Contributor

    Given that \ is not a wildcard-expression metacharacter, I would expect it to be treated as a literal in -Path arguments too.

    In #6833 (comment) you've talked about the need to support escaping \ in paths, without being specific about -Path vs. -LiteralPath:

    Set-Content -LiteralPath '`\foo.txt' -Value "bar"
    Set-Content  '`\foo.txt' -Value "bar" # implied -Path

    To me it makes sense to treat the \ issue separately from the -Path / -LiteralPath dichotomy, as the latter is at least currently strictly about wildcards.

  2. SteveL-MSFT commented on Oct 31, 2018

    @SteveL-MSFT
    MemberAuthor

    Michael Klement (@mklement0) the problem isn't that \ is treated as wildcard, but that the cmdlets apply path normalization first before using the path. So all \ gets changed to / on non-Windows (and vice versa for Windows).

    Even:

    set-content "`\bar.txt" -Value "foo"

    fails as it tries to write to /bar.txt

  3. mklement0 commented on Oct 31, 2018

    @mklement0
    Contributor

    Understood, Steve Lee (@SteveL-MSFT).

    My example commands showed how it should work once fixed, based on my interpretation of the conclusion reached in #6833 - did I misread that conclusion?

    My point was that this escaping should work with both -Path and -LiteralPath arguments, as the latter distinction - at least so far - relates to wildcards only, and the \ / / issue is conceptually separate.

    After all, I shouldn't have to forgo wildcard matching just so I can get my \ chars. interpreted as-is.

    An aside re your example: If you use double quotes, you'll have to use two ` chars., because a single one is "eaten" by string parsing.

  4. SteveL-MSFT commented on Oct 31, 2018

    @SteveL-MSFT
    MemberAuthor

    Michael Klement (@mklement0) ok, I see what you mean. Yes, if escaped properly, it should work with -Path as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-DuplicateThe issue is a duplicate.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions