// Package shipped collects what Docs ships, for its Colophon (estilo's // colophon package, Help → Colophon). Nothing here is a list written by // hand: each part is read from the build that ships it. // // - Go modules: go list -deps of ./cmd/docs for linux/arm64, the release // build (scripts/build-release.sh), through estilo's credits/catalog. // - JavaScript packages: the input files esbuild reads for the bundles // the app serves (fixtures/bundles.mjs, via fixtures/colophon-inputs.mjs). // - Rust crates: cargo metadata for core/ (the WebAssembly the server // runs) and, for the Apple apps, native/crdt (the WebAssembly they run) // and native/host (the static library that runs it). // - Typefaces: internal/fonts/fonts.json, and estilo's retired Onest, // whose files the server still answers for (app.go). // - Icons: Lucide, the web chrome's interface icons, served as // static/brand/LICENSE.lucide. The Apple apps draw none. // - Swift packages: native/apple/Package.resolved, with each package's // licence vendored under native/notices/ against the pinned revision. // - The Go and Rust standard libraries, which are compiled in. // - Tools: what the build and the tests are run with, as pinned: npm // devDependencies, go.mod requires the server does not link, cargo // dev-dependencies, the pip pins in scripts/, and the toolchains. // A tool's own dependencies are its own. // // TestWebColophonMatchesWhatShips and TestNativeColophonMatchesWhatShips // regenerate all of it and fail if the committed Colophon differs; estilo's // colophon.Build fails if the catalog has no entry for something here, an // entry has no usage note, or a usage note names something not shipped. // Regenerate with `go run ./cmd/docs-colophon`. package shipped import ( "bytes" "context" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "html" "os" "os/exec" "path/filepath" "regexp" "sort" "strings" "time" "amadan.net/oficina/estilo.git/colophon" "amadan.net/oficina/estilo.git/credits" "amadan.net/oficina/estilo.git/credits/catalog" ) // FirstParty is Oficina's own modules: the suite's code, not a project it // is made with. Rastrillo is listed, as every Oficina app lists it: it is // its own project with its own licence, and that includes Rastrillo // Native's server half (amadan.net/rastrillo/native/server), which // internal/docs links as nativeauth. var FirstParty = map[string]bool{ "amadan.net/oficina/agent-access.git": true, "amadan.net/oficina/estilo.git": true, "amadan.net/oficina/home/connect": true, "amadan.net/oficina/home/dashboard": true, "amadan.net/oficina/home/handoff": true, "amadan.net/oficina/home/roster": true, "amadan.net/oficina/home/teammode": true, // The suite contract (spec §8.2.2): Oficina's own, like Home's modules. "amadan.net/oficina/suite": true, } // toolchain is a toolchain the build or the checks run, found by its // invocation in the script that runs it: a script that stops running one // fails here until it is taken out. The list is kept by hand, so it is // checked one way only; its scope (AGENTS.md, "The Colophon") is the // open-source language toolchains, package managers, browsers and sandboxes // that build or test what ships, not the shell, git, curl or Apple's // proprietary Xcode tools (xcodebuild, lipo, xcrun). type toolchain struct { name, file, needle string } var ( webToolchains = []toolchain{ {name: "node", file: ".amadan/ci.d/10-gate", needle: "node --test"}, {name: "npm", file: ".amadan/ci.d/10-gate", needle: "npm ci"}, {name: "rustup", file: ".amadan/ci.d/10-gate", needle: "rustup target list"}, {name: "chromium", file: "scripts/browser-drive.sh", needle: `"$chrome" --headless=new`}, {name: "python", file: "scripts/vendor-fonts.py", needle: "#!/usr/bin/env python3"}, {name: "docker", file: "scripts/build-wasm.sh", needle: "docker run"}, {name: "bwrap", file: "scripts/build-wasm.sh", needle: "bwrap --tmpfs"}, } nativeToolchains = []toolchain{ {name: "python", file: "native/scripts/check-native.sh", needle: "python3 native/scripts/sync-fonts.py"}, {name: "npm", file: "native/scripts/check-native.sh", needle: "npm ci"}, {name: "swift", file: "native/scripts/check-native.sh", needle: "swift test"}, {name: "xcodegen", file: "native/scripts/build-iphone.sh", needle: "xcodegen generate"}, } ) // toolchains is each toolchain in list, as a tool, once its invocation is // found. None is pinned, so none has a version or a copied licence. func toolchains(root string, list []toolchain) ([]colophon.Package, error) { var out []colophon.Package for _, tc := range list { raw, err := os.ReadFile(filepath.Join(root, tc.file)) if err != nil { return nil, err } if !strings.Contains(string(raw), tc.needle) { return nil, fmt.Errorf("shipped: %s no longer runs %q; take %s out of the toolchains", tc.file, tc.needle, tc.name) } out = append(out, colophon.Package{Ecosystem: colophon.Toolchain, Name: tc.name, Tool: true, Notices: []credits.Notice{}}) } return out, nil } // WebPath and NativePath are where the generated Colophons are committed, // relative to the repository root. const ( WebPath = "internal/docs/colophon.json" NativePath = "native/apple/Sources/DocsKit/Resources/Colophon.json" ) func run(ctx context.Context, dir string, env []string, name string, args ...string) ([]byte, error) { command := exec.CommandContext(ctx, name, args...) command.Dir = dir if env != nil { command.Env = env } var stderr bytes.Buffer command.Stderr = &stderr out, err := command.Output() if err != nil { return nil, fmt.Errorf("shipped: %s %s: %v\n%s", name, strings.Join(args, " "), err, stderr.String()) } return out, nil } // goEnv is the environment of the release build: linux/arm64, no cgo, no // workspace. func goEnv() []string { var env []string for _, e := range os.Environ() { if !strings.HasPrefix(e, "GOOS=") && !strings.HasPrefix(e, "GOARCH=") && !strings.HasPrefix(e, "CGO_ENABLED=") && !strings.HasPrefix(e, "GOWORK=") && !strings.HasPrefix(e, "GOFLAGS=") { env = append(env, e) } } return append(env, "GOOS=linux", "GOARCH=arm64", "CGO_ENABLED=0", "GOWORK=off", "GOFLAGS=-mod=readonly") } func notice(name, path string) ([]credits.Notice, error) { raw, err := os.ReadFile(path) if err != nil { return nil, err } if strings.TrimSpace(string(raw)) == "" { return nil, fmt.Errorf("shipped: %s is empty", path) } return []credits.Notice{{Name: name, Text: string(raw)}}, nil } // Web is everything the Docs server ships, and the tools it is built and // tested with. func Web(root string) ([]colophon.Package, error) { root, err := filepath.Abs(root) if err != nil { return nil, err } ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) defer cancel() var out []colophon.Package add := func(p []colophon.Package, err error) error { out = append(out, p...) return err } production, err := goModules(ctx, root) if err != nil { return nil, err } for _, step := range []func() error{ func() error { return add(production, nil) }, func() error { return add(goToolchain(ctx, root)) }, func() error { return add(npmBundles(ctx, root)) }, func() error { return add(cargo(ctx, filepath.Join(root, "core", "Cargo.toml"), "wasm32-unknown-unknown", true)) }, func() error { return add(rustToolchain(ctx, root)) }, func() error { return add(typefaces(root, filepath.Join(root, "internal", "fonts", "files"))) }, func() error { o := credits.Onest() out = append(out, colophon.Package{Ecosystem: colophon.Font, Name: o.Name, Version: o.Version, Notices: o.Notices}) return nil }, func() error { return add(lucide(filepath.Join(root, "internal", "docs", "brand", "LICENSE.lucide"))) }, func() error { return add(npmTools(root, "fixtures"), nil) }, func() error { return add(npmTools(root, filepath.Join("fixtures", "engines")), nil) }, func() error { return add(goTools(ctx, root, production)) }, func() error { return add(pipTools(root)) }, func() error { return add(toolchains(root, webToolchains)) }, } { if err := step(); err != nil { return nil, err } } return out, nil } // Native is everything the Mac and iPhone apps ship. Sparkle is the Mac's // alone. func Native(root string) ([]colophon.Package, error) { root, err := filepath.Abs(root) if err != nil { return nil, err } ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) defer cancel() var out []colophon.Package crdt, err := cargo(ctx, filepath.Join(root, "native", "crdt", "Cargo.toml"), "wasm32-unknown-unknown", false) if err != nil { return nil, err } out = append(out, crdt...) // The host library is built per Apple target; each graph's crates are // marked with the platform it is for, and Build joins them, so a crate // on one target alone would show in that app alone. The crdt // WebAssembly runs on both, unmarked. for _, target := range []struct{ triple, platform string }{ {"aarch64-apple-darwin", "mac"}, {"x86_64-apple-darwin", "mac"}, {"aarch64-apple-ios", "ios"}, {"aarch64-apple-ios-sim", "ios"}, {"x86_64-apple-ios", "ios"}, } { host, err := cargo(ctx, filepath.Join(root, "native", "host", "Cargo.toml"), target.triple, false) if err != nil { return nil, err } for i := range host { host[i].Platforms = []string{target.platform} } out = append(out, host...) } rust, err := rustToolchain(ctx, root) if err != nil { return nil, err } for i := range rust { rust[i].Tool = false } out = append(out, rust...) fontPackages, err := typefaces(root, filepath.Join(root, "native", "apple", "Sources", "DocsKit", "Fonts")) if err != nil { return nil, err } out = append(out, fontPackages...) // No Lucide: nothing in the apps draws a Lucide icon (their icons are // Oficina's own and the system's symbols), so they carry no notice. swift, err := swiftPackages(root) if err != nil { return nil, err } out = append(out, swift...) entities, err := notice("NOTICE", filepath.Join(root, "native", "notices", "html-entities", "NOTICE")) if err != nil { return nil, err } out = append(out, colophon.Package{Ecosystem: colophon.Asset, Name: "html-entities", Notices: entities}) tools, err := toolchains(root, nativeToolchains) return append(out, tools...), err } func goModules(ctx context.Context, root string) ([]colophon.Package, error) { raw, err := run(ctx, root, goEnv(), "go", "list", "-buildvcs=false", "-deps", "-json", "./cmd/docs") if err != nil { return nil, err } inventory, err := catalog.Collect(bytes.NewReader(raw), FirstParty) if err != nil { return nil, err } return colophon.FromComponents(colophon.Go, inventory.Components), nil } var goDirective = regexp.MustCompile(`(?m)^go (\d+\.\d+(\.\d+)?)$`) // goToolchain is Go's standard library, which is compiled into the // server. The version is go.mod's, which the release build is held to. func goToolchain(ctx context.Context, root string) ([]colophon.Package, error) { mod, err := os.ReadFile(filepath.Join(root, "go.mod")) if err != nil { return nil, err } m := goDirective.FindSubmatch(mod) if m == nil { return nil, errors.New("shipped: go.mod has no go directive") } goroot, err := run(ctx, root, goEnv(), "go", "env", "GOROOT") if err != nil { return nil, err } text, err := notice("LICENSE", filepath.Join(strings.TrimSpace(string(goroot)), "LICENSE")) if err != nil { return nil, err } return []colophon.Package{{Ecosystem: colophon.Toolchain, Name: "go", Version: "go" + string(m[1]), Notices: text}}, nil } func npmBundles(ctx context.Context, root string) ([]colophon.Package, error) { fixtures := filepath.Join(root, "fixtures") if _, err := os.Stat(filepath.Join(fixtures, "node_modules")); err != nil { return nil, errors.New("shipped: fixtures/node_modules is missing; run `npm ci` in fixtures (10-gate does)") } raw, err := run(ctx, fixtures, nil, "node", "colophon-inputs.mjs") if err != nil { return nil, err } var inputs []string if err := json.Unmarshal(raw, &inputs); err != nil { return nil, err } return colophon.CollectNPM(fixtures, inputs) } // cargo is one manifest's crates for target: what the build links, and, // with tools, its direct dev-dependencies as tools. func cargo(ctx context.Context, manifest, target string, tools bool) ([]colophon.Package, error) { raw, err := run(ctx, filepath.Dir(manifest), nil, "cargo", "metadata", "--locked", "--format-version", "1", "--filter-platform", target, "--manifest-path", manifest) if err != nil { return nil, err } out, err := colophon.CollectCargo(bytes.NewReader(raw)) if err != nil { return nil, err } if tools { dev, err := colophon.CollectCargoTools(bytes.NewReader(raw)) if err != nil { return nil, err } out = append(out, dev...) } return upstreamNotices(filepath.Join(filepath.Dir(manifest), ".."), out) } // upstreamNotices gives a crate published without its licence file the // files its repository has, vendored at native/notices/crates// with // a provenance.json naming the crates and exact versions they are for and // each file's sha256. root is any directory under the repository. func upstreamNotices(root string, packages []colophon.Package) ([]colophon.Package, error) { for !exists(filepath.Join(root, "go.mod")) { parent := filepath.Dir(root) if parent == root { return nil, errors.New("shipped: no repository root above the manifest") } root = parent } dirs, err := filepath.Glob(filepath.Join(root, "native", "notices", "crates", "*", "provenance.json")) if err != nil { return nil, err } for _, path := range dirs { raw, err := os.ReadFile(path) if err != nil { return nil, err } var p struct { Crates map[string]string Sha256 map[string]string } if err := json.Unmarshal(raw, &p); err != nil { return nil, err } notices, err := colophon.DirNotices(filepath.Dir(path)) if err != nil { return nil, err } if len(notices) != len(p.Sha256) { return nil, fmt.Errorf("shipped: %s names %d files but its directory holds %d", path, len(p.Sha256), len(notices)) } for _, n := range notices { if sum := sha256.Sum256([]byte(n.Text)); hex.EncodeToString(sum[:]) != p.Sha256[n.Name] { return nil, fmt.Errorf("shipped: %s is not the file %s recorded", n.Name, path) } } for i, pkg := range packages { if version, ok := p.Crates[pkg.Name]; ok && version == pkg.Version && len(pkg.Notices) == 0 { packages[i].Notices = notices } } } return packages, nil } func exists(path string) bool { _, err := os.Stat(path) return err == nil } var channel = regexp.MustCompile(`(?m)^channel\s*=\s*"([0-9.]+)"`) // rustToolchain is Rust's standard library, compiled into the // WebAssembly, at rust-toolchain.toml's version. Its notice is the // toolchain's own COPYRIGHT-library.html, as text. func rustToolchain(ctx context.Context, root string) ([]colophon.Package, error) { pin, err := os.ReadFile(filepath.Join(root, "rust-toolchain.toml")) if err != nil { return nil, err } m := channel.FindSubmatch(pin) if m == nil { return nil, errors.New("shipped: rust-toolchain.toml pins no channel") } version, err := run(ctx, root, nil, "rustc", "--version") if err != nil { return nil, err } if !strings.HasPrefix(string(version), "rustc "+string(m[1])+" ") { return nil, fmt.Errorf("shipped: rustc is %q, not rust-toolchain.toml's %s (put rustup's cargo first on PATH: . ~/.cargo/env)", strings.TrimSpace(string(version)), m[1]) } sysroot, err := run(ctx, root, nil, "rustc", "--print", "sysroot") if err != nil { return nil, err } raw, err := os.ReadFile(filepath.Join(strings.TrimSpace(string(sysroot)), "share", "doc", "rust", "COPYRIGHT-library.html")) if err != nil { return nil, err } return []colophon.Package{{Ecosystem: colophon.Toolchain, Name: "rust", Version: string(m[1]), Notices: []credits.Notice{{Name: "COPYRIGHT-library.html, as text", Text: htmlText(string(raw))}}}}, nil } var ( blockTag = regexp.MustCompile(`(?i)<(/?(p|h[1-6]|li|pre|br|div|tr|ul|ol|table)\b[^>]*)>`) anyTag = regexp.MustCompile(`<[^>]*>`) blanks = regexp.MustCompile(`\n{3,}`) ) // htmlText is an HTML notice as plain text: block elements become line // breaks, other tags go, and entities are decoded. func htmlText(s string) string { s = blockTag.ReplaceAllString(s, "\n") s = anyTag.ReplaceAllString(s, "") s = html.UnescapeString(s) var lines []string for _, line := range strings.Split(s, "\n") { lines = append(lines, strings.TrimRight(line, " \t\r")) } return strings.TrimSpace(blanks.ReplaceAllString(strings.Join(lines, "\n"), "\n\n")) + "\n" } type manifest struct { Families []struct { Name string `json:"name"` Dir string `json:"dir"` Licence string `json:"licence"` } `json:"families"` } // typefaces is every family in internal/fonts/fonts.json, with its licence // read from files (internal/fonts/files for the server, the DocsKit copy // for the Apple apps; sync-fonts.py holds the two identical). func typefaces(root, files string) ([]colophon.Package, error) { raw, err := os.ReadFile(filepath.Join(root, "internal", "fonts", "fonts.json")) if err != nil { return nil, err } var m manifest if err := json.Unmarshal(raw, &m); err != nil { return nil, err } var out []colophon.Package for _, f := range m.Families { text, err := notice(f.Licence, filepath.Join(files, f.Dir, f.Licence)) if err != nil { return nil, err } out = append(out, colophon.Package{Ecosystem: colophon.Font, Name: f.Name, Notices: text}) } if len(out) == 0 { return nil, errors.New("shipped: fonts.json lists no families") } return out, nil } // lucide is the icons' licence as this build carries it; it must be the // same text estilo attributes. func lucide(path string) ([]colophon.Package, error) { text, err := notice("LICENSE", path) if err != nil { return nil, err } if text[0].Text != credits.Lucide().Notices[0].Text { return nil, fmt.Errorf("shipped: %s is not estilo's Lucide licence", path) } return []colophon.Package{{Ecosystem: colophon.Asset, Name: "lucide", Notices: text}}, nil } func npmTools(root, dir string) []colophon.Package { raw, err := os.ReadFile(filepath.Join(root, dir, "package.json")) if err != nil { return nil } var p struct { DevDependencies map[string]string `json:"devDependencies"` } if json.Unmarshal(raw, &p) != nil { return nil } var out []colophon.Package for name, version := range p.DevDependencies { pkg := colophon.Package{Ecosystem: colophon.NPM, Name: name, Version: version, Tool: true, Notices: []credits.Notice{}} // The licence is copied when the package is installed; 10-gate // installs fixtures/ but not fixtures/engines/, so read neither: // a tool's text is optional, and the Colophon must not depend on // which of them happen to be installed. out = append(out, pkg) } sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) return out } var require = regexp.MustCompile(`(?m)^\s*(?:require\s+)?([a-z0-9.\-]+\.[a-z]+/[^\s]+)\s+(v[^\s]+)(\s*//.*)?$`) // goTools is go.mod's direct requires that the server does not link: the // test tooling (chromedp, which drives the browser checks). func goTools(ctx context.Context, root string, production []colophon.Package) ([]colophon.Package, error) { mod, err := os.ReadFile(filepath.Join(root, "go.mod")) if err != nil { return nil, err } linked := map[string]bool{} for _, p := range production { linked[p.Name] = true } var out []colophon.Package for _, m := range require.FindAllStringSubmatch(string(mod), -1) { if strings.Contains(m[3], "indirect") || linked[m[1]] || FirstParty[m[1]] { continue } out = append(out, colophon.Package{Ecosystem: colophon.Go, Name: m[1], Version: m[2], Tool: true, Notices: []credits.Notice{}}) } sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) return out, nil } var pipPin = regexp.MustCompile(`pip install ((?:[A-Za-z0-9_.\-]+==[0-9A-Za-z.]+\s*)+)`) // pipTools is the Python packages scripts/ say to install, pinned: the // font vendoring and the shaping goldens. func pipTools(root string) ([]colophon.Package, error) { scripts, err := filepath.Glob(filepath.Join(root, "scripts", "*.py")) if err != nil { return nil, err } seen := map[string]bool{} var out []colophon.Package for _, path := range scripts { raw, err := os.ReadFile(path) if err != nil { return nil, err } for _, m := range pipPin.FindAllStringSubmatch(string(raw), -1) { for _, pin := range strings.Fields(m[1]) { name, version, _ := strings.Cut(pin, "==") name = strings.ToLower(name) if seen[name+version] { continue } seen[name+version] = true out = append(out, colophon.Package{Ecosystem: colophon.Pip, Name: name, Version: version, Tool: true, Notices: []credits.Notice{}}) } } } sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) return out, nil } // swiftPackages is Package.resolved's pins. Each one's licence is vendored // at native/notices// with a provenance.json naming the revision // it was copied from, which must be the pinned one. func swiftPackages(root string) ([]colophon.Package, error) { raw, err := os.ReadFile(filepath.Join(root, "native", "apple", "Package.resolved")) if err != nil { return nil, err } var resolved struct { Pins []struct { Identity string `json:"identity"` Location string `json:"location"` State struct { Revision string `json:"revision"` Version string `json:"version"` } `json:"state"` } `json:"pins"` } if err := json.Unmarshal(raw, &resolved); err != nil { return nil, err } var out []colophon.Package for _, pin := range resolved.Pins { dir := filepath.Join(root, "native", "notices", pin.Identity) prov, err := os.ReadFile(filepath.Join(dir, "provenance.json")) if err != nil { return nil, fmt.Errorf("shipped: Swift package %s has no vendored licence at native/notices/%s: %w", pin.Identity, pin.Identity, err) } var p struct { Location, Revision string Platforms []string } if err := json.Unmarshal(prov, &p); err != nil { return nil, err } if p.Revision != pin.State.Revision || p.Location != pin.Location { return nil, fmt.Errorf("shipped: native/notices/%s was copied from %s at %s, but Package.resolved pins %s at %s; copy its licence again", pin.Identity, p.Location, p.Revision, pin.Location, pin.State.Revision) } notices, err := colophon.DirNotices(dir) if err != nil { return nil, err } version := pin.State.Version if version == "" { version = pin.State.Revision[:12] } out = append(out, colophon.Package{Ecosystem: colophon.Swift, Name: pin.Identity, Version: version, Platforms: p.Platforms, Notices: notices}) } return out, nil }