From 2c9f46f5740eeed6786d81c2a9ec7e93ecea70d3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 10:18:07 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/m1/ffi-packaging/api-checks.md Quest-Claim: d65eeeaeee58a970 From ff5bd01dce924ff993af6c207e6509f8c7c07d10 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 11:31:03 -0800 Subject: [PATCH 2/5] fix(ffi): the bindings test runs the api checks with their generated transcript `just ffi transcripts ` writes the api fixture's Python, C++, and web checks, each as check. beside the script tests/fixtures/transcript generates. bindings-test.yml uploads them as an artifact, which the builder unpacks over its checkout through the new `checks-artifact` input, and `python-check`, `c-check`, and `web-check` may name a directory holding the check beside the files it reads. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/bindings-test.yml | 28 +++++++++++++-- .github/workflows/bindings.yml | 51 +++++++++++++++++++-------- ffi/justfile | 3 ++ ffi/release/README.md | 8 ++++- ffi/release/package.py | 52 +++++++++++++++++++--------- ffi/release/test_package.py | 33 ++++++++++++++++++ ffi/tests/fixtures.rs | 44 +++++++++++++++++++++++ quest/m1/ffi-packaging/api-checks.md | 28 --------------- 8 files changed, 185 insertions(+), 62 deletions(-) delete mode 100644 quest/m1/ffi-packaging/api-checks.md diff --git a/.github/workflows/bindings-test.yml b/.github/workflows/bindings-test.yml index ef438281e..fc48842fa 100644 --- a/.github/workflows/bindings-test.yml +++ b/.github/workflows/bindings-test.yml @@ -10,21 +10,43 @@ permissions: contents: read jobs: + # The api fixture's checks, each beside the script generated from its transcript, which the + # tree does not hold: the builder unpacks them over its checkout as api-checks/. + transcripts: + name: api fixture's checks + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2 + with: + tool: just + - run: just ffi transcripts generated/api-checks + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: api-checks + path: generated + if-no-files-found: error + api: name: api fixture + needs: transcripts uses: ./.github/workflows/bindings.yml with: manifest-path: ffi/fixtures/api/Cargo.toml features: doubled example: transcript - python-check: ffi/tests/api.py + checks-artifact: api-checks + python-check: api-checks/python # The small crate, so the second build costs little: moq's stand-in leaves it to its release. reproduce: true # api.cpp rather than api.c, whose broken cases name items only wasm32 has. - c-check: ffi/tests/api.cpp + c-check: api-checks/cpp web-manifest-path: ffi/fixtures/api/Cargo.toml scope: kixel-ai - web-check: ffi/tests/api.ts + web-check: api-checks/web # A module path a proxy could serve. No go-check, as no swift-check: api.go needs the # wasm32 items this export lacks, so the consumer only loads the runtime. go-module: example.com/api diff --git a/.github/workflows/bindings.yml b/.github/workflows/bindings.yml index 9bafb13fd..a6c1f64f0 100644 --- a/.github/workflows/bindings.yml +++ b/.github/workflows/bindings.yml @@ -21,7 +21,7 @@ on: default: '' type: string python-check: - description: A program run on each installed wheel from a clean directory + description: A program run on each installed wheel from a clean directory, or a directory holding it as check.py beside the modules it imports default: '' type: string swift-check: @@ -33,7 +33,7 @@ on: default: false type: boolean c-check: - description: A C or C++ program built by clean CMake and pkg-config consumers of each C archive + description: A C or C++ program built by clean CMake and pkg-config consumers of each C archive, or a directory holding it as check.c or check.cpp beside the headers it includes default: '' type: string web-manifest-path: @@ -45,7 +45,7 @@ on: default: '' type: string web-check: - description: A TypeScript module run on the installed npm package, importing the binding as ./web/.ts does beside the export + description: A TypeScript module run on the installed npm package, importing the binding as ./web/.ts does beside the export, or a directory holding it as check.ts beside the modules it imports default: '' type: string web-features: @@ -64,6 +64,10 @@ on: description: The main.go of a program, importing the module's packages, run against it default: '' type: string + checks-artifact: + description: An artifact of the caller's run unpacked over its checkout before the checks are read, for checks it generates + default: '' + type: string outputs: version: description: The package's Cargo version @@ -363,6 +367,13 @@ jobs: python3 -I rust2/ffi/release/package.py sdist --manifest-path "source/$MANIFEST" --export export --out again/dist fi python3 -I rust2/ffi/release/package.py same dist again/dist + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + if: inputs.checks-artifact != '' + with: + name: ${{ inputs.checks-artifact }} + path: source + # A Python or C check lands as a directory holding it as check., whichever form the + # input takes: a file is copied into one, and a directory whole. - name: Checks env: PYTHON_CHECK: ${{ inputs.python-check }} @@ -371,10 +382,13 @@ jobs: GO_CHECK: ${{ inputs.go-check }} run: | mkdir -p dist/checks - if [[ -n "$PYTHON_CHECK" ]]; then cp "source/$PYTHON_CHECK" dist/checks/check.py; fi + stage() { + if [[ -d "source/$1" ]]; then cp -R "source/$1" "$2"; else mkdir "$2" && cp "source/$1" "$2/$3"; fi + } + if [[ -n "$PYTHON_CHECK" ]]; then stage "$PYTHON_CHECK" dist/checks/python check.py; fi if [[ -n "$SWIFT_CHECK" ]]; then cp "source/$SWIFT_CHECK" dist/checks/main.swift; fi - # Its extension says whether it is C or C++. - if [[ -n "$C_CHECK" ]]; then mkdir dist/checks/c && cp "source/$C_CHECK" dist/checks/c/; fi + # A file's extension says whether it is C or C++. + if [[ -n "$C_CHECK" ]]; then stage "$C_CHECK" dist/checks/c "check.${C_CHECK##*.}"; fi if [[ -n "$GO_CHECK" ]]; then cp "source/$GO_CHECK" dist/checks/main.go; fi - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: @@ -540,8 +554,8 @@ jobs: "$py" -m pip install --no-index --no-deps dist/wheels/*.whl args=() if [[ -f dist/native.txt ]]; then args+=(--native dist/native.txt); fi - if [[ -f dist/checks/check.py ]]; then - "$py" -I rust2/ffi/release/package.py consume-python --check dist/checks/check.py "${args[@]}" + if [[ -d dist/checks/python ]]; then + "$py" -I rust2/ffi/release/package.py consume-python --check dist/checks/python "${args[@]}" else "$py" -I rust2/ffi/release/package.py consume-python --import "$(cat dist/lib.txt)" fi @@ -605,8 +619,8 @@ jobs: tag=$(basename "${wheels[0]}" .whl | cut -d- -f3) [[ "$tag" =~ ^cp3([0-9]+)$ ]] || { echo "::error::no CPython floor in ${wheels[0]}"; exit 1; } echo "version=3.${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT" - args=(--check dist/checks/check.py) - if [[ ! -f dist/checks/check.py ]]; then args=(--import "$(cat dist/lib.txt)"); fi + args=(--check dist/checks/python) + if [[ ! -d dist/checks/python ]]; then args=(--import "$(cat dist/lib.txt)"); fi if [[ -f native/native.txt ]]; then args+=(--native native/native.txt); fi echo "args=${args[*]}" >> "$GITHUB_OUTPUT" # Alpine's own Python and rustup's Rust, whose host is the musl target the export names. @@ -722,8 +736,7 @@ jobs: [[ ${#archives[@]} == 1 ]] || { echo "::error::expected one C archive, found ${archives[*]}"; exit 1; } args=(--archive "${archives[0]}") if [[ -f dist/native.txt ]]; then args+=(--native dist/native.txt); fi - checks=(dist/checks/c/*) - if [[ ${#checks[@]} == 1 ]]; then args+=(--check "${checks[0]}"); fi + if [[ -d dist/checks/c ]]; then args+=(--check dist/checks/c); fi python -I rust2/ffi/release/package.py consume-c "${args[@]}" # The web binding's npm and JSR packages: web-manifest-path exported for wasm32-wasip1 @@ -753,6 +766,11 @@ jobs: if: needs.facts.outputs.web-fixture == 'true' working-directory: source run: just ffi standin "" web + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + if: inputs.checks-artifact != '' + with: + name: ${{ inputs.checks-artifact }} + path: source # npm packs, and Node's WASI runs the native example. - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: @@ -782,7 +800,12 @@ jobs: features=$WEB_FEATURES example=$WEB_EXAMPLE fi python3 -I rust2/ffi/release/package.py web --manifest-path "source/$WEB_MANIFEST" --features "$features" --scope "$SCOPE" --version "$VERSION" --example "$example" --out web - if [[ -n "$WEB_CHECK" ]]; then cp "source/$WEB_CHECK" web/check.ts; fi + # A directory whole, or a file copied into one as check.ts. + if [[ -n "$WEB_CHECK" && -d "source/$WEB_CHECK" ]]; then + cp -R "source/$WEB_CHECK" web/check + elif [[ -n "$WEB_CHECK" ]]; then + mkdir web/check && cp "source/$WEB_CHECK" web/check/check.ts + fi # The build again, from a new export, which must pack the same tarball. if [[ "$REPRODUCE" == true ]]; then python3 -I rust2/ffi/release/package.py web --manifest-path "source/$WEB_MANIFEST" --features "$features" --scope "$SCOPE" --version "$VERSION" --example "" --out web-again @@ -822,7 +845,7 @@ jobs: - name: Install and run run: | args=() - if [[ -f web/check.ts ]]; then args+=(--check web/check.ts); fi + if [[ -d web/check ]]; then args+=(--check web/check); fi if [[ -f web/native.txt ]]; then args+=(--native web/native.txt); fi python3 -I rust2/ffi/release/package.py consume-web --dist web "${args[@]}" diff --git a/ffi/justfile b/ffi/justfile index 59c13263c..9122bed84 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -53,6 +53,9 @@ corpus-update: protocol: cargo test --locked --test fixtures protocol_nightly -- --ignored +# Write the api fixture's Python, C++, and web checks, each beside the script tests/fixtures/transcript generates, into /python, /cpp, and /web, the directories the release builder's check inputs take. +transcripts dir: + RUST2_TRANSCRIPTS="$(cd {{ quote(invocation_directory()) }} && mkdir -p {{ quote(dir) }} && cd {{ quote(dir) }} && pwd)" cargo test --locked --test fixtures -- --ignored --exact transcripts # Run the api fixture's web test in headless Chromium, Firefox, and on Linux WebKit, in the bench shell, which carries them: nix develop .#bench. browsers: cargo test --locked --test fixtures -- --ignored --exact api_browsers diff --git a/ffi/release/README.md b/ffi/release/README.md index 137e2e1ee..c8255e13a 100644 --- a/ffi/release/README.md +++ b/ffi/release/README.md @@ -64,6 +64,11 @@ TypeScript module importing the binding as `./web/.ts` does beside an ex whose imports the consumer rewrites to the package's name. Its crate's version must be the release's. `features` and `example` apply to it when it is the crate's own; a web crate apart from it takes `web-features` and `web-example` instead. +A `python-check`, `c-check`, or `web-check` may instead name a directory holding the program +as `check.py`, `check.c` or `check.cpp`, or `check.ts`, beside the files it reads, which each +consumer places beside it: a check that imports a module or includes a header of its own. +A check its tests generate comes through `checks-artifact`, an artifact of the caller's run +the builder unpacks over its checkout before reading the checks. ## What the builder does @@ -193,7 +198,8 @@ crate apart from it takes `web-features` and `web-example` instead. rust2 runs it on every manual Check and nightly through [`bindings-test.yml`](/.github/workflows/bindings-test.yml): on the api fixture, whose -wheels, C++ consumers, and web package must print its native transcript, and on moq's +wheels, C++ consumers, and web package must print its native transcript through the checks +`just ffi transcripts` writes, each test beside the script generated from it, and on moq's stand-in facade at the exporter's moq pin, through `moq/moq.py`, `moq/moq.swift`, `moq/moq.cpp`, and `moq/moq.go`, with its wasm32 stand-in as the web package. diff --git a/ffi/release/package.py b/ffi/release/package.py index a7470dbbc..4dc62cf76 100644 --- a/ffi/release/package.py +++ b/ffi/release/package.py @@ -1089,12 +1089,20 @@ def owned(member): return path +def check_program_in(check, names): + """The program of a check directory: the one file of `names` it holds, beside the files it reads.""" + found = [Path(check) / name for name in names if (Path(check) / name).is_file()] + if len(found) != 1: + raise SystemExit(f"{check} must hold exactly one of {', '.join(names)}, beside the files it reads") + return found[0] + + def c_check(lib, check, work): - """The consumer's program: the caller's check, or else one that links a function of each header.""" + """The consumer's program: the caller's check, its directory copied whole so that its + quoted includes resolve beside it, or else one that links a function of each header.""" if check: - source = work / Path(check).name - shutil.copy(check, source) - return source + shutil.copytree(check, work / "check") + return check_program_in(work / "check", ("check.c", "check.cpp", "check.cc", "check.cxx")) source = work / "smoke.cpp" source.write_text(f'#include "{lib}.h"\n#include "{lib}.hpp"\n\n' f"int main() {{\n\tvoid (*volatile fn)({lib}_buf) = {lib}_buf_free;\n" @@ -1212,10 +1220,16 @@ def compare(native, actual, what): def consume_python(check, native, module): - """Run a check program, or else an import, on the installed wheel from an empty directory.""" - program = [str(Path(check).resolve())] if check else ["-c", f"import {module}"] - with tempfile.TemporaryDirectory() as clean: - result = subprocess.run([sys.executable, "-I", "-X", "dev", "-W", "error", *program], + """Run a check program, or else an import, on the installed wheel from an empty directory. + The check's directory is copied apart, and it alone joins the path, where the check's own + modules are found: `-E -s` is `-I` but for that.""" + with tempfile.TemporaryDirectory() as clean, tempfile.TemporaryDirectory() as scripts: + if check: + shutil.copytree(check, scripts, dirs_exist_ok=True) + program = [str(check_program_in(scripts, ("check.py",)))] + else: + program = ["-I", "-c", f"import {module}"] + result = subprocess.run([sys.executable, "-E", "-s", "-X", "dev", "-W", "error", *program], capture_output=True, cwd=clean) sys.stderr.buffer.write(result.stderr) if result.returncode: @@ -2018,7 +2032,7 @@ def web(args): def check_program(check, name): - """A check written beside an export, importing `./web/.ts`, as it imports the package.""" + """A check's module written beside an export, importing `./web/.ts`, as it imports the package.""" text = Path(check).read_text(encoding="utf-8") # Whitespace and comments, which may separate the keyword from its specifier. gap = r"(?:\s|/\*[\s\S]*?\*/|//[^\n]*\n)*" @@ -2041,8 +2055,12 @@ def consume_web(dist, check, native): if len(tarballs) != 1: raise SystemExit(f"{dist} holds {len(tarballs)} npm tarballs") name = json.loads((dist / "npm" / "package.json").read_text(encoding="utf-8"))["name"] - program = check_program(check, name) if check else ( - f"import * as binding from {json.dumps(name)};\n\nawait binding.load();\n") + # The check's modules, each importing the package; `check.ts` runs, importing the others by path. + if check: + check_program_in(check, ("check.ts",)) + modules = {p.name: check_program(p, name) for p in sorted(Path(check).glob("*.ts"))} + else: + modules = {"check.ts": f"import * as binding from {json.dumps(name)};\n\nawait binding.load();\n"} expected = Path(native).read_bytes() if native and check else None with tempfile.TemporaryDirectory() as clean: clean = Path(clean) @@ -2050,17 +2068,19 @@ def consume_web(dist, check, native): "devDependencies": BUNDLERS} (clean / "package.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8") run("npm", "install", "--no-audit", "--no-fund", "--ignore-scripts", cwd=clean, capture_output=True) - (clean / "check.ts").write_text(program, encoding="utf-8") + for module, program in modules.items(): + (clean / module).write_text(program, encoding="utf-8") # The bundles target the browser, whose `document` Vite's preload polyfill reads; # each runs on Node from where the bundler wrote it, a relative base reaching its module. (clean / "index.html").write_text('\n', encoding="utf-8") (clean / "vite.config.js").write_text( 'export default { base: "./", build: { target: "es2024", modulePreload: { polyfill: false } } };\n', encoding="utf-8") - run("node", "--no-warnings", "--input-type=module", "-e", - 'import { stripTypeScriptTypes } from "node:module"; import { readFileSync, writeFileSync } from "node:fs";' - ' writeFileSync("check.js", stripTypeScriptTypes(readFileSync("check.ts", "utf8")));', cwd=clean) + # webpack reads the check's TypeScript through Node's own type stripping, as Node runs it. + (clean / "strip-types.cjs").write_text( + 'module.exports = (source) => require("node:module").stripTypeScriptTypes(source);\n', encoding="utf-8") (clean / "webpack.config.js").write_text( - 'export default { mode: "production", entry: "./check.js", target: ["web", "es2024"],' + 'export default { mode: "production", entry: "./check.ts", target: ["web", "es2024"],' + r' module: { rules: [{ test: /\.ts$/, exclude: /node_modules/, loader: new URL("./strip-types.cjs", import.meta.url).pathname }] },' ' experiments: { outputModule: true },' ' output: { module: true, path: new URL("./webpack", import.meta.url).pathname, filename: "check.js" } };\n', encoding="utf-8") diff --git a/ffi/release/test_package.py b/ffi/release/test_package.py index 393f39a49..8f651a4d5 100644 --- a/ffi/release/test_package.py +++ b/ffi/release/test_package.py @@ -1007,6 +1007,39 @@ def test_check_imports_the_package(self): with self.assertRaises(SystemExit): package.check_program(check, "@moq/moq-ffi") + def test_check_directory_names_its_program(self): + with tempfile.TemporaryDirectory() as tmp: + (Path(tmp) / "transcript.ts").write_text("") + with self.assertRaises(SystemExit): + package.check_program_in(tmp, ("check.ts",)) + (Path(tmp) / "check.ts").write_text("") + self.assertEqual(package.check_program_in(tmp, ("check.ts",)), Path(tmp) / "check.ts") + (Path(tmp) / "check.c").write_text("") + (Path(tmp) / "check.cpp").write_text("") + with self.assertRaises(SystemExit): + package.check_program_in(tmp, ("check.c", "check.cpp")) + + def test_python_check_imports_its_modules_alone(self): + # The check's directory is on the path, and only it: not the working directory, and + # not PYTHONPATH. The generated script reaches the check's functions as `__main__`. + with tempfile.TemporaryDirectory() as tmp: + check, elsewhere = Path(tmp) / "check", Path(tmp) / "elsewhere" + check.mkdir() + elsewhere.mkdir() + (elsewhere / "stray.py").write_text("") + (check / "check.py").write_text( + "import importlib.util\n" + "def hook():\n return 'hooked'\n" + "assert importlib.util.find_spec('stray') is None\n" + "import transcript\ntranscript.script()\n") + (check / "transcript.py").write_text("import __main__ as local\ndef script():\n print(local.hook())\n") + (Path(tmp) / "native.txt").write_text("hooked\n") + with mock.patch.dict(os.environ, {"PYTHONPATH": str(elsewhere)}): + package.consume_python(check, Path(tmp) / "native.txt", None) + (Path(tmp) / "native.txt").write_text("other\n") + with self.assertRaises(SystemExit): + package.consume_python(check, Path(tmp) / "native.txt", None) + if __name__ == "__main__": unittest.main() diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 1243fa48c..94bef9e41 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -173,6 +173,50 @@ fn transcript() -> &'static transcript::Transcript { }) } +/// The api fixture's Python, C++, and web checks as the release builder takes them, into +/// `RUST2_TRANSCRIPTS`: a directory each, `python`, `cpp`, and `web`, holding the test as +/// `check.` beside its generated script. Only the export runs, not `api`'s builds. +#[test] +#[ignore = "writes outside the target: just ffi transcripts "] +fn transcripts() { + let dir = PathBuf::from(std::env::var_os("RUST2_TRANSCRIPTS").expect("RUST2_TRANSCRIPTS names the directory")); + let out = scratch().join("transcripts").join("out"); + // The export `api` makes, so each script is the one its test runs. + let output = export("api", &out, &[host(), WEB], &["--features", "doubled"]); + assert!( + output.status.success(), + "exporting failed:\n{}", + String::from_utf8_lossy(&output.stderr) + ); + let t = transcript::Transcript::load( + &out.join("api.json"), + &manifest_dir().join("fixtures/api/examples/transcript.rs"), + ); + let tests = manifest_dir().join("tests"); + // The web script imports the test's hooks by its name, which is `check.ts` here. + let typescript = transcript::typescript::emit(&t); + let hooks = "import * as hook from \"./api.ts\";"; + assert_eq!( + typescript.matches(hooks).count(), + 1, + "the web script imports the hooks once" + ); + let typescript = typescript.replace(hooks, "import * as hook from \"./check.ts\";"); + for (language, check, file, script) in [ + ("python", "api.py", "transcript.py", transcript::python::emit(&t)), + ("cpp", "api.cpp", "transcript.hpp", transcript::cpp::emit(&t)), + ("web", "api.ts", "transcript.ts", typescript), + ] { + let checks = dir.join(language); + if checks.exists() { + std::fs::remove_dir_all(&checks).unwrap(); + } + let ext = Path::new(check).extension().unwrap(); + step::copy(&tests.join(check), &checks.join("check").with_extension(ext)); + step::write(&checks.join(file), script); + } +} + /// `contents` written as `file` in a scratch directory of `language`'s, which the /// language's test puts on its path. fn generated(language: &str, file: &str, contents: &str) -> PathBuf { diff --git a/quest/m1/ffi-packaging/api-checks.md b/quest/m1/ffi-packaging/api-checks.md deleted file mode 100644 index 157c23222..000000000 --- a/quest/m1/ffi-packaging/api-checks.md +++ /dev/null @@ -1,28 +0,0 @@ -# [S] The bindings test's api checks with their generated transcript - -## Goal - -`bindings-test.yml` runs the api fixture's `ffi/tests/api.py`, `api.cpp`, and `api.ts` on -the released wheels, C archives, and npm package again, each with the script `tests/fixtures/transcript` -generates from `fixtures/api/examples/transcript.rs`, and each prints native Rust's transcript. - -## Plan - -Decided with the maintainer on 2026-11-19, in the pick of [#909](https://github.com/kixel-ai/rust2/pull/909): ✅ kept, with a `just ffi transcripts ` recipe and check inputs that may name a directory (rejected: committing the generated scripts; dropping the checks to imports; dropping the quest). - -Found by [the Python packaging quest's PR](https://github.com/kixel-ai/rust2/pull/909): since -[#885](https://github.com/kixel-ai/rust2/pull/885), `api.py` imports a generated `transcript` -module, `api.cpp` includes `transcript.hpp`, and `api.ts` imports `./transcript.ts`, none of -which the builder's single-file `python-check`, `c-check`, and `web-check` carry, so the api -fixture's consumers fail from the first nightly after it. moq's stand-in checks import no -generated script and still pass. - -Recommended: a `just ffi transcripts ` recipe writes each language's generated script, -which `bindings-test.yml` runs before calling the builder, and each check input may name a -directory, whose other files every consumer places beside the check: on Python's path -(the consumer runs `-I`, which keeps the script's directory off it), in the C consumer's -include path, and beside the npm consumer's module. Rejected: committing the generated -scripts, which `#885` keeps out of the tree, and dropping the fixture's checks to imports, -which loses the byte-for-byte transcript. - -Public API: the builder's check inputs, if they take a directory. From cc6b8d471215306f265c01eed0e23178b7e81ddf Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 12:28:21 -0800 Subject: [PATCH 3/5] fix(ffi): the sdist moves the generated crate's relative paths Since #917 the generated manifest names each local package relative to the export, which the sdist's relocation, keyed by absolute paths, refused. Co-Authored-By: Claude Opus 5.5 --- ffi/release/package.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ffi/release/package.py b/ffi/release/package.py index 4dc62cf76..7e0672d69 100644 --- a/ffi/release/package.py +++ b/ffi/release/package.py @@ -423,7 +423,11 @@ def moves(at): for root in roots: at = place(root.parent) files[f"{at}/Cargo.toml"] = (relocate(workspace_manifest(root), moves(at)).encode(), False) - generated = relocate((export / "Cargo.toml").read_text(encoding="utf-8"), moves("binding"), strict=True) + # The generated manifest names each package relative to the export, where a relative path + # reads right, and absolute elsewhere. + binding = moves("binding") + binding |= {Path(os.path.relpath(d, export)).as_posix(): binding[str(d)] for d in placed} + generated = relocate((export / "Cargo.toml").read_text(encoding="utf-8"), binding, strict=True) files["binding/Cargo.toml"] = (generated.encode(), False) for path in [export / "Cargo.lock", export / "build.rs", export / "python" / f"{info['lib']}.py", *sorted(p for p in (export / "src").rglob("*") if p.is_file())]: From 019f89c3523ed43a1b4fd04f781730397c0b2094 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 12:33:15 -0800 Subject: [PATCH 4/5] fix(ffi): the web consumer carries a check directory whole Co-Authored-By: Claude Opus 5.5 --- ffi/release/package.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ffi/release/package.py b/ffi/release/package.py index 7e0672d69..6e360770e 100644 --- a/ffi/release/package.py +++ b/ffi/release/package.py @@ -2059,21 +2059,25 @@ def consume_web(dist, check, native): if len(tarballs) != 1: raise SystemExit(f"{dist} holds {len(tarballs)} npm tarballs") name = json.loads((dist / "npm" / "package.json").read_text(encoding="utf-8"))["name"] - # The check's modules, each importing the package; `check.ts` runs, importing the others by path. if check: check_program_in(check, ("check.ts",)) - modules = {p.name: check_program(p, name) for p in sorted(Path(check).glob("*.ts"))} - else: - modules = {"check.ts": f"import * as binding from {json.dumps(name)};\n\nawait binding.load();\n"} expected = Path(native).read_bytes() if native and check else None with tempfile.TemporaryDirectory() as clean: clean = Path(clean) + # The check's directory whole, each TypeScript module in it importing the package; + # `check.ts` runs, reaching the rest by path. The project's own files then replace any + # it holds of the same names. + if check: + shutil.copytree(check, clean, dirs_exist_ok=True) + for module in clean.rglob("*.ts"): + module.write_text(check_program(module, name), encoding="utf-8") + else: + (clean / "check.ts").write_text( + f"import * as binding from {json.dumps(name)};\n\nawait binding.load();\n", encoding="utf-8") manifest = {"type": "module", "private": True, "dependencies": {name: f"file:{tarballs[0]}"}, "devDependencies": BUNDLERS} (clean / "package.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8") run("npm", "install", "--no-audit", "--no-fund", "--ignore-scripts", cwd=clean, capture_output=True) - for module, program in modules.items(): - (clean / module).write_text(program, encoding="utf-8") # The bundles target the browser, whose `document` Vite's preload polyfill reads; # each runs on Node from where the bundler wrote it, a relative base reaching its module. (clean / "index.html").write_text('\n', encoding="utf-8") From 5c8142631f83bb5b0652e3bfcc3d0d436f797fc8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 18:16:47 -0800 Subject: [PATCH 5/5] fix(ffi): relocate each sdist path once, and skip a relative alias across Windows drives Co-Authored-By: Claude Opus 5.5 --- ffi/justfile | 2 +- ffi/release/package.py | 14 +++++++++++--- ffi/release/test_package.py | 12 ++++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ffi/justfile b/ffi/justfile index 9122bed84..e93bfafc8 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -55,7 +55,7 @@ protocol: # Write the api fixture's Python, C++, and web checks, each beside the script tests/fixtures/transcript generates, into /python, /cpp, and /web, the directories the release builder's check inputs take. transcripts dir: - RUST2_TRANSCRIPTS="$(cd {{ quote(invocation_directory()) }} && mkdir -p {{ quote(dir) }} && cd {{ quote(dir) }} && pwd)" cargo test --locked --test fixtures -- --ignored --exact transcripts + out="$(cd {{ quote(invocation_directory()) }} && mkdir -p {{ quote(dir) }} && cd {{ quote(dir) }} && pwd)" && RUST2_TRANSCRIPTS="$out" cargo test --locked --test fixtures -- --ignored --exact transcripts # Run the api fixture's web test in headless Chromium, Firefox, and on Linux WebKit, in the bench shell, which carries them: nix develop .#bench. browsers: cargo test --locked --test fixtures -- --ignored --exact api_browsers diff --git a/ffi/release/package.py b/ffi/release/package.py index 6e360770e..6d2d671bd 100644 --- a/ffi/release/package.py +++ b/ffi/release/package.py @@ -328,9 +328,11 @@ def workspace_manifest(path): def relocate(manifest, moves, strict=False): """A manifest with each local package's absolute path moved, as `moves` maps them, checked by reading it back: nothing else changed, and when `strict`, every `path` is one moved.""" + # One pass, so a path already moved is never matched again by another package's key. + spelled = {f"path = {json.dumps(old, ensure_ascii=False)}": f"path = {json.dumps(new)}" for old, new in moves.items()} text = manifest - for old, new in moves.items(): - text = text.replace(f"path = {json.dumps(old, ensure_ascii=False)}", f"path = {json.dumps(new)}") + if spelled: + text = re.sub("|".join(map(re.escape, spelled)), lambda m: spelled[m.group(0)], manifest) def moved(value): if isinstance(value, dict): @@ -426,7 +428,13 @@ def moves(at): # The generated manifest names each package relative to the export, where a relative path # reads right, and absolute elsewhere. binding = moves("binding") - binding |= {Path(os.path.relpath(d, export)).as_posix(): binding[str(d)] for d in placed} + for d in placed: + try: + binding[Path(os.path.relpath(d, export)).as_posix()] = binding[str(d)] + except ValueError: + # On another Windows drive than the export, no relative path reaches it; only the + # absolute one names it. + pass generated = relocate((export / "Cargo.toml").read_text(encoding="utf-8"), binding, strict=True) files["binding/Cargo.toml"] = (generated.encode(), False) for path in [export / "Cargo.lock", export / "build.rs", export / "python" / f"{info['lib']}.py", diff --git a/ffi/release/test_package.py b/ffi/release/test_package.py index 8f651a4d5..a5a760a34 100644 --- a/ffi/release/test_package.py +++ b/ffi/release/test_package.py @@ -748,6 +748,14 @@ def test_moves_each_local_path(self): self.assertEqual(moved["target"]["cfg(unix)"]["dependencies"]["tls"]["path"], "../sources/src/tls") self.assertEqual(moved["patch"]["crates-io"]["url"]["path"], "../sources/vendor/url") + def test_moves_each_path_once(self): + # One package's new path is another's old one, as relative keys can make: each path + # moves by its original value alone. + manifest = '[dependencies]\na = { path = "../a" }\nb = { path = "../sources/a" }\n' + moves = {"../a": "../sources/a", "../sources/a": "../sources/sources/a"} + moved = tomllib.loads(package.relocate(manifest, moves, strict=True)) + self.assertEqual(moved["dependencies"], {"a": {"path": "../sources/a"}, "b": {"path": "../sources/sources/a"}}) + def test_refuses_a_path_no_package_lies_at(self): manifest = '[dependencies]\na = { path = "/src/a" }\nb = { path = "/elsewhere/b" }\n' with self.assertRaises(SystemExit): @@ -1033,10 +1041,10 @@ def test_python_check_imports_its_modules_alone(self): "assert importlib.util.find_spec('stray') is None\n" "import transcript\ntranscript.script()\n") (check / "transcript.py").write_text("import __main__ as local\ndef script():\n print(local.hook())\n") - (Path(tmp) / "native.txt").write_text("hooked\n") + (Path(tmp) / "native.txt").write_bytes(b"hooked\n") with mock.patch.dict(os.environ, {"PYTHONPATH": str(elsewhere)}): package.consume_python(check, Path(tmp) / "native.txt", None) - (Path(tmp) / "native.txt").write_text("other\n") + (Path(tmp) / "native.txt").write_bytes(b"other\n") with self.assertRaises(SystemExit): package.consume_python(check, Path(tmp) / "native.txt", None)