Repository navigation
Conversation
On wasm32 `size_t` is `unsigned long`, but `SIZE_MAX == UINT_MAX`, so the `SIZE_MAX` tests select `__builtin_uadd_overflow`/`__builtin_umul_overflow`, whose `unsigned int *` result type does not match a `size_t *`; clang rejects every caller with -Wincompatible-pointer-types. Use the type-generic `__builtin_add_overflow`/`__builtin_mul_overflow` there, which are correct for any width.
WASI preview 1 has no sockets (no `<netdb.h>`, `<sys/socket.h>` or `<netinet/*>`). Build the socket stream there without the BSD socket code: the default stream fails with an error that says to register a stream (`git_stream_register`) or a transport (`git_transport_register`). A custom `GIT_STREAM_STANDARD` registration is still honoured.
WASI has no users: no `<pwd.h>`, `getpwuid_r`, `getuid` or `geteuid`. Treat it as a single-user system whose home and XDG directories come from `$HOME` and `$XDG_CONFIG_HOME` only; the passwd lookup reports not found.
WASI has no processes (no fork, exec, pipes or `<sys/wait.h>`). Only the `exec` SSH transport uses `git_process`, and SSH is not available on WASI; compile the unix implementation out there.
llhttp's api.c declares `wasm_on_*` functions for its own build, which runs in a JavaScript host that implements them. Any other WebAssembly build that links llhttp then has eight undefined imports. Exclude them under `__wasi__`, where llhttp is used as an ordinary C library.
WASI has no `getpid`, `getppid`, `getpgid`, `getsid`, `getuid` or `getgid`. The seed comes from `getentropy` there (which WASI provides and CMake detects); the time-based fallback leaves the ids out.
WASI has no users: no `geteuid`, and `stat` reports no owner. Give WASI its own `git_fs_path_owner_is` that treats every existing file as the current user's, so the ownership checks of `safe.directory` pass, as they do for a single-user system.
Define NO_MMAP for `CMAKE_SYSTEM_NAME` WASI: WASI preview 1 has no `mmap`
(wasi-libc offers only an emulation behind `_WASI_EMULATED_MMAN`), so
packfiles and other mapped files are read into allocated memory, as on other
platforms without mmap.
With the preceding changes, libgit2 builds for wasm32-wasip1 with wasi-sdk's
toolchain file and
cmake -DCMAKE_TOOLCHAIN_FILE=<wasi-sdk>/share/cmake/wasi-sdk-p1.cmake \
-DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=OFF -DBUILD_CLI=OFF \
-DUSE_THREADS=OFF -DUSE_HTTPS=OFF -DUSE_SSH=OFF
Network access needs a custom transport or stream, since WASI preview 1 has
no sockets.
wasi-libc's realpath() calls readlink() on every prefix of the path, and a prefix that is not inside a preopened directory fails: "/a" for the preopen "/a/b". So with a preopen of more than one path component, nothing below it resolves, and `git_repository_init`, `git_repository_open` and others fail with "failed to resolve path". When realpath() fails that way on WASI, resolve the path lexically (`.` and `..`, relative to the working directory) and check that it exists. Symbolic links are not resolved in that case.
Node's WASI (uvwasi) fails readlink() with ENOBUFS when the link's target fills the buffer exactly, where POSIX truncates it. libgit2 reads a symbolic link into a buffer of its target's length (`st_size`), so on Node every symlink in a work tree fails: diffs and status report "failed to read symlink data ... No buffer space available". On WASI, read into a buffer one byte longer and copy the target.
pankgeorg
added a commit
to pankgeorg/LibGit2_wasm_jll.jl
that referenced
this pull request
Nov 6, 2026
This branch has not been deployed
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.
This lets libgit2 build for WebAssembly with wasi-sdk, as a static library for
wasm32-wasip1. There are ten small commits, each guarded by__wasi__(or by CMake'sWASIsystem name), so other platforms are unchanged.WASI preview 1 offers a POSIX-like filesystem through wasi-libc. It has no sockets, processes, users,
mmapor threads. With these changes the local parts of libgit2 work under any WASI runtime (repositories, objects, refs, index, checkout, diff, the local transport). Network access works through a custom transport or stream (git_transport_register/git_stream_register) that the embedder provides.Commits
size_tisunsigned longwhileSIZE_MAX == UINT_MAX. TheSIZE_MAXtests therefore pick__builtin_uadd_overflow, and clang rejects every caller (-Wincompatible-pointer-types).__builtin_add_overflow/__builtin_mul_overfloware used there instead.GIT_STREAM_STANDARDregistration is still used.<pwd.h>/getpwuid_r/getuid. Home and XDG directories come from$HOME/$XDG_CONFIG_HOMEonly.api.cdeclareswasm_on_*imports for llhttp's own JS-hosted build, which leaves 8 undefined imports in any other wasm build that links it. (This one touchesdeps/llhttp. I'm happy to send it to llhttp as well, or to drop it if you'd rather carry it differently.)getentropy, which WASI has and CMake already detects. The time-based fallback leaves outgetpid/getppid/getpgid/getsid/getuid/getgid, which WASI doesn't have.geteuid, andstatreports no owner. WASI gets its owngit_fs_path_owner_isthat treats every existing file as the current user's, as on a single-user system, so thesafe.directorychecks pass.NO_MMAPforCMAKE_SYSTEM_NAMEWASI, so packs are read into memory as on other mmap-less platforms.realpath()callsreadlink()on every prefix of the path. A prefix that is not inside a preopened directory fails, for example/afor the preopen/a/b. So with a preopen of more than one component,git_repository_init/openfail with "failed to resolve path". On WASI, such a path is resolved lexically and checked for existence instead. (The root cause is in wasi-libc; this keeps libgit2 usable with today's runtimes.)readlink()with ENOBUFS when the target fills the buffer exactly, where POSIX truncates. libgit2 passes buffers of the target's length, so under Node every symlink in a work tree fails diffs and status ("failed to read symlink data ... No buffer space available"). On WASI, the target is read into a buffer one byte longer.Building
With wasi-sdk (tested with wasi-sdk 34, clang 23):
This produces
libgit2.awith no warnings. Bundled zlib, PCRE, llhttp, xdiff and SHA1DC are used.Testing
I don't add CI here, and I'd welcome guidance if you want a WASI job (the clar suite would need a WASI runner such as wasmtime or Node, plus a list of tests that need sockets or processes).
What I checked:
wasm32-wasip1reactor module and ran it under Node's WASI, againstgit2.43:git fsck --strictclean; the tree equalsgit write-tree);git cat-file),ls-tree, refs;git_checkout_treefrom a bare repository into atarget_directory(identical togit archive);https(stateless RPC, I/O through a host-provided fetch) and used it to clone, shallow clone (depth = 1), unshallow fetch andls-remoteagainst github.com. The refs were identical to native libgit2 andgit fsckwas clean./work,/a/b, a host path used as the guest path);wasi-v1.9.7).Known WASI limits that need no code here: there are no permission bits, so
core.filemodeis detected as false at init and the executable bit is not restored on checkout. There is nochmod.The patches, tests and this description were drafted with an AI agent (Claude Code, Claude Opus 5.5) and reviewed before opening.
🤖 Generated with Claude Code