dotnet / command-line-api

Command line parsing, invocation, and rendering of terminal output.
https://github.com/dotnet/command-line-api/wiki
MIT License
3.35k stars 375 forks source link

Issues with dotnet-suggest #2451

Open daniel-mizsak opened 2 weeks ago

daniel-mizsak commented 2 weeks ago

I was trying to follow this tutorial to see if I was able to create a CLI tool with tab completion, but I had a few issues along the way.

1. dotnet-suggest might not be compatible with dotnet8

I am using Ubuntu 22.04 with Docker version 27.0.3, build 7d4bcd8. Downloaded dotnet8 docker image from here. Created the container and made sure dotnet8 was installed:

root@24c5f81164ca:/# dotnet --info
.NET SDK:
 Version:           8.0.302
 Commit:            ef14e02af8
 Workload version:  8.0.300-manifests.f6879a9a
 MSBuild version:   17.10.4+10fbfbf2e

Runtime Environment:
 OS Name:     debian
 OS Version:  12
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/8.0.302/

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.6
  Architecture: x64
  Commit:       3b8b000a0e

.NET SDKs installed:
  8.0.302 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Installed the latest dotnet-suggest, which is including dotnet7, but should be compatible with dotnet8. However, running dotnet-suggest results in:

root@24c5f81164ca:/# dotnet-suggest
You must install or update .NET to run this application.

App: /root/.dotnet/tools/dotnet-suggest
Architecture: x64
Framework: 'Microsoft.NETCore.App', version '7.0.0' (x64)
.NET location: /usr/share/dotnet

The following frameworks were found:
  8.0.6 at [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Learn more:
https://aka.ms/dotnet/app-launch-failed

To install missing framework, download:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=7.0.0&arch=x64&rid=linux-x64&os=debian.12

It might be just me having insufficient understanding on how nuget compatibility works.

2. dotnet-suggest-shim.bash wrong line ending

I was doing the same as before, only with dotnet6.

root@f0e2498d9c85:/# dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.423
 Commit:    c5c9e53229

Runtime Environment:
 OS Name:     debian
 OS Version:  11
 OS Platform: Linux
 RID:         debian.11-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.423/

global.json file:
  Not found

Host:
  Version:      6.0.31
  Architecture: x64
  Commit:       e2ca2f8a1c

.NET SDKs installed:
  6.0.423 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.31 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.31 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

Installed the second most recent dotnet-suggest as it is compatible with dotnet6.

The package installs fine, but when I add script to bashrc, it looks like that the line endings are not following the unix standard:

root@f0e2498d9c85:/# dotnet-suggest script bash >~/.dotnet-suggest-shim.bash
root@f0e2498d9c85:/# echo '. ~/.dotnet-suggest-shim.bash' >>~/.bashrc
root@f0e2498d9c85:/# source ~/.bashrc
bash: /root/.dotnet-suggest-shim.bash: line 2: syntax error near unexpected token `$'\r''
'ash: /root/.dotnet-suggest-shim.bash: line 2: `_dotnet_bash_complete()

Just copying the content of the file into ~/.dotnet-suggest-shim.bash' seems to fix the issue.

3. No suggestion for app created with System.CommandLine

I recreated the example solution shown here.

root@f0e2498d9c85:/# mkdir /scl
root@f0e2498d9c85:/# cd !$
cd /scl
root@f0e2498d9c85:/scl# dotnet new console --framework net6.0
The template "Console App" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /scl/scl.csproj...
  Determining projects to restore...
  Restored /scl/scl.csproj (in 111 ms).
Restore succeeded.

root@f0e2498d9c85:/scl# dotnet add package System.CommandLine --prerelease
  Determining projects to restore...
  Writing /tmp/tmpTBPQcd.tmp
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/share/dotnet/sdk/6.0.423/trustedroots/codesignctl.pem'.
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/share/dotnet/sdk/6.0.423/trustedroots/timestampctl.pem'.
info : Adding PackageReference for package 'System.CommandLine' into project '/scl/scl.csproj'.
info :   GET https://api.nuget.org/v3/registration5-gz-semver2/system.commandline/index.json
info :   OK https://api.nuget.org/v3/registration5-gz-semver2/system.commandline/index.json 283ms
info : Restoring packages for /scl/scl.csproj...
info :   GET https://api.nuget.org/v3-flatcontainer/system.commandline/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/system.commandline/index.json 142ms
info :   GET https://api.nuget.org/v3-flatcontainer/system.commandline/2.0.0-beta4.22272.1/system.commandline.2.0.0-beta4.22272.1.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/system.commandline/2.0.0-beta4.22272.1/system.commandline.2.0.0-beta4.22272.1.nupkg 8ms
info : Installed System.CommandLine 2.0.0-beta4.22272.1 from https://api.nuget.org/v3/index.json with content hash 1uqED/q2H0kKoLJ4+hI2iPSBSEdTuhfCYADeJrAqERmiGQ2NNacYKRNEQ+gFbU4glgVyK8rxI+ZOe1onEtr/Pg==.
info : Package 'System.CommandLine' is compatible with all the specified frameworks in project '/scl/scl.csproj'.
info : PackageReference for package 'System.CommandLine' version '2.0.0-beta4.22272.1' added to file '/scl/scl.csproj'.
info : Writing assets file to disk. Path: /scl/obj/project.assets.json
log  : Restored /scl/scl.csproj (in 367 ms).
root@f0e2498d9c85:/scl# vi Program.cs
root@f0e2498d9c85:/scl# dotnet publish -o publish
MSBuild version 17.3.4+a400405ba for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  scl -> /scl/bin/Debug/net6.0/scl.dll
  scl -> /scl/publish/
root@f0e2498d9c85:/scl# cd ./publish
root@f0e2498d9c85:/scl/publish# export PATH=$PATH:$(pwd)
root@f0e2498d9c85:/scl/publish# scl --file scl.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    }
  }
}

