Skip to content

"twc --install-completion" can break the Powershell profile #10

@lifestreamy

Description

@lifestreamy

Steps to reproduce:

  1. Run notepad $PROFILE in powershell
  2. Copy and paste
function Sample {
	Write-Output "Sample"
}

Set-Alias -Name example -Value Sample

at the end of file, without trailing whitespaces or line breaks. Save the file.
3. Run twc --install-completion in a new terminal.
4. The block gets directly appended to the existing configuration, breaking both involved parts

Expected behavior:
The completion block should be added on a new line, preserving the existing alias configuration.

Actual behavior:
The completion block is concatenated directly to -Value Sample, creating invalid syntax:

Set-Alias -Name example -Value SampleImport-Module PSReadLine
Set-PSReadLineKeyHandler -Chord Tab -Function MenuComplete
$scriptblock = {
...

and producing an error in a fresh terminal session:

Set-Alias : A positional parameter cannot be found that accepts argument 'PSReadLine'.
At A:\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:67 char:1
+ Set-Alias -Name example -Value SampleImport-Module PSReadLine
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Alias], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

The gist:
Best practices recommend ending files with a trailing newline, but real-world configuration files frequently don't follow this convention, and robust installation scripts must handle all cases.

And it did in fact break my existing powershell profile, which I had to fix manually afterwards.

Possible solutions:

  • Use the PowerShell's built-in Add-Content cmdlet to correctly append to the powershell profile
  • Alternatively, prefix the appended content with `r`n to guarantee separation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions