Skip to content

Commit 2cf4b60

Browse files
authored
Merge pull request #15 from joyeecheung/fix-build-cache
Fix permission and make ninja build cache work
2 parents bd9ba17 + c3106fd commit 2cf4b60

File tree

7 files changed

+55
-20
lines changed

7 files changed

+55
-20
lines changed

Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ RUN echo 'Defaults env_keep += "DEBIAN_FRONTEND"' >> /etc/sudoers.d/env_keep
1818
ENV DEBIAN_FRONTEND=1
1919
ENV PATH=/usr/lib/ccache:$PATH
2020

21-
COPY --chown=root:developer ./scripts/ /home/developer/scripts/
21+
# Copy scripts and make them executable by both root and developer
22+
COPY --chown=root:developer --chmod=0755 ./scripts/ /home/developer/scripts/
2223
RUN /home/developer/scripts/install.sh
23-
RUN /home/developer/scripts/mkdir.sh
2424
RUN /home/developer/scripts/ccache.sh
25+
26+
USER developer
2527
RUN /home/developer/scripts/clone.sh
2628
RUN /home/developer/scripts/build.sh
27-
RUN make install -C /home/developer/nodejs/node
28-
RUN /home/developer/scripts/ncu.sh
2929

30+
ENV PATH=/home/developer/.local/bin:$PATH
3031
WORKDIR /home/developer/nodejs/node
32+
RUN /home/developer/scripts/install-node.sh
33+
RUN /home/developer/scripts/ncu.sh

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,53 @@
88

99
To run locally on your machine, you'll want to install [Docker Desktop](https://www.docker.com/products/docker-desktop/) and start it up.
1010

11-
Once you've got Docker Destop running, you can run the following command to pull and start the image:
11+
It's recommended to use Visual Studio Code with the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)
12+
installed, but you can also run the container directly with Docker CLI or the [Dev Container CLI](https://github.com/devcontainers/cli), or attach any editor that supports the concept of [`devcontainers`](https://containers.dev/) to this container.
13+
14+
Once you've got Docker Desktop running, you can run the following command to pull and start the image:
1215

1316
```sh
1417
docker pull nodejs/devcontainer:nightly
1518
docker run -it nodejs/devcontainer:nightly /bin/bash
1619
```
1720

18-
Once you've run those commands, you'll be in a shell inside the running container. If you need to escape, type `exit`. You should be good to jump to [Working in the Container](#working-in-the-container).
21+
To use it as a devcontainer, create a `.devcontainer/devcontainer.json` file in the project with the following content:
22+
23+
```json
24+
{
25+
"name": "Node.js Dev Container",
26+
"image": "nodejs/devcontainer:nightly",
27+
"workspaceMount": "source=${localWorkspaceFolder},target=/home/developer/nodejs/node,type=bind,consistency=cached",
28+
"workspaceFolder": "/home/developer/nodejs/node",
29+
"remoteUser": "developer",
30+
"mounts": [
31+
"source=build-cache,target=/home/developer/nodejs/node/out,type=volume"
32+
],
33+
"postCreateCommand": "git restore-mtime"
34+
}
35+
```
36+
37+
For example, to use it with Visual Studio Code, use the "Dev Containers: Reopen in Container" command from the Command Palette (`Ctrl+Shift+P` or `Cmd+Shift+P`). After the container is built and started, you should be inside the container with the project mounted in the working directory, while the build cache volume mounted at `out/` to speed up builds.
1938

2039
### Working in the Container
2140

22-
- The project is located at `/home/developer/nodejs/node`.
23-
- Once this directory is your active directory, you should be good to go.
24-
- If you want to build the project in the container, run with ninja (rather than just make):
25-
- `/home/developer/nodejs/node/configure --ninja && make -C /home/developer/nodejs/node`
26-
- You should be able to attach any editor that supports the concept of [`devcontainers`](https://containers.dev/) to this
41+
- The project is located at `/home/developer/nodejs/node`. After the container is started, you should be automatically placed in this directory.
42+
- If you want to build the project in the container, run with `ninja` (rather than just `make`):
43+
- To build the release build, run `ninja -C out/Release`
44+
- The container comes with a release build that can be picked up by `ninja`. As long as your mounted local checkout is not too far behind the checkout in the container, incremental builds should be fast.
45+
- If you notice that the build is not picking up your changes after checking out a different branch, run `git restore-mtime` in the container to sync the mtimes of the files in your checkout with the git commit timestamps.
46+
- You can also set up a git hook to sync the mtime automatically on checkout to keep the build cache effective. From the container, run:
47+
48+
```bash
49+
mkdir -p /home/developer/nodejs/node/.git/hooks
50+
cp /home/developer/scripts/post-checkout /home/developer/nodejs/node/.git/hooks/post-checkout
51+
```
52+
53+
Note that if you install this git hook to your mounted project, and you still wish to run `git checkout` from you local system, you will need to install [`git-restore-mtime`](https://github.com/MestreLion/git-tools) on your local system as well.
2754

2855
### Personal Configuration
2956

30-
Assuming you've already got the Docker container running:
57+
Do this from your local system, not in the container. The `git` configuration will be used in the container since the project is mounted from your local system.
3158

3259
- Set the git `origin` to your own fork rather than `nodejs/node`
3360
- Example, where `USERNAME` is your GitHub username: `$ git remote set-url origin https://github.com/USERNAME/node.git`

scripts/clone.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
set -e # Exit with nonzero exit code if anything fails
44

5+
mkdir -p /home/developer/nodejs
56
cd /home/developer/nodejs
67
git clone https://github.com/nodejs/node.git --single-branch --branch main --depth 1
78
cd /home/developer/nodejs/node
89
git remote add upstream https://github.com/nodejs/node.git
9-
git fetch upstream
10+
git restore-mtime # Restore file modification times to commit times for build cache to match.

scripts/install-node.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -e # Exit with nonzero exit code if anything fails
4+
5+
make install PREFIX=/home/developer/.local -C /home/developer/nodejs/node
6+
echo '' >> /home/developer/.bashrc
7+
echo 'export PATH=/home/developer/.local/bin:$PATH' >> /home/developer/.bashrc

scripts/install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ package_list="
2020
pkg-config \
2121
locales \
2222
gpg \
23-
wget"
23+
wget \
24+
git-restore-mtime"
2425

2526
# Install Packages
2627
apt-get update

scripts/mkdir.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/post-checkout

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
git-restore-mtime

0 commit comments

Comments
 (0)