Debug command line arguments appearing out of nowhere after upgrade from .NET Framework to .NET 6
Asked Answered
A

1

6

I upgraded a project from .NET Framework 4.7.2 to .NET 6. When I ran the program in the Visual Studio debugger, an unexpected argument (call it foo) was passed to Program.Main.

Where did foo come from? The project properties' Debug page shows no arguments:

Application arguments (empty)

Nothing to see here, in other words. Then I added a bar argument:

Application arguments (with 'bar')

Program.Main now got passed an array of two strings, foo and bar. What's happening?

Augite answered 26/8, 2022 at 11:2 Comment(0)
A
12

The upgrade part turns out to be central here. Most likely, just switching to the modern .csproj format is enough to trigger this weirdness.

Visual Studio now keeps things like application arguments in Properties\launchSettings.json, e.g.:

{
  "profiles": {
    "MyProgram": {
      "commandName": "Project",
      "commandLineArgs": "bar"
    }
  }
}

Before the upgrade, that information lived in MyProject.csproj.user:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
    <StartArguments>foo</StartArguments>
  </PropertyGroup>
</Project>

Visual Studio apparently reads both files when starting debugging; it's not clear whether this is intentional or just a bug. (It would've made sense if the .NET Upgrade Assistant had simply moved that information, from old place to new.

Solution

I deleted the .csproj.user file, though removing the StartArguments tag will surely work too. Don't forget to restart Visual Studio!

Augite answered 26/8, 2022 at 11:2 Comment(1)
In 17.7 we've added a warning to the launch profile editor to highlight when a StartArguments value exists. github.com/dotnet/project-system/pull/9074. Hopefully that will help avoid this confusion.Venation

© 2022 - 2024 — McMap. All rights reserved.