Description
GlobalToolsSuggestionRegistration returns suggestion registrations for all executables in ~/.dotnet/tools/, and dotnet-suggest can then run such executables with the [suggest] directive even if they have not been explicitly registered. However, supporting this directive is not a requirement for creating a .NET tool. For example, dotnet-runtimeinfo 1.0.4, dotnet-script 1.1.0, and microsoft.visualstudio.slngen.tool 8.5.0 do not support it. There is a risk that the tool does something unexpected when given '[suggest]' which it does not recognize as a directive. For example, it could create a file with that name, or spend time trying to connect to a computer with that name.
I think the best solution would be:
- Add something into the DotnetToolSettings.xml file to indicate that the command supports
[suggest]. If it becomes possible to have multiple commands in the same tool package, this information should be specific to each command. The GenerateToolsSettingsFile task in .NET SDK generates DotnetToolSettings.xml and could read some property that the System.CommandLine package would set by default. - Make
dotnet tool installcopy this information to some place from whichdotnet-suggestcan find it, regardless of whetherdotnet-suggesthas been installed. For example, place it in ~/.dotnet/tools-suggest.json, or copy the whole XML file to tools/.meta/command/DotnetToolSettings.xml. - Make
dotnet-suggestconsult the information from step 2 to ascertain whether the command supports[suggest].
If a tool supports [suggest] but is too old to include this information in its DotnetToolSettings.xml file, then the user can dotnet-suggest register it manually.
I also considered adding some AssemblyMetadataAttribute to the entry point assembly of the command, but that doesn't really seem any easier because the executables in ~/.dotnet/tools/ are shims that don't contain CLR metadata. Also it would not be so easily extensible to wholly unmanaged tools.
Activity
KalleOlaviNiemitalo commentedon Jun 22, 2022
Related #190 and #379.
baronfel commentedon Jun 22, 2022
I don't know how you do it, but the broad strokes of what you outline is directly in line with what @jonsequitur, @KathleenDollard, and I had been brainstorming for the 'next wave' of suggestions-related features. Kudos!
KalleOlaviNiemitalo commentedon Jun 22, 2022
There is a security aspect. Using an AssemblyMetadataAttribute or a custom PE section to indicate support for
[suggest]would be okay for dotnet tools because the user has taken action to install them and presumably trusts them. But it would not be okay for tab completion when the user has typed the path of some unregistered executable and presses TAB without intending to execute that file right away.KalleOlaviNiemitalo commentedon Jun 22, 2022
A new XML element in an assembly manifest as a Win32 resource might be another possibility. However, if it were possible to refer to a separate executable for retrieving the suggestions, then the relative path of that executable would have to be in the manifest, and this relative path would have to be kept valid when files are copied around. Having the suggestion-support information in DotnetToolSettings.xml seems easier because that file is easier to update at install time if paths change.
KalleOlaviNiemitalo commentedon Jul 27, 2022
The format of DotnetToolSettings.xml doesn't seem to be documented at https://docs.microsoft.com/. The .NET SDK repository has some documentation for it in Tool NuGet package format (not updated for dotnet/sdk#9179), and the code that validates it is in ToolConfigurationDeserializer.cs. In the NuGet wiki, Global Tools NuGet Implementation also links to some specifications, but those links lead to a repository that is private or has been deleted.
dotnet tool installwarns if the version number is higher than 1, but it ignores unrecognized attributes and elements. The information about supported directives could thus be added to theCommandelement without breaking compatibility with older versions of .NET SDK.Possible approaches for global tools:
Define a
Command/@SupportsSuggestDirectiveattribute with a Boolean value, and makedotnet tool install --globalcopy that value into ~/.dotnet/tools-suggest.json.DotnetToolSettings.xml:
~/.dotnet/tools-suggest.json:
{ "commands": { "slngen": { "supportsSuggestDirective": false } } }Disadvantage: You might later need define something like
Command/@SupportsLanguageServerDirectivefor Using System.CommandLine for powershell completers #1220 and makedotnet tool install --globalsupport that too.Define a
Command/Suggest/@SupportsSuggestDirectiveattribute with a Boolean value, and makedotnet tool install --globalcopy the wholeCommand/Suggestelement into ~/.dotnet/tools-suggest.xml. Future versions of System.CommandLine would then be able to define more attributes and child elements in that, without needing further changes in .NET SDK.DotnetToolSettings.xml:
~/.dotnet/tools-suggest.xml:
Disadvantage: Tool developers might be tempted to abuse the
Command/Suggestelement for custom metadata that is not related todotnet-suggest, so that their own applications could then read this metadata from ~/.dotnet/tools-suggest.xml.Define a
Command/Suggest/@SupportsSuggestDirectiveattribute with a Boolean value and makedotnet tool install --globalcopy the whole file to ~/.dotnet/tools/.meta/command/DotnetToolSettings.xml. Tool developers would be able to define their own elements for custom metadata and would not be tempted to abuse theCommand/Suggestelement.DotnetToolSettings.xml and ~/.dotnet/tools/.meta/slngen/DotnetToolSettings.xml:
Define a
Command/Suggest/@SupportsSuggestDirectiveattribute with a Boolean value and makedotnet tool install --globalcreate a ~/.dotnet/tools/.meta/command.json file that points to the path ofDotnetToolSettings.xml. This would allow attributes and properties of theCommand/Suggestelement to reference other files, e.g. an XML file that defines the command-line syntax so thatdotnet-suggestneed not run the target executable.DotnetToolSettings.xml: As above.
~/.dotnet/tools/.meta/slngen.json:
{ "toolSettingsPath": "/home/you/.nuget/packages/.dotnet/tools/.store/microsoft.visualstudio.slngen.tool/8.5.0/microsoft.visualstudio.slngen.tool/8.5.0/tools/net5.0/any/DotnetToolSettings.xml" }Similar features for local tools seem more difficult. AFAIK, to find the local tool that
dotnet tool runwill run, you first have to parse .config/dotnet-tools.json to get the package name and the requested version, and then parse ~/.dotnet/toolResolverCache/1/package or ~/.nuget/packages/. The toolResolverCache implementation in .NET SDK has no public API, and future .NET SDK versions might use a different directory layout or file format. NuGet has public APIs but I assume they would add latency (why else would there be a toolResolverCache). I think a maintainable solution would be to make .NET SDK implement adotnet tool get-settings-path commandcommand that outputs the path of DotnetToolSettings.xml, anddotnet-suggestwould then run that and parse the DotnetToolSettings.xml file. This would ensure that completions correspond to the tool thatdotnet tool runwould run, even if the tool resolution algorithm changes in a future version of .NET SDK. If this approach is chosen, then it will be simplest to makedotnet-suggestparse DotnetToolSettings.xml for global tools as well, rather than makedotnet tool install --globalcopy the information to some other file.iSazonov commentedon Oct 5, 2022
It is not security issue since the application is already accessible in the user session. This is rather an undesirable action. And meta information is the best thing that can solve this problem.
What about non dotnet tools? The solution must be for all custom applications.
This XML file could have been generated and distributed with custom application. Even better if it were inside as meta information.
Then even PowerShell could use that XML without running the application.
KathleenDollard commentedon Oct 5, 2022
I think it would be desirable to solve this in a general way of indicating that an executable supports specific features, rather than solving it specifically for .NET Tools, although .NET Tools is a key scenario.
KalleOlaviNiemitalo commentedon Oct 5, 2022
Perhaps so, if the directory of the executable file is in PATH. But if the file is in an untrusted directory, then it should not be executed to get completions, unless the user has registered it to allow this. Users won't expect such execution.
Imagine a user is typing a command line
cd /some/dir && ./some-appand then presses TAB. The shell function that runsdotnet-suggestis unlikely to understand that this command line would change the working directory before resolving./some-app. The shell function might then rundotnet-suggest ./some-appin the wrong directory. If that directory also contains asome-appexecutable and is not in PATH, thendotnet-suggestshould not execute the file.Alternatively, imagine a user is editing a shell script that is intended to run by a different user "samuel". This shell script contains a command
/home/samuel/bin/clip. If the editor invokes a language service provider to get completions for this command, and the /home/samuel/bin directory is not in PATH of the user who is editing, then the LSP should not execute /home/samuel/bin/clip to get completions.KalleOlaviNiemitalo commentedon Oct 5, 2022
Do you mean the XML would describe all the options and subcommands supported by the application? Rather than just state that the application supports the
[suggest]directive.If so, I think it would be a good feature but out of scope for this issue.
iSazonov commentedon Oct 5, 2022
Yes.
StefanGreve commentedon Sep 2, 2026
I have run into this issue today as well, are there any updates?
Cache every native completion script instead of regenerating it per l…
KalleOlaviNiemitalo commentedon Sep 30, 2026
Revisiting the limitations I mentioned in earlier comments.
It is still not possible;
ToolConfigurationExceptionis thrown if you try. https://github.com/dotnet/sdk/blob/15a545cb978bb0be6cad2f5d121d4452fba35dbd/src/Cli/dotnet/ToolPackage/ToolConfigurationDeserializer.cs#L76-L79The work towards RID-specific tools in dotnet/sdk#48575 added support for
/DotNetCliTool/Command/@Runner = "executable", which may now be usable for tools implemented in technologies other than .NET, even if it was not intended for that use.