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
5 changes: 3 additions & 2 deletions .github/workflows/core-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ jobs:
id: select
with:
script: |
const { selectChecks } = require('./tools/scripts/ci-change-scope.cjs');
const { selectChecks, readWorkspace, publishedBinaries } = require('./tools/scripts/ci-change-scope.cjs');
let publication = true;
try {
if (context.eventName === 'pull_request') {
const files = await github.paginate(github.rest.pulls.listFiles, {
...context.repo, pull_number: context.issue.number, per_page: 100
});
const evidence = { eventName: context.eventName,
complete: files.length === context.payload.pull_request.changed_files && files.length < 3000 };
complete: files.length === context.payload.pull_request.changed_files && files.length < 3000,
workspace: readWorkspace('.'), binaries: publishedBinaries('.') };
const path = '.github/workflows/core-binaries.yml';
if (files.some(f => f.filename === path || f.previous_filename === path)) {
const YAML = require(`${process.env.RUNNER_TEMP}/publication-yaml/node_modules/yaml`);
Expand Down
80 changes: 76 additions & 4 deletions tools/scripts/ci-change-scope.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,82 @@ function publicationConfig(value) {
return copy;
}

function selectPublication(paths, evidence) {
// The binaries the publication builds, read from the publisher so the list is written once: every
// `--bin <name>` in core-binaries.yml, and every crate it builds by a literal `--manifest-path`.
function publishedBinaries(root = '.') {
const text = fs.readFileSync(path.join(root, publisherPath), 'utf8');
const bins = [...new Set([...text.matchAll(/--bin\s+([A-Za-z0-9_-]+)/g)].map(([, name]) => name))];
const manifests = [...new Set([...text.matchAll(/--manifest-path\s+"?([A-Za-z0-9_./-]+)\/Cargo\.toml/g)].map(([, dir]) => dir))];
return { bins, manifests };
}

// Every workspace crate a published binary compiles: the crates owning its bins (declared, or the
// implicit `src/bin/<name>.rs` / `src/bin/<name>/main.rs`) and the crates the publisher builds by
// manifest, then everything they depend on by path. `null` when a binary's owner cannot be found:
// nothing narrower can be proven then.
function binaryClosure(workspace, binaries, root = '.') {
if (!workspace || !Array.isArray(workspace.crates) || !binaries) return null;
const exists = file => fs.existsSync(path.join(root, file));
const owns = (c, name) => (c.bins ?? []).some(b => b.name === name)
|| exists(`${c.dir}/src/bin/${name}.rs`) || exists(`${c.dir}/src/bin/${name}/main.rs`);
const roots = new Set(binaries.manifests);
for (const name of binaries.bins) {
const owner = workspace.crates.find(c => owns(c, name));
if (!owner) return null;
roots.add(owner.dir);
}
const byDir = new Map(workspace.crates.map(c => [c.dir, c]));
const closure = new Set();
const visit = dir => {
if (closure.has(dir)) return true;
const crate = byDir.get(dir);
if (!crate) return false;
closure.add(dir);
return crate.deps.every(visit);
};
return [...roots].every(visit) ? closure : null;
}

// Whether a manifest or lock change only adds workspace members the binaries never compile, read
// from the PR's own patch: the root Cargo.toml adds member lines and nothing else; Cargo.lock adds
// whole `[[package]]` blocks of those members (no `source`, so no new external crate) and changes
// nothing else. A missing patch (too large for the API) proves nothing.
function onlyAddsMembers(file, inert) {
if (typeof file.patch !== 'string') return false;
const lines = file.patch.split('\n').filter(l => !l.startsWith('@@') && !l.startsWith('\\'));
if (lines.some(l => l.startsWith('-'))) return false;
const added = lines.filter(l => l.startsWith('+')).map(l => l.slice(1).trim()).filter(Boolean);
if (file.filename === 'Cargo.toml') {
return added.length > 0 && added.every(l => inert.has(l.match(/^"([^"]+)",?$/)?.[1]));
}
const names = added.map(l => l.match(/^name = "([^"]+)"$/)?.[1]).filter(Boolean);
return names.length > 0
&& names.every(name => inert.has(`name:${name}`))
&& added.filter(l => l === '[[package]]').length === names.length
&& !added.some(l => l.startsWith('source ') || l.startsWith('checksum '));
}

// A changed path that cannot alter a published binary: a member outside the binaries' closure, or a
// manifest/lock edit that only adds such members (#5727, #5729: each new leaf crate edited the CI
// scope test and paid for three platforms of binaries it cannot affect).
function inertToBinaries(files, evidence) {
const closure = binaryClosure(evidence.workspace, evidence.binaries, evidence.root);
if (!closure) return () => false;
const outside = evidence.workspace.crates.filter(c => !closure.has(c.dir));
const inert = new Set([...outside.map(c => c.dir), ...outside.map(c => `name:${c.name}`)]);
const byPath = new Map(files.map(f => [f.filename, f]));
return p => {
if (p === 'Cargo.toml' || p === 'Cargo.lock') return byPath.has(p) && onlyAddsMembers(byPath.get(p), inert);
if (global(p)) return false;
return outside.some(c => p.startsWith(`${c.dir}/`));
};
}

function selectPublication(paths, evidence, files = []) {
if (evidence.eventName !== 'pull_request' || evidence.complete !== true) return true;
const inert = inertToBinaries(files, evidence);
if (paths.some(p => p !== publisherPath && !docs(p) && !schedulingPaths.has(p)
&& p !== 'core/continuum-cli-lifecycle/src/prebuilt_artifact/tests.rs')) return true;
&& p !== 'core/continuum-cli-lifecycle/src/prebuilt_artifact/tests.rs' && !inert(p))) return true;
if (!paths.includes(publisherPath)) return false;
try {
return !isDeepStrictEqual(publicationConfig(evidence.publicationBefore), publicationConfig(evidence.publicationAfter));
Expand Down Expand Up @@ -216,9 +288,9 @@ function selectChecks(files, evidence = {}) {
const workspace = evidence.eventName === 'push' ? null : evidence.workspace;
const scope = !rust ? { crates: [], cli: false } : selectCrates(rustPaths, workspace);
const targets = selectTargets(rustPaths, workspace, scope.crates);
return { rust, installer: rust || paths.some(windows), publication: selectPublication(paths, evidence), ...scope, ...targets };
return { rust, installer: rust || paths.some(windows), publication: selectPublication(paths, evidence, files), ...scope, ...targets };
}
module.exports = { selectChecks, readWorkspace, readPolicy, deriveCrates, TESTED_CRATES, TEST_FEATURES, LIB_TESTS };
module.exports = { selectChecks, readWorkspace, readPolicy, deriveCrates, publishedBinaries, TESTED_CRATES, TEST_FEATURES, LIB_TESTS };

// Shell and Rust readers take the derived sets from here, as JSON: `warning-crates` (the warnings
// ratchet), `crate-sets` (source-hygiene's guard, which checks its own Rust derivation against it).
Expand Down
38 changes: 37 additions & 1 deletion tools/scripts/tests/ci-change-scope.test.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const { selectChecks, readWorkspace, readPolicy, deriveCrates, TESTED_CRATES, TEST_FEATURES, LIB_TESTS } = require('../ci-change-scope.cjs');
const { selectChecks, readWorkspace, readPolicy, deriveCrates, publishedBinaries, TESTED_CRATES, TEST_FEATURES, LIB_TESTS } = require('../ci-change-scope.cjs');
const ALL = { crates: TESTED_CRATES, cli: true };
const NONE = { crates: [], cli: false };
const select = (...paths) => {
Expand Down Expand Up @@ -85,6 +85,42 @@ test('missing or malformed publication source cannot prove a skip', () => {
assert.equal(selectChecks([{ filename: publisher, previous_filename: 12 }], evidence()).publication, true);
});

// A PR adding a crate no published binary compiles skips the three platforms of binaries
// (#5727, #5729: each new leaf crate edited this test, so the publisher ran, and its other paths
// forced the build). Only a provable addition skips: the closure is read from the publisher's own
// --bin list and the workspace graph; a patch that changes anything else builds.
const bins = { bins: ['server'], manifests: [] };
const graph = { crates: [
{ name: 'core', dir: 'core/core', deps: ['core/kernel'], bins: [{ name: 'server', path: 'src/main.rs' }] },
{ name: 'kernel', dir: 'core/kernel', deps: [], bins: [] },
{ name: 'leaf', dir: 'core/leaf', deps: ['core/kernel'], bins: [] },
] };
const addsMember = { filename: 'Cargo.toml', patch: '@@ -10,6 +10,7 @@\n "core/kernel",\n+ "core/leaf",\n "core/core",' };
const addsLock = { filename: 'Cargo.lock', patch: '@@ -1,3 +1,10 @@\n+[[package]]\n+name = "leaf"\n+version = "0.1.0"\n+dependencies = [\n+ "kernel",\n+]\n+\n [[package]]' };
const leafPR = [{ filename: 'core/leaf/src/lib.rs' }, { filename: 'core/leaf/Cargo.toml' }, addsMember, addsLock,
{ filename: 'tools/scripts/tests/ci-change-scope.test.cjs' }];
const proof = { eventName: 'pull_request', complete: true, workspace: graph, binaries: bins };

test('a new crate no published binary compiles skips the binaries, and only a provable one', () => {
assert.equal(selectChecks(leafPR, proof).publication, false, 'a leaf crate, its member line and its lock block');
const builds = (files, why) => assert.equal(selectChecks(files, proof).publication, true, why);
builds([...leafPR, { filename: 'core/kernel/src/lib.rs' }], 'a crate the binary compiles');
builds([{ filename: 'core/leaf/src/lib.rs' }, { ...addsLock, patch: addsLock.patch + '\n+source = "registry+https://github.com/rust-lang/crates.io-index"' }], 'a new external crate in the lock');
builds([{ filename: 'core/leaf/src/lib.rs' }, { ...addsLock, patch: addsLock.patch + '\n-version = "1.0.0"' }], 'a lock line changed');
builds([{ filename: 'core/leaf/src/lib.rs' }, { ...addsMember, patch: addsMember.patch + '\n+[profile.release]' }], 'a manifest change beyond member lines');
builds([{ filename: 'core/leaf/src/lib.rs' }, { filename: 'Cargo.lock' }], 'a lock diff too large to carry its patch');
assert.equal(selectChecks(leafPR, { ...proof, binaries: { bins: ['missing'], manifests: [] } }).publication, true, 'a binary whose owner is unknown');
assert.equal(selectChecks(leafPR, { ...proof, eventName: 'workflow_dispatch' }).publication, true, 'outside a pull request');
});

test('the real publisher names binaries every one of which has an owner in the workspace', () => {
const real = publishedBinaries(repo);
assert.ok(real.bins.includes('continuum-core-server') && real.manifests.includes('core/livekit-bridge'), JSON.stringify(real));
const files = [{ filename: 'core/continuum-core/src/lib.rs' }];
const realProof = { eventName: 'pull_request', complete: true, workspace: readWorkspace(repo), binaries: real, root: repo };
assert.equal(selectChecks(files, realProof).publication, true, 'the core itself is in the closure');
});

// The crate scope reads the real workspace, so a new member or dependency edge is covered
// the moment it lands in a Cargo.toml: nothing here lists who depends on whom.
const repo = path.resolve(__dirname, '../../..');
Expand Down
Loading