Skip to content

install.ps1 crashes with Substring ArgumentOutOfRangeException on non-English Windows locale (Remove-IndexUrlCredentials) #7279

Description

@akkanee-chatanavin

Environment

  • OS: Windows 11
  • PowerShell: 7.6.3 (also reproduced identically on Windows PowerShell 5.1)
  • GPU: NVIDIA GeForce RTX 3060 (12GB)
  • Windows system locale/region: 1054 th-TH Thai (Thailand)
  • Install command: irm https://raw.githubusercontent.com/unslothai/unsloth/main/install.ps1 | iex
    (also reproduced with .\install.ps1 --local after git clone)

Bug

On a Windows machine with a non-English system locale/region (Thai, in my case),
install.ps1 crashes immediately after GPU detection, before the PyTorch install
step, with:

Exception calling "Substring" with "1" argument(s): "startIndex cannot be larger
than length of string. (Parameter 'startIndex')"
At install.ps1:2043 (or 2044 on latest main)

  • …$host_ = if ($at -ge 0) { $authority.Substring($at + 1) } else …

Root cause

Remove-IndexUrlCredentials() computes:

$sep = $Url.IndexOf('://')

without specifying an ordinal string comparison. On a non-English Windows
culture, [string]::IndexOf(string) uses culture-aware (linguistic) comparison
by default, which can return the wrong index for a punctuation-only substring
like "://" (punctuation/symbols are treated as low-weight/ignorable in some
non-English collations). This causes $sep to resolve incorrectly, which
corrupts $scheme / $rest / $authority downstream, and $authority ends up empty
while $at (LastIndexOf('@')) still returns a stale/incorrect index, causing
Substring($at + 1) to exceed the (now-empty) string length.

Debug output confirms this:

[DEBUG] Url=[https://download.pytorch.org/whl/cu130] authority=[] at=[0]
        scheme=[] rest=[ps://download.pytorch.org/whl/cu130]

Note "rest" is missing the leading "htt" — $Url.IndexOf('://') returned 0
instead of the correct value (5) for a plain https:// URL.

Fix

Use ordinal (not culture-aware) string operations in
Remove-IndexUrlCredentials, e.g.:

$sep = $Url.IndexOf('://', [System.StringComparison]::Ordinal)

and likewise for any other .IndexOf / .LastIndexOf calls on URLs in
install.ps1 that don't already specify StringComparison.Ordinal.

Workaround

Setting the current thread culture to Invariant before running the installer
avoids the crash:

[System.Threading.Thread]::CurrentThread.CurrentCulture =
    [System.Globalization.CultureInfo]::InvariantCulture
[System.Threading.Thread]::CurrentThread.CurrentUICulture =
    [System.Globalization.CultureInfo]::InvariantCulture

Steps to reproduce

  1. Set Windows system locale/region to Thai (or any non-English locale using
    linguistic/culture-aware string collation)
  2. Run: irm https://raw.githubusercontent.com/unslothai/unsloth/main/install.ps1 | iex
  3. Observe crash immediately after "gpu NVIDIA GPU detected", before PyTorch
    install begins

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