Skip to content

Interpret control characters pasted into -LiteralPath arguments as verbatim characters. #4072

Closed
@RokeJulianLockhart

Description

@RokeJulianLockhart

Prerequisites

  • Write a descriptive title.

Description of the new feature/enhancement

As PowerShell/PowerShell#24037 (comment) explains:

Expected behaviour

Originally, this issue was going to be about how PowerShell can't handle certain legal UTF-8 characters in inode paths. However, after creating a test script:

#!/usr/bin/env pwsh

# Prerequisites
# -------------

	# ~~~pwsh
	# #Requires -PSEdition 'Core'
	# #Requires -Version 7
	# ~~~
	
	Set-Location -LiteralPath "$HOME"

# Example Directory
# -----------------

	# Creates an example directory.
	
	$DirectoryPath = 'TabPrefixed	ExampleDirectory.dir'
	$PrintPath = "$DirectoryPath"
	Write-Output (Show-Markdown -InputObject "The path is ``$PrintPath``.")
	
	New-Item `
		-Path "$HOME" `
		-Name "$DirectoryPath" `
		-ItemType 'directory'

# Example File
# ------------

	# Creates an example file.
	
	$FilePath = 'TabPrefixed	ExampleFile.txt'
	$PrintPath = "$FilePath"
	Write-Output (Show-Markdown -InputObject "The path is ``$PrintPath``.")
	
	New-Item `
		-Path ("$HOME" + [IO.Path]::DirectorySeparatorChar + "$DirectoryPath") `
		-Name "$FilePath" `
		-ItemType 'file' `
		-Value 'Example text.'

# Path Acquisition
# ----------------

	# Lists both.
	
	$FullFilePath = (`
	Get-ChildItem `
		-LiteralPath (
			"$HOME"          + [IO.Path]::DirectorySeparatorChar +
			"$DirectoryPath" + [IO.Path]::DirectorySeparatorChar +
			"$FilePath")
		)[0] | Select-Object -ExpandProperty 'FullName'

# File Content Acquisition
# ------------------------

	# This should fail.
	
	$PrintPath = "$FullFilePath"
	Write-Output (Show-Markdown -InputObject "The path is ``$PrintPath``.")
	
	Get-Content -LiteralPath "$FullFilePath"
	Write-Output $?

I realized that it could. Instead, I've narrowed it to a fault in how PowerShell, via Konsole and VSCode's terminals, interprets certain characters (like - tabs) as control codes.

Steps to reproduce

Try pasting your equivalent to the undermentioned command:

#!/usr/bin/env pwsh
Get-Content -LiteralPath '/home/RokeJulianLockhart/TabPrefixed    ExampleDirectory.dir/TabPrefixed        ExampleFile.txt'

...into the shell after successfully invoking it.

This value is able to be acquired by the undermentioned command:

#!/usr/bin/env pwsh
(Get-ChildItem -LiteralPath "$DirectoryPath")[0].FullName

Actual behavior

  1. The undermentioned is a command that has been pasted:

    #!/usr/bin/env pwsh
    Get-Content -LiteralPath "/home/RokeJulianLockhart/Documents/pyside6/prefix^Iexample.txt/example.txt"

    Note within it, the "prefix^Iexample". The original content does not contain that control code.

  2. Alternatively, sometimes, it appears to paste the path correctly, but doesn't recognise the value unless passed in a script:

    1. PS /home/RokeJulianLockhart> (Get-ChildItem -LiteralPath "$DirectoryPath")[0].FullName
      /home/RokeJulianLockhart/TabPrefixed    ExampleDirectory.dir/TabPrefixed        ExampleFile.txt
    2. PS /home/RokeJulianLockhart> Get-Content -LiteralPath '/home/RokeJulianLockhart/TabPrefixed    ExampleDirectory.dir/TabPrefixed        ExampleFile.txt'                                                                                                                                 
      Get-Content: Cannot find path '/home/RokeJulianLockhart/TabPrefixed    ExampleDirectory.dir/TabPrefixed        ExampleFile.txt' because it does not exist.
    3. PS /home/RokeJulianLockhart> Get-Content -LiteralPath "$FullFilePath"                                                                                  
      Example text.
    4. PS /home/RokeJulianLockhart> Write-Output "$FullFilePath"            
      /home/RokeJulianLockhart/TabPrefixed    ExampleDirectory.dir/TabPrefixed        ExampleFile.txt

Visuals

Screencast_20240710_125007.webm

Proposed technical implementation details (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions