Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

18 feature scripts contained identical 20-line blocks for determining the non-root user during installation. This creates maintenance burden and drift risk.

Changes

  • New helper: src/_common/common-setup.sh with determine_user_from_input() function
  • Migration: Replaced inline logic in 18 features with 4-line helper call
  • Tests: Added test/_common/test-common-setup.sh covering edge cases
  • Docs: Added src/_common/README.md with API and migration guide

Implementation

The helper consolidates user detection priority:

  1. _REMOTE_USER env var (if set and not root, with existence validation)
  2. Priority list: devcontainer → vscode → node → codespace → UID 1000
  3. Configurable fallback (typically root or vscode)
# Before (20+ lines per feature)
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
    USERNAME=""
    POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
    for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
        if id -u ${CURRENT_USER} > /dev/null 2>&1; then
            USERNAME=${CURRENT_USER}
            break
        fi
    done
    if [ "${USERNAME}" = "" ]; then
        USERNAME=root
    fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
    USERNAME=root
fi

# After (4 lines)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/../_common/common-setup.sh"
USERNAME=$(determine_user_from_input "${USERNAME}" "root")

Notable Details

  • common-utils special case: Preserved USERNAME="none"USER_UID=0/USER_GID=0 behavior by checking original input before transformation
  • _REMOTE_USER validation: Added existence check to prevent invalid usernames from propagating
  • Backward compatible: All existing behavior preserved; features updated: anaconda, common-utils, conda, desktop-lite, docker-in-docker, docker-outside-of-docker, go, hugo, java (+ wrapper.sh), kubectl-helm-minikube, node, oryx, php, python, ruby, rust, sshd

Net: -188 lines of duplicated logic, +87 lines shared implementation (excluding tests/docs).

Original prompt

This section details on the original issue you should resolve

<issue_title>Avoid duplicating user selection logic in features</issue_title>
<issue_description>Multiple scripts have similar / the same logic for determining which user should be selected for feature installation. We need to unify this to avoid duplication and annoying issues down the line.
https://github.com/devcontainers/features/pull/1530/changes#diff-26d9422c201004aa5880edd23a05e8e5cd69c42a2395391dd23b4ab2a62aa26bR246-R264</issue_description>

<agent_instructions>Please find all occurrences where this logic is used and unify it into a helper script. Ensure that all edge-cases are considered, and tests are written for the helper (not needed for the other scripts).</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits January 29, 2026 13:42
…ehensive tests

Co-authored-by: abdurriq <137001048+abdurriq@users.noreply.github.com>
…ection

Co-authored-by: abdurriq <137001048+abdurriq@users.noreply.github.com>
Co-authored-by: abdurriq <137001048+abdurriq@users.noreply.github.com>
…handling, remove dead code

Co-authored-by: abdurriq <137001048+abdurriq@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor user selection logic into a helper script Consolidate duplicated user selection logic into shared helper Jan 29, 2026
Copilot AI requested a review from abdurriq January 29, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid duplicating user selection logic in features

2 participants