fix(wasm): route clock reads through web-time so bashkit runs on wasm32-unknown-unknown#2140
Merged
Merged
Conversation
…32-unknown-unknown
std::time::{Instant, SystemTime} panic at runtime on wasm32-unknown-unknown
("time not implemented on this platform"): the target has no OS clock and
std provides no extension point. All wall-clock reads in the bashkit crate
now go through a new crate::time_compat module — a re-export of web-time
(Performance.now()/Date.now() via web-sys) on wasm32, and of std::time
everywhere else with zero overhead — plus two chrono bridge helpers, since
chrono only implements From<std::time::SystemTime>, not web-time's.
Guards so this can't silently rot like examples/browser did:
- CI job checking bashkit for wasm32-unknown-unknown with -D warnings
- source-scan test keeping std::time clock types out of src/ (escape
hatch: `// std-time-ok: <reason>`)
Also gates Interpreter::clear_cancelled_execution_state to non-wasm: its
only caller is the native-only tokio timeout recovery path, so it was
dead code (a warning) on wasm targets.
chaliy
reviewed
Jul 5, 2026
| match t.duration_since(UNIX_EPOCH) { | ||
| Ok(dur) => chrono::DateTime::from_timestamp(dur.as_secs() as i64, dur.subsec_nanos()) | ||
| .unwrap_or_else(epoch), | ||
| Err(e) => { |
Contributor
There was a problem hiding this comment.
This loses subsecond precision for timestamps before UNIX_EPOCH. For example, 1969-12-31T23:59:59.500Z should round-trip as 500ms before epoch, but this path converts using whole negative seconds and zero nanos. Please add focused tests for pre-epoch subsecond values and decompose negative timestamps so nanos are preserved in both to_chrono_utc and from_chrono.
Contributor
Author
There was a problem hiding this comment.
Thanks for quick review. Done!
…versions duration_since's Err branch dropped nanos when negating whole seconds, so times before UNIX_EPOCH (e.g. epoch - 500ms) collapsed to the wrong second in both to_chrono_utc and from_chrono. Decompose using chrono's floor+remainder convention instead, and add round-trip tests covering the pre-epoch subsecond case.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
std::time::{Instant, SystemTime}panic at runtime onwasm32-unknown-unknown("time not implemented on this platform"): the target has no OS clock, and std provides no extension point (library/std/src/sys/time/unsupported.rs). Any script touching time —date,touch,ls -l,time, exec duration tracking — aborts when bashkit is embedded in a browser via wasm-bindgen.Fix
All wall-clock reads in the
bashkitcrate now go through a newcrate::time_compatmodule:web-time(Performance.now()/Date.now()via web-sys)std::time— zero overhead, and the dependency is only pulled in for wasm32 targetsFrom<std::time::SystemTime>but not web-time's)Also gates
Interpreter::clear_cancelled_execution_stateto non-wasm: its only caller is the native-only tokio timeout recovery path, so it was a dead-code warning on wasm targets.Guards so this can't rot
cargo check -p bashkit --target wasm32-unknown-unknown --features scripted_tool,jqwith-D warnings(python is excluded: monty → ahash → getrandom 0.3 lacks itswasm_jsfeature there — separable follow-up)release_profile_tests.rs) keepingstd::timeclock types out ofsrc/, with a// std-time-ok: <reason>escape hatchVerification
just checkequivalent passes (fmt, clippy-D warnings, workspace tests + doctests)cargo check --target wasm32-unknown-unknownpasses warning-freedate -ureturning the actual current time, andtouch/ls -lashowing correct timestamps