Skip to content

Commit 25b3f77

Browse files
Merge 12ee3c0 into a9e893b
2 parents a9e893b + 12ee3c0 commit 25b3f77

5 files changed

Lines changed: 308 additions & 51 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'e2b': patch
3+
'@e2b/python-sdk': patch
4+
---
5+
6+
Match BuildKit when filtering the template build context with `.dockerignore`: a leading `**` followed by literal characters (e.g. `**.txt`) now matches at any depth, a negated pattern matching only a parent directory (e.g. `src/app.ts` then `!src`) no longer re-includes the path, and in the JS SDK `?` and bracket expressions match characters outside the BMP (e.g. emoji) as a single character.

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

Lines changed: 141 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,64 @@ const WILDCARD_CHARS = /[*?[\\]/
2525
const backslashIsSeparator = () => path.sep === '\\'
2626

2727
function escapeRegex(ch: string): string {
28-
return ch.replace(/[.*+?^${}()|[\]\\/-]/g, '\\$&')
28+
return ch.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')
29+
}
30+
31+
// Escapes that keep their regex meaning in a bracket expression, like in Python
32+
const CLASS_ESCAPES = new Set('dDsSwWfnrtv')
33+
const CLASS_CONTROL_ESCAPES: Record<string, string> = { a: '\\x07', b: '\\x08' }
34+
const CLASS_HEX_ESCAPE_LENGTHS: Record<string, number> = { x: 2, u: 4, U: 8 }
35+
// Characters that must stay escaped in a bracket expression with the `u` flag
36+
const CLASS_SYNTAX_CHARS = new Set('^$\\.*+?()[]{}|/-')
37+
38+
// Make a bracket expression body valid with the regex `u` flag
39+
function translateClass(body: string, backslashIsEscape: boolean): string {
40+
let cls = ''
41+
let i = 0
42+
while (i < body.length) {
43+
const ch = body[i++]
44+
if (ch !== '\\') {
45+
cls += ch
46+
} else if (!backslashIsEscape || i >= body.length) {
47+
cls += '\\\\'
48+
} else {
49+
const next = body[i++]
50+
if (CLASS_ESCAPES.has(next)) {
51+
cls += '\\' + next
52+
} else if (next in CLASS_CONTROL_ESCAPES) {
53+
cls += CLASS_CONTROL_ESCAPES[next]
54+
} else if (next in CLASS_HEX_ESCAPE_LENGTHS) {
55+
const length = CLASS_HEX_ESCAPE_LENGTHS[next]
56+
const hex = body.slice(i, i + length)
57+
if (hex.length < length || !/^[0-9a-fA-F]+$/.test(hex)) {
58+
throw new Error(`incomplete escape \\${next}${hex}`)
59+
}
60+
const code = parseInt(hex, 16)
61+
if (code > 0x10ffff) {
62+
throw new Error(`bad escape \\${next}${hex}`)
63+
}
64+
cls += `\\u{${code.toString(16)}}`
65+
i += length
66+
} else if (/[0-7]/.test(next)) {
67+
let octal = next
68+
while (octal.length < 3 && /[0-7]/.test(body[i] ?? '')) {
69+
octal += body[i++]
70+
}
71+
const code = parseInt(octal, 8)
72+
if (code > 0o377) {
73+
throw new Error(
74+
`octal escape value \\${octal} outside of range 0-0o377`
75+
)
76+
}
77+
cls += `\\u{${code.toString(16)}}`
78+
} else if (/[A-Za-z0-9]/.test(next)) {
79+
throw new Error(`bad escape \\${next}`)
80+
} else {
81+
cls += CLASS_SYNTAX_CHARS.has(next) ? '\\' + next : next
82+
}
83+
}
84+
}
85+
return cls
2986
}
3087

3188
// Equivalent of Go's filepath.Clean followed by filepath.ToSlash
@@ -36,12 +93,25 @@ function clean(pattern: string): string {
3693
return path.posix.normalize(pattern).replace(/(.)\/$/, '$1')
3794
}
3895

39-
function compile(pattern: string): RegExp {
96+
type Matcher = (p: string) => boolean
97+
98+
enum MatchType {
99+
Exact = 'exact',
100+
Prefix = 'prefix',
101+
Suffix = 'suffix',
102+
Regex = 'regex',
103+
}
104+
105+
// Like moby, use plain string checks for patterns without wildcards and only
106+
// fall back to a regex otherwise
107+
function compile(pattern: string): Matcher {
40108
let regex = '^'
109+
let matchType = MatchType.Exact
41110
const n = pattern.length
42111
const backslashIsEscape = !backslashIsSeparator()
43112
let i = 0
44113
while (i < n) {
114+
const first = i === 0
45115
const ch = pattern[i++]
46116
if (ch === '*') {
47117
if (pattern[i] === '*') {
@@ -50,14 +120,26 @@ function compile(pattern: string): RegExp {
50120
if (pattern[i] === '/') {
51121
i++
52122
}
53-
regex += i >= n ? '.*' : '(.*/)?'
123+
if (i >= n) {
124+
regex += '.*'
125+
matchType =
126+
matchType === MatchType.Exact ? MatchType.Prefix : MatchType.Regex
127+
} else {
128+
regex += '(.*/)?'
129+
matchType = MatchType.Regex
130+
}
131+
if (first) {
132+
matchType = MatchType.Suffix
133+
}
54134
} else {
55135
regex += '[^/]*'
136+
matchType = MatchType.Regex
56137
}
57138
} else if (ch === '?') {
58139
regex += '[^/]'
140+
matchType = MatchType.Regex
59141
} else if (ch === '[') {
60-
// Copy a bracket expression as is, a leading "^" negates it
142+
// Copy a bracket expression, a leading "^" negates it
61143
let j = i
62144
if (pattern[j] === '^') {
63145
j++
@@ -68,28 +150,54 @@ function compile(pattern: string): RegExp {
68150
}
69151
j++
70152
}
71-
regex += pattern.slice(i - 1, j + 1)
153+
regex +=
154+
'[' +
155+
translateClass(pattern.slice(i, Math.min(j, n)), backslashIsEscape) +
156+
(j < n ? ']' : '')
72157
i = j + 1
158+
matchType = MatchType.Regex
159+
} else if (ch === ']') {
160+
regex += '\\]'
161+
matchType = MatchType.Regex
73162
} else if (LITERAL_REGEX_CHARS.has(ch)) {
74163
regex += '\\' + ch
75164
} else if (ch === '\\' && backslashIsEscape) {
76165
// Escape the next character
77166
if (i < n) {
78167
regex += escapeRegex(pattern[i++])
168+
matchType = MatchType.Regex
79169
} else {
80170
regex += '\\\\'
81171
}
82172
} else {
83173
regex += ch
84174
}
85175
}
86-
return new RegExp(regex + '$')
176+
177+
switch (matchType) {
178+
case MatchType.Exact:
179+
return (p) => p === pattern
180+
case MatchType.Prefix: {
181+
const prefix = pattern.slice(0, -2)
182+
return (p) => p.startsWith(prefix)
183+
}
184+
case MatchType.Suffix: {
185+
const suffix = pattern.slice(2)
186+
// "**/foo" also matches "foo"
187+
return (p) =>
188+
p.endsWith(suffix) || (suffix[0] === '/' && p === suffix.slice(1))
189+
}
190+
case MatchType.Regex: {
191+
const re = new RegExp(regex + '$', 'u')
192+
return (p) => re.test(p)
193+
}
194+
}
87195
}
88196

89197
interface Pattern {
90198
exclusion: boolean
91199
dirs: string[]
92-
regex: RegExp
200+
match: Matcher
93201
}
94202

95203
/**
@@ -119,43 +227,48 @@ export class PatternMatcher {
119227
if (pattern.length > 1 && pattern.startsWith('/')) {
120228
pattern = pattern.slice(1)
121229
}
122-
let regex: RegExp
230+
let match: Matcher
123231
try {
124-
regex = compile(pattern)
232+
match = compile(pattern)
125233
} catch (err) {
126234
throw new TemplateError(
127235
`Invalid ignore pattern '${original}': ${(err as Error).message}`
128236
)
129237
}
130-
this.patterns.push({ exclusion, dirs: pattern.split('/'), regex })
238+
this.patterns.push({ exclusion, dirs: pattern.split('/'), match })
131239
}
132240
}
133241

134242
/**
135-
* Whether the path or one of its parent directories is excluded.
243+
* Whether the path is excluded. Like BuildKit, the patterns are evaluated
244+
* on each parent directory first, and a pattern that matched a parent
245+
* directory also matches the paths under it.
136246
*
137247
* @param p Slash-separated path relative to the context root
138248
* @returns True if the path is excluded
139249
*/
140250
matches(p: string): boolean {
141-
const parentPath = path.posix.dirname(p)
142-
const parentDirs = parentPath === '.' ? [] : parentPath.split('/')
143-
251+
const segments = p.split('/')
252+
let parentMatched: boolean[] = []
144253
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-
}
254+
for (let depth = 1; depth <= segments.length; depth++) {
255+
const current = segments.slice(0, depth).join('/')
256+
matched = false
257+
parentMatched = this.patterns.map((pattern, i) => {
258+
let match = parentMatched[i] ?? false
259+
if (!match) {
260+
// An inclusion can't change an already matched path, and an
261+
// exclusion can't change a path that hasn't matched yet
262+
if (pattern.exclusion !== matched) {
263+
return false
264+
}
265+
match = pattern.match(current)
266+
}
267+
if (match) {
268+
matched = !pattern.exclusion
269+
}
270+
return match
271+
})
159272
}
160273
return matched
161274
}

‎packages/js-sdk/tests/template/utils/getAllFilesInPath.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,34 @@ describe('getAllFilesInPath', () => {
413413
])
414414
})
415415

