Skip to content

Commit 372e2d2

Browse files
authored
dotnet: set up tab completions for SDK 10+ (#1596)
* 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 * Add tab completion assertions * Sync dotnet docs options with feature metadata
1 parent e04e9ce commit 372e2d2

6 files changed

Lines changed: 86 additions & 6 deletions

File tree

src/dotnet/NOTES.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Installing only the .NET Runtime or the ASP.NET Core Runtime. (The SDK includes
5757
"ghcr.io/devcontainers/features/dotnet:2": {
5858
"version": "none",
5959
"dotnetRuntimeVersions": "latest, lts",
60-
"aspnetCoreRuntimeVersions": "latest, lts",
60+
"aspNetCoreRuntimeVersions": "latest, lts",
6161
}
6262
}
6363
```
@@ -80,7 +80,7 @@ Installing prerelease builds. Supports `preview` and `daily` suffixes.
8080
"version": "10.0-preview",
8181
"additionalVersions": "10.0.1xx-daily",
8282
"dotnetRuntimeVersions": "10.0-daily",
83-
"aspnetCoreRuntimeVersions": "10.0-daily"
83+
"aspNetCoreRuntimeVersions": "10.0-daily"
8484
}
8585
}
8686
```
@@ -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/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This Feature installs the latest .NET SDK, which includes the .NET CLI and the s
2020
| dotnetRuntimeVersions | Enter additional .NET runtime versions, separated by commas. Use 'latest' for the latest version, 'lts' for the latest LTS version, 'X.Y' or 'X.Y.Z' for a specific version, 'X.Y-preview' or 'X.Y-daily' for prereleases. | string | - |
2121
| aspNetCoreRuntimeVersions | Enter additional ASP.NET Core runtime versions, separated by commas. Use 'latest' for the latest version, 'lts' for the latest LTS version, 'X.Y' or 'X.Y.Z' for a specific version, 'X.Y-preview' or 'X.Y-daily' for prereleases. | string | - |
2222
| workloads | Enter additional .NET SDK workloads, separated by commas. Use 'dotnet workload search' to learn what workloads are available to install. | string | - |
23+
| tabCompletions | Install shell tab completions for the dotnet CLI. Requires SDK 10 or newer. | boolean | true |
2324

2425
## Customizations
2526

@@ -85,7 +86,7 @@ Installing only the .NET Runtime or the ASP.NET Core Runtime. (The SDK includes
8586
"ghcr.io/devcontainers/features/dotnet:2": {
8687
"version": "none",
8788
"dotnetRuntimeVersions": "latest, lts",
88-
"aspnetCoreRuntimeVersions": "latest, lts",
89+
"aspNetCoreRuntimeVersions": "latest, lts",
8990
}
9091
}
9192
```

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

test/dotnet/test.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ test -L /usr/bin/dotnet -a "$(readlink -f /usr/bin/dotnet)" = "$DOTNET_ROOT/dotn
2121

2222
expected=$(fetch_latest_version)
2323

24-
check "Latest .NET SDK version installed" \
24+
check "Latest .NET SDK version $expected installed" \
2525
is_dotnet_sdk_version_installed "$expected"
2626

27+
check "Bash completion script installed" \
28+
test -s /usr/share/bash-completion/completions/dotnet
29+
30+
check "Zsh completion script installed" \
31+
test -s /usr/share/zsh/site-functions/_dotnet
32+
33+
check "Fish completion script installed" \
34+
test -s /usr/share/fish/vendor_completions.d/dotnet.fish
35+
2736
# Report results
2837
# If any of the checks above exited with a non-zero exit code, the test will fail.
2938
reportResults

0 commit comments

Comments
 (0)