Skip to content

feat: allow customizing workspace directory #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: test
test:
devcontainer features test
10 changes: 8 additions & 2 deletions src/code-server/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

CODE_SERVER_INSTALL_ARGS=""

if [ -n "$VERSION" ]; then
if [[ -n $VERSION ]]; then
CODE_SERVER_INSTALL_ARGS="$CODE_SERVER_INSTALL_ARGS --version=\"$VERSION\""
fi

Expand All @@ -17,12 +17,18 @@ do
code-server --install-extension "$extension"
done

CODE_SERVER_WORKSPACE="$_REMOTE_USER_HOME"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$_REMOTE_USER_HOME should always be set


if [[ -n $WORKSPACE ]]; then
CODE_SERVER_WORKSPACE="$WORKSPACE"
fi

cat > /usr/local/bin/code-server-entrypoint \
<< EOF
#!/usr/bin/env bash
set -e

su $_REMOTE_USER -c 'code-server --bind-addr "$HOST:$PORT" \$ARGS'
su $_REMOTE_USER -c 'code-server --bind-addr "$HOST:$PORT" $ARGS "$CODE_SERVER_WORKSPACE"'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving from \$ARGS to $ARGS because escaping here meant that the script written to file would have the literal $ARGS in it instead of the expanded result, which was not intended.

Copy link
Member

@mafredri mafredri Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does ARGS come from and what does it contain? I wonder if we need to worry about quoting and/or newlines?

Right now a ', " or \n would/could break the su command, example:

root@5110bc305062:/# su root -c 'echo hi
>
> there'
hi
bash: -c: line 2: syntax error near unexpected token `newline'
bash: -c: line 2: `>'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARGS comes from the feature. A user could supply additional arguments here that are not covered by the feature. It could contain anything but should be in the form of additional command line arguments.

Maybe we need to escape this? (or just drop it altogether)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, maybe we could use eval for simplicity?

> ARGS="--foo \"some thing\" --'bar'"
> eval "declare -a args=($ARGS)"
> declare -p args
declare -a args=([0]="--foo" [1]="some thing" [2]="--bar")

Used like this:

code-server "${args[@]}"

You can still do something sneaky, like:

ARGS="$(rm -rf /)"

But I don't know if we need care? Someone could just as well include a malicious feature? 🤷🏻‍♂️

The alternative is that we encode supported code-server flags/opts as explicit feature options, which lets us drop ARGS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL of eval 😄

I think the best course of action would be to ensure we support every CLI flag with a feature and drop ARGS

EOF

chmod +x /usr/local/bin/code-server-entrypoint
15 changes: 15 additions & 0 deletions test/code-server/code-server-workspace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

# Optional: Import test library bundled with the devcontainer CLI
source dev-container-features-test-lib

# Feature-specific tests
check "code-server version" code-server --version
check "code-server running" pgrep -f 'code-server/lib/node.*/code-server'
check "code-server listening" lsof -i "@127.0.0.1:8080"

check "code-server workspace" grep $'\'code-server.*"/home"\'' < /usr/local/bin/code-server-entrypoint

# Report results
reportResults
8 changes: 8 additions & 0 deletions test/code-server/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@
"version": "4.98.0"
}
}
},
"code-server-workspace": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"code-server": {
"workspace": "/home"
}
}
}
}