From 9ef833c532fce2271af64c05b0d24cdc6212e2a0 Mon Sep 17 00:00:00 2001 From: "google-labs-stitch[bot]" <315546542+google-labs-stitch[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:55:55 +0000 Subject: [PATCH] ci: split Wasm build from cross-OS native checks - Check lints, rustdoc and the crate list in build-wasm on Linux - Upload pkg/ from build-wasm as a short-lived artifact - Verify native release libraries and npm archives on three OS runners - Keep verify as the final gate job for branch protection - Add --ignore-scripts and Windows npm lookup to verify-pack.mjs - Document the cross-OS checks and shell commands in AGENTS.md --- .github/workflows/ci.yml | 87 ++++++++++- AGENTS.md | 31 +++- scripts/verify-pack.mjs | 309 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 397 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2562d00..c723461 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,10 @@ # Continuous integration for the Rust crate, the Wasm bindings and the # npm package. +# +# First, build-wasm checks the Rust lints, the docs and the Wasm target on +# Linux, then uploads pkg/. Next, check-os builds the native library, compiles +# TypeScript and checks the npm archive on Linux, macOS and Windows. Last, +# verify reports one status check for branch protection. name: CI on: @@ -18,7 +23,7 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - verify: + build-wasm: runs-on: ubuntu-latest timeout-minutes: 15 steps: @@ -46,16 +51,88 @@ jobs: - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: "24" - cache: "npm" - run: cargo fmt --check - run: cargo clippy --locked --lib -- -D warnings - - run: cargo build --locked --release - run: cargo doc --locked --no-deps env: RUSTDOCFLAGS: "-D warnings" - run: cargo package --list --locked - - run: npm ci - run: npm run check:wasm env: RUSTDOCFLAGS: "-D warnings" - - run: npm run verify:pack + - run: npm run build:wasm + # Upload the built pkg/ files for the cross-OS matrix. + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: wasm-pkg + path: | + pkg/tunelet.js + pkg/tunelet.d.ts + pkg/tunelet_bg.wasm + retention-days: 1 + if-no-files-found: error + + check-os: + needs: build-wasm + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + include: + - os: ubuntu-latest + cdylib: target/release/libtunelet.so + - os: macos-latest + cdylib: target/release/libtunelet.dylib + - os: windows-latest + cdylib: target/release/tunelet.dll + runs-on: ${{ matrix.os }} + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@df2a88e9fe4098bad09cb42c3dec7187f15861fc # master + with: + toolchain: stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + cache-bin: "false" + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: wasm-pkg + path: pkg + digest-mismatch: error + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: "24" + cache: "npm" + - run: cargo build --locked --release + # Check that Cargo built the native dynamic library for this OS. + - run: node --input-type=module -e "import assert from 'node:assert/strict'; import fs from 'node:fs'; const stat = fs.statSync(process.env.CDYLIB); assert.ok(stat.isFile() && stat.size > 0);" + env: + CDYLIB: ${{ matrix.cdylib }} + - run: npm ci + - run: npm run build:ts + # Pack and check the pre-built files without a second Wasm build. + - run: node scripts/verify-pack.mjs --ignore-scripts + + verify: + needs: + - build-wasm + - check-os + if: always() + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + # Fail if build-wasm or check-os did not succeed. + - run: | + if [ "${BUILD_WASM}" != "success" ] || [ "${CHECK_OS}" != "success" ]; then + echo "Upstream job results: build-wasm=${BUILD_WASM}, check-os=${CHECK_OS}" >&2 + exit 1 + fi + env: + BUILD_WASM: ${{ needs.build-wasm.result }} + CHECK_OS: ${{ needs.check-os.result }} diff --git a/AGENTS.md b/AGENTS.md index 4bc6d47..5e335a8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -90,16 +90,31 @@ this file, rustdoc, code comments, TypeScript comments and commit messages. ## Build and verify +Run these Cargo checks in a POSIX shell (`sh`, `bash` or `zsh`): + ```sh cargo build --locked --release cargo fmt --check cargo clippy --locked --lib -- -D warnings RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps +cargo package --list --locked rustup target add wasm32-unknown-unknown cargo clippy --locked --lib --target wasm32-unknown-unknown --features wasm -- -D warnings RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps --target wasm32-unknown-unknown --features wasm ``` +In PowerShell on Windows, set `RUSTDOCFLAGS` in the environment before `cargo doc` and `npm run check:wasm`: + +```powershell +$env:RUSTDOCFLAGS = "-D warnings" +cargo doc --locked --no-deps +cargo doc --locked --no-deps --target wasm32-unknown-unknown --features wasm +npm run check:wasm +Remove-Item Env:RUSTDOCFLAGS +``` + +In `cmd.exe` on Windows, use `set "RUSTDOCFLAGS=-D warnings"` before the doc commands and `set "RUSTDOCFLAGS="` after them. + If you change the Wasm or TypeScript code, also run these commands. `npm run check:wasm` runs Clippy and rustdoc for `wasm32-unknown-unknown`. `npm run build` compiles `src/wasm.rs` and `src/index.ts`. @@ -111,8 +126,20 @@ RUSTDOCFLAGS="-D warnings" npm run check:wasm npm run verify:pack ``` -- `.github/workflows/ci.yml` runs these checks on each pull request and each - push to `main`. +`node scripts/verify-pack.mjs --ignore-scripts` checks pre-built `pkg/` and +`dist/` files without a second Wasm build. If `pkg/` is ready, run +`npm run build:ts` before `node scripts/verify-pack.mjs --ignore-scripts`. +You can also run `node scripts/verify-pack.mjs` directly outside `npm run` +on Linux, macOS and Windows. + +- `.github/workflows/ci.yml` runs on each pull request and each push to `main`. + `build-wasm` checks the format, Clippy, rustdoc, the crate file list and + `check:wasm` on `ubuntu-latest`, then builds and uploads `pkg/`. `check-os` + runs on `ubuntu-latest`, `macos-latest` and `windows-latest` with isolated + failures (`fail-fast: false`). Each runner builds `libtunelet.so`, + `libtunelet.dylib` or `tunelet.dll`, compiles TypeScript and runs + `verify-pack.mjs`. `verify` reports the combined status for branch + protection. - Make sure that there are no new warnings. This includes broken rustdoc links. - The crate has no tests (`test = false`). Do not add tests or test diff --git a/scripts/verify-pack.mjs b/scripts/verify-pack.mjs index 921fa6f..d01a0d6 100644 --- a/scripts/verify-pack.mjs +++ b/scripts/verify-pack.mjs @@ -23,8 +23,50 @@ const EXPECTED_ARCHIVE_PATHS = [ const PACKAGE_NAME_PATTERN = /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/; +const CHOCOLATEY_PACKAGES = [ + "npm", + "nodejs", + "nodejs-lts", + "nodejs.commandline", + "nodejs.install", +]; + const repoRoot = fileURLToPath(new URL("..", import.meta.url)); +/** Return true if `filePath` is a regular file on disk. */ +function isRegularFile(filePath) { + try { + return fs.statSync(filePath).isFile(); + } catch { + return false; + } +} + +/** Parse the CLI flags for `verify-pack.mjs`. */ +function parsePackOptions(argv) { + let ignoreScripts = false; + for (const arg of argv) { + if (arg === "--ignore-scripts") { + ignoreScripts = true; + } else { + throw new Error(`Unknown option: ${arg}`); + } + } + return { ignoreScripts }; +} + +/** Check that all archive source files exist before an `--ignore-scripts` pack. */ +function verifyPrebuiltFiles() { + for (const archivePath of EXPECTED_ARCHIVE_PATHS) { + const relativeFile = archivePath.slice("package/".length); + const fullPath = path.join(repoRoot, relativeFile); + assert.ok( + isRegularFile(fullPath), + `Expected built file at ${relativeFile}`, + ); + } +} + /** Read and check the current package name so scoped names also work. */ function readPackageName() { const packageJsonPath = path.join(repoRoot, "package.json"); @@ -43,33 +85,253 @@ function readPackageName() { return packageName; } +/** Check standard and split relative paths from `dir` for `npm-cli.js`. */ +function findNpmCliInDir(dir) { + const candidates = [ + path.join(dir, "node_modules", "npm", "bin", "npm-cli.js"), + path.resolve(dir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"), + path.resolve(dir, "..", "node_modules", "npm", "bin", "npm-cli.js"), + path.join(dir, "npm-cli.js"), + path.join(dir, "bin", "npm-cli.js"), + path.resolve(dir, "..", "bin", "npm-cli.js"), + ]; + for (const candidate of candidates) { + if (isRegularFile(candidate)) { + return candidate; + } + } + return undefined; +} + +/** Check the tool directories of Chocolatey packages in `libDir` for `npm-cli.js`. */ +function findNpmCliInChocolateyLib(libDir) { + for (const pkg of CHOCOLATEY_PACKAGES) { + const toolsDir = path.join(libDir, pkg, "tools"); + const direct = findNpmCliInDir(toolsDir); + if (direct) { + return direct; + } + try { + for (const entry of fs.readdirSync(toolsDir, { withFileTypes: true })) { + if (entry.isDirectory() && entry.name !== "node_modules") { + const nested = findNpmCliInDir(path.join(toolsDir, entry.name)); + if (nested) { + return nested; + } + } + } + } catch { + // Ignore absent Chocolatey tool directories. + } + } + return undefined; +} + +/** Check Volta image directories relative to `nodeDir` for `npm`. */ +function findNpmInVoltaImage(nodeDir) { + const leafName = path.basename(nodeDir).toLowerCase(); + const baseNodeDir = + leafName === "bin" || leafName === "x64" || leafName === "arm64" + ? path.dirname(nodeDir) + : nodeDir; + const nodeTypeDir = path.dirname(baseNodeDir); + const imageDir = path.dirname(nodeTypeDir); + if ( + path.basename(nodeTypeDir).toLowerCase() !== "node" || + path.basename(imageDir).toLowerCase() !== "image" + ) { + return undefined; + } + + for (const imageKind of ["npm", "bundled-npm"]) { + const kindDir = path.join(imageDir, imageKind); + try { + const versions = fs + .readdirSync(kindDir, { withFileTypes: true }) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + .sort((a, b) => b.localeCompare(a, undefined, { numeric: true })); + for (const version of versions) { + const cliCandidate = findNpmCliInDir(path.join(kindDir, version)); + if (cliCandidate) { + return { command: process.execPath, prefixArgs: [cliCandidate] }; + } + } + } catch { + // Ignore absent Volta image directories. + } + } + + const voltaRoot = path.resolve(imageDir, "..", ".."); + for (const exeCandidate of [ + path.join(voltaRoot, "bin", "npm.exe"), + path.join(voltaRoot, "npm.exe"), + ]) { + if (isRegularFile(exeCandidate)) { + return { command: exeCandidate, prefixArgs: [] }; + } + } + return undefined; +} + +/** Check one candidate directory `dir` for `npm-cli.js` or `npm.exe`. */ +function findWindowsNpmInDir(dir) { + const cliPath = findNpmCliInDir(dir); + if (cliPath) { + return { command: process.execPath, prefixArgs: [cliPath] }; + } + + const dirName = path.basename(dir).toLowerCase(); + const parentName = path.basename(path.dirname(dir)).toLowerCase(); + if (dirName === "bin") { + const chocoCli = findNpmCliInChocolateyLib(path.resolve(dir, "..", "lib")); + if (chocoCli) { + return { command: process.execPath, prefixArgs: [chocoCli] }; + } + } else if (dirName === "tools") { + const chocoCli = findNpmCliInChocolateyLib(path.resolve(dir, "..", "..")); + if (chocoCli) { + return { command: process.execPath, prefixArgs: [chocoCli] }; + } + } else if (parentName === "tools") { + const chocoCli = findNpmCliInChocolateyLib( + path.resolve(dir, "..", "..", ".."), + ); + if (chocoCli) { + return { command: process.execPath, prefixArgs: [chocoCli] }; + } + } + + const npmExePath = path.join(dir, "npm.exe"); + if ( + isRegularFile(npmExePath) && + path.resolve(npmExePath) !== path.resolve(process.execPath) + ) { + return { command: npmExePath, prefixArgs: [] }; + } + return undefined; +} + +/** Check a list of directories for `npm` and skip duplicates. */ +function searchWindowsNpmDirs(dirs, seen) { + for (const dir of dirs) { + const key = path.normalize(dir).toLowerCase(); + if (seen.has(key)) { + continue; + } + seen.add(key); + const resolved = findWindowsNpmInDir(dir); + if (resolved) { + return resolved; + } + } + return undefined; +} + +/** Locate `npm-cli.js` or `npm.exe` across MSI, Chocolatey, MSYS2 and Volta. */ +function resolveWindowsNpm() { + const nodeDirs = [path.dirname(process.execPath)]; + try { + const realNodeDir = path.dirname(fs.realpathSync(process.execPath)); + if (!nodeDirs.includes(realNodeDir)) { + nodeDirs.push(realNodeDir); + } + } catch { + // If realpath fails, keep the first directory. + } + + const rawPath = process.env.PATH ?? process.env.Path ?? ""; + const pathDelimiter = rawPath.includes(";") ? ";" : path.delimiter; + const pathDirs = rawPath + .split(pathDelimiter) + .map((entry) => entry.trim().replace(/^"+|"+$/g, "")) + .filter((entry) => entry.length > 0); + + const seen = new Set(); + const fromNodeOrPath = searchWindowsNpmDirs([...nodeDirs, ...pathDirs], seen); + if (fromNodeOrPath) { + return fromNodeOrPath; + } + + for (const nodeDir of nodeDirs) { + const voltaMatch = findNpmInVoltaImage(nodeDir); + if (voltaMatch) { + return voltaMatch; + } + } + + const envDirs = []; + if (process.env.VOLTA_HOME) { + envDirs.push(path.join(process.env.VOLTA_HOME, "bin")); + envDirs.push(process.env.VOLTA_HOME); + } + if (process.env.LOCALAPPDATA) { + envDirs.push(path.join(process.env.LOCALAPPDATA, "Volta", "bin")); + } + if (process.env.ChocolateyInstall) { + envDirs.push(path.join(process.env.ChocolateyInstall, "bin")); + } + if (process.env.ProgramData) { + envDirs.push(path.join(process.env.ProgramData, "chocolatey", "bin")); + } + if (process.env.APPDATA) { + envDirs.push(path.join(process.env.APPDATA, "npm")); + } + if (process.env.ProgramFiles) { + envDirs.push(path.join(process.env.ProgramFiles, "Volta")); + envDirs.push(path.join(process.env.ProgramFiles, "nodejs")); + } + + const fromEnvDirs = searchWindowsNpmDirs(envDirs, seen); + if (fromEnvDirs) { + return fromEnvDirs; + } + + for (const chocoRoot of [ + process.env.ChocolateyInstall, + process.env.ProgramData + ? path.join(process.env.ProgramData, "chocolatey") + : undefined, + ]) { + if (chocoRoot) { + const chocoCli = findNpmCliInChocolateyLib(path.join(chocoRoot, "lib")); + if (chocoCli) { + return { command: process.execPath, prefixArgs: [chocoCli] }; + } + } + } + + throw new Error("Cannot find npm-cli.js or npm.exe on Windows."); +} + /** Run `npm pack` without a shell and return the relative archive path. */ -function packArchive(packDir) { +function packArchive(packDir, { ignoreScripts }) { + if (ignoreScripts) { + verifyPrebuiltFiles(); + } + + const packArgs = ignoreScripts + ? ["pack", "--ignore-scripts", "--pack-destination", packDir] + : ["pack", "--pack-destination", packDir]; + const npmExecPath = process.env.npm_execpath; if (npmExecPath) { - execFileSync( - process.execPath, - [npmExecPath, "pack", "--pack-destination", packDir], - { cwd: repoRoot, stdio: "inherit" }, - ); - } else if (process.platform === "win32") { - const npmCliPath = path.join( - path.dirname(process.execPath), - "node_modules", - "npm", - "bin", - "npm-cli.js", - ); - if (!fs.existsSync(npmCliPath)) { - throw new Error(`npm-cli.js was not found at ${npmCliPath}`); + if (npmExecPath.toLowerCase().endsWith(".exe")) { + execFileSync(npmExecPath, packArgs, { cwd: repoRoot, stdio: "inherit" }); + } else { + execFileSync(process.execPath, [npmExecPath, ...packArgs], { + cwd: repoRoot, + stdio: "inherit", + }); } - execFileSync( - process.execPath, - [npmCliPath, "pack", "--pack-destination", packDir], - { cwd: repoRoot, stdio: "inherit" }, - ); + } else if (process.platform === "win32") { + const { command, prefixArgs } = resolveWindowsNpm(); + execFileSync(command, [...prefixArgs, ...packArgs], { + cwd: repoRoot, + stdio: "inherit", + }); } else { - execFileSync("npm", ["pack", "--pack-destination", packDir], { + execFileSync("npm", packArgs, { cwd: repoRoot, stdio: "inherit", }); @@ -257,6 +519,7 @@ function verifyTypeDeclarations(consumerDir, packageName) { ); } +const packOptions = parsePackOptions(process.argv.slice(2)); const packageName = readPackageName(); const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "tunelet-verify-pack-")); @@ -266,7 +529,7 @@ try { fs.mkdirSync(packDir); fs.mkdirSync(consumerDir); - const relativeArchivePath = packArchive(packDir); + const relativeArchivePath = packArchive(packDir, packOptions); verifyArchiveEntries(tempDir, relativeArchivePath); installArchive(tempDir, relativeArchivePath, consumerDir, packageName); verifyRuntimeImport(consumerDir, packageName);