"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
completion: fix powershell completion corrupting non-ASCII names - fixes #9412
Yash Anil59c86b01bb39
diff --git a/cmd/genautocomplete/genautocomplete_powershell.go b/cmd/genautocomplete/genautocomplete_powershell.goindex 19b254b39..d42e4e55d 100644--- a/cmd/genautocomplete/genautocomplete_powershell.go+++ b/cmd/genautocomplete/genautocomplete_powershell.go@@ -1,8 +1,10 @@package genautocompleteimport (+ "bytes""fmt""os"+ "strings""github.com/rclone/rclone/cmd""github.com/rclone/rclone/fs"@@ -13,6 +15,34 @@ func init() {completionDefinition.AddCommand(powershellCommandDefinition)}+// powerShellInvokeLine is the line in the Cobra generated PowerShell completion+// script that captures rclone's output through a pipeline.+const powerShellInvokeLine = `Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null`++// powerShellUTF8Fix forces the captured output to be decoded as UTF-8. When+// PowerShell captures a child process' stdout through a pipeline it decodes the+// bytes using [Console]::OutputEncoding, which on non-UTF-8 systems (for+// example PowerShell 5.1 on a Windows install with an OEM code page such as+// CP852) corrupts the UTF-8 that rclone emits. Setting the encoding to UTF-8 is+// safe on PowerShell 7+, where it is already the default.+const powerShellUTF8Fix = `[Console]::OutputEncoding = [System.Text.Encoding]::UTF8`++// patchPowerShellCompletion injects the UTF-8 output encoding fix immediately+// before the Invoke-Expression call in the Cobra generated PowerShell+// completion script. If the expected line is not found (for example because the+// upstream Cobra template changed), the script is returned unmodified so we+// never emit a corrupted completion script.+func patchPowerShellCompletion(script string) string {+ idx := strings.Index(script, powerShellInvokeLine)+ if idx == -1 {+ return script+ }+ // Reuse the indentation of the Invoke-Expression line for the inserted line.+ lineStart := strings.LastIndex(script[:idx], "\n") + 1+ indent := script[lineStart:idx]+ return script[:lineStart] + indent + powerShellUTF8Fix + "\n" + script[lineStart:]+}+var powershellCommandDefinition = &cobra.Command{Use: "powershell [output_file]",Short: `Output powershell completion script for rclone.`,@@ -30,15 +60,18 @@ to your powershell profile.If output_file is "-" or missing, then the output will be written to stdout.`,Run: func(command *cobra.Command, args []string) {cmd.CheckArgs(0, 1, command, args)+ var buf bytes.Buffer+ if err := cmd.Root.GenPowerShellCompletion(&buf); err != nil {+ fs.Fatal(nil, fmt.Sprint(err))+ }+ script := patchPowerShellCompletion(buf.String())if len(args) == 0 || (len(args) > 0 && args[0] == "-") {- err := cmd.Root.GenPowerShellCompletion(os.Stdout)- if err != nil {+ if _, err := os.Stdout.WriteString(script); err != nil {fs.Fatal(nil, fmt.Sprint(err))}return}- err := cmd.Root.GenPowerShellCompletionFile(args[0])- if err != nil {+ if err := os.WriteFile(args[0], []byte(script), 0644); err != nil {fs.Fatal(nil, fmt.Sprint(err))}},diff --git a/cmd/genautocomplete/genautocomplete_test.go b/cmd/genautocomplete/genautocomplete_test.goindex fab32f3d1..55c6c9a66 100644--- a/cmd/genautocomplete/genautocomplete_test.go+++ b/cmd/genautocomplete/genautocomplete_test.go@@ -2,6 +2,7 @@ package genautocompleteimport ("os"+ "strings""testing""github.com/stretchr/testify/assert"@@ -107,3 +108,58 @@ func TestCompletionFishStdout(t *testing.T) {assert.NoError(t, err)assert.NotEmpty(t, string(output))}++func TestCompletionPowershell(t *testing.T) {+ tempFile, err := os.CreateTemp("", "completion_powershell")+ assert.NoError(t, err)+ defer func() {+ _ = tempFile.Close()+ _ = os.Remove(tempFile.Name())+ }()++ powershellCommandDefinition.Run(powershellCommandDefinition, []string{tempFile.Name()})++ bs, err := os.ReadFile(tempFile.Name())+ assert.NoError(t, err)+ assert.NotEmpty(t, string(bs))+ // The generated script must force UTF-8 output decoding so that non-ASCII+ // remote names are not corrupted on non-UTF-8 PowerShell hosts.+ assert.Contains(t, string(bs), powerShellUTF8Fix)+}++func TestCompletionPowershellStdout(t *testing.T) {+ originalStdout := os.Stdout+ tempFile, err := os.CreateTemp("", "completion_powershell")+ assert.NoError(t, err)+ defer func() {+ _ = tempFile.Close()+ _ = os.Remove(tempFile.Name())+ }()++ os.Stdout = tempFile+ defer func() { os.Stdout = originalStdout }()++ powershellCommandDefinition.Run(powershellCommandDefinition, []string{"-"})++ output, err := os.ReadFile(tempFile.Name())+ assert.NoError(t, err)+ assert.NotEmpty(t, string(output))+ assert.Contains(t, string(output), powerShellUTF8Fix)+}++func TestPatchPowerShellCompletion(t *testing.T) {+ t.Run("injects the encoding fix before the invoke line", func(t *testing.T) {+ script := "before\n " + powerShellInvokeLine + "\nafter\n"+ got := patchPowerShellCompletion(script)+ // The fix is inserted on its own line, sharing the indentation of the+ // invoke line, immediately before it.+ want := "before\n " + powerShellUTF8Fix + "\n " + powerShellInvokeLine + "\nafter\n"+ assert.Equal(t, want, got)+ assert.Less(t, strings.Index(got, powerShellUTF8Fix), strings.Index(got, powerShellInvokeLine))+ })++ t.Run("leaves the script unchanged when the invoke line is absent", func(t *testing.T) {+ script := "some other script\nwithout the expected line\n"+ assert.Equal(t, script, patchPowerShellCompletion(script))+ })+}