dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.75k stars 1.07k forks source link

`dotnet run` cannot run the project due to `System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. (Parameter 'startIndex')` exception #23148

Open timelessnesses opened 2 years ago

timelessnesses commented 2 years ago

Describe the bug

dotnet run is unable to run the program due to error but dotnet build works fine.

To Reproduce

  1. Use dotnet 6.0
  2. Create a new dotnet project by do dotnet new console
  3. Write some random program (Here's mine)
    // See https://aka.ms/new-console-template for more information
    Console.WriteLine("Hello, World!");
  4. Run it by do dotnet run

Exceptions (if any)

System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. (Parameter 'startIndex')
   at System.String.Substring(Int32 startIndex, Int32 length)
   at System.String.Substring(Int32 startIndex)
   at Microsoft.DotNet.Tools.Run.RunCommand.TryGetTargetArchitecture(String runtimeIdentifier, Nullable`1& targetArchitecture)
   at Microsoft.DotNet.Tools.Run.RunCommand.GetTargetCommand()
   at Microsoft.DotNet.Tools.Run.RunCommand.Execute()
   at Microsoft.DotNet.Tools.Run.RunCommand.Run(String[] args)
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)

Further technical details

Runtime Environment: OS Name: Windows OS Version: 10.0.19044 OS Platform: Windows RID: win10-x64 Base Path: c:\program files\dotnet\sdk\6.0.100\

Host (useful for support): Version: 6.0.0 Commit: 4822e3c3aa

.NET SDKs installed: 5.0.403 [c:\program files\dotnet\sdk] 5.0.404 [c:\program files\dotnet\sdk] 6.0.100 [c:\program files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 3.1.21 [c:\program files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.22 [c:\program files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.12 [c:\program files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.13 [c:\program files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [c:\program files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.21 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.22 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.12 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.13 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.21 [c:\program files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.22 [c:\program files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.12 [c:\program files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.13 [c:\program files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [c:\program files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

- The IDE (VS / VS Code/ VS4Mac) you're running on, and its version

F:\Desktop\test>code -v 1.63.2 899d46d82c4c95423fb7e10e68eba52050e30ba3 x64

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

sakesun commented 2 years ago

I've got the same problem on Raspberry Pi 400.

pi@raspberrypi:~/projects/ttb-ndis $ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     raspbian
 OS Version:  10
 OS Platform: Linux
 RID:         linux-arm
 Base Path:   /opt/dotnet/sdk/6.0.101/

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

.NET SDKs installed:
  6.0.101 [/opt/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.1 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.1 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
pi@raspberrypi:~/projects/ttb-ndis $ 
KalleOlaviNiemitalo commented 2 years ago

This is a duplicate of https://github.com/dotnet/sdk/issues/23004, which seems to be triggered by a locale setting. On the Raspberry Pi, you might be able to work around the bug by changing the LANG or LC_COLLATE or LC_ALL environment variable.

ponlawat-w commented 2 years ago

Thanks @KalleOlaviNiemitalo, for Windows PowerShell I found the environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT being true solves the issue as current workaround.

> $env:DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='True'
> dotnet run
timelessnesses commented 2 years ago

Thanks @KalleOlaviNiemitalo, for Windows PowerShell I found the environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT being true solves the issue as current workaround.

> $env:DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='True'
> dotnet run

sometimes dotnet program won't even run

ponlawat-w commented 2 years ago

The environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT is not really working well for me, especially when my project contains string sorting because there won't be cultures data at all under this mode, according to this document: https://github.com/dotnet/runtime/blob/main/docs/design/features/globalization-invariant-mode.md

sippagon commented 2 years ago

I have the same problem as well. I solved it by changing Date format on windows to Christian era and Regional format to US I hope it will help you. refer https://docs.microsoft.com/en-us/dotnet/core/runtime-config/globalization

Codingjames commented 2 years ago

Thanks @KalleOlaviNiemitalo, for Windows PowerShell I found the environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT being true solves the issue as current workaround.

> $env:DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='True'
> dotnet run

sometimes dotnet program won't even run

Thank you so much.

grindarius commented 1 year ago

I have the same problem as well. I solved it by changing Date format on windows to Christian era and Regional format to US I hope it will help you. refer https://docs.microsoft.com/en-us/dotnet/core/runtime-config/globalization

Thank you, This helped me too

This happened on my friend's machine where he used to use Thailand as his regional format. The date on his machine used to be written in Thai, Upon changing the regional to English (United States), the problem's solved.

We're on SDK v7

baronfel commented 1 year ago

Folks - if you're having this issue can you please comment (or edit your existing comments) with

I think this was fixed in 7.0.200 by https://github.com/dotnet/sdk/pull/27566, so if you're using 7.0 SDKs make sure you're at least on that version.

KeNJiKunG commented 1 year ago

Thanks @KalleOlaviNiemitalo, for Windows PowerShell I found the environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT being true solves the issue as current workaround.

> $env:DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='True'
> dotnet run

Thank you so much.

My locale is Thai/Thai. SDK is 7.0.400. I had the issue while try to install Microsoft.dotnet-interactive.

dotnet tool install --global Microsoft.dotnet-interactive --version 1.0.446104
jhagerman99 commented 2 months ago

I have the same problem as well. I solved it by changing Date format on windows to Christian era and Regional format to US I hope it will help you. refer https://docs.microsoft.com/en-us/dotnet/core/runtime-config/globalization

you save my life, thank a lot