Skip to content

Commit d337166

Browse files
committed
[rust] - Introducing new options to install components
1 parent a69dd5c commit d337166

9 files changed

Lines changed: 279 additions & 4 deletions

src/rust/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Installs Rust, common Rust utilities, and their required dependencies
1818
| version | Select or enter a version of Rust to install. | string | latest |
1919
| profile | Select a rustup install profile. | string | minimal |
2020
| targets | Optional comma separated list of additional Rust targets to install. | string | - |
21+
| customComponents | Optional a flag to allow installing rust components based on input. | boolean | false |
22+
| components | Optional comma separeated list of rust components to be installed based on input. | string | - |
2123

2224
## Customizations
2325

src/rust/devcontainer-feature.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "rust",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"name": "Rust",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/rust",
66
"description": "Installs Rust, common Rust utilities, and their required dependencies",
@@ -57,7 +57,23 @@
5757
"armv7-unknown-linux-gnueabihf",
5858
"x86_64-unknown-redox,x86_64-unknown-uefi"
5959
]
60-
}
60+
},
61+
"customComponents": {
62+
"type": "boolean",
63+
"default": false,
64+
"description": "Use custom components list instead of default components (rust-analyzer, rust-src, rustfmt, clippy)."
65+
},
66+
"components": {
67+
"type": "string",
68+
"default": "",
69+
"description": "Optional comma separated list of Rust components to install when customComponents is true.",
70+
"proposals": [
71+
"rust-analyzer,rust-src,rustfmt,clippy",
72+
"rust-analyzer,rust-src",
73+
"rustfmt,clippy,rust-docs",
74+
"llvm-tools-preview,rust-src,rustfmt"
75+
]
76+
}
6177
},
6278
"customizations": {
6379
"vscode": {

src/rust/install.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
RUST_VERSION="${VERSION:-"latest"}"
1111
RUSTUP_PROFILE="${PROFILE:-"minimal"}"
1212
RUSTUP_TARGETS="${TARGETS:-""}"
13+
CUSTOM_COMPONENTS="${CUSTOMCOMPONENTS:-"false"}"
14+
RUST_COMPONENTS="${COMPONENTS:-""}"
1315

1416
export CARGO_HOME="${CARGO_HOME:-"/usr/local/cargo"}"
1517
export RUSTUP_HOME="${RUSTUP_HOME:-"/usr/local/rustup"}"
@@ -394,8 +396,25 @@ if [ "${UPDATE_RUST}" = "true" ]; then
394396
echo "Updating Rust..."
395397
rustup update 2>&1
396398
fi
397-
echo "Installing common Rust dependencies..."
398-
rustup component add rust-analyzer rust-src rustfmt clippy 2>&1
399+
# Install Rust components based on flag
400+
if [ "${CUSTOM_COMPONENTS}" = "true" ] && [ -n "${RUST_COMPONENTS}" ]; then
401+
echo "Installing custom Rust components..."
402+
IFS=',' read -ra components <<< "${RUST_COMPONENTS}"
403+
for component in "${components[@]}"; do
404+
# Trim whitespace
405+
component=$(echo "${component}" | xargs)
406+
if [ -n "${component}" ]; then
407+
echo "Installing Rust component: ${component}"
408+
if ! rustup component add "${component}" 2>&1; then
409+
echo "Warning: Failed to install component '${component}'. It may not be available for this toolchain." >&2
410+
exit 1
411+
fi
412+
fi
413+
done
414+
else
415+
echo "Installing common Rust dependencies..."
416+
rustup component add rust-analyzer rust-src rustfmt clippy 2>&1
417+
fi
399418

400419
if [ -n "${RUSTUP_TARGETS}" ]; then
401420
IFS=',' read -ra targets <<< "${RUSTUP_TARGETS}"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Helper function to check component is installed
9+
check_component_installed() {
10+
local component=$1
11+
if rustup component list | grep -q "${component}.*installed"; then
12+
return 0 # Component is installed (success)
13+
else
14+
return 1 # Component is not installed (failure)
15+
fi
16+
}
17+
18+
# Helper function to check component is NOT installed
19+
check_component_not_installed() {
20+
local component=$1
21+
if rustup component list | grep -q "${component}.*installed"; then
22+
return 1 # Component is installed (failure)
23+
else
24+
return 0 # Component is not installed (success)
25+
fi
26+
}
27+
28+
# Definition specific tests
29+
check "cargo version" cargo --version
30+
check "rustc version" rustc --version
31+
32+
# Check that specified custom components are installed
33+
check "rust-analyzer is installed" check_component_installed "rust-analyzer"
34+
check "rust-src is installed" check_component_installed "rust-src"
35+
check "rustfmt is installed" check_component_installed "rustfmt"
36+
37+
# Check that clippy NOT installed
38+
check "clippy not installed" check_component_not_installed "clippy"
39+
40+
# Report result
41+
reportResults
42+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Helper function to check component is installed
9+
check_component_installed() {
10+
local component=$1
11+
if rustup component list | grep -q "${component}.*installed"; then
12+
return 0 # Component is installed (success)
13+
else
14+
return 1 # Component is not installed (failure)
15+
fi
16+
}
17+
18+
# Definition specific tests
19+
check "cargo version" cargo --version
20+
check "rustc version" rustc --version
21+
22+
# Check that default components are installed
23+
check "rust-analyzer is installed" check_component_installed "rust-analyzer"
24+
check "rust-src is installed" check_component_installed "rust-src"
25+
check "rustfmt is installed" check_component_installed "rustfmt"
26+
check "clippy is installed" check_component_installed "clippy"
27+
28+
# Report result
29+
reportResults
30+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Helper function to check component is installed
9+
check_component_installed() {
10+
local component=$1
11+
if rustup component list | grep -q "${component}.*installed"; then
12+
return 0 # Component is installed (success)
13+
else
14+
return 1 # Component is not installed (failure)
15+
fi
16+
}
17+
18+
# Helper function to check component is NOT installed
19+
check_component_not_installed() {
20+
local component=$1
21+
if rustup component list | grep -q "${component}.*installed"; then
22+
return 1 # Component is installed (failure)
23+
else
24+
return 0 # Component is not installed (success)
25+
fi
26+
}
27+
28+
# Definition specific tests
29+
check "cargo version" cargo --version
30+
check "rustc version" rustc --version
31+
32+
# Check that no additional components are installed when empty list is provided
33+
# Only the basic rust toolchain should be available
34+
check "basic rust toolchain" rustc --version
35+
36+
# Verify that default components are automatically installed
37+
check "rust-analyzer is installed" check_component_installed "rust-analyzer"
38+
check "rust-src is installed" check_component_installed "rust-src"
39+
check "rustfmt is installed" check_component_installed "rustfmt"
40+
check "clippy is installed" check_component_installed "clippy"
41+
42+
# Report result
43+
reportResults
44+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Helper function to check component is installed
9+
check_component_installed() {
10+
local component=$1
11+
if rustup component list | grep -q "${component}.*installed"; then
12+
return 0 # Component is installed (success)
13+
else
14+
return 1 # Component is not installed (failure)
15+
fi
16+
}
17+
18+
# Definition specific tests
19+
check "cargo version" cargo --version
20+
check "rustc version" rustc --version
21+
22+
# Check that all specified extended components are installed
23+
check "rust-analyzer is installed" check_component_installed "rust-analyzer"
24+
check "rust-src is installed" check_component_installed "rust-src"
25+
check "rustfmt is installed" check_component_installed "rustfmt"
26+
check "clippy is installed" check_component_installed "clippy"
27+
check "rust-docs is installed" check_component_installed "rust-docs"
28+
29+
# Report result
30+
reportResults
31+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Helper function to check component is installed
9+
check_component_installed() {
10+
local component=$1
11+
if rustup component list | grep -q "${component}.*installed"; then
12+
return 0 # Component is installed (success)
13+
else
14+
return 1 # Component is not installed (failure)
15+
fi
16+
}
17+
18+
# Helper function to check component is NOT installed
19+
check_component_not_installed() {
20+
local component=$1
21+
if rustup component list | grep -q "${component}.*installed"; then
22+
return 1 # Component is installed (failure)
23+
else
24+
return 0 # Component is not installed (success)
25+
fi
26+
}
27+
28+
# Definition specific tests
29+
check "cargo version" cargo --version
30+
check "rustc version" rustc --version
31+
32+
# Check that only specified minimal components are installed
33+
check "rust-analyzer is installed" check_component_installed "rust-analyzer"
34+
check "rust-src is installed" check_component_installed "rust-src"
35+
36+
# Check that other default components are NOT installed
37+
check "rustfmt not installed" check_component_not_installed "rustfmt"
38+
check "clippy not installed" check_component_not_installed "clippy"
39+
40+
# Report result
41+
reportResults
42+

test/rust/scenarios.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,55 @@
1616
}
1717
}
1818
},
19+
"rust_with_default_components": {
20+
"image": "ubuntu:noble",
21+
"features": {
22+
"rust": {
23+
"version": "latest",
24+
"customComponents": false
25+
}
26+
}
27+
},
28+
"rust_with_custom_components": {
29+
"image": "ubuntu:noble",
30+
"features": {
31+
"rust": {
32+
"version": "latest",
33+
"customComponents": true,
34+
"components": "rust-analyzer,rust-src,rustfmt"
35+
}
36+
}
37+
},
38+
"rust_with_minimal_components": {
39+
"image": "ubuntu:noble",
40+
"features": {
41+
"rust": {
42+
"version": "latest",
43+
"customComponents": true,
44+
"components": "rust-analyzer,rust-src"
45+
}
46+
}
47+
},
48+
"rust_with_extended_components": {
49+
"image": "ubuntu:noble",
50+
"features": {
51+
"rust": {
52+
"version": "latest",
53+
"customComponents": true,
54+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
55+
}
56+
}
57+
},
58+
"rust_with_empty_components": {
59+
"image": "ubuntu:noble",
60+
"features": {
61+
"rust": {
62+
"version": "latest",
63+
"customComponents": true,
64+
"components": ""
65+
}
66+
}
67+
},
1968
"rust_with_centos": {
2069
"image": "centos:centos7",
2170
"features": {

0 commit comments

Comments
 (0)