Repository navigation
Description
What I did
A hub package lives in $BEND_LIB/0x<hash>/ and its files get the namespace 0x<hash>/<file>. book_load decides whether an import goes to the cache by testing the normalized import path against /^0x[0-9a-f]+\//, but computes the namespace afterwards as path.posix.join(path.posix.dirname(ns), rel) (book_load). From a file in a subdirectory, ../0x<hash>/f.bend does not start with 0x, so it is read from the local disk, while the join normalizes its namespace to 0x<hash>/f: exactly the namespace of the package file it imitates.
In the examples below, 0x0123… stands for 0x0123456789abcdef0123456789abcdef. The package is already in the cache (BEND_LIB points at a scratch directory, BEND_HUB is unreachable, and there is no network access).
$BEND_LIB/0x0123…/f.bend (the published package):
import Base
def two() -> Nat:
Nat.add(1n, 1n)
What happened
On its own, the local file collides with the package. Put a local proj/0x0123…/f.bend next to the project:
import Base
def two() -> Nat:
3n
proj/sub/n.bend:
import Base
import ../0x0123456789abcdef0123456789abcdef/f.bend as Fake
def unused() -> Nat:
0n
proj/main.bend:
import Base
import ./sub/n.bend as M
import 0x0123456789abcdef0123456789abcdef/f.bend as F
def main() -> Nat:
F.two()
Error:
- expected : a fresh name (duplicate declaration: 0x0123456789abcdef0123456789abcdef/f.two)
The two files have different real paths, so seen loads both, and the "one namespace per file" check never fires: two files share one namespace. The real package can no longer be loaded, and the error names the package's own declaration as the duplicate.
Together with #1002, the local file changes what the package means. Replace the local file with proj/0x0123…/f.Nat.bend:
import Base
def add(a: Nat, b: Nat) -> Nat:
7n
and import it from proj/sub/m.bend as import ../0x0123456789abcdef0123456789abcdef/f.Nat.bend as N, with main.bend importing ./sub/m.bend before the package. bun bend2/main.ts main.bend then prints 7n instead of 2n: the package's Nat.add resolves to the local 0x0123…/f.Nat.add by the rule reported in #1002. The package file in the cache is byte-for-byte what was published; its meaning is not.
A name@version/f.bend import is rewritten to the same hash path before any of this (name_hash), so named packages are affected the same way. I read this from the code but did not run it, because it needs the hub.
Why it matters
A hash (or a name@version that never moves) is supposed to fix what a dependency is. Here a local file can occupy the dependency's namespace, and then:
- either it collides with the dependency and blocks it from loading;
- or, through Names resolve to keys other files register under the importer's namespace: an app file can rewrite what LAWS.bend says, or make PROOF.bend check different code than
bend main.bendruns #1002, it rewrites what the dependency's names refer to.
For the LAWS.bend gate, this means laws about a hub library the human trusts by hash can be proven against a different library, and nothing in LAWS.bend, PROOF.bend or the cached package changes.
pkg_files also skips every file whose namespace starts with 0x when publishing, on the assumption that such files come from the hub. From reading the code, a local file that took such a namespace would be checked by the publisher but left out of the published package. I have not run this.
Expected behavior
The namespace should follow where the file actually comes from: book_load should reject a namespace starting with 0x for any file outside BEND_LIB. It may also be worth enforcing "one file per namespace" as well as "one namespace per file". This is separate from #1002: fixing name resolution there removes the rewrite, but the collision above remains.
Version
bend 2.0.26
commit 6a77e124
Bun 1.4.2
Darwin arm64
Activity
jasisz commentedon Sep 23, 2026
One more way into the same boundary, which also affects the fix proposed above.
BEND_LIBcomes fromprocess.env(bend.ts#L1015). Bun loads a.envfrom the working directory intoprocess.envwhen the shell does not set the variable, both forbun bend2/main.tsand for a binary built withbun build --compileusing the default options.book_loadchecks a package against its hash only when the file is missing fromBEND_LIB(bend.ts#L1052-L1065). So a project can pointBEND_LIBat its own copy of a package, and that copy is used without any check.Repro (
0x0123…=0x0123456789abcdef0123456789abcdef,BEND_HUBunreachable, no network):def f() -> Nat: 2nLAWS.bendimports0x0123…/lib.bend as Land declareslaw f_is_5: {L.f() == 5n : Nat}PROOF.bendimports it too and fills the law with{==}.cache/0x0123…/lib.bend(the same file, withfreturning5n) and a one-line.env:BEND_LIB=./.cacheNo file is renamed and no namespace is taken; the cached file is simply different. Rejecting a
0x…namespace for files outsideBEND_LIB, as proposed above, would not help here, because the project decides whereBEND_LIBis.Two changes would close it:
bendshould not read.envfrom the working directory (bun build --compilecan turn dotenv autoloading off). This also stops a project from settingCCorBEND_ORIGINfor the user, for example makingbend main.bend -o apprun a script from the project.Tested on 6a77e12, Bun 1.4.2, Darwin arm64.
A name is words joined by dots, a namespace is the file's real path, …