Skip to content

Commit 9f3ad13

Browse files
fix: toggle DENO_NO_PACKAGE_JSON conditionally (#4372)
* fix: toggle `DENO_NO_PACKAGE_JSON` conditionally * stamp: meowmeow * stamp: meow! * chore: update `main.ts` * chore: update `main.ts` * chore: move package json check to bundler * fix: handle custom import map path * chore: respect global default --------- Co-authored-by: Qiao Han <qiao@supabase.io>
1 parent a057b74 commit 9f3ad13

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

internal/functions/deploy/bundle.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
6262
cmd = append(cmd, function.BundleFlags...)
6363

6464
env := []string{}
65+
if !function.ShouldUsePackageJsonDiscovery(entrypoint, importMap, afero.NewIOFS(b.fsys)) {
66+
env = append(env, "DENO_NO_PACKAGE_JSON=1")
67+
}
6568
if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" {
6669
env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry)
6770
}

internal/functions/serve/templates/main.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ async function verifyJWT(jwt: string): Promise<boolean> {
116116
return true;
117117
}
118118

119+
// Ref: https://docs.deno.com/examples/checking_file_existence/
120+
async function shouldUsePackageJsonDiscovery({ entrypointPath, importMapPath }: FunctionConfig): Promise<boolean> {
121+
if (importMapPath) {
122+
return false
123+
}
124+
const packageJsonPath = posix.join(posix.dirname(entrypointPath), "package.json")
125+
try {
126+
await Deno.lstat(packageJsonPath);
127+
} catch (err) {
128+
if (err instanceof Deno.errors.NotFound) {
129+
return false
130+
}
131+
}
132+
return true
133+
}
134+
119135
Deno.serve({
120136
handler: async (req: Request) => {
121137
const url = new URL(req.url);
@@ -178,6 +194,7 @@ Deno.serve({
178194

179195
const absEntrypoint = posix.join(Deno.cwd(), functionsConfig[functionName].entrypointPath);
180196
const maybeEntrypoint = posix.toFileUrl(absEntrypoint).href;
197+
const usePackageJson = await shouldUsePackageJsonDiscovery(functionsConfig[functionName]);
181198

182199
const staticPatterns = functionsConfig[functionName].staticFiles;
183200

@@ -187,6 +204,7 @@ Deno.serve({
187204
memoryLimitMb,
188205
workerTimeoutMs,
189206
noModuleCache,
207+
noNpm: !usePackageJson,
190208
importMapPath: functionsConfig[functionName].importMapPath,
191209
envVars,
192210
forceCreate,

pkg/function/bundle.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
6868
cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...)
6969
cmd.Stderr = os.Stderr
7070
cmd.Stdout = os.Stdout
71+
if fsys, ok := b.fsys.(fs.StatFS); ok && !ShouldUsePackageJsonDiscovery(entrypoint, importMap, fsys) {
72+
cmd.Env = append(cmd.Environ(), "DENO_NO_PACKAGE_JSON=1")
73+
}
7174
if err := cmd.Run(); err != nil {
7275
return meta, errors.Errorf("failed to bundle function: %w", err)
7376
}
@@ -81,6 +84,17 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
8184
return meta, Compress(eszipBytes, output)
8285
}
8386

87+
func ShouldUsePackageJsonDiscovery(entrypoint, importMap string, fsys fs.StatFS) bool {
88+
if len(importMap) > 0 {
89+
return false
90+
}
91+
packageJsonPath := filepath.Join(filepath.Dir(entrypoint), "package.json")
92+
if _, err := fsys.Stat(packageJsonPath); errors.Is(err, os.ErrNotExist) {
93+
return false
94+
}
95+
return true
96+
}
97+
8498
const compressedEszipMagicID = "EZBR"
8599

86100
func Compress(r io.Reader, w io.Writer) error {

0 commit comments

Comments
 (0)