Skip to content

Commit

Permalink
fix(functions): always resolve absolute path in container
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Apr 18, 2024
1 parent 1639067 commit f262424
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
23 changes: 9 additions & 14 deletions internal/utils/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,37 +278,32 @@ func resolveHostPath(hostPath string, fsys afero.Fs) string {
if filepath.IsAbs(hostPath) {
return getModulePath(hostPath)
}
cwd, err := os.Getwd()
if err != nil {
return hostPath
}
rel := filepath.Join(FunctionsDir, hostPath)
if strings.HasPrefix(rel, FunctionsDir) {
return hostPath
}
rebased := filepath.Join(cwd, rel)
exists, err := afero.Exists(fsys, rebased)
exists, err := afero.Exists(fsys, rel)
if err != nil {
logger := GetDebugLogger()
fmt.Fprintln(logger, err)
}
if !exists {
return hostPath
}
if strings.HasPrefix(rel, FunctionsDir) {
suffix := strings.TrimPrefix(rel, FunctionsDir)
return path.Join(DockerFuncDirPath, filepath.ToSlash(suffix))
}
// Directory imports need to be suffixed with /
// Ref: https://deno.com/[email protected]/basics/import_maps
if strings.HasSuffix(hostPath, "/") {
rel += "/"
if strings.HasSuffix(hostPath, string(filepath.Separator)) {
rel += string(filepath.Separator)
}
return getModulePath(rel)
}

func getModulePath(hostPath string) string {
mod := path.Join(DockerModsDir, GetPathHash(hostPath))
if strings.HasSuffix(hostPath, "/") {
if strings.HasSuffix(hostPath, string(filepath.Separator)) {
mod += "/"
}
if ext := filepath.Ext(hostPath); len(ext) > 0 {
} else if ext := filepath.Ext(hostPath); len(ext) > 0 {
mod += ext
}
return mod
Expand Down
10 changes: 4 additions & 6 deletions internal/utils/deno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ func TestResolveImports(t *testing.T) {
}
// Setup in-memory fs
fsys := afero.NewMemMapFs()
cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, fsys.Mkdir(filepath.Join(cwd, "common"), 0755))
require.NoError(t, fsys.Mkdir(filepath.Join(cwd, DbTestsDir), 0755))
require.NoError(t, fsys.Mkdir(filepath.Join(cwd, FunctionsDir, "child"), 0755))
require.NoError(t, fsys.Mkdir("common", 0755))
require.NoError(t, fsys.Mkdir(DbTestsDir, 0755))
require.NoError(t, fsys.Mkdir(filepath.Join(FunctionsDir, "child"), 0755))
// Run test
resolved := importMap.Resolve(fsys)
// Check error
assert.Equal(t, "/home/deno/modules/ac351c7174c8f47a9a9056bd96bcd71cfb980c906daee74ab9bce8308c68b811/", resolved.Imports["abs/"])
assert.Equal(t, "/home/deno/modules/92a5dc04bd6f9fb8f29f8066fed8a5c1e81bc59ad48a11283b63736867e4f2a8", resolved.Imports["root"])
assert.Equal(t, "/home/deno/modules/faaed96206118cf98625ea8065b6b3864f8cf9484814c423b58ebaa9b2d1e47b", resolved.Imports["parent"])
assert.Equal(t, "child", resolved.Imports["child"])
assert.Equal(t, "/home/deno/functions/child", resolved.Imports["child"])
assert.Equal(t, "../missing", resolved.Imports["missing"])
})

Expand Down

0 comments on commit f262424

Please sign in to comment.