Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cmd/mo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//
// Package managers that can only expose an executable (winget portable
// installs) put this on PATH as `mo`. It passes every argument through as a
// literal PowerShell string, the same call shape as the mo.cmd launcher that
// install.ps1 writes, and returns the script's exit code.
// literal PowerShell string and returns the script's exit code.
package main

import (
Expand Down Expand Up @@ -110,6 +109,16 @@ func commandLine(script string, args []string) string {
return strings.Join(parts, " ") + "; exit $LASTEXITCODE"
}

// PowerShell also treats these Unicode characters as single-quote delimiters.
// Double each delimiter to preserve it inside a single-quoted literal.
var singleQuoteEscaper = strings.NewReplacer(
"'", "''",
"\u2018", "\u2018\u2018",
"\u2019", "\u2019\u2019",
"\u201a", "\u201a\u201a",
"\u201b", "\u201b\u201b",
)

func quote(value string) string {
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
return "'" + singleQuoteEscaper.Replace(value) + "'"
}
10 changes: 10 additions & 0 deletions cmd/mo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func TestCommandLineWithoutArgumentsOpensTheMenu(t *testing.T) {
}
}

func TestCommandLineQuotesSmartSingleQuotes(t *testing.T) {
got := commandLine("C:\\Users\\O\u2019Brien\\mole.ps1", []string{
"\u2018left", "right\u2019", "low\u201a", "reversed\u201b", "\u201cquoted\u201d\u201e",
})
want := "& 'C:\\Users\\O\u2019\u2019Brien\\mole.ps1' '\u2018\u2018left' 'right\u2019\u2019' 'low\u201a\u201a' 'reversed\u201b\u201b' '\u201cquoted\u201d\u201e'; exit $LASTEXITCODE"
if got != want {
t.Fatalf("commandLine:\n got %s\nwant %s", got, want)
}
}

func TestPowerShellCandidatesTryWindowsPowerShellFirst(t *testing.T) {
t.Setenv("SystemRoot", `C:\TestWindows`)
got := powerShellCandidates()
Expand Down
1 change: 1 addition & 0 deletions docs/incidents.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ record which rows it finds.
| I-25 | 2026-11-11 | Updating an existing source install through `quick-install.ps1` in Windows PowerShell 5.1 stopped with "Installation failed: From https://github.com/tw93/Mole", and reinstalling hit the same path because the source folder stays | 92b19729 | 8b43e8d8 | write-time | `Defines Invoke-GitCommand the same way in both scripts`, `Returns git's exit code instead of throwing on its stderr progress in <_>` |
| I-26 | 2026-11-12 | After `cmd/status` and `cmd/analyze` changed past V1.44.0-windows, a fresh `quick-install.ps1` on a PC without Go installed neither `status.exe` nor `analyze.exe`, and `mo status` said to check the network | 10334ee2 | ead9c74d | write-time | `Uses the verified release build for missing <_> when only upstream commits changed it`, `Offers the tags in HEAD history newest first, and none for local edits, a HEAD beside the tags or a Git override` |
| I-27 | 2026-11-12 | Once `cmd/` changed after a release, `mo update` on a source install without Go stopped with "Could not refresh ... Install Go and rerun this installer", and a fresh install after a version bump whose release was not out yet got no `status.exe` or `analyze.exe` | 59b3bcc4 | f9603ca4 | write-time | `Refreshes both tools from the release build without Go when the source is ahead of it`, `Tries the previous release when the newest one is not published yet` |
| I-28 | 2026-11-17 | `mo.exe` failed to start from an install path containing a typographic single quote, and the same quotes in arguments could change the values passed to the script (#2021) | unknown | 816fbeea | unit | `TestCommandLineQuotesSmartSingleQuotes`, `Preserves quote U+<Code> in the native script path and arguments` |
38 changes: 38 additions & 0 deletions tests/InstallLifecycle.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@ BeforeAll {
$script:Colors = @{ Red=''; Green=''; Yellow=''; Blue=''; Cyan=''; Gray=''; NC='' }
}

Describe 'Native launcher literal arguments' {
BeforeAll {
$script:NativeLauncher = Join-Path $TestDrive 'native-mo.exe'
Push-Location $script:RepoRoot
try {
& go build -o $script:NativeLauncher ./cmd/mo
if ($LASTEXITCODE -ne 0) { throw 'Could not build the native launcher' }
}
finally { Pop-Location }
}

It 'Preserves quote U+<Code> in the native script path and arguments' -ForEach @(
@{ Code = '0027'; Value = 0x0027 }
@{ Code = '2018'; Value = 0x2018 }
@{ Code = '2019'; Value = 0x2019 }
@{ Code = '201A'; Value = 0x201A }
@{ Code = '201B'; Value = 0x201B }
) {
$root = Join-Path $TestDrive ('O' + [char]$Value + 'Brien-' + $Code)
$null = New-Item -ItemType Directory -Path $root
$launcher = Join-Path $root 'mo.exe'
Copy-Item -LiteralPath $script:NativeLauncher -Destination $launcher
@'
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @($args) -Compress)))
exit 7
'@ | Set-Content -LiteralPath (Join-Path $root 'mole.ps1') -Encoding ASCII
$argument = 'literal ' + [char]$Value + ' $env:USERPROFILE ' + [char]0x201C + 'double' + [char]0x201D + [char]0x201E
$output = & $launcher '--version' $argument
$LASTEXITCODE | Should -Be 7
@($output).Count | Should -Be 1
$json = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String([string]$output))
$actual = ConvertFrom-Json $json
@($actual).Count | Should -Be 2
$actual[0] | Should -BeExactly '--version'
$actual[1] | Should -BeExactly $argument
}
}

Describe 'Source native tool refresh' {
BeforeEach {
$script:Root = Join-Path $TestDrive ([guid]::NewGuid().ToString('N'))
Expand Down