Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
70 changes: 68 additions & 2 deletions .github/workflows/pr-win32-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ on:
description: Reason for selecting the Agent Host test profile
type: string
default: Change detection unavailable
workspace_image_branch:
description: The branch whose workspace images the jobs use (see .github/workflows/workspace_image/README.md)
type: string
default: main

env:
NPM_ARCH: x64
Expand Down Expand Up @@ -74,6 +78,60 @@ jobs:
UNIT_TESTS: 'true'
TEST_SELECTION: 'true'
steps: &steps
# A workspace image of main holds a workspace whose dependencies, Electron and Playwright browsers
# are installed, which the checkout updates (see .github/workflows/workspace_image/README.md). Its
# scripts run before the checkout, so they come from the API, at the commit that the job tests, as
# in pr-workspace-images.yml. The smoke test shard with the WSL suite keeps its workspace on drive
# C (see the Dev Drive step below), and sets up without an image.
- name: Download the workspace image scripts
id: image-scripts
if: ${{ !(env.ELECTRON_TESTS == 'true' && env.SMOKE_TESTS == 'true' && env.SMOKE_SHARD_INDEX == '1') }}
continue-on-error: true
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
dir="$RUNNER_TEMP/workspace-image"
mkdir -p "$dir"
echo '{ "type": "module" }' > "$dir/package.json"
# The scripts keep their folders below workspace_image, such as win32/vhd.ps1
files=(.nvmrc .github/workflows/workspace_image/{image.ts,win32/vhd.ps1,win32/Vhd.cs})
args=()
for file in "${files[@]}"; do
args+=(-o "$dir/${file#.github/workflows/workspace_image/}" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/contents/$file?ref=$GITHUB_SHA")
done
curl -fsSL --retry 3 --parallel --create-dirs -H "Authorization: Bearer $GH_TOKEN" -H 'Accept: application/vnd.github.raw' "${args[@]}"
for file in "${files[@]}"; do
[ -s "$dir/${file#.github/workflows/workspace_image/}" ]
done
echo "node-version=$(tr -d '[:space:]' < "$dir/.nvmrc")" >> "$GITHUB_OUTPUT"

- name: Setup Node.js
if: steps.image-scripts.outcome == 'success'
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: ${{ steps.image-scripts.outputs.node-version }}

# The image is a Dev Drive in a VHDX, mounted at the parent of the workspace like the Dev Drive
# below, which the job creates without an image. The first PowerShell of a job takes seconds to
# start, so this step runs in bash, and the PowerShell that attaches the image (vhd.ps1) starts
# while the image downloads. image.ts gives up before the timeout of the step, which could stop it
# with the image half attached, and then detaches the image as after any other error, so that the
# job sets up as without an image. Pull requests to release branches look for images of their own
# branch, which pr-workspace-images.yml doesn't build.
- name: Restore the workspace image
id: image
if: steps.image-scripts.outcome == 'success'
continue-on-error: true
timeout-minutes: 5
shell: bash
working-directory: ${{ runner.temp }}
run: node "$RUNNER_TEMP/workspace-image/image.ts" restore --name workspace-image-win32-x64.img --branch "$WORKSPACE_IMAGE_BRANCH" --timeout-seconds 240
env:
GH_TOKEN: ${{ github.token }}
WORKSPACE_IMAGE_BRANCH: ${{ inputs.workspace_image_branch }}

# The larger runners keep the workspace on the OS disk, where creating many small files, such as
# extracting node_modules, takes twice as long as on the 1ES runners' temporary disk. A Dev Drive in
# a VHDX on the same disk is faster than both. It is mounted at the parent of the workspace, so that
Expand All @@ -82,7 +140,7 @@ jobs:
# runs-on of electron-smoke-1), and WSL cannot open folders on a volume that is mounted in a folder of
# drive C: anyway.
- name: Move the workspace to a Dev Drive
if: ${{ !(env.ELECTRON_TESTS == 'true' && env.SMOKE_TESTS == 'true' && env.SMOKE_SHARD_INDEX == '1') }}
if: ${{ steps.image.outputs.used != 'true' && !(env.ELECTRON_TESTS == 'true' && env.SMOKE_TESTS == 'true' && env.SMOKE_SHARD_INDEX == '1') }}
shell: pwsh
working-directory: ${{ runner.temp }}
run: |
Expand Down Expand Up @@ -119,6 +177,8 @@ jobs:
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
lfs: true
# With a workspace image, keeps its untracked files, such as node_modules, and reuses its .git
clean: ${{ steps.image.outputs.used != 'true' }}

# The WSL steps only run in shard 1, which is where test/smoke/src/shard.ts pins the WSL suite.
# pr-node-modules.yml fills this cache on main, so pull requests don't download the installer.
Expand Down Expand Up @@ -175,20 +235,24 @@ jobs:
WSL_PREPARED_DISTRO_VHD: ${{ github.workspace }}\.build\wsl-distro\vscode-wsl-smoke.vhdx

- name: Setup Node.js
if: steps.image-scripts.outcome != 'success'
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: .nvmrc

- name: Setup pnpm
uses: ./.github/actions/setup-pnpm

# A workspace image holds the node_modules of its commit, which the install step updates
- name: Restore node_modules cache
id: cache-node-modules
if: steps.image.outputs.used != 'true'
uses: ./.github/actions/restore-node-modules
with:
key-prefix: node_modules-windows
key-args: "win32 ${{ env.VSCODE_ARCH }} $(node -p process.arch)"

# On a workspace image, pnpm installs only what changed since the commit of the image
- name: Install dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
shell: pwsh
Expand Down Expand Up @@ -261,8 +325,10 @@ jobs:
run: pnpm run compile

# pr-node-modules.yml fills this cache on main, so pull requests don't download the browsers.
# A workspace image holds them already, and points PLAYWRIGHT_BROWSERS_PATH at them, which this
# action would point into the workspace.
- name: Restore Playwright browsers
if: ${{ env.BROWSER_TESTS == 'true' }}
if: ${{ env.BROWSER_TESTS == 'true' && steps.image.outputs.used != 'true' }}
uses: ./.github/actions/restore-playwright
with:
browsers: --only-shell chromium
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr-workspace-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: Code OSS (workspace images)
# pr-darwin-test.yml and pr-win32-test.yml): a disk image of a workspace of main, with its
# dependencies, Electron and the Playwright browsers installed, at the path of the workspaces of the
# jobs, and with a .git that their checkouts reuse. A job that mounts an image installs only what
# changed since. The test workflows adopt the images one platform at a time in follow-up pull
# requests, and until then nothing uses them. See .github/workflows/workspace_image/README.md.
# changed since. The Windows test jobs use the images, and the Linux and macOS test jobs adopt them
# in follow-up pull requests. See .github/workflows/workspace_image/README.md.
# The daily and manual runs also prepare the WSL distribution of the Windows smoke tests (the
# wsl-distro job).
on:
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ jobs:
name: Windows
needs: changes
if: ${{ !cancelled() }}
# actions: read finds and downloads the workspace image (see .github/workflows/workspace_image/README.md)
permissions:
contents: read
pull-requests: read
actions: read
uses: ./.github/workflows/pr-win32-test.yml
with:
electron_unit_affected: ${{ needs.changes.result != 'success' || needs.changes.outputs.selected-unit-tests == 'true' }}
Expand All @@ -546,6 +551,8 @@ jobs:
# The provider integration tests and the Agent Host E2E tests read the profile.
agent_host_test_profile: ${{ needs.changes.outputs.agent-host-test-profile || 'full' }}
agent_host_test_profile_reason: ${{ needs.changes.outputs.agent-host-test-profile-reason || 'Change detection unavailable' }}
# Pull requests to release branches find no images of their branch, and install everything.
workspace_image_branch: ${{ github.base_ref }}

copilot-check-test-cache:
name: Copilot - Check Test Cache
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/workspace_image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

Workspace images let the test jobs of pull requests ([pr-linux-test.yml](../pr-linux-test.yml), [pr-darwin-test.yml](../pr-darwin-test.yml) and [pr-win32-test.yml](../pr-win32-test.yml)) start from a disk image of a workspace of `main` instead of an empty folder: its dependencies, Electron and the Playwright browsers are installed, and its `.git` holds the commit it was built at. A job mounts the image, checks out its own commit, and installs only what changed since, which for most pull requests is nothing.

The test workflows adopt the images one platform at a time, in follow-up pull requests. Until then nothing uses the images, and the test jobs install everything as before: [What a job does](#what-a-job-does) describes the steps that those pull requests add.
The Windows test jobs use the images, except the smoke test shard with the WSL suite, which runs on the 1ES pool and keeps its workspace on drive C. The Linux and macOS test jobs adopt them in follow-up pull requests, and until then install everything as before. [What a job does](#what-a-job-does) describes the steps of a job that uses the images.

[pr-workspace-images.yml](../pr-workspace-images.yml) builds an image for each platform on the runners of the jobs every day at 05:00 UTC, and when one of its inputs changes on `main`: those of the install, of the native builds (`NATIVE_BUILD_INPUTS` in `image.ts`) and of the prepared Electron. It doesn't build them for other pushes, as the checkout of a job fetches and writes only what changed since the commit of its image. The images are kept for 2 days, so the daily build keeps a recent image available. [image.ts](image.ts) restores, checks and builds them; it has no dependencies, as the jobs run it before they check out the repository.

## What a job does

1. **Downloads the scripts** of its commit from the API (`image.ts`, and on Windows `win32/vhd.ps1` and `win32/Vhd.cs`), and `.nvmrc` for the Node.js version.
2. **Restores the image** (`image.ts restore`): finds the newest image of `main`, downloads it with parallel range requests, and mounts it at the parent folder of the workspace, so that the workspace keeps its path. Without an image, or on any error, it warns and the job installs everything as before. It gives up after 4 minutes (`--timeout-seconds`), before the timeout of its step could stop it with the image half attached, and then, as after any other error, unmounts and detaches the image, so that the workspace is an empty folder again; on Windows, it also deletes the VHDX, whose data Windows keeps in memory. The job also doesn't use an image when the inputs of the base key of the `node_modules` caches (`computeNodeModulesBaseCacheKey` in `build/azure-pipelines/common/cacheKeys.ts`: the salt of the CI caches, the pnpmfiles, the `nativeBuild` targets and `packageManager` of `package.json`, the Node.js versions and the pnpm settings of the install roots, mirrored in `NATIVE_BUILD_INPUTS` of `image.ts`) differ between the commit of the image and its own, which the restore compares through the API before it downloads anything: installing on top of an image wouldn't build its native modules again, for example after an Electron update.
2. **Restores the image** (`image.ts restore`): finds the newest image of `main`, downloads it with parallel range requests, and mounts it at the parent folder of the workspace, so that the workspace keeps its path. Without an image, or on any error, it warns and the job installs everything as before. It gives up after 4 minutes (`--timeout-seconds`), before the timeout of its step could stop it with the image half attached, and then, as after any other error, unmounts and detaches the image, so that the workspace is an empty folder again; on Windows, it also deletes the VHDX, whose data Windows keeps in memory. Pull requests to release branches look for images of their own branch (`--branch`), which aren't built, and install everything as before. The job also doesn't use an image when the inputs of the base key of the `node_modules` caches (`computeNodeModulesBaseCacheKey` in `build/azure-pipelines/common/cacheKeys.ts`: the salt of the CI caches, the pnpmfiles, the `nativeBuild` targets and `packageManager` of `package.json`, the Node.js versions and the pnpm settings of the install roots, mirrored in `NATIVE_BUILD_INPUTS` of `image.ts`) differ between the commit of the image and its own, which the restore compares through the API before it downloads anything: installing on top of an image wouldn't build its native modules again, for example after an Electron update.
3. **Checks out its commit** with `clean: false`, which keeps the untracked files of the image and reuses its checkout: the shallow fetch sends the tag at the commit of the image, and downloads only what changed since, and git writes only the files that changed, as it compares their sizes and modification times, which the image keeps.
4. **Installs the dependencies** with its usual install steps, on the `node_modules` of the image. `pnpm install`, whose postinstall installs every other install root, installs only what changed since the commit of the image, as pnpm checks itself what is up to date.
5. **Keeps the prepared Electron** when it was prepared from the same inputs (see [setup-electron](../../actions/setup-electron/action.yml)), and finds the Playwright browsers in the image (`PLAYWRIGHT_BROWSERS_PATH`).
4. **Installs the dependencies** with its usual install steps, on the `node_modules` of the image instead of those of the `node_modules` cache. `pnpm install`, whose postinstall installs every other install root, installs only what changed since the commit of the image, as pnpm checks itself what is up to date.
5. **Keeps the prepared Electron** when it was prepared from the same inputs (see [setup-electron](../../actions/setup-electron/action.yml)), and finds the Playwright browsers in the image (`PLAYWRIGHT_BROWSERS_PATH`), instead of in their cache, which would point `PLAYWRIGHT_BROWSERS_PATH` into the workspace.

## The images

Expand Down
13 changes: 10 additions & 3 deletions build/lib/test/detectMissingCaches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,20 @@ suite('Detect missing caches', () => {
});

test('the Windows cache warmer installs on the Dev Drive of the Windows test jobs', () => {
// pr-win32-test.yml explains the step. It must come first, as it needs an empty workspace.
// pr-win32-test.yml explains the step. It needs an empty workspace: the cache warmer creates it
// first, and the test jobs after the steps of the workspace image, which leave the workspace
// empty when they mount no image (see .github/workflows/workspace_image/README.md).
const devDrive = (relativePath: string, jobName: string) => {
const steps = loadYaml<{ jobs: Record<string, IJob> }>(relativePath).jobs[jobName].steps;
const index = steps.findIndex(step => step.name === 'Move the workspace to a Dev Drive');
return { index, run: steps[index]?.run, shell: steps[index]?.shell, workingDirectory: steps[index]?.['working-directory'] };
return { before: steps.slice(0, index).map(step => step.name), run: steps[index]?.run, shell: steps[index]?.shell, workingDirectory: steps[index]?.['working-directory'] };
};
assert.deepStrictEqual(devDrive('.github/workflows/pr-node-modules.yml', 'windows'), devDrive('.github/workflows/pr-win32-test.yml', 'electron-unit'));
const { before: warmerBefore, ...warmer } = devDrive('.github/workflows/pr-node-modules.yml', 'windows');
const { before: testsBefore, ...tests } = devDrive('.github/workflows/pr-win32-test.yml', 'electron-unit');
assert.deepStrictEqual(
{ warmerBefore, testsBefore, tests },
{ warmerBefore: [], testsBefore: ['Download the workspace image scripts', 'Setup Node.js', 'Restore the workspace image'], tests: warmer }
);
});

test('jobs run when one of their caches is missing, has another version or cannot be listed', async () => {
Expand Down
23 changes: 23 additions & 0 deletions build/lib/test/workspaceImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { suite, test } from 'node:test';
import { FAILSAFE_SCHEMA, load } from 'js-yaml';
import { NATIVE_BUILD_INPUTS } from '../../../.github/workflows/workspace_image/image.ts';
import { computeNodeModulesBaseCacheKey } from '../../azure-pipelines/common/cacheKeys.ts';
import { distroRoots } from '../../npm/dirs.ts';
Expand All @@ -17,7 +18,29 @@ function readRepositoryFile(file: string): Buffer {
return readFileSync(path.join(repositoryRoot, file));
}

interface IStep {
readonly id?: string;
readonly name?: string;
readonly shell?: string;
readonly env?: Record<string, string>;
readonly run?: string;
}

function loadSteps(workflow: string, job: string): readonly IStep[] {
const { jobs } = load(readRepositoryFile(`.github/workflows/${workflow}`).toString(), { schema: FAILSAFE_SCHEMA }) as { jobs: Record<string, { steps: IStep[] }> };
return jobs[job].steps;
}

suite('workspace images', () => {
// image.ts finds vhd.ps1 in the win32 folder beside it, which the download keeps
test('the Windows test jobs download the scripts like the job that builds the Windows image', () => {
const download = (workflow: string, job: string) => {
const step = loadSteps(workflow, job).find(step => step.id === 'image-scripts');
return { name: step?.name, shell: step?.shell, env: step?.env, run: step?.run };
};
assert.deepEqual(download('pr-win32-test.yml', 'electron-unit'), download('pr-workspace-images.yml', 'windows'));
});

// image.ts runs before the checkout, without the files of the repository, so it lists the inputs
// of the base key of the node_modules caches itself
test('compare the inputs of the base key of the node_modules caches', () => {
Expand Down
1 change: 1 addition & 0 deletions build/ripple/vscode.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
".github/workflows/pr-darwin-test.yml",
".github/workflows/pr-win32-test.yml",
".github/workflows/node_modules_cache/**",
".github/workflows/workspace_image/**",
".github/actions/setup-electron/**",
".github/actions/setup-pnpm/**",
".github/actions/restore-node-modules/**",
Expand Down
Loading