416+
test('should not re-include a path through a negated parent directory', async () => {
417+
const files = await relativePaths('.', ['src/app.ts', '**/*.js', '!src'])
418+
expect(files).not.toContain('src/app.ts')
419+
expect(files).not.toContain('src/node_modules/lib.js')
420+
expect(files).toContain('src/app.spec.ts')
421+
})
422+
423+
test('should match a leading globstar with a literal suffix', async () => {
424+
for (const name of ['root.txt', 'src/nested.txt', 'keep.txt.bak']) {
425+
await writeFile(join(testDir, name), 'x')
426+
}
427+
const files = await relativePaths('.', ['**.txt', '**/generated'])
428+
expect(files).not.toContain('root.txt')
429+
expect(files).not.toContain('src/nested.txt')
430+
expect(files).not.toContain('src/generated/api.ts')
431+
expect(files).toContain('keep.txt.bak')
432+
})
433+
434+
test('should match characters outside the BMP as a single character', async () => {
435+
for (const name of ['😀.txt', '😀😀.txt']) {
436+
await writeFile(join(testDir, name), 'x')
437+
}
438+
const files = await relativePaths('*', ['?.txt'])
439+
expect(files).not.toContain('😀.txt')
440+
expect(files).toContain('😀😀.txt')
441+
expect(await relativePaths('*', ['[😀]?.txt'])).not.toContain('😀😀.txt')
442+
})
443+
416444
test('should make absolute patterns inside the context relative', async () => {
417445
const files = await relativePaths('.', [
418446
join(testDir, 'src'),
@@ -438,6 +466,23 @@ describe('getAllFilesInPath', () => {
438466
)
439467
})
440468

469+
test.skipIf(process.platform === 'win32')(
470+
'should handle escapes in a bracket expression like Python',
471+
async () => {
472+
await expect(
473+
getAllFilesInPath('.', testDir, ['[\\q]'])
474+
).rejects.toThrow("Invalid ignore pattern '[\\q]'")
475+
await writeFile(join(testDir, 'a'), 'x')
476+
expect(await relativePaths('*', ['[\\a]'])).toContain('a')
477+
await writeFile(join(testDir, 'b'), 'x')
478+
await writeFile(join(testDir, 'c'), 'x')
479+
const files = await relativePaths('*', ['[\\x61]', '[\\142]'])
480+
expect(files).not.toContain('a')
481+
expect(files).not.toContain('b')
482+
expect(files).toContain('c')
483+
}
484+
)
485+
441486
test('should keep the files hash stable when ignored files change', async () => {
442487
const hash = () =>
443488
calculateFilesHash('.', '/app', testDir, ['.git'], false, undefined)

0 commit comments

Comments
 (0)