Skip to content

Commit 4d088fb

Browse files
Merge 0aca725 into c05a642
2 parents c05a642 + 0aca725 commit 4d088fb

12 files changed

Lines changed: 953 additions & 43 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'e2b': minor
3+
'@e2b/python-sdk': minor
4+
---
5+
6+
Apply `.dockerignore` and `fileIgnorePatterns` / `file_ignore_patterns` the way Docker does when copying files into a template, so ignored files are no longer uploaded or included in the files hash:
7+
8+
- A pattern that matches a directory (`node_modules`, `.git`, `dist/`) now excludes everything under it, and a leading `/` is ignored.
9+
- `!` patterns re-include paths; the last matching pattern wins.
10+
- In Python, patterns now also apply when the copied path contains `.` or `..` segments (for example `copy(".")`).
11+
- `fileIgnorePatterns` / `file_ignore_patterns` are applied after the `.dockerignore` lines, so they take precedence.
12+
- Absolute patterns pointing into the context directory are treated as relative to it.
13+
- Brace expansion (`{a,b}`) is not supported, as in Docker. In JS, `fileIgnorePatterns` such as `**/*.{env,pem}` previously expanded and now match `{env,pem}` literally; list each pattern separately instead (`**/*.env`, `**/*.pem`).
14+
- In Python, copying a symlink to a directory copies the link itself instead of the directory's contents, as in JS.
15+
- Copying a path inside an ignored directory now fails with "No files found", as in Docker. An invalid pattern (such as an unterminated `[`) now raises an error.
16+
17+
Directory sizes are no longer part of the files hash, since they depend on the filesystem rather than on the copied files. The files hash of `copy()` steps that copy directories or are affected by ignore patterns changes once, so those steps are rebuilt on the next build.
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/**
2+
* Matching of `.dockerignore` patterns, following the semantics Docker uses to
3+
* filter the build context.
4+
*
5+
* Port of the pattern matcher from moby/patternmatcher (Apache-2.0):
6+
* https://github.com/moby/patternmatcher
7+
*/
8+
import path from 'node:path'
9+
import { TemplateError } from '../errors'
10+
11+
// Characters that have a meaning in a regex but not in a Docker pattern
12+
const LITERAL_REGEX_CHARS = new Set([
13+
'.',
14+
'+',
15+
'(',
16+
')',
17+
'|',
18+
'{',
19+
'}',
20+
'$',
21+
'^',
22+
])
23+
const WILDCARD_CHARS = /[*?[\\]/
24+
// Evaluated lazily, as `node:path` is not available when loaded in the browser
25+
const backslashIsSeparator = () => path.sep === '\\'
26+
27+
function escapeRegex(ch: string): string {
28+
return ch.replace(/[.*+?^${}()|[\]\\/-]/g, '\\$&')
29+
}
30+
31+
// Equivalent of Go's filepath.Clean followed by filepath.ToSlash
32+
function clean(pattern: string): string {
33+
if (backslashIsSeparator()) {
34+
pattern = pattern.replace(/\\/g, '/')
35+
}
36+
return path.posix.normalize(pattern).replace(/(.)\/$/, '$1')
37+
}
38+
39+
function compile(pattern: string): RegExp {
40+
let regex = '^'
41+
const n = pattern.length
42+
const backslashIsEscape = !backslashIsSeparator()
43+
let i = 0
44+
while (i < n) {
45+
const ch = pattern[i++]
46+
if (ch === '*') {
47+
if (pattern[i] === '*') {
48+
i++
49+
// Treat "**/" as "**"
50+
if (pattern[i] === '/') {
51+
i++
52+
}
53+
regex += i >= n ? '.*' : '(.*/)?'
54+
} else {
55+
regex += '[^/]*'
56+
}
57+
} else if (ch === '?') {
58+
regex += '[^/]'
59+
} else if (ch === '[') {
60+
// Copy a bracket expression as is, a leading "^" negates it
61+
let j = i
62+
if (pattern[j] === '^') {
63+
j++
64+
}
65+
while (j < n && pattern[j] !== ']') {
66+
if (pattern[j] === '\\' && backslashIsEscape) {
67+
j++
68+
}
69+
j++
70+
}
71+
regex += pattern.slice(i - 1, j + 1)
72+
i = j + 1
73+
} else if (LITERAL_REGEX_CHARS.has(ch)) {
74+
regex += '\\' + ch
75+
} else if (ch === '\\' && backslashIsEscape) {
76+
// Escape the next character
77+
if (i < n) {
78+
regex += escapeRegex(pattern[i++])
79+
} else {
80+
regex += '\\\\'
81+
}
82+
} else {
83+
regex += ch
84+
}
85+
}
86+
return new RegExp(regex + '$')
87+
}
88+
89+
interface Pattern {
90+
exclusion: boolean
91+
dirs: string[]
92+
regex: RegExp
93+
}
94+
95+
/**
96+
* Match paths relative to the context root against `.dockerignore` patterns.
97+
*
98+
* A pattern that matches a directory excludes everything under it, a leading
99+
* `/` is ignored, and `!` patterns re-include paths (the last matching
100+
* pattern wins).
101+
*/
102+
export class PatternMatcher {
103+
private readonly patterns: Pattern[] = []
104+
105+
constructor(patterns: string[]) {
106+
for (const original of patterns) {
107+
let pattern = original.trim()
108+
if (!pattern || pattern.startsWith('#')) {
109+
continue
110+
}
111+
const exclusion = pattern.startsWith('!')
112+
if (exclusion) {
113+
pattern = pattern.slice(1).trim()
114+
if (!pattern) {
115+
continue
116+
}
117+
}
118+
pattern = clean(pattern)
119+
if (pattern.length > 1 && pattern.startsWith('/')) {
120+
pattern = pattern.slice(1)
121+
}
122+
let regex: RegExp
123+
try {
124+
regex = compile(pattern)
125+
} catch (err) {
126+
throw new TemplateError(
127+
`Invalid ignore pattern '${original}': ${(err as Error).message}`
128+
)
129+
}
130+
this.patterns.push({ exclusion, dirs: pattern.split('/'), regex })
131+
}
132+
}
133+
134+
/**
135+
* Whether the path or one of its parent directories is excluded.
136+
*
137+
* @param p Slash-separated path relative to the context root
138+
* @returns True if the path is excluded
139+
*/
140+
matches(p: string): boolean {
141+
const parentPath = path.posix.dirname(p)
142+
const parentDirs = parentPath === '.' ? [] : parentPath.split('/')
143+
144+
let matched = false
145+
for (const pattern of this.patterns) {
146+
// An inclusion can't change an already matched path, and an
147+
// exclusion can't change a path that hasn't matched yet
148+
if (pattern.exclusion !== matched) {
149+
continue
150+
}
151+
const match =
152+
pattern.regex.test(p) ||
153+
parentDirs.some((_, i) =>
154+
pattern.regex.test(parentDirs.slice(0, i + 1).join('/'))
155+
)
156+
if (match) {
157+
matched = !pattern.exclusion
158+
}
159+
}
160+
return matched
161+
}
162+
163+
/**
164+
* Whether a `!` pattern could re-include a path under the directory,
165+
* in which case an excluded directory must still be walked.
166+
*
167+
* @param dirPath Slash-separated directory path relative to the context root
168+
* @returns True if a path under the directory could be re-included
169+
*/
170+
mayMatchUnder(dirPath: string): boolean {
171+
const dirSegments = dirPath.split('/')
172+
return this.patterns.some(
173+
(pattern) =>
174+
pattern.exclusion && patternMayMatchUnder(pattern, dirSegments)
175+
)
176+
}
177+
}
178+
179+
function patternMayMatchUnder(pattern: Pattern, dirSegments: string[]) {
180+
for (let i = 0; i < dirSegments.length; i++) {
181+
if (i >= pattern.dirs.length) {
182+
return false
183+
}
184+
const segment = pattern.dirs[i]
185+
if (segment.includes('**')) {
186+
return true
187+
}
188+
if (!WILDCARD_CHARS.test(segment) && segment !== dirSegments[i]) {
189+
return false
190+
}
191+
}
192+
return pattern.dirs.length > dirSegments.length
193+
}

‎packages/js-sdk/src/template/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,8 @@ export class TemplateBase
11331133
url,
11341134
headers,
11351135
ignorePatterns: [
1136-
...this.fileIgnorePatterns,
11371136
...readDockerignore(this.fileContextPath.toString()),
1137+
...this.fileIgnorePatterns,
11381138
],
11391139
resolveSymlinks: instruction.resolveSymlinks ?? RESOLVE_SYMLINKS,
11401140
gzip: instruction.gzip ?? GZIP,
@@ -1224,10 +1224,10 @@ export class TemplateBase
12241224
dest,
12251225
this.fileContextPath.toString(),
12261226
[
1227-
...this.fileIgnorePatterns,
12281227
...(runtime === 'browser'
12291228
? []
12301229
: readDockerignore(this.fileContextPath.toString())),
1230+
...this.fileIgnorePatterns,
12311231
],
12321232
instruction.resolveSymlinks ?? RESOLVE_SYMLINKS,
12331233
stackTrace

‎packages/js-sdk/src/template/types.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export type TemplateOptions = {
1414
*/
1515
fileContextPath?: PathLike
1616
/**
17-
* Array of glob patterns to ignore when copying files.
17+
* Patterns in `.dockerignore` syntax for files to exclude when copying.
18+
* They are applied after the `.dockerignore` file in the context directory, so they take precedence over it.
19+
* An invalid pattern (such as an unterminated `[`) throws a `TemplateError`.
20+
* Brace expansion (`{a,b}`) is not supported, as in Docker.
1821
*/
1922
fileIgnorePatterns?: string[]
2023
}

0 commit comments

Comments
 (0)