Skip to content

Commit 9087e0c

Browse files
authored
fix: invoking code-server in integrated terminal (#5360)
* Include bin scripts for all platforms These will get symlinked as part of the postinstall. These scripts provide everything ours does inside the integrated terminal plus more. * Improve OS detection Specifically for Windows although we do not yet support Windows. Also standardize the duplicate arch functions since they had drifted from each other bit. * Remove duplicate asar symlink Since standalone releases run the postinstall they will get the asar symlink there. That means the symlink will not exist for the npm package and we will not need to ignore it. The symlink portion is split out so it can be re-used for other symlinks (for example linking bin scripts). * Add symlinks to bin scripts * Add test for opening a file from the terminal * Add global Playwright timeout Otherwise it will exceed the Actions timeout and get rudely killed without any output. * Make sed work on macOS * Fix Node path in bin scripts * Disable shellcheck expansion error * Make scripts executable * Remove .bak files created by sed * Include Code build script in cache hash Otherwise if we change the script it will not rebuild Code. * Make sure the terminal opens The selector was timing out even though it matched more than one element but matching on the focused one appears to work. In addition add a loop so it can keep trying to open the terminal if something goes wrong with the focus.
1 parent 0022473 commit 9087e0c

File tree

9 files changed

+167
-93
lines changed

9 files changed

+167
-93
lines changed

.github/workflows/ci.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ jobs:
156156
uses: actions/cache@v3
157157
with:
158158
path: lib/vscode-reh-web-*
159-
key: vscode-reh-package-${{ secrets.VSCODE_CACHE_VERSION }}-${{ steps.vscode-rev.outputs.rev }}-${{ steps.version.outputs.version }}-${{ hashFiles('patches/*.diff') }}
159+
key: vscode-reh-package-${{ secrets.VSCODE_CACHE_VERSION }}-${{ steps.vscode-rev.outputs.rev }}-${{ steps.version.outputs.version }}-${{ hashFiles('patches/*.diff', 'ci/build/build-vscode.sh') }}
160160

161161
- name: Build vscode
162162
if: steps.cache-vscode.outputs.cache-hit != 'true'
@@ -499,7 +499,7 @@ jobs:
499499
./test/node_modules/.bin/playwright install
500500
501501
- name: Run end-to-end tests
502-
run: yarn test:e2e
502+
run: yarn test:e2e --global-timeout 840000
503503

504504
- name: Upload test artifacts
505505
if: always()

ci/build/build-release.sh

-4
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ bundle_vscode() {
110110
rsync "$VSCODE_SRC_PATH/extensions/package.json" "$VSCODE_OUT_PATH/extensions/package.json"
111111
rsync "$VSCODE_SRC_PATH/extensions/yarn.lock" "$VSCODE_OUT_PATH/extensions/yarn.lock"
112112
rsync "$VSCODE_SRC_PATH/extensions/postinstall.mjs" "$VSCODE_OUT_PATH/extensions/postinstall.mjs"
113-
114-
pushd "$VSCODE_OUT_PATH"
115-
symlink_asar
116-
popd
117113
}
118114

119115
main "$@"

ci/build/build-standalone-release.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ main() {
2727
ln -s "./bin/code-server" "$RELEASE_PATH/code-server"
2828
ln -s "./lib/node" "$RELEASE_PATH/node"
2929

30-
cd "$RELEASE_PATH"
30+
pushd "$RELEASE_PATH"
3131
yarn --production --frozen-lockfile
32+
popd
3233
}
3334

3435
main "$@"

ci/build/build-vscode.sh

+46-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,38 @@ set -euo pipefail
66
# MINIFY controls whether a minified version of vscode is built.
77
MINIFY=${MINIFY-true}
88

9+
delete-bin-script() {
10+
rm -f "lib/vscode-reh-web-linux-x64/bin/$1"
11+
}
12+
13+
copy-bin-script() {
14+
local script="$1"
15+
local dest="lib/vscode-reh-web-linux-x64/bin/$script"
16+
cp "lib/vscode/resources/server/bin/$script" "$dest"
17+
sed -i.bak "s/@@VERSION@@/$(vscode_version)/g" "$dest"
18+
sed -i.bak "s/@@COMMIT@@/$VSCODE_DISTRO_COMMIT/g" "$dest"
19+
sed -i.bak "s/@@APPNAME@@/code-server/g" "$dest"
20+
21+
# Fix Node path on Darwin and Linux.
22+
# We do not want expansion here; this text should make it to the file as-is.
23+
# shellcheck disable=SC2016
24+
sed -i.bak 's/^ROOT=\(.*\)$/VSROOT=\1\nROOT="$(dirname "$(dirname "$VSROOT")")"/g' "$dest"
25+
sed -i.bak 's/ROOT\/out/VSROOT\/out/g' "$dest"
26+
27+
# Fix Node path on Windows.
28+
sed -i.bak 's/^set ROOT_DIR=\(.*\)$/set ROOT_DIR=%~dp0..\\..\\..\\..\r\nset VSROOT_DIR=\1/g' "$dest"
29+
sed -i.bak 's/%ROOT_DIR%\\out/%VSROOT_DIR%\\out/g' "$dest"
30+
31+
chmod +x "$dest"
32+
rm "$dest.bak"
33+
}
34+
935
main() {
1036
cd "$(dirname "${0}")/../.."
1137

1238
source ./ci/lib.sh
1339

14-
cd lib/vscode
40+
pushd lib/vscode
1541

1642
# Set the commit Code will embed into the product.json. We need to do this
1743
# since Code tries to get the commit from the `.git` directory which will fail
@@ -58,13 +84,31 @@ main() {
5884
EOF
5985
) > product.json
6086

61-
# Any platform works since we have our own packaging step (for now).
87+
# Any platform here works since we will do our own packaging. We have to do
88+
# this because we have an NPM package that could be installed on any platform.
89+
# The correct platform dependencies and scripts will be installed as part of
90+
# the post-install during `npm install` or when building a standalone release.
6291
yarn gulp "vscode-reh-web-linux-x64${MINIFY:+-min}"
6392

6493
# Reset so if you develop after building you will not be stuck with the wrong
6594
# commit (the dev client will use `oss-dev` but the dev server will still use
6695
# product.json which will have `stable-$commit`).
6796
git checkout product.json
97+
98+
popd
99+
100+
# These provide a `code-server` command in the integrated terminal to open
101+
# files in the current instance.
102+
delete-bin-script remote-cli/code-server
103+
copy-bin-script remote-cli/code-darwin.sh
104+
copy-bin-script remote-cli/code-linux.sh
105+
copy-bin-script remote-cli/code.cmd
106+
107+
# These provide a way for terminal applications to open browser windows.
108+
delete-bin-script helpers/browser.sh
109+
copy-bin-script helpers/browser-darwin.sh
110+
copy-bin-script helpers/browser-linux.sh
111+
copy-bin-script helpers/browser.cmd
68112
}
69113

70114
main "$@"

ci/build/npm-postinstall.sh

+62-26
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,69 @@
11
#!/usr/bin/env sh
22
set -eu
33

4-
# Copied from arch() in ci/lib.sh.
5-
detect_arch() {
6-
case "$(uname -m)" in
7-
aarch64)
8-
echo arm64
9-
;;
10-
x86_64 | amd64)
11-
echo amd64
12-
;;
13-
*)
14-
# This will cause the download to fail, but is intentional
15-
uname -m
16-
;;
4+
# Copied from ../lib.sh.
5+
arch() {
6+
cpu="$(uname -m)"
7+
case "$cpu" in
8+
aarch64) cpu=arm64 ;;
9+
x86_64) cpu=amd64 ;;
1710
esac
11+
echo "$cpu"
1812
}
1913

