Skip to content

Commit 5314351

Browse files
authored
fix: handle deno import url separately (#3860)
1 parent dd54320 commit 5314351

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

pkg/function/deno.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"io/fs"
9+
"net/url"
910
"os"
1011
"path"
1112
"path/filepath"
@@ -54,7 +55,7 @@ func (m *ImportMap) Load(imPath string, fsys fs.FS, opts ...func(string, io.Read
5455
if err := m.Parse(data); err != nil {
5556
return err
5657
}
57-
if err := m.Resolve(imPath, fsys); err != nil {
58+
if err := m.Resolve(imPath); err != nil {
5859
return err
5960
}
6061
for _, apply := range opts {
@@ -74,29 +75,29 @@ func (m *ImportMap) Parse(data []byte) error {
7475
return nil
7576
}
7677

77-
func (m *ImportMap) Resolve(imPath string, fsys fs.FS) error {
78+
func (m *ImportMap) Resolve(imPath string) error {
7879
// Resolve all paths relative to current file
7980
for k, v := range m.Imports {
80-
m.Imports[k] = resolveHostPath(imPath, v, fsys)
81+
m.Imports[k] = resolveHostPath(imPath, v)
8182
}
8283
for module, mapping := range m.Scopes {
8384
for k, v := range mapping {
84-
m.Scopes[module][k] = resolveHostPath(imPath, v, fsys)
85+
m.Scopes[module][k] = resolveHostPath(imPath, v)
8586
}
8687
}
8788
return nil
8889
}
8990

90-
func resolveHostPath(jsonPath, hostPath string, fsys fs.FS) string {
91+
func resolveHostPath(jsonPath, hostPath string) string {
9192
// Leave absolute paths unchanged
9293
if path.IsAbs(hostPath) {
9394
return hostPath
9495
}
95-
resolved := path.Join(path.Dir(jsonPath), hostPath)
96-
if _, err := fs.Stat(fsys, filepath.FromSlash(resolved)); err != nil {
97-
// Leave URLs unchanged
96+
// Leave URLs unchanged
97+
if parsed, err := url.Parse(hostPath); err == nil && len(parsed.Scheme) > 0 {
9898
return hostPath
9999
}
100+
resolved := path.Join(path.Dir(jsonPath), hostPath)
100101
// Directory imports need to be suffixed with /
101102
// Ref: https://deno.com/[email protected]/basics/import_maps
102103
if strings.HasSuffix(hostPath, "/") {

pkg/function/deno_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestImportPaths(t *testing.T) {
5858
im := ImportMap{Imports: map[string]string{
5959
"module-name/": "../shared/",
6060
}}
61-
assert.NoError(t, im.Resolve("testdata/modules/deno.json", testImports))
61+
assert.NoError(t, im.Resolve("testdata/modules/deno.json"))
6262
// Run test
6363
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
6464
// Check error
@@ -79,7 +79,7 @@ func TestImportPaths(t *testing.T) {
7979
im := ImportMap{Imports: map[string]string{
8080
"module-name/": "./shared/",
8181
}}
82-
assert.NoError(t, im.Resolve("testdata/import_map.json", testImports))
82+
assert.NoError(t, im.Resolve("testdata/import_map.json"))
8383
// Run test
8484
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
8585
// Check error
@@ -116,7 +116,7 @@ func TestResolveImports(t *testing.T) {
116116
assert.Equal(t, "./common", resolved.Imports["root"])
117117
assert.Equal(t, "./supabase/tests", resolved.Imports["parent"])
118118
assert.Equal(t, "./supabase/functions/child/", resolved.Imports["child"])
119-
assert.Equal(t, "../missing", resolved.Imports["missing"])
119+
assert.Equal(t, "./supabase/missing", resolved.Imports["missing"])
120120
})
121121

122122
t.Run("resolves parent scopes", func(t *testing.T) {

0 commit comments

Comments
 (0)