sourceload

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package sourceload provides file-finding and load-stack tracking for locating source files across virtual filesystems.

The package is focused on file-path traversal, file loading, and load-stack management — isolated from Scheme evaluation concerns.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = werr.NewStaticError("sourceload: file not found")

ErrNotFound is returned when no matching file can be located across all provided search directories.

Functions

func IsHidden

func IsHidden(name string) bool

IsHidden reports whether name is a hidden file or directory (starts with ".").

func Walk

func Walk(fsys fs.FS, searchDirs []string, accept func(name string) bool, fn func(relPath string)) error

Walk traverses fsys under each search directory, calling fn for every file where accept returns true. Hidden directories (name starting with ".") are skipped. Non-existent directories are silently skipped. Other root-level errors (permission, I/O) are propagated.

accept receives the filename (not the full path). fn receives the slash-separated path relative to the search directory.

No "." fallback — only walks directories explicitly provided. No deduplication — caller handles domain-specific identity. Per-directory errors are accumulated via errors.Join and returned alongside partial results. Individual file-level errors within a directory are skipped (best-effort enumeration).

Types

type Finder

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

Finder locates files within an fs.FS by searching an ordered list of directories. It supports an optional LoadStack for current-directory-relative resolution and an optional canonicalize function applied to resolved paths.

func NewFinder

func NewFinder(fsys fs.FS, searchDirs []string, opts ...FinderOption) *Finder

NewFinder constructs a Finder that searches fsys in the given directories. It panics if fsys is nil.

func (*Finder) Open

func (p *Finder) Open(name string) (fs.File, string, error)

Open searches for the named path and returns an open file, the resolved path, and nil on success. It returns ErrNotFound when no match is found.

Search order:

  1. stack.CurrentDir() — if stack is non-nil, CurrentDir is non-empty, and is not "."
  2. Each searchDir provided to NewFinder, in order
  3. FS root "."

The returned path goes through canonicalize (if set) before being returned. Returns ErrNotFound immediately for an empty path argument.

type FinderOption

type FinderOption func(*Finder)

FinderOption is a functional option for Finder.

func WithCanonicalize

func WithCanonicalize(fn func(string) string) FinderOption

WithCanonicalize sets a path transformation applied to every resolved path before it is returned from Open. It does not affect which file is opened — only the returned path string.

func WithStack

func WithStack(s *LoadStack) FinderOption

WithStack attaches a LoadStack to the Finder. When set, the directory of the currently-loading file is searched first (before explicit searchDirs).

type LoadStack

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

LoadStack tracks the chain of source files currently being loaded. Each Push records the path of a file being processed; Pop removes it when loading is complete. The stack enables directory-relative resolution of include paths — the current file's directory is the base for includes.

All methods are safe for concurrent use.

func NewLoadStack

func NewLoadStack() *LoadStack

NewLoadStack returns an empty LoadStack with pre-allocated capacity.

func (*LoadStack) Current

func (p *LoadStack) Current() string

Current returns the path at the top of the stack, or "" if the stack is empty.

func (*LoadStack) CurrentDir

func (p *LoadStack) CurrentDir() string

CurrentDir returns the directory component of the top path using slash-separated semantics (path.Dir, not filepath.Dir). Returns "" if the stack is empty.

func (*LoadStack) Depth

func (p *LoadStack) Depth() int

Depth returns the number of paths on the stack.

func (*LoadStack) Pop

func (p *LoadStack) Pop()

Pop removes the top path from the stack. It is a no-op if the stack is empty, which supports defer patterns where Pop may be called regardless of whether Push succeeded.

func (*LoadStack) Push

func (p *LoadStack) Push(path string)

Push records path as the current file being loaded. It panics if path is empty, as that indicates a programming error — only meaningful, non-empty paths should appear on the stack.

Jump to

Keyboard shortcuts

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