cgen: `#include <sys/ptrace.h>` is emitted for wasm32_emscripten, so emcc builds that import math/os fail

Open Beginner friendly
#32,905 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, wasm
Domain
compilers

Research direction

The include is emitted in vlib/v/gen/c/cleanc.v near the #if !defined(__sun) guard around sys/ptrace.h. Reproduce with v -cc emcc -os wasm32_emscripten on a program that imports math, then check whether the generated C still includes that header. Done when that build compiles and the Emscripten guard is covered by a check, while the wasm .wasm output issue (#32903) is tracked separately.

Written by the indexing model from the issue text.

Description

Describe the bug

Generated C always contains #include <sys/ptrace.h> on non-Windows targets except Solaris. Emscripten's sysroot has no sys/ptrace.h, so any -os wasm32_emscripten build that pulls in this preamble fails. Importing math or os is enough.

Reproduction Steps

a.v:

import math

fn main() {
	println(int(math.sqrt(16.0)))
}
v -cc emcc -os wasm32_emscripten -o a.js a.v
Expected Behavior

The program compiles with emcc.

Current Behavior
src.c:1384:10: fatal error: 'sys/ptrace.h' file not found
 1384 | #include <sys/ptrace.h>
1 error generated.
emcc: error: '.../clang -target wasm32-unknown-emscripten ...' failed (returned 1)

The same error appears with import os. A plain println('hi') program compiles because it does not get this include.

Possible Solution

vlib/v/gen/c/cleanc.v (around line 21272):

-	g.writeln('#if !defined(__sun)')
+	g.writeln('#if !defined(__sun) && !defined(__EMSCRIPTEN__)')
 	g.writeln('#include <sys/ptrace.h>')

I tested this one-line change locally. It also needs the -sSINGLE_FILE=1 workaround for the missing .wasm output (#32903). With both, a 10k-agent float/array benchmark built with -cc emcc -prod -os wasm32_emscripten ran correctly in Node, with the same checksum as the native build.

V version

V 0.5.2 f16ac7a (master, 2026-11-25)

Environment details
  • macOS 27.0 (26A428), arm64 (Apple M1 Max)
  • Apple clang 21.0.0
  • emcc 6.0.5 (Homebrew)
  • Node.js 22.22.0
Dominant language
V
Stars
38.1k
Forks
2.3k
Avg merge
22h 8m
Merged PRs (30d)
636

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vlang/v

All issues in vlang/v

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.