Skip to content

Auto-recover from corrupt Wasm builds that emit undefined env imports#4287

Open
Keavon wants to merge 2 commits into
masterfrom
heal-corrupt-incremental-build
Open

Auto-recover from corrupt Wasm builds that emit undefined env imports#4287
Keavon wants to merge 2 commits into
masterfrom
heal-corrupt-incremental-build

Conversation

@Keavon

@Keavon Keavon commented Jun 28, 2026

Copy link
Copy Markdown
Member

An interrupted or concurrent cargo build --target wasm32-unknown-unknown can leave a wasm crate's incremental codegen inconsistent, producing a .wasm with undefined symbols that rust-lld emits as env imports. wasm-bindgen writes these into the JS glue as import … from "env", which Vite can't resolve, so the build succeeds but the dev server breaks, previously needing a manual cargo clean.

cargo-run now checks the generated glue for the from "env" marker after each wasm build and, when present, wipes only the wasm32-unknown-unknown target directory (not the shared host debug/build artifacts, which other tools can hold locked on Windows) and rebuilds once. This runs on both the startup build and the watcher's rebuilds via a new sequence_then, which appends the recovery steps to the same killable sequence.

Example of a poisoned scenario's terminal output after opening the localhost page in a browser:

C:\Users\Keavon\Projects\Graphite>cargo run
    Finished `dev` profile [optimized + debuginfo] target(s) in 0.52s
     Running `target\debug\cargo-run.exe`

Checking Requirements:
 ✓ Rust (rustc 1.95.0 (59807616e 2026-04-14))
 ✓ Rust - Wasm Target
 ✓ Wasm Bindgen (0.2.121)
 ✓ Cargo About (cargo-about 0.8.4)
 ✓ Node.js (v24.12.0)

    Finished `dev` profile [optimized + debuginfo] target(s) in 0.49s
