Skip to content

Commit 67510a2

Browse files
fix(function): support multiline import type statements in import scanning (#4872)
* fix(function): support multiline import type statements in import scanning The regex pattern `.*?` does not match newlines, causing multiline `import type { X }` statements to be skipped during bind-mount file scanning. This results in type-only imports not being mounted in the Docker container, causing runtime errors like: worker boot error: Module not found "file:///.../types/MyType.ts" Changed `.*?` to `[\s\S]*?` to match any character including newlines, consistent with the `{[^{}]+}` pattern used for braced imports. This fix enables proper handling of Deno 2.x style multiline type imports: ```typescript import type { MyType, OtherType, } from './types.ts' ``` Includes test case to prevent regression. * test(function): add non-braced default type import to fixture Cover `import type Foo from '...'` pattern in the multiline import type test. This form also routes through the `[\s\S]*?` branch and was previously untested. * fix(function): use (?:type\s+)? for multiline import type matching Narrow the regex fix for multiline import type statements per PR review feedback. Instead of widening .*? to [\s\S]*? (which affects all import patterns), add (?:type\s+)? to consume the type keyword so braced imports route into the {[^{}]+} branch that already handles newlines. --------- Co-authored-by: Andrew Valleteau <avallete@users.noreply.github.com>
1 parent 9efbdaa commit 67510a2

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

pkg/function/deno.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func resolveHostPath(jsonPath, hostPath string) string {
111111
}
112112

113113
// Ref: https://regex101.com/r/DfBdJA/1
114-
var importPathPattern = regexp.MustCompile(`(?i)(?:import|export)\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](.*?)['"]|import\(\s*['"](.*?)['"]\)`)
114+
var importPathPattern = regexp.MustCompile(`(?i)(?:import|export)\s+(?:type\s+)?(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](.*?)['"]|import\(\s*['"](.*?)['"]\)`)
115115

116116
func (importMap *ImportMap) WalkImportPaths(srcPath string, readFile func(curr string, w io.Writer) error) error {
117117
seen := map[string]struct{}{}

pkg/function/deno_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ func TestImportPaths(t *testing.T) {
8686
assert.NoError(t, err)
8787
fsys.AssertExpectations(t)
8888
})
89+
90+
t.Run("iterates multiline import type statements", func(t *testing.T) {
91+
// This test verifies that multiline import type statements are correctly parsed.
92+
// The (?:type\s+)? group consumes the type keyword so braced imports hit the {[^{}]+} branch.
93+
// Setup in-memory fs
94+
fsys := MockFS{}
95+
fsys.On("ReadFile", "testdata/modules/import_types.ts").Once()
96+
fsys.On("ReadFile", "testdata/types/database.ts").Once()
97+
// Run test
98+
im := ImportMap{}
99+
err := im.WalkImportPaths("testdata/modules/import_types.ts", fsys.ReadFile)
100+
// Check error
101+
assert.NoError(t, err)
102+
fsys.AssertExpectations(t)
103+
})
89104
}
90105

91106
func TestResolveImports(t *testing.T) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test file for multiline import type statements
2+
// This pattern requires (?:type\s+)? to route braced imports into the {[^{}]+} branch
3+
4+
// Multiline import type - should be matched by the regex
5+
import type {
6+
Database,
7+
Json
8+
} from '../types/database.ts'
9+
10+
// Single line import type - should also work
11+
import type { Database as DB } from '../types/database.ts'
12+
13+
// Re-export type to verify export pattern
14+
export type { Database } from '../types/database.ts'
15+
16+
// Multiline export type
17+
export type {
18+
Json
19+
} from '../types/database.ts'
20+
21+
// Non-braced default type import - exercises the .*? branch on single-line
22+
import type Database from '../types/database.ts'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type Database = {
2+
public: {
3+
Tables: {
4+
users: {
5+
Row: { id: string; name: string }
6+
}
7+
}
8+
}
9+
}
10+
11+
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]

0 commit comments

Comments
 (0)