Skip to content

Commit

Permalink
recognize L7_NODE_VERSION var; add test for nodejs version shims
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Jul 4, 2024
1 parent 4baf8c1 commit 239d527
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ jobs:
- ghauth
- node_corepack
- lsp_typescript
- node_majors
ubuntu-version:
- 24.04
steps:
Expand Down Expand Up @@ -198,6 +199,10 @@ jobs:
make -j6 image_nvim image_runner_node
make -j6 test_runner_node
if: ${{ matrix.test == 'node_corepack' }}
- name: Build images
run: |
make -j6 image_nvim image_runner_node_all
if: ${{ matrix.test == 'node_majors' }}
- name: Build images
run: |
make -j6 images_test
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ image_runner_node_20: submodules image_caddy
--build-arg "SHELL=${USER_SHELL}" \
--build-arg "NODE_VERSION=20" \
-t "${IMAGE_NAME}:20-${IMAGE_TAG}" \
-t "${IMAGE_NAME}:latest" \
-f './sidecars/node-runner/Containerfile' \
.
image_runner_node_22 : IMAGE_NAME = ${RUNNER_IMAGE_NAME}
Expand Down Expand Up @@ -438,6 +439,12 @@ test_e2e_node_corepack: # image_nvim
set -e
./test/runner-node/test-corepack-pms.sh

test_e2e_node_majors: IMAGE_NAME = ${RUNNER_IMAGE_NAME}
test_e2e_node_majors: IMAGE_TAG = ${RUNNER_IMAGE_TAG}
test_e2e_node_majors: # image_nvim
set -e
./test/runner-node/test-node-majors.sh

test_e2e_lsp_typescript : IMAGE_NAME = ${NVIM_IMAGE_NAME}
test_e2e_lsp_typescript : IMAGE_TAG = ${NVIM_IMAGE_TAG}
test_e2e_lsp_typescript : image_nvim_test test_lsp_node
Expand Down
4 changes: 4 additions & 0 deletions contrib/bin/l7-run-node
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

export RUNNER_IMAGE="${RUNNER_IMAGE:-${NODE_RUNNER_IMAGE}}"

if [[ "${RUNNER_IMAGE}" != *:* ]]; then
export RUNNER_IMAGE=${RUNNER_IMAGE}:${L7_NODE_VERSION:-20}-bookworm
fi