20-
ARCH="${NPM_CONFIG_ARCH:-$(detect_arch)}"
14+
# Copied from ../lib.sh except we do not rename Darwin since the cloud agent
15+
# uses "darwin" in the release names and we do not need to detect Alpine.
16+
os() {
17+
osname=$(uname | tr '[:upper:]' '[:lower:]')
18+
case $osname in
19+
cygwin* | mingw*) osname="windows" ;;
20+
esac
21+
echo "$osname"
22+
}
23+
24+
# Create a symlink at $2 pointing to $1 on any platform. Anything that
25+
# currently exists at $2 will be deleted.
26+
symlink() {
27+
source="$1"
28+
dest="$2"
29+
rm -rf "$dest"
30+
case $OS in
31+
windows) mklink /J "$dest" "$source" ;;
32+
*) ln -s "$source" "$dest" ;;
33+
esac
34+
}
35+
36+
# VS Code bundles some modules into an asar which is an archive format that
37+
# works like tar. It then seems to get unpacked into node_modules.asar.
38+
#
39+
# I don't know why they do this but all the dependencies they bundle already
40+
# exist in node_modules so just symlink it. We have to do this since not only
41+
# Code itself but also extensions will look specifically in this directory for
42+
# files (like the ripgrep binary or the oniguruma wasm).
43+
symlink_asar() {
44+
symlink node_modules node_modules.asar
45+
}
46+
47+
# Make a symlink at bin/$1/$3 pointing to the platform-specific version of the
48+
# script in $2. The extension of the link will be .cmd for Windows otherwise it
49+
# will be whatever is in $4 (or no extension if $4 is not set).
50+
symlink_bin_script() {
51+
oldpwd="$(pwd)"
52+
cd "bin/$1"
53+
source="$2"
54+
dest="$3"
55+
ext="${4-}"
56+
case $OS in
57+
windows) symlink "$source.cmd" "$dest.cmd" ;;
58+
darwin | macos) symlink "$source-darwin.sh" "$dest$ext" ;;
59+
*) symlink "$source-linux.sh" "$dest$ext" ;;
60+
esac
61+
cd "$oldpwd"
62+
}
63+
64+
ARCH="${NPM_CONFIG_ARCH:-$(arch)}"
65+
OS="$(os)"
66+
2167
# This is due to an upstream issue with RHEL7/CentOS 7 comptability with node-argon2
2268
# See: https://github.com/cdr/code-server/pull/3422#pullrequestreview-677765057
2369
export npm_config_build_from_source=true
@@ -56,8 +102,6 @@ main() {
56102
;;
57103
esac
58104

59-
OS="$(uname | tr '[:upper:]' '[:lower:]')"
60-
61105
mkdir -p ./lib
62106

63107
if curl -fsSL "https://github.com/coder/cloud-agent/releases/latest/download/cloud-agent-$OS-$ARCH" -o ./lib/coder-cloud-agent; then
@@ -79,22 +123,14 @@ main() {
79123
fi
80124
}
81125

82-
# This is a copy of symlink_asar in ../lib.sh. Look there for details.
83-
symlink_asar() {
84-
rm -rf node_modules.asar
85-
if [ "${WINDIR-}" ]; then
86-
mklink /J node_modules.asar node_modules
87-
else
88-
ln -s node_modules node_modules.asar
89-
fi
90-
}
91-
92126
vscode_yarn() {
93127
echo 'Installing Code dependencies...'
94128
cd lib/vscode
95129
yarn --production --frozen-lockfile --no-default-rc
96130

97131
symlink_asar
132+
symlink_bin_script remote-cli code code-server
133+
symlink_bin_script helpers browser browser .sh
98134

99135
cd extensions
100136
yarn --production --frozen-lockfile

ci/lib.sh

+18-41
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,30 @@ vscode_version() {
1818
}
1919

2020
os() {
21-
local os
22-
os=$(uname | tr '[:upper:]' '[:lower:]')
23-
if [[ $os == "linux" ]]; then
24-
# Alpine's ldd doesn't have a version flag but if you use an invalid flag
25-
# (like --version) it outputs the version to stderr and exits with 1.
26-
local ldd_output
27-
ldd_output=$(ldd --version 2>&1 || true)
28-
if echo "$ldd_output" | grep -iq musl; then
29-
os="alpine"
30-
fi
31-
elif [[ $os == "darwin" ]]; then
32-
os="macos"
33-
fi
34-
echo "$os"
21+
osname=$(uname | tr '[:upper:]' '[:lower:]')
22+
case $osname in
23+
linux)
24+
# Alpine's ldd doesn't have a version flag but if you use an invalid flag
25+
# (like --version) it outputs the version to stderr and exits with 1.
26+
# TODO: Better to check /etc/os-release; see ../install.sh.
27+
ldd_output=$(ldd --version 2>&1 || true)
28+
if echo "$ldd_output" | grep -iq musl; then
29+
osname="alpine"
30+
fi
31+
;;
32+
darwin) osname="macos" ;;
33+
cygwin* | mingw*) osname="windows" ;;
34+
esac
35+
echo "$osname"
3536
}
3637

