ci: pin the toolchain in rust-toolchain.toml

The file is the one source: the jobs install what it names through
craft-common-actions' rust-channel action, and the nightly run opens a pull
request that changes only the file when a newer stable exists.

Part of #15711
This commit is contained in:
aiongg committed 2026-11-20 04:14:10 +00:00
1 parent 629c0ffb75
commit 5612b2dfd4
7 files changed
+201 -58

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
"""Moves rust-toolchain.toml to the newest stable Rust release.
python3 scripts/ci-toolchain-bump.py prints the newest stable release when it is newer than the file's
python3 scripts/ci-toolchain-bump.py --write V sets the file's channel to V
CI's nightly run (ci.yml `toolchain-bump`) runs both and opens a pull request that changes only
rust-toolchain.toml. Prints nothing when the file is current.
"""
import re
import sys
import tomllib
import urllib.request
from pathlib import Path
FILE = Path(__file__).resolve().parent.parent / 'rust-toolchain.toml'
MANIFEST = 'https://static.rust-lang.org/dist/channel-rust-stable.toml'
CHANNEL = re.compile(r'^(channel\s*=\s*")([^"]*)(")', re.M)
RELEASE = re.compile(r'^\d+\.\d+\.\d+$')
def numbers(version):
return tuple(int(part) for part in version.split('.'))
def current(text):
return tomllib.loads(text)['toolchain']['channel']
def latest(manifest_text):
"""`1.101.0` from the stable manifest's `[pkg.rust] version = "1.101.0 (<hash> <date>)"`."""
version = tomllib.loads(manifest_text)['pkg']['rust']['version'].split()[0]
if not RELEASE.match(version):
raise ValueError(f'unexpected stable version {version!r}')
return version
def newer(file_text, manifest_text):
"""The stable release when it is newer than the file's channel (a `1.x.y` release), else None."""
pinned = current(file_text)
stable = latest(manifest_text)
if RELEASE.match(pinned) and numbers(stable) <= numbers(pinned):
return None
return stable
def with_channel(file_text, version):
if not RELEASE.match(version):
raise ValueError(f'not a release: {version!r}')
return CHANNEL.sub(lambda m: m.group(1) + version + m.group(3), file_text, count=1)
def main(argv):
if argv[:1] == ['--write']:
FILE.write_text(with_channel(FILE.read_text(), argv[1]))
return
with urllib.request.urlopen(MANIFEST, timeout=30) as response:
stable = newer(FILE.read_text(), response.read().decode())
if stable:
print(stable)
if __name__ == '__main__':
main(sys.argv[1:])
+29
View File
@@ -0,0 +1,29 @@
from importlib.machinery import SourceFileLoader
from pathlib import Path
import unittest
bump = SourceFileLoader('bump', str(Path(__file__).with_name('ci-toolchain-bump.py'))).load_module()
FILE = '# note\n[toolchain]\nchannel = "1.100.0"\ncomponents = ["clippy"]\n'
def manifest(version):
return f'[pkg.rust]\nversion = "{version} (0ef1bc386 2026-11-09)"\n'
class TestBump(unittest.TestCase):
def test_a_newer_stable_is_reported_and_the_same_or_older_is_not(self):
self.assertEqual(bump.newer(FILE, manifest('1.101.0')), '1.101.0')
self.assertEqual(bump.newer(FILE, manifest('1.110.0')), '1.110.0')
self.assertIsNone(bump.newer(FILE, manifest('1.100.0')))
self.assertIsNone(bump.newer(FILE, manifest('1.99.2')))
def test_a_pin_that_is_not_a_release_moves_to_stable(self):
self.assertEqual(bump.newer(FILE.replace('1.100.0', 'stable'), manifest('1.100.0')), '1.100.0')
def test_writing_changes_only_the_channel(self):
self.assertEqual(bump.with_channel(FILE, '1.101.0'), FILE.replace('1.100.0', '1.101.0'))
if __name__ == '__main__':
unittest.main()
-32
View File
@@ -1,32 +0,0 @@
"""ci.yml and perf-nightly.yml build every job with one Rust release: TOOLCHAIN_PIN, and the gate calls' `toolchain`."""
from pathlib import Path
import re
import unittest
WORKFLOWS = Path(__file__).resolve().parent.parent / '.github' / 'workflows'
CI = WORKFLOWS / 'ci.yml'
PERF = WORKFLOWS / 'perf-nightly.yml'
class TestToolchainPin(unittest.TestCase):
def setUp(self):
self.text = CI.read_text()
def test_the_gate_calls_use_the_pinned_release(self):
pin = re.search(r'^ TOOLCHAIN_PIN: "([^"]+)"$', self.text, re.M)
self.assertIsNotNone(pin, 'ci.yml sets no TOOLCHAIN_PIN')
calls = re.findall(r'^ toolchain: "([^"]+)"$', self.text, re.M)
self.assertEqual(calls, [pin.group(1)] * 2, 'the Linux and Windows gate calls must name the pin')
def test_perf_nightly_uses_the_same_release(self):
pins = [re.search(r'^ TOOLCHAIN_PIN: "([^"]+)"$', t, re.M) for t in (self.text, PERF.read_text())]
self.assertTrue(all(pins), 'a workflow sets no TOOLCHAIN_PIN')
self.assertEqual(pins[0].group(1), pins[1].group(1))
def test_no_job_installs_a_floating_stable(self):
self.assertNotIn('toolchain: stable', self.text)
self.assertNotIn('toolchain: stable', PERF.read_text())
if __name__ == '__main__':
unittest.main()