export RUNNER_ENV="${RUNNER_ENV}
NODE_OPTIONS=${NODE_OPTIONS}
HOME=/home/node
Expand Down
1 change: 0 additions & 1 deletion contrib/bin/node
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash
export RUNNER_IMAGE=${NODE_RUNNER_IMAGE:-${RUNNER_IMAGE}}
l7-run-node node ${@}
2 changes: 1 addition & 1 deletion contrib/bin/node-16
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
export RUNNER_IMAGE="localhost/l7/node:16-bookworm"
export L7_NODE_VERSION=16
l7-run-node node ${@}
2 changes: 1 addition & 1 deletion contrib/bin/node-18
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
export RUNNER_IMAGE="localhost/l7/node:18-bookworm"
export L7_NODE_VERSION=18
l7-run-node node ${@}
2 changes: 1 addition & 1 deletion contrib/bin/node-20
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
export RUNNER_IMAGE="localhost/l7/node:20-bookworm"
export L7_NODE_VERSION=20
l7-run-node node ${@}
2 changes: 1 addition & 1 deletion contrib/bin/node-22
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
export RUNNER_IMAGE="localhost/l7/node:22-bookworm"
export L7_NODE_VERSION=22
l7-run-node node ${@}
4 changes: 3 additions & 1 deletion devenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ runtime_config () {
IMAGE=${IMAGE:-$IMAGE_NAME:$IMAGE_TAG}

GO_RUNNER_IMAGE="${GO_RUNNER_IMAGE:-localhost/l7/go:1.20-bookworm}"
NODE_RUNNER_IMAGE="${NODE_RUNNER_IMAGE:-localhost/l7/node:20-bookworm}"
NODE_RUNNER_IMAGE="${NODE_RUNNER_IMAGE:-localhost/l7/node}"
NODE_VERSION="${NODE_VERSION:-20}"
GPG_IMAGE="${GPG_IMAGE:-localhost/l7/gpg-vault:pk}"

CONTAINER_DNS="${CONTAINER_DNS:-10.7.8.133}"
Expand Down Expand Up @@ -327,6 +328,7 @@ else
-e "CONTAINER_HOST=tcp://10.7.9.2:2375" \
-e "GO_RUNNER_IMAGE=${GO_RUNNER_IMAGE}" \
-e "NODE_RUNNER_IMAGE=${NODE_RUNNER_IMAGE}" \
-e "L7_NODE_VERSION=${NODE_VERSION}" \
-e "GPG_IMAGE=${GPG_IMAGE}" \
-e HOME=/home/user \
-e "SRC_DIR=${SRC_DIR}" \
Expand Down
45 changes: 42 additions & 3 deletions docs/3-nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

During development, we often need to run various nodejs modules - be it workflow tools, linting, or even the application itself under test. In order to reduce impact of potentially untrusted code, Node.js commands are shimmed and proxied to a sibling container. While the sibling container has the same source directories available, it is not otherwise able to access the editor or host contexts. Each command is run in an ephemeral container. Apart from the `SRC_DIR`, caches are persisted for `npm`, `pnpm`, and `yarn`.

## Node.js versions

Inside the editor context, each invocation of `node` spawns a new container using the default Node.js version. If you want to run a different version of Node.js, there are two ways:

```
> node --version
v20.14.0
> node-18 --version
v18.20.3
❯ L7_NODE_VERSION=18 node --version
v18.20.3
```

The `node-xx` shims are provided for convenience.
You can `export L7_NODE_VERSION=22` to have the desired version used by scripts.
Using either of these methods is preferred over using third-party version managers like nvm or asdf.

## Package manager versions

Any supported package manager should be auto-detected as long as it's properly specified in the `packageManager` field in `package.json`:
Expand All @@ -22,21 +41,41 @@ Any supported package manager should be auto-detected as long as it's properly s
4.2.2
```

If you need to manually call a specific version of a package manager, there are shims available:

```
> npm<tab><tab>
npm npm10 npm7 npm9
> npm --version
9.9.3
> npm10 --version
10.8.1
```

The prebundled package manager versions can be overridden by the `COREPACK_PMS` build-arg:

```
$ make BUILD_OPTIONS='--build-arg COREPACK_PMS="[email protected] npm@10 ..."' images
```

## The node runner container
## The node runner containers

You may shell into a node container if you need longer sessions or troubleshooting;

```
> RUNNER_OPTS='-t ' l7-cnt-run zsh
> l7-run-node bash
```

`l7-cnt-run` is also the entrypoint for the node command shims like `node`, `npm`, `yarn`, `allow-scripts`, etc. Shims can be overridden by the `NODE_BINS` build-arg:
You can override arguments passed to the container `run` command:
```
# Run as root
> RUNNER_OPTS='--user=root' l7-run-node bash
```


`l7-run-node` is also the entrypoint for the node command shims like `node`, `npm`, `yarn`, `allow-scripts`, etc. Shims can be overridden by the `NODE_BINS` build-arg:

```
$ make BUILD_OPTIONS='--build-arg NODE_BINS="allow-script npx ..."' images
Expand Down
21 changes: 21 additions & 0 deletions test/runner-node/test-node-majors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
# test shims for major nodejs versions
export L7_SRC_DIR=$(pwd)
for nv in 18 20 22; do
version=$(NAME="l7ide-test-runner-nv" ./devenv.sh node-${nv} --version)
# compare if major version is same as expected
if [[ "$(echo "${version}" | grep -o '[0-9]*' | head -n1)" != "${nv}" ]]; then
echo "FAIL: node-${nv} is ${version}" >&2
export TESTFAIL=1
if [[ -z "${version}" ]]; then
echo "(Try 'make image_runner_node_${nv}')"
fi
continue
fi
echo "PASS: node-${nv} is ${version}" >&2
done
if [[ -n "${TESTFAIL}" ]]; then
echo "(Try 'make image_runner_node_all')"
exit 1
fi

0 comments on commit 239d527

Please sign in to comment.