I tried to get inspired from this, this and this issue, but could not make it work.

root@f0e2498d9c85:/scl/publish# scl [suggest]
--file
--help
--version
-?
-h
/?
/h
root@f0e2498d9c85:/scl/publish# dotnet-suggest get --executable scl
--file
--help
--version
-?
-h
/?
root@f0e2498d9c85:/scl/publish# dotnet-suggest list
dotnet-suggest
dotnet suggest
scl

It looks like autocomplete does work with dotnet-suggest itself:

root@f0e2498d9c85:/scl/publish# dotnet-suggest
--help     --version  -\?        -h         /\?        /h         get        list       register   script

But not with scl:

root@f0e2498d9c85:/scl/publish# scl
System.CommandLine.dll  es/                     ja/                     pt-BR/                  scl.deps.json           scl.runtimeconfig.json  zh-Hant/
cs/                     fr/                     ko/                     ru/                     scl.dll                 tr/
de/                     it/                     pl/                     scl                     scl.pdb                 zh-Hans/
root@f0e2498d9c85:/scl/publish# scl [suggest] --f

Also does not return anything.

I did not do any in-depth debugging with the method descripted here, but I can give it a try if needed. I appreciate any help! :)

KalleOlaviNiemitalo commented 2 weeks ago

In .NET SDK 8, dotnet tool install supports the --allow-roll-forward option, which would let you run dotnet-suggest on the .NET 8.0 runtime.

daniel-mizsak commented 2 weeks ago

Thank you for your comment @KalleOlaviNiemitalo.

I was able to make it work the following way:

But does this also mean that it is not possible to make dotnet-suggest work with dotnet6?

KalleOlaviNiemitalo commented 2 weeks ago

dotnet-suggest 1.1.327201 targets .NET 6.0; you could still install that version if you don't want to use .NET 8.0. That version was built from commit 209b724a3c843253d3071e8348c353b297b0b8b5, which has tag 2.0.0-beta4.22272.1 in this repository.

daniel-mizsak commented 2 weeks ago

I tried running the same steps as I was using for dotnet8 with dotnet6, but I believe that since I am not able to use the latest dotnet-suggest version (as it is not compatible with dotnet6) there is something broken with the older version.

It behaves the same as I describen in the original issue.