3738
arch() {
3839
cpu="$(uname -m)"
3940
case "$cpu" in
40-
aarch64)
41-
echo arm64
42-
;;
43-
x86_64 | amd64)
44-
echo amd64
45-
;;
46-
*)
47-
echo "$cpu"
48-
;;
41+
aarch64) cpu=arm64 ;;
42+
x86_64) cpu=amd64 ;;
4943
esac
44+
echo "$cpu"
5045
}
5146

5247
# Grabs the most recent ci.yaml github workflow run that was triggered from the
@@ -104,21 +99,3 @@ export OS
10499
# RELEASE_PATH is the destination directory for the release from the root.
105100
# Defaults to release
106101
RELEASE_PATH="${RELEASE_PATH-release}"
107-
108-
# VS Code bundles some modules into an asar which is an archive format that
109-
# works like tar. It then seems to get unpacked into node_modules.asar.
110-
#
111-
# I don't know why they do this but all the dependencies they bundle already
112-
# exist in node_modules so just symlink it. We have to do this since not only VS
113-
# Code itself but also extensions will look specifically in this directory for
114-
# files (like the ripgrep binary or the oniguruma wasm).
115-
symlink_asar() {
116-
rm -rf node_modules.asar
117-
if [ "${WINDIR-}" ]; then
118-
# mklink takes the link name first.
119-
mklink /J node_modules.asar node_modules
120-
else
121-
# ln takes the link name second.
122-
ln -s node_modules node_modules.asar
123-
fi
124-
}

