project

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package project resolves the project root selected by the current directory.

Index

Constants

View Source
const ConfigFileName = "grat.config"

ConfigFileName is the file that marks a directory as a grat project. It is declared once, here, because a scan that looks for one name and a writer that creates another would disagree about which directories are projects.

View Source
const MaxScanDepth = 6

MaxScanDepth is how far below a registered directory a scan looks for a project root.

A grat.config sits at the root of a project, and a registered directory is where projects are kept, so the marker is a small number of levels down. Going deeper than this only walks the contents of projects rather than finding more of them, and the contents are where the entry counts come from: an icon set alone measured 70,146 entries on one machine.

View Source
const MaxScanEntries = 250_000

MaxScanEntries is how many directory entries one scan may look at in total.

A development folder holds far more files than projects, so the bound is what keeps a scan from spending its time inside one of them: an icon set alone measured 70,146 entries on one machine, and an unpacked module tree measures more. It is one number rather than one per caller, because a scan that is bounded in one command and unbounded in another is unbounded.

Variables

View Source
var CurrentUser = os.Getuid

CurrentUser reports the account grat is running as.

It is a variable so a test can drive the checks below without a second account on the machine, which is the one thing they are about and the one thing a test cannot arrange.

View Source
var ErrConfigNotFound = errors.New("grat.config not found")

ErrConfigNotFound means no grat.config exists between the start path and the filesystem root.

Functions

func DeeperThanScan

func DeeperThanScan(root string, path string) bool

DeeperThanScan reports whether path lies further below root than a scan for project roots looks.

It is asked of a directory before descending into it, so a directory exactly at the limit is still read for its files and only its children are left out.

func FindRoot

func FindRoot(start string) (string, error)

FindRoot walks from start toward the filesystem root and returns the nearest directory that contains a regular grat.config file.

func FindRootBy

func FindRootBy(start string, holds func(directory string) (bool, error)) (string, error)

FindRootBy walks from start toward the filesystem root and returns the nearest directory that holds answers true for.

The walk is shared rather than repeated, because the rule about where it stops is the security-relevant part and a second copy of it would be a second answer to which directories may become a project.

func OwnedByCurrentUser

func OwnedByCurrentUser(path string) bool

OwnedByCurrentUser reports whether path belongs to the account grat runs as.

A path that cannot be inspected is not owned, rather than assumed to be: the answer decides whether a command from that file is executed.

func RefuseUnsafeConfig

func RefuseUnsafeConfig(info os.FileInfo, path string) error

RefuseUnsafeConfig reports a grat.config that grat will not run.

Two things disqualify one. It belongs to another account, so somebody else decided what it says. Or it is writable by its group or by everybody, so somebody else can decide later. A configuration names commands that run through /bin/sh as whoever typed the grat command, so both are the same question: who chose this.

git closed the same shape in CVE-2022-24765 and this follows it, including the direction: refuse and say why, rather than silently looking elsewhere.

It takes the description of an already-open file rather than a path, so the answer is about the bytes that will actually be read and there is no moment between the two in which the file could be exchanged. path is for the message.

func SkipsScanning

func SkipsScanning(name string) bool

SkipsScanning reports whether a directory of this name is walked when looking for project roots.

func Walk

func Walk(root string, maxEntries int, visit func(path string, entry fs.DirEntry) error) (int, error)

Walk visits every regular file below root that a scan for project roots is allowed to see.

It is the one traversal both the port registry and project discovery use, so that what counts as a project cannot differ between the command that finds one and the command that allocates its ports. The bounds are the same in both cases: no deeper than MaxScanDepth, never into a directory a tool wrote or a package manager unpacked, never through a symlink, and never past maxEntries so that a directory holding a whole development folder cannot make the scan run away.

visit sees every directory and every regular file that survives those bounds, so a caller decides for itself which of the two it cares about. Returning fs.SkipDir from it skips the rest of the current directory.

The number of entries visited comes back so that a caller walking several roots can spend one budget across all of them, which is what keeps the limit a statement about the whole scan rather than about each root separately.

Types

type ErrTooManyEntries

type ErrTooManyEntries struct {
	Root  string
	Limit int
}

ErrTooManyEntries says a scan was stopped because the directory holds more than it was allowed to look at, and names the limit so a caller can say which one was reached.

func (ErrTooManyEntries) Error

func (err ErrTooManyEntries) Error() string

Jump to

Keyboard shortcuts

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