IgnoreFile
Parses .gitignore and .dockerignore files into a PathFilter, which
matches root-relative paths without accessing the file system. To find the
paths an ignore file keeps, pass it to FileSystem.glob, or restrict a
selection with it through PathFilter.restrict.
Details
In both formats the last matching line wins and ! adds an exception. They
differ otherwise:
- Git lines apply below their
scopedirectory. A line with a/other than a trailing one is anchored to the scope; otherwise it matches a name at any depth. A trailing/matches only directories. An exception cannot re-include a path inside an excluded directory. - Docker lines are anchored at the build context root,
**may appear anywhere, a trailing/is ignored, and an exception can re-include a path inside an excluded directory.
Callers find and read the ignore files. For Git, combine them with
PathFilter.concat from lowest to highest precedence: core.excludesFile,
.git/info/exclude, then each .gitignore from the root down with its
directory as scope. For Docker, use <Dockerfile>.dockerignore next to
the Dockerfile if it exists, otherwise the context root's .dockerignore.
Gotchas
Git compatibility has limits. Names are matched by Unicode code point rather
than by byte, so ? and negated classes such as [!a] match a non-ASCII
character that Git would not. A / inside a bracket class makes the line
match nothing. Matching is case-sensitive (core.ignorecase is not
supported) and tracked files are not considered.
Docker patterns use / on every platform. Patterns that Docker would read
as regular expression syntax fail with a ParseError; see
dockerignore.
Constructors
Parses the contents of a .dockerignore file into a PathFilter.
Details
Lines are read as Docker reads them: a line starting with # is a comment,
surrounding whitespace is trimmed, and each pattern is cleaned, so ./a,
/a and a/ all mean a. ** matches any number of characters including
/ wherever it appears.
Every other line becomes one rule with subtree: "inherit", in order: an
exclusion, or for a ! line an inclusion. As in BuildKit, a pattern that
matches a directory also applies to every path inside it, but only if it
changes the directory's result at that point in the file: in d,
!d/keep, d the third line does not, so d/keep is kept.
Fails with a ParseError where Docker rejects the file, such as for a
lone ! or an unclosed [.
Gotchas
Docker reads some patterns as Go regular expression syntax rather than glob syntax. These fail with a ParseError; each has a working spelling:
*or?inside a bracket class, such as[*], which Docker reads as part of a larger expression. Write[\*]or[\?]for the literal character.- A trailing backslash, such as
a*\, which Docker reads as a literal$. Write\for a literal backslash. ^outside a bracket class, which Docker reads as an anchor. Write\^.- A backslash before a letter or digit, such as
\d, which Docker reads as a regular expression class, and a backslash before a non-ASCII character, which Docker rejects. Write the character without the backslash.
Unknown character class names, such as [[:foo:]], also fail, because Docker
cannot compile them. The supported names are alnum, alpha, ascii,
blank, cntrl, digit, graph, lower, print, punct, space,
upper, word and xdigit, each optionally negated as [:^name:].
Signature
declare function dockerignore(source: string): Result<PathFilter, ParseError>Example Re-including a file
import { IgnoreFile, PathFilter, Result } from "effect"
const filter = Result.getOrThrow(IgnoreFile.dockerignore("node_modules\n!node_modules/keep.txt\n"))
PathFilter.excludes(filter, "node_modules/pkg/index.js") // => truePathFilter.excludes(filter, "node_modules/keep.txt") // => falseResult.isFailure(IgnoreFile.dockerignore("!")) // => trueParses the contents of a .gitignore file into a PathFilter.
Details
scope is the /-separated directory, relative to the root, that contains
the file; it defaults to the root. Lines only apply to paths strictly below
it, and its segments are matched literally.
Lines are read as Git reads them: blank lines and lines starting with #
are skipped, \# and \! escape a leading # or !, and trailing spaces
are dropped unless escaped with \. Only **/, /** and /**/ are
special; any other ** is a plain *. Malformed lines, such as an unclosed
[ or a trailing ``, match nothing, as in Git, and produce no rule.
Every other line becomes one rule, in order: an exclusion with subtree: "barrier", or for a ! line an inclusion with subtree: "self".
Gotchas
A scope with an empty, . or .. segment is a defect.
Signature
declare function gitignore(source: string, options?: { readonly scope?: string;}): PathFilterExample Matching Git rules
import { IgnoreFile, PathFilter } from "effect"
const filter = IgnoreFile.gitignore("node_modules/\n*.log\n!keep.log\n")
PathFilter.excludes(filter, "node_modules", { directory: true }) // => truePathFilter.excludes(filter, "src/debug.log") // => truePathFilter.excludes(filter, "keep.log") // => false// a file named `node_modules` is not matched by the directory rulePathFilter.excludes(filter, "node_modules") // => falseExample Combining nested .gitignore files
import { IgnoreFile, PathFilter } from "effect"
const filter = PathFilter.concat( IgnoreFile.gitignore("*.log\n"), IgnoreFile.gitignore("!/keep.log\n", { scope: "logs" }))
PathFilter.excludes(filter, "keep.log") // => truePathFilter.excludes(filter, "logs/keep.log") // => falsePathFilter.excludes(filter, "logs/other.log") // => trueErrors
The error returned by dockerignore for a pattern that Docker rejects or reads as regular expression syntax.
Details
line is the 1-based line number in the source, counting blank and comment
lines, and pattern is that line without surrounding whitespace.
Signature
declare class ParseError extends YieldableError<this> & { readonly _tag: "IgnoreFileParseError";} & Readonly<{ readonly line: number; readonly message: string; readonly pattern: string;}> { constructor(args: { readonly line: number; readonly message: string; readonly pattern: string; });}