Skip to content

Commit dc43177

Browse files
committed
dotnet: set up tab completions for SDK 10+
Generate bash, zsh, and fish completion scripts using 'dotnet completions script' and place them in the standard system-wide completion directories: - /usr/share/bash-completion/completions/dotnet - /usr/share/zsh/site-functions/_dotnet - /usr/share/fish/vendor_completions.d/dotnet.fish Gated behind SDK 10+ via version check since the 'dotnet completions script' command is only available starting with .NET 10. Skipped for runtime-only installs. Reference: https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete
1 parent 0c2cd3f commit dc43177

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/dotnet/NOTES.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,21 @@ Installing prerelease builds. Supports `preview` and `daily` suffixes.
9090
This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed.
9191

9292
`bash` is required to execute the `install.sh` script.
93+
94+
## Tab completions
95+
96+
When using .NET SDK 10 or newer, tab completions for the `dotnet` CLI are automatically installed for bash, zsh, and fish. The completion scripts are placed in the standard system-wide directories so they work for all users:
97+
98+
- **Bash**: `/usr/share/bash-completion/completions/dotnet`
99+
- **Zsh**: `/usr/share/zsh/site-functions/_dotnet`
100+
- **Fish**: `/usr/share/fish/vendor_completions.d/dotnet.fish`
101+
102+
To disable this, set `tabCompletions` to `false`:
103+
104+
``` json
105+
"features": {
106+
"ghcr.io/devcontainers/features/dotnet:2": {
107+
"tabCompletions": false
108+
}
109+
}
110+
```

src/dotnet/devcontainer-feature.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "dotnet",
3-
"version": "2.4.2",
3+
"version": "2.5.0",
44
"name": "Dotnet CLI",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/dotnet",
66
"description": "This Feature installs the latest .NET SDK, which includes the .NET CLI and the shared runtime. Options are provided to choose a different version or additional versions.",
@@ -39,6 +39,11 @@
3939
"type": "string",
4040
"default": "",
4141
"description": "Enter additional .NET SDK workloads, separated by commas. Use 'dotnet workload search' to learn what workloads are available to install."
42+
},
43+
"tabCompletions": {
44+
"type": "boolean",
45+
"default": true,
46+
"description": "Install shell tab completions for the dotnet CLI. Requires SDK 10 or newer."
4247
}
4348
},
4449
"containerEnv": {
@@ -64,4 +69,4 @@
6469
"installsAfter": [
6570
"ghcr.io/devcontainers/features/common-utils"
6671
]
67-
}
72+
}

src/dotnet/install.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ADDITIONAL_VERSIONS="${ADDITIONALVERSIONS:-""}"
1111
DOTNET_RUNTIME_VERSIONS="${DOTNETRUNTIMEVERSIONS:-""}"
1212
ASPNETCORE_RUNTIME_VERSIONS="${ASPNETCORERUNTIMEVERSIONS:-""}"
1313
WORKLOADS="${WORKLOADS:-""}"
14+
TAB_COMPLETIONS="${TABCOMPLETIONS:-"true"}"
1415

1516
# Prevent "Welcome to .NET" message from dotnet
1617
export DOTNET_NOLOGO=true
@@ -146,6 +147,10 @@ if [ ! -e /usr/bin/dotnet ]; then
146147
ln --symbolic "$DOTNET_ROOT/dotnet" /usr/bin/dotnet
147148
fi
148149

150+
if [ "$TAB_COMPLETIONS" = "true" ]; then
151+
install_completions
152+
fi
153+
149154
# Add .NET Core SDK tools to PATH for bash and zsh users
150155
# This is where 'dotnet tool install --global <tool>' installs tools to
151156
# Use single-quoted EOF to defer $PATH expansion until sourcing the file

src/dotnet/scripts/dotnet-helpers.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,46 @@ parse_version_and_quality() {
184184
quality=""
185185
fi
186186
echo "$clean_version" "$quality"
187+
}
188+
189+
# Checks if the installed .NET SDK is at least the given major version.
190+
# Returns 0 (true) if the SDK major version >= the specified version, 1 otherwise.
191+
# Also returns 1 if no SDK is installed (e.g. runtime-only installs).
192+
# Usage: is_at_least_sdk_version <major_version>
193+
# Example: is_at_least_sdk_version 10
194+
is_at_least_sdk_version() {
195+
local required_major="$1"
196+
local dotnet_version
197+
dotnet_version=$("$DOTNET_ROOT/dotnet" --version 2>/dev/null || true)
198+
local major_version="${dotnet_version%%.*}"
199+
[[ "$major_version" =~ ^[0-9]+$ ]] && [ "$major_version" -ge "$required_major" ]
200+
}
201+
202+
# Sets up dotnet tab completions for bash, zsh, and fish.
203+
# The 'dotnet completions script' command is only available in .NET SDK 10+.
204+
# Older SDKs and runtime-only installs will naturally skip this since the
205+
# command won't be available.
206+
# Reference: https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete
207+
# Completion scripts are generated at install time and placed in the standard
208+
# system-wide completion directories, which are auto-discovered by
209+
# bash-completion, zsh, and fish without modifying any rc files.
210+
install_completions() {
211+
if ! is_at_least_sdk_version 10; then
212+
echo "Skipping dotnet tab completions (requires SDK 10+)."
213+
return
214+
fi
215+
216+
echo "Setting up dotnet tab completions..."
217+
218+
# Bash: drop into the standard bash-completion directory
219+
mkdir -p /usr/share/bash-completion/completions
220+
"$DOTNET_ROOT/dotnet" completions script bash > /usr/share/bash-completion/completions/dotnet
221+
222+
# Zsh: drop into the standard site-functions directory
223+
mkdir -p /usr/share/zsh/site-functions
224+
"$DOTNET_ROOT/dotnet" completions script zsh > /usr/share/zsh/site-functions/_dotnet
225+
226+
# Fish: drop into the standard vendor completions directory
227+
mkdir -p /usr/share/fish/vendor_completions.d
228+
"$DOTNET_ROOT/dotnet" completions script fish > /usr/share/fish/vendor_completions.d/dotnet.fish
187229
}

0 commit comments

Comments
 (0)