Files
coder/scripts/clidocgen/main.go
T
Mathias Fredriksson a6a8fd94d7 build(Makefile): enable parallel make -j gen with correct dependency graph (#22612)
`make gen` could not run with `-j` because inter-target dependency edges
were missing. Multiple recipes compile `coderd/rbac` (which includes
generated files like `object_gen.go`), and without explicit ordering,
parallel runs produced syntax errors from mid-write reads.

Three main changes:

**Dependency graph fixes** declare the compile-time chain through
`coderd/rbac` so that `object_gen.go` is written before anything that
imports it is compiled. The DB generation targets use a GNU Make 4.3+
grouped target (`&:`) so Make knows `generate.sh` co-produces
`querier.go`, `unique_constraint.go`, `dbmetrics`, and `dbauthz` in a
single invocation. `SKIP_DUMP_SQL=1` avoids re-entrant `make` inside
`generate.sh` when the Makefile already guarantees `dump.sql` is fresh.

**`scripts/atomicwrite` package** replaces `os.WriteFile` in all gen
scripts with a temp-file-in-same-dir + rename pattern, preventing
interrupted runs from leaving partial files.

**`.PRECIOUS` and shell atomic writes** protect git-tracked generated
files from Make's default delete-on-error behavior. Since these files
are committed, deletion is worse than staleness -- `git restore` is the
recovery path.

CI now runs `make -j --output-sync -B gen` (~32s, down from ~85s
serial).

| Scenario                          | Before             | After    |
|-----------------------------------|--------------------|----------|
| `make gen` (serial)               | 95s                | 95s      |
| `make -j gen` (parallel)          | race error         | **22s**  |
| CI `make -j --output-sync -B gen` | forced serial ~85s | **~32s** |
2026-03-05 11:58:10 +00:00

202 lines
4.4 KiB
Go

package main
import (
"encoding/json"
"os"
"path/filepath"
"sort"
"strings"
"github.com/coder/coder/v2/enterprise/cli"
"github.com/coder/coder/v2/scripts/atomicwrite"
"github.com/coder/flog"
"github.com/coder/serpent"
)
// route is an individual page object in the docs manifest.json.
type route struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Path string `json:"path,omitempty"`
IconPath string `json:"icon_path,omitempty"`
State []string `json:"state,omitempty"`
Children []route `json:"children,omitempty"`
}
// manifest describes the entire documentation index.
type manifest struct {
Versions []string `json:"versions,omitempty"`
Routes []route `json:"routes,omitempty"`
}
func prepareEnv() {
// Unset CODER_ environment variables
for _, env := range os.Environ() {
if strings.HasPrefix(env, "CODER_") {
split := strings.SplitN(env, "=", 2)
if err := os.Unsetenv(split[0]); err != nil {
panic(err)
}
}
}
// Override default OS values to ensure the same generated results.
err := os.Setenv("CLIDOCGEN_CACHE_DIRECTORY", "~/.cache")
if err != nil {
panic(err)
}
err = os.Setenv("CLIDOCGEN_CONFIG_DIRECTORY", "~/.config/coderv2")
if err != nil {
panic(err)
}
err = os.Setenv("TMPDIR", "/tmp")
if err != nil {
panic(err)
}
}
func deleteEmptyDirs(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
ents, err := os.ReadDir(path)
if err != nil {
return err
}
if len(ents) == 0 {
flog.Infof("deleting empty dir\t %v", path)
err = os.Remove(path)
if err != nil {
return err
}
}
return nil
})
}
func main() {
prepareEnv()
workdir, err := os.Getwd()
if err != nil {
flog.Fatalf("getwd: %v", err)
}
root := (&cli.RootCmd{})
// wroteMap indexes file paths to commands.
wroteMap := make(map[string]*serpent.Command)
var (
docsDir = filepath.Join(workdir, "docs")
cliMarkdownDir = filepath.Join(docsDir, "reference/cli")
)
if d := os.Getenv("DOCS_DIR"); d != "" {
docsDir = d
cliMarkdownDir = filepath.Join(docsDir, "reference/cli")
}
cmd, err := root.Command(root.EnterpriseSubcommands())
if err != nil {
flog.Fatalf("creating command: %v", err)
}
err = genTree(
cliMarkdownDir,
cmd,
wroteMap,
)
if err != nil {
flog.Fatalf("generating markdowns: %v", err)
}
// Delete old files
err = filepath.Walk(cliMarkdownDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
_, ok := wroteMap[path]
if !ok {
flog.Infof("deleting old doc\t %v", path)
if err := os.Remove(path); err != nil {
return err
}
}
return nil
})
if err != nil {
flog.Fatalf("deleting old docs: %v", err)
}
err = deleteEmptyDirs(cliMarkdownDir)
if err != nil {
flog.Fatalf("deleting empty dirs: %v", err)
}
// Update manifest
manifestPath := filepath.Join(docsDir, "manifest.json")
manifestByt, err := os.ReadFile(manifestPath)
if err != nil {
flog.Fatalf("reading manifest: %v", err)
}
var manifest manifest
err = json.Unmarshal(manifestByt, &manifest)
if err != nil {
flog.Fatalf("unmarshalling manifest: %v", err)
}
var found bool
for i := range manifest.Routes {
rt := &manifest.Routes[i]
if rt.Title != "Reference" {
continue
}
for j := range rt.Children {
child := &rt.Children[j]
if child.Title != "Command Line" {
continue
}
child.Children = nil
found = true
for path, cmd := range wroteMap {
relPath, err := filepath.Rel(docsDir, path)
if err != nil {
flog.Fatalf("getting relative path: %v", err)
}
child.Children = append(child.Children, route{
Title: fullName(cmd),
Description: cmd.Short,
Path: relPath,
})
}
// Sort children by title because wroteMap iteration is
// non-deterministic.
sort.Slice(child.Children, func(i, j int) bool {
return child.Children[i].Title < child.Children[j].Title
})
}
}
if !found {
flog.Fatalf("could not find Command Line route in manifest")
}
manifestByt, err = json.MarshalIndent(manifest, "", " ")
if err != nil {
flog.Fatalf("marshaling manifest: %v", err)
}
err = atomicwrite.File(manifestPath, manifestByt)
if err != nil {
flog.Fatalf("writing manifest: %v", err)
}
}