modfind

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 5 Imported by: 0

README

modfind

Centralized Go module discovery + replace/cache classification for tinywasm tooling.

Runs go list -m -json all once per project root, caches it, and classifies each module as writable (main module or local replace → tooling may generate files there) or read-only (module cache → read only). Replaces the byte-identical go list -m -json all loops previously copy-pasted in ssr, image/min, and imagemin.

Sibling to depfind: depfind maps the package import graph ("which main to recompile"); modfind enumerates modules ("where each module lives, and may I write there"). Tool-side only (//go:build !wasm); deps: stdlib + tinywasm/fmt.

Usage

f := modfind.New()
mods, err := f.Discover(rootDir) // []modfind.Module, cached after first call

for _, m := range mods {
    if m.Writable() {
        // main module or local replace → generate files in m.Dir
    } else {
        // read-only cache → only read m.Dir
    }
}

dirs, _ := f.Dirs(rootDir) // []string convenience (drop-in for old []dir loops)
f.Refresh(rootDir)         // invalidate after a go.mod change

In a tool with several consumers (assets + schema), construct one *modfind.Finder and inject it into each (ssr, image, ormc) so go list runs a single time per session.

Module

Field Meaning
Path import path
Dir on-disk dir (cache path or local replace path)
Version empty for main / local replace
IsMain the project's root module
IsReplace satisfied by a local filesystem replace (writable)
Indirect transitive dependency
Writable() `IsMain

Docs

  • docs/ARCHITECTURE.md — what modfind is: the Module model, the writable/read-only classification, the finder API, and constraints.
  • docs/DESIGN.md — why a dedicated package (not depfind, devflow, or app) and the duplication it removes.

Documentation

Overview

Package modfind is the centralized Go module discovery primitive for tinywasm tooling. It runs `go list -m -json all` once per project root, caches the parsed result, and classifies each module as writable (main module or local replace — tooling may generate files there) or read-only (module cache).

It replaces the byte-identical `go list -m -json all` loops previously copy-pasted in ssr, image/min and imagemin. Sibling to depfind: depfind maps the package import graph; modfind enumerates modules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Finder added in v0.0.2

type Finder struct {
	// contains filtered or unexported fields
}

Finder runs `go list -m -json all` once per rootDir and caches the result.

func New

func New() *Finder

New returns a Finder using the real `go list` runner.

func (*Finder) Dirs added in v0.0.2

func (f *Finder) Dirs(rootDir string) ([]string, error)

Dirs returns just the Dir of every discovered module — the []string shape ssr/image/imagemin previously produced. Order matches Discover.

func (*Finder) Discover added in v0.0.2

func (f *Finder) Discover(rootDir string) ([]Module, error)

Discover returns all modules visible from rootDir (cached after first call). Modules with no on-disk Dir are skipped.

func (*Finder) Refresh added in v0.0.2

func (f *Finder) Refresh(rootDir string)

Refresh invalidates the cache for rootDir (call after a go.mod change).

func (*Finder) Seed added in v0.0.2

func (f *Finder) Seed(rootDir string, mods []Module)

Seed pre-populates the cache for rootDir, bypassing `go list`. It is the cross-package test seam: a consumer's tests inject a fixed module set without a real toolchain round-trip. A subsequent Discover(rootDir) returns mods.

func (*Finder) SetLog added in v0.0.2

func (f *Finder) SetLog(fn func(...any))

SetLog sets the warning sink. If unset, warnings are discarded.

type Module added in v0.0.2

type Module struct {
	Path      string // import path, e.g. "github.com/veltylabs/item-catalog"
	Dir       string // absolute on-disk dir (cache path or local replace path)
	Version   string // empty for Main and for local replace targets
	IsMain    bool   // the root module of the project at rootDir
	IsReplace bool   // satisfied by a local (filesystem) replace directive
	Indirect  bool   // transitive dependency (not a direct require)
}

Module is one Go module on disk, classified for tooling.

func (Module) Writable added in v0.0.2

func (m Module) Writable() bool

Writable reports whether tooling may generate files inside Dir. True for the main module and for local replace targets; false for the read-only module cache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL