-
Notifications
You must be signed in to change notification settings - Fork 5k
ast: make cargo test -p bun_ast link natively #37611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/2099cfb5/bun-ast-native-cargo-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44c7e6e
ast: make cargo test -p bun_ast link natively
robobun d607aec
ast: point the test shim doc comment at rust-ast-cargo-test.test.ts
robobun 9ba4b94
ast: shorten the test shim comments
robobun b8007c4
ast: trim the test shim module comment to the precedent's shape
robobun 60444cb
ast: assert on the tape test group instead of one test name
robobun 48eb7a2
ast: one-line comment on the shim helper
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| //! mimalloc symbols the `TapeAlloc::Arena` arm makes this crate's `cargo test` | ||
| //! binary reference; mimalloc is only linked into bun. Never compiled into the | ||
| //! real build. | ||
|
|
||
| use core::ffi::c_void; | ||
|
|
||
| use bun_alloc::mimalloc::Heap; | ||
|
|
||
| /// Nothing here references `mi_heap_new` or `mi_heap_main`, so no arena exists to get here. | ||
| fn no_mimalloc_in_test_binary(symbol: &str) -> ! { | ||
| unreachable!("{symbol} called, but bun_ast's test binary does not link mimalloc") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_heap_malloc(_heap: *mut Heap, _size: usize) -> *mut c_void { | ||
| no_mimalloc_in_test_binary("mi_heap_malloc") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_heap_malloc_aligned( | ||
| _heap: *mut Heap, | ||
| _size: usize, | ||
| _alignment: usize, | ||
| ) -> *mut c_void { | ||
| no_mimalloc_in_test_binary("mi_heap_malloc_aligned") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_malloc_usable_size(_p: *const c_void) -> usize { | ||
| no_mimalloc_in_test_binary("mi_malloc_usable_size") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_is_in_heap_region(_p: *const c_void) -> bool { | ||
| no_mimalloc_in_test_binary("mi_is_in_heap_region") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_free(_p: *mut c_void) { | ||
| no_mimalloc_in_test_binary("mi_free") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_free_size(_p: *mut c_void, _size: usize) { | ||
| no_mimalloc_in_test_binary("mi_free_size") | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| extern "C" fn mi_free_size_aligned(_p: *mut c_void, _size: usize, _alignment: usize) { | ||
| no_mimalloc_in_test_binary("mi_free_size_aligned") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // The Miri lane (scripts/rust-miri.ts) runs bun_ast's tests without linking, | ||
| // so only a host `cargo test -p bun_ast` notices when the test binary | ||
| // references symbols that exist only in the full bun link. The tape tests keep | ||
| // the TapeAlloc::Arena arm live, and src/ast/native_test_shims.rs defines the | ||
| // mimalloc symbols it reaches; without it: | ||
| // ld.lld: error: undefined symbol: mi_heap_malloc | ||
| // The set differs per profile (bun_alloc's mi_free_checked uses mi_free_size* | ||
| // under debug_assertions and mi_free otherwise), hence both rows; the tests | ||
| // are run, not just linked, because the shims abort if anything reaches them. | ||
| // | ||
| // Unix only: this relies on the linker reporting only references from live | ||
| // code (lld --gc-sections). link.exe reports dead ones too, so a bun_core | ||
| // dependent's test binary there either fails on ~100 unrelated externs or has | ||
| // them forced through; neither says anything about these shims. Remaining | ||
| // prerequisites as in rust-windows-sys-link.test.ts: cargo, and a configured | ||
| // checkout (vendor/lolhtml, build_options.rs), which test-only CI lanes lack. | ||
| import { which } from "bun"; | ||
| import { expect, test } from "bun:test"; | ||
| import { isWindows } from "harness"; | ||
| import { existsSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const cargo = which("cargo"); | ||
| const repoRoot = join(import.meta.dir, "..", ".."); | ||
| const workspaceResolvable = | ||
| existsSync(join(repoRoot, "vendor", "lolhtml", "Cargo.toml")) && | ||
| existsSync(join(repoRoot, "build", "debug", "codegen", "build_options.rs")); | ||
|
|
||
| test.skipIf(isWindows || !cargo || !workspaceResolvable).each([ | ||
| ["dev", []], | ||
| ["release", ["--release"]], | ||
| ] as const)( | ||
| "cargo test -p bun_ast links and passes without the rest of bun (%s profile)", | ||
| async (_profile, profileArgs) => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [cargo!, "test", "--locked", "-p", "bun_ast", "--lib", ...profileArgs], | ||
| cwd: repoRoot, | ||
| env: { ...process.env, CARGO_TERM_COLOR: "never" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).not.toContain("undefined symbol"); | ||
| // The tape tests are what make the arena arm live; they must have run here, natively. | ||
| expect(stdout).toMatch(/^test e::json_tape_tests::\w+ \.\.\. ok$/m); | ||
| expect(stdout).toContain("test result: ok."); | ||
| expect({ stdout, stderr, exitCode }).toMatchObject({ exitCode: 0 }); | ||
| }, | ||
| // Cold target dir: compiles bun_core and ~30 other crates first, and the | ||
| // release row ends in a fat-LTO link. | ||
| 180_000, | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.