ci/steps/publish-npm.sh

-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ main() {
8181
# https://github.com/actions/upload-artifact/issues/38
8282
tar -xzf release-npm-package/package.tar.gz
8383

84-
# Ignore symlink when publishing npm package
85-
# See: https://github.com/coder/code-server/pull/3935
86-
echo "node_modules.asar" > release/.npmignore
87-
8884
# We use this to set the name of the package in the
8985
# package.json
9086
PACKAGE_NAME="code-server"

test/e2e/models/CodeServer.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,31 @@ export class CodeServerPage {
283283
}
284284

285285
/**
286-
* Focuses Integrated Terminal
287-
* by using "Terminal: Focus Terminal"
288-
* from the Command Palette
286+
* Focuses the integrated terminal by navigating through the command palette.
289287
*
290-
* This should focus the terminal no matter
291-
* if it already has focus and/or is or isn't
292-
* visible already.
288+
* This should focus the terminal no matter if it already has focus and/or is
289+
* or isn't visible already. It will always create a new terminal to avoid
290+
* clobbering parallel tests.
293291
*/
294292
async focusTerminal() {
295-
await this.executeCommandViaMenus("Terminal: Focus Terminal")
293+
const doFocus = async (): Promise<boolean> => {
294+
await this.executeCommandViaMenus("Terminal: Create New Terminal")
295+
try {
296+
await this.page.waitForLoadState("load")
297+
await this.page.waitForSelector("textarea.xterm-helper-textarea:focus-within", { timeout: 5000 })
298+
return true
299+
} catch (error) {
300+
return false
301+
}
302+
}
303+
304+
let attempts = 1
305+
while (!(await doFocus())) {
306+
++attempts
307+
this.codeServer.logger.debug(`no focused terminal textarea, retrying (${attempts}/∞)`)
308+
}
296309

297-
// Wait for terminal textarea to show up
298-
await this.page.waitForSelector("textarea.xterm-helper-textarea")
310+
this.codeServer.logger.debug(`opening terminal took ${attempts} ${plural(attempts, "attempt")}`)
299311
}
300312

301313
/**
@@ -423,7 +435,7 @@ export class CodeServerPage {
423435
let context = new Context()
424436
while (!(await Promise.race([openThenWaitClose(context), navigate(context)]))) {
425437
++attempts
426-
logger.debug("closed, retrying (${attempt}/∞)")
438+
logger.debug(`closed, retrying (${attempts}/∞)`)
427439
context.cancel()
428440
context = new Context()
429441
}

0 commit comments

Comments
 (0)