[RUST]     Finished `dev` profile [optimized + debuginfo] target(s) in 0.73s
[RUST]      Running `target\debug\cargo-run-internal-watch.exe`
[VITE]     Finished `dev` profile [optimized + debuginfo] target(s) in 1.52s
[VITE]      Running `C:\Users\Keavon\Projects\Graphite\target\debug\third-party-licenses.exe`
[VITE] Changes in licenses detected, generating new license file...
[VITE] 
[VITE]   VITE v8.0.3  ready in 91423 ms
[VITE] 
[VITE]   ➜  Local:   http://localhost:8080/
[VITE]   ➜  Network: http://192.168.1.73:8080/
[VITE]   ➜  press h + enter to show help
[VITE] 3:35:47 PM [vite] Internal server error: Failed to resolve import "env" from "wrapper/pkg/graphite_wasm_wrapper.js". Does the file exist?
[VITE]   Plugin: vite:import-analysis
[VITE]   File: C:/Users/Keavon/Projects/Graphite/frontend/wrapper/pkg/graphite_wasm_wrapper.js:2:26
[VITE]   1  |  /* @ts-self-types="./graphite_wasm_wrapper.d.ts" */
[VITE]   2  |  import * as import1 from "env"
[VITE]      |                            ^
[VITE]   3  |  import * as import2 from "env"
[VITE]   4  |  
[VITE]       at TransformPluginContext._formatLog (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:30259:39)
[VITE]       at TransformPluginContext.error (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:30256:14)
[VITE]       at normalizeUrl (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:27544:18)
[VITE]       at async file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:27607:30
[VITE]       at async Promise.all (index 0)
[VITE]       at async TransformPluginContext.transform (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:27575:4)
[VITE]       at async EnvironmentPluginContainer.transform (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:30048:14)
[VITE]       at async loadAndTransform (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:24177:26)
[VITE] 3:35:47 PM [vite] (client) Pre-transform error: Failed to resolve import "env" from "wrapper/pkg/graphite_wasm_wrapper.js". Does the file exist?
[VITE]   Plugin: vite:import-analysis
[VITE]   File: C:/Users/Keavon/Projects/Graphite/frontend/wrapper/pkg/graphite_wasm_wrapper.js:2:26
[VITE]   1  |  /* @ts-self-types="./graphite_wasm_wrapper.d.ts" */
[VITE]   2  |  import * as import1 from "env"
[VITE]      |                            ^
[VITE]   3  |  import * as import2 from "env"
[VITE]   4  |  
[VITE] 3:35:48 PM [vite] Internal server error: Failed to resolve import "env" from "wrapper/pkg/graphite_wasm_wrapper.js". Does the file exist?
[VITE]   Plugin: vite:import-analysis
[VITE]   File: C:/Users/Keavon/Projects/Graphite/frontend/wrapper/pkg/graphite_wasm_wrapper.js:2:26
[VITE]   1  |  /* @ts-self-types="./graphite_wasm_wrapper.d.ts" */
[VITE]   2  |  import * as import1 from "env"
[VITE]      |                            ^
[VITE]   3  |  import * as import2 from "env"
[VITE]   4  |  
[VITE]       at TransformPluginContext._formatLog (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:30259:39)
[VITE]       at TransformPluginContext.error (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:30256:14)
[VITE]       at normalizeUrl (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:27544:18)
[VITE]       at async file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:27607:30
[VITE]       at async Promise.all (index 0)
[VITE]       at async TransformPluginContext.transform (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:27575:4)
[VITE]       at async EnvironmentPluginContainer.transform (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:30048:14)
[VITE]       at async loadAndTransform (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:24177:26)
[VITE]       at async viteTransformMiddleware (file:///C:/Users/Keavon/Projects/Graphite/frontend/node_modules/vite/dist/node/chunks/node.js:24986:20)

@Keavon Keavon requested a review from timon-schelling June 28, 2026 00:23

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces automatic recovery for corrupt incremental WebAssembly builds. It adds a new sequence_then utility to run follow-up steps after a command sequence completes, and uses it to detect corrupt env imports in the generated JS glue, wiping the target directory and rebuilding if necessary. The review feedback correctly identifies a potential race condition in sequence_then where the follow_up closure (which deletes the target directory) could still execute after the sequence is killed, and suggests checking the kill flag before proceeding.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tools/cargo-run/src/cmd.rs Outdated
@Keavon Keavon force-pushed the heal-corrupt-incremental-build branch from a906395 to 5e832b7 Compare June 28, 2026 00:25
@Keavon

Keavon commented Jun 28, 2026

Copy link
Copy Markdown
Member Author

@cubic-dev-ai

@cubic-dev-ai

cubic-dev-ai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai

@Keavon I have started the AI code review. It will take a few minutes to complete.

@Keavon Keavon changed the title Auto-recover from corrupt wasm builds that emit undefined env imports Auto-recover from corrupt Wasm builds that emit undefined env imports Jun 28, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 3 files

Confidence score: 2/5

  • In tools/cargo-run/src/frontend.rs, the recovery logic needs to recognize the single-quoted env import pattern emitted by wasm-bindgen; if it doesn’t, poisoned glue can slip past detection and the self-heal path won’t run. Add this import variant to the detector before merging so recovery is consistently triggered.
  • In tools/cargo-run/src/cmd.rs, follow_up can still run after a watcher build is cancelled unless the killed flag is checked first, which risks cleanup side effects and then skipping the recovery rebuild. Guard follow_up behind an early killed check before merging to avoid wiping/racing build artifacts.

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread tools/cargo-run/src/frontend.rs Outdated
Comment thread tools/cargo-run/src/cmd.rs
- cmd.rs: skip the follow-up when the sequence was killed, so a build the
  watcher superseded doesn't run the destructive heal and force the next build
  into an unnecessary full rebuild.
- frontend.rs: also match a single-quoted `from 'env'` marker so a wasm-bindgen
  quote-style change can't hide poisoned glue from the detector.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant