From eccd5991c22fecc6bac85b57cca7f8a296e91ffd Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Wed, 29 Oct 2025 20:54:34 +0900 Subject: [PATCH 1/8] fix: toggle `DENO_NO_PACKAGE_JSON` conditionally --- internal/functions/deploy/bundle.go | 11 ++++++++++- internal/functions/deploy/bundle_test.go | 2 ++ internal/functions/deploy/deploy.go | 6 ++++++ pkg/config/config.go | 11 ++++++----- pkg/function/api.go | 2 +- pkg/function/batch.go | 2 +- pkg/function/batch_test.go | 2 +- pkg/function/bundle.go | 10 +++++++++- pkg/function/bundle_test.go | 2 ++ 9 files changed, 38 insertions(+), 10 deletions(-) diff --git a/internal/functions/deploy/bundle.go b/internal/functions/deploy/bundle.go index 13b9a6360..99412d493 100644 --- a/internal/functions/deploy/bundle.go +++ b/internal/functions/deploy/bundle.go @@ -25,7 +25,7 @@ func NewDockerBundler(fsys afero.Fs) function.EszipBundler { return &dockerBundler{fsys: fsys} } -func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (function.FunctionDeployMetadata, error) { +func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (function.FunctionDeployMetadata, error) { meta := function.NewMetadata(slug, entrypoint, importMap, staticFiles) fmt.Fprintln(os.Stderr, "Bundling Function:", utils.Bold(slug)) cwd, err := os.Getwd() @@ -62,9 +62,18 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap cmd = append(cmd, function.BundleFlags...) env := []string{} + denoNoPackageJsonValue := "1" + if usePackageJson { + denoNoPackageJsonValue = "0" + } if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" { env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry) } + if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" { + env = append(env, "DENO_NO_PACKAGE_JSON="+deno_no_package_json) + } else { + env = append(env, "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue) + } // Run bundle if err := utils.DockerRunOnceWithConfig( ctx, diff --git a/internal/functions/deploy/bundle_test.go b/internal/functions/deploy/bundle_test.go index 5933e77dc..de2343f2b 100644 --- a/internal/functions/deploy/bundle_test.go +++ b/internal/functions/deploy/bundle_test.go @@ -59,6 +59,7 @@ func TestDockerBundle(t *testing.T) { filepath.Join("hello", "index.ts"), filepath.Join("hello", "deno.json"), []string{filepath.Join("hello", "data.pdf")}, + false, &body, ) // Check error @@ -86,6 +87,7 @@ func TestDockerBundle(t *testing.T) { "hello/index.ts", "", nil, + false, nil, ) // Check error diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index bcee53c55..051ea0b5a 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -136,6 +136,12 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, functionsUsingDeprecatedGlobalFallback = append(functionsUsingDeprecatedGlobalFallback, name) } } + packageJsonPath := filepath.Join(functionDir, "package.json") + packageJsonExists := false + if _, err := fsys.Stat(packageJsonPath); err == nil { + packageJsonExists = true + } + function.UsePackageJson = len(function.ImportMap) == 0 && packageJsonExists if noVerifyJWT != nil { function.VerifyJWT = !*noVerifyJWT } diff --git a/pkg/config/config.go b/pkg/config/config.go index 531bc3dea..8000616f0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -200,11 +200,12 @@ type ( FunctionConfig map[string]function function struct { - Enabled bool `toml:"enabled" json:"-"` - VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"` - ImportMap string `toml:"import_map" json:"importMapPath,omitempty"` - Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"` - StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"` + Enabled bool `toml:"enabled" json:"-"` + UsePackageJson bool `toml:"-"` + VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"` + ImportMap string `toml:"import_map" json:"importMapPath,omitempty"` + Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"` + StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"` } analytics struct { diff --git a/pkg/function/api.go b/pkg/function/api.go index df4443240..8663a419c 100644 --- a/pkg/function/api.go +++ b/pkg/function/api.go @@ -24,7 +24,7 @@ type FunctionDeployMetadata struct { } type EszipBundler interface { - Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) + Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) } func NewEdgeRuntimeAPI(project string, client api.ClientWithResponses, opts ...withOption) EdgeRuntimeAPI { diff --git a/pkg/function/batch.go b/pkg/function/batch.go index fad409853..e4ab8cdda 100644 --- a/pkg/function/batch.go +++ b/pkg/function/batch.go @@ -60,7 +60,7 @@ OUTER: } } var body bytes.Buffer - meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, &body) + meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, function.UsePackageJson, &body) if errors.Is(err, ErrNoDeploy) { fmt.Fprintln(os.Stderr, "Skipping undeployable Function:", slug) continue diff --git a/pkg/function/batch_test.go b/pkg/function/batch_test.go index d47b28be4..8015bea82 100644 --- a/pkg/function/batch_test.go +++ b/pkg/function/batch_test.go @@ -18,7 +18,7 @@ import ( type MockBundler struct { } -func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) { +func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) { if staticFiles == nil { staticFiles = []string{} } diff --git a/pkg/function/bundle.go b/pkg/function/bundle.go index fb13ae64c..817478a03 100644 --- a/pkg/function/bundle.go +++ b/pkg/function/bundle.go @@ -48,7 +48,7 @@ var ( } ) -func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) { +func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) { meta := NewMetadata(slug, entrypoint, importMap, staticFiles) outputPath := filepath.Join(b.tempDir, slug+".eszip") // TODO: make edge runtime write to stdout @@ -65,9 +65,17 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap defer cancel() // release resources if command exits before timeout ctx = timeoutCtx } + denoNoPackageJsonValue := "1" + if usePackageJson { + denoNoPackageJsonValue = "0" + } + cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout + if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" { + cmd.Env = append(os.Environ(), "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue) + } if err := cmd.Run(); err != nil { return meta, errors.Errorf("failed to bundle function: %w", err) } diff --git a/pkg/function/bundle_test.go b/pkg/function/bundle_test.go index 1f16b87f4..d84e4c76d 100644 --- a/pkg/function/bundle_test.go +++ b/pkg/function/bundle_test.go @@ -48,6 +48,7 @@ func TestBundleFunction(t *testing.T) { "hello/index.ts", "hello/deno.json", []string{"hello/data.pdf"}, + true, &body, ) // Check error @@ -78,6 +79,7 @@ func TestBundleFunction(t *testing.T) { "hello/index.ts", "", nil, + true, &body, ) // Check error From e9a2d739f11caab22f4bbbe1d602215c2e4deb6a Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Thu, 30 Oct 2025 08:58:40 +0900 Subject: [PATCH 2/8] stamp: meowmeow --- pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 8000616f0..5d8a69cc6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -201,7 +201,7 @@ type ( function struct { Enabled bool `toml:"enabled" json:"-"` - UsePackageJson bool `toml:"-"` + UsePackageJson bool `toml:"-" join:"-"` VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"` ImportMap string `toml:"import_map" json:"importMapPath,omitempty"` Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"` From 8848e4998f9bbc19893aa7348c62d06144394e40 Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Thu, 30 Oct 2025 09:29:33 +0900 Subject: [PATCH 3/8] stamp: meow! --- pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 5d8a69cc6..9fce2772c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -201,7 +201,7 @@ type ( function struct { Enabled bool `toml:"enabled" json:"-"` - UsePackageJson bool `toml:"-" join:"-"` + UsePackageJson bool `toml:"-" json:"-"` VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"` ImportMap string `toml:"import_map" json:"importMapPath,omitempty"` Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"` From 27988b5ad71c2190b1dca9c8570f0217abe7cf3b Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Tue, 4 Nov 2025 11:29:46 +0900 Subject: [PATCH 4/8] chore: update `main.ts` --- internal/functions/serve/templates/main.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/functions/serve/templates/main.ts b/internal/functions/serve/templates/main.ts index 568c547f8..ed6c09cbc 100644 --- a/internal/functions/serve/templates/main.ts +++ b/internal/functions/serve/templates/main.ts @@ -116,6 +116,17 @@ async function verifyJWT(jwt: string): Promise { return true; } +async function shouldUsePackageJsonDiscovery(absDir: string): Promise { + const [a, b, c, d] = (await Promise.allSettled([ + Deno.stat(posix.join(absDir, "deno.json")), + Deno.stat(posix.join(absDir, "deno.jsonc")), + Deno.stat(posix.join(absDir, "import_map.json")), + Deno.stat(posix.join(absDir, "package.json")), + ])).map(v => v.status === "fulfilled"); + + return !a && !b && !c && d; +} + Deno.serve({ handler: async (req: Request) => { const url = new URL(req.url); @@ -178,6 +189,7 @@ Deno.serve({ const absEntrypoint = posix.join(Deno.cwd(), functionsConfig[functionName].entrypointPath); const maybeEntrypoint = posix.toFileUrl(absEntrypoint).href; + const usePackageJsonDiscovery = await shouldUsePackageJsonDiscovery(absEntrypoint); const staticPatterns = functionsConfig[functionName].staticFiles; @@ -187,6 +199,7 @@ Deno.serve({ memoryLimitMb, workerTimeoutMs, noModuleCache, + noNpm: !usePackageJsonDiscovery, importMapPath: functionsConfig[functionName].importMapPath, envVars, forceCreate, From c739890439ad838ba37e414c63f421b7d7150e3a Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Wed, 5 Nov 2025 08:42:04 +0900 Subject: [PATCH 5/8] chore: update `main.ts` --- internal/functions/serve/templates/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/functions/serve/templates/main.ts b/internal/functions/serve/templates/main.ts index ed6c09cbc..67a80567a 100644 --- a/internal/functions/serve/templates/main.ts +++ b/internal/functions/serve/templates/main.ts @@ -189,7 +189,7 @@ Deno.serve({ const absEntrypoint = posix.join(Deno.cwd(), functionsConfig[functionName].entrypointPath); const maybeEntrypoint = posix.toFileUrl(absEntrypoint).href; - const usePackageJsonDiscovery = await shouldUsePackageJsonDiscovery(absEntrypoint); + const usePackageJsonDiscovery = await shouldUsePackageJsonDiscovery(posix.dirname(absEntrypoint)); const staticPatterns = functionsConfig[functionName].staticFiles; From 526c38f12d646cb429751870e7f12ac4aeed675c Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Thu, 13 Nov 2025 15:51:22 +0800 Subject: [PATCH 6/8] chore: move package json check to bundler --- internal/functions/deploy/bundle.go | 13 ++++--------- internal/functions/deploy/bundle_test.go | 2 -- internal/functions/deploy/deploy.go | 6 ------ pkg/config/config.go | 11 +++++------ pkg/function/api.go | 2 +- pkg/function/batch.go | 2 +- pkg/function/batch_test.go | 2 +- pkg/function/bundle.go | 14 ++++++-------- pkg/function/bundle_test.go | 2 -- 9 files changed, 18 insertions(+), 36 deletions(-) diff --git a/internal/functions/deploy/bundle.go b/internal/functions/deploy/bundle.go index 99412d493..3fcfe4691 100644 --- a/internal/functions/deploy/bundle.go +++ b/internal/functions/deploy/bundle.go @@ -25,7 +25,7 @@ func NewDockerBundler(fsys afero.Fs) function.EszipBundler { return &dockerBundler{fsys: fsys} } -func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (function.FunctionDeployMetadata, error) { +func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (function.FunctionDeployMetadata, error) { meta := function.NewMetadata(slug, entrypoint, importMap, staticFiles) fmt.Fprintln(os.Stderr, "Bundling Function:", utils.Bold(slug)) cwd, err := os.Getwd() @@ -62,17 +62,12 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap cmd = append(cmd, function.BundleFlags...) env := []string{} - denoNoPackageJsonValue := "1" - if usePackageJson { - denoNoPackageJsonValue = "0" - } if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" { env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry) } - if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" { - env = append(env, "DENO_NO_PACKAGE_JSON="+deno_no_package_json) - } else { - env = append(env, "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue) + packageJsonPath := filepath.Join(filepath.Dir(entrypoint), "package.json") + if exists, _ := afero.Exists(b.fsys, packageJsonPath); !exists { + env = append(env, "DENO_NO_PACKAGE_JSON=1") } // Run bundle if err := utils.DockerRunOnceWithConfig( diff --git a/internal/functions/deploy/bundle_test.go b/internal/functions/deploy/bundle_test.go index de2343f2b..5933e77dc 100644 --- a/internal/functions/deploy/bundle_test.go +++ b/internal/functions/deploy/bundle_test.go @@ -59,7 +59,6 @@ func TestDockerBundle(t *testing.T) { filepath.Join("hello", "index.ts"), filepath.Join("hello", "deno.json"), []string{filepath.Join("hello", "data.pdf")}, - false, &body, ) // Check error @@ -87,7 +86,6 @@ func TestDockerBundle(t *testing.T) { "hello/index.ts", "", nil, - false, nil, ) // Check error diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index 051ea0b5a..bcee53c55 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -136,12 +136,6 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, functionsUsingDeprecatedGlobalFallback = append(functionsUsingDeprecatedGlobalFallback, name) } } - packageJsonPath := filepath.Join(functionDir, "package.json") - packageJsonExists := false - if _, err := fsys.Stat(packageJsonPath); err == nil { - packageJsonExists = true - } - function.UsePackageJson = len(function.ImportMap) == 0 && packageJsonExists if noVerifyJWT != nil { function.VerifyJWT = !*noVerifyJWT } diff --git a/pkg/config/config.go b/pkg/config/config.go index 9fce2772c..531bc3dea 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -200,12 +200,11 @@ type ( FunctionConfig map[string]function function struct { - Enabled bool `toml:"enabled" json:"-"` - UsePackageJson bool `toml:"-" json:"-"` - VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"` - ImportMap string `toml:"import_map" json:"importMapPath,omitempty"` - Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"` - StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"` + Enabled bool `toml:"enabled" json:"-"` + VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"` + ImportMap string `toml:"import_map" json:"importMapPath,omitempty"` + Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"` + StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"` } analytics struct { diff --git a/pkg/function/api.go b/pkg/function/api.go index 8663a419c..df4443240 100644 --- a/pkg/function/api.go +++ b/pkg/function/api.go @@ -24,7 +24,7 @@ type FunctionDeployMetadata struct { } type EszipBundler interface { - Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) + Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) } func NewEdgeRuntimeAPI(project string, client api.ClientWithResponses, opts ...withOption) EdgeRuntimeAPI { diff --git a/pkg/function/batch.go b/pkg/function/batch.go index e4ab8cdda..fad409853 100644 --- a/pkg/function/batch.go +++ b/pkg/function/batch.go @@ -60,7 +60,7 @@ OUTER: } } var body bytes.Buffer - meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, function.UsePackageJson, &body) + meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, &body) if errors.Is(err, ErrNoDeploy) { fmt.Fprintln(os.Stderr, "Skipping undeployable Function:", slug) continue diff --git a/pkg/function/batch_test.go b/pkg/function/batch_test.go index 8015bea82..d47b28be4 100644 --- a/pkg/function/batch_test.go +++ b/pkg/function/batch_test.go @@ -18,7 +18,7 @@ import ( type MockBundler struct { } -func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) { +func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) { if staticFiles == nil { staticFiles = []string{} } diff --git a/pkg/function/bundle.go b/pkg/function/bundle.go index 817478a03..7abef1a60 100644 --- a/pkg/function/bundle.go +++ b/pkg/function/bundle.go @@ -48,7 +48,7 @@ var ( } ) -func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) { +func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) { meta := NewMetadata(slug, entrypoint, importMap, staticFiles) outputPath := filepath.Join(b.tempDir, slug+".eszip") // TODO: make edge runtime write to stdout @@ -65,16 +65,14 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap defer cancel() // release resources if command exits before timeout ctx = timeoutCtx } - denoNoPackageJsonValue := "1" - if usePackageJson { - denoNoPackageJsonValue = "0" - } - cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout - if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" { - cmd.Env = append(os.Environ(), "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue) + if fsys, ok := b.fsys.(fs.StatFS); ok { + packageJsonPath := filepath.Join(filepath.Dir(entrypoint), "package.json") + if _, err := fsys.Stat(packageJsonPath); errors.Is(err, os.ErrNotExist) { + cmd.Env = append(cmd.Environ(), "DENO_NO_PACKAGE_JSON=1") + } } if err := cmd.Run(); err != nil { return meta, errors.Errorf("failed to bundle function: %w", err) diff --git a/pkg/function/bundle_test.go b/pkg/function/bundle_test.go index d84e4c76d..1f16b87f4 100644 --- a/pkg/function/bundle_test.go +++ b/pkg/function/bundle_test.go @@ -48,7 +48,6 @@ func TestBundleFunction(t *testing.T) { "hello/index.ts", "hello/deno.json", []string{"hello/data.pdf"}, - true, &body, ) // Check error @@ -79,7 +78,6 @@ func TestBundleFunction(t *testing.T) { "hello/index.ts", "", nil, - true, &body, ) // Check error From 6e4f1c5b50556d2ad80fc3dc171feac0a2176d41 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Thu, 13 Nov 2025 16:39:57 +0800 Subject: [PATCH 7/8] fix: handle custom import map path --- internal/functions/deploy/bundle.go | 7 ++---- internal/functions/serve/templates/main.ts | 27 +++++++++++++--------- pkg/function/bundle.go | 17 ++++++++++---- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/internal/functions/deploy/bundle.go b/internal/functions/deploy/bundle.go index 3fcfe4691..cf367dee6 100644 --- a/internal/functions/deploy/bundle.go +++ b/internal/functions/deploy/bundle.go @@ -61,14 +61,11 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap } cmd = append(cmd, function.BundleFlags...) - env := []string{} + usePackageJson := function.ShouldUsePackageJsonDiscovery(entrypoint, importMap, afero.NewIOFS(b.fsys)) + env := []string{fmt.Sprintf("DENO_NO_PACKAGE_JSON=%t", !usePackageJson)} if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" { env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry) } - packageJsonPath := filepath.Join(filepath.Dir(entrypoint), "package.json") - if exists, _ := afero.Exists(b.fsys, packageJsonPath); !exists { - env = append(env, "DENO_NO_PACKAGE_JSON=1") - } // Run bundle if err := utils.DockerRunOnceWithConfig( ctx, diff --git a/internal/functions/serve/templates/main.ts b/internal/functions/serve/templates/main.ts index 67a80567a..4d5430efe 100644 --- a/internal/functions/serve/templates/main.ts +++ b/internal/functions/serve/templates/main.ts @@ -116,15 +116,20 @@ async function verifyJWT(jwt: string): Promise { return true; } -async function shouldUsePackageJsonDiscovery(absDir: string): Promise { - const [a, b, c, d] = (await Promise.allSettled([ - Deno.stat(posix.join(absDir, "deno.json")), - Deno.stat(posix.join(absDir, "deno.jsonc")), - Deno.stat(posix.join(absDir, "import_map.json")), - Deno.stat(posix.join(absDir, "package.json")), - ])).map(v => v.status === "fulfilled"); - - return !a && !b && !c && d; +// Ref: https://docs.deno.com/examples/checking_file_existence/ +async function shouldUsePackageJsonDiscovery({ entrypointPath, importMapPath }: FunctionConfig): Promise { + if (importMapPath) { + return false + } + const packageJsonPath = posix.join(posix.dirname(entrypointPath), "package.json") + try { + await Deno.lstat(packageJsonPath); + } catch (err) { + if (err instanceof Deno.errors.NotFound) { + return false + } + } + return true } Deno.serve({ @@ -189,7 +194,7 @@ Deno.serve({ const absEntrypoint = posix.join(Deno.cwd(), functionsConfig[functionName].entrypointPath); const maybeEntrypoint = posix.toFileUrl(absEntrypoint).href; - const usePackageJsonDiscovery = await shouldUsePackageJsonDiscovery(posix.dirname(absEntrypoint)); + const usePackageJson = await shouldUsePackageJsonDiscovery(functionsConfig[functionName]); const staticPatterns = functionsConfig[functionName].staticFiles; @@ -199,7 +204,7 @@ Deno.serve({ memoryLimitMb, workerTimeoutMs, noModuleCache, - noNpm: !usePackageJsonDiscovery, + noNpm: !usePackageJson, importMapPath: functionsConfig[functionName].importMapPath, envVars, forceCreate, diff --git a/pkg/function/bundle.go b/pkg/function/bundle.go index 7abef1a60..970a75635 100644 --- a/pkg/function/bundle.go +++ b/pkg/function/bundle.go @@ -69,10 +69,8 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout if fsys, ok := b.fsys.(fs.StatFS); ok { - packageJsonPath := filepath.Join(filepath.Dir(entrypoint), "package.json") - if _, err := fsys.Stat(packageJsonPath); errors.Is(err, os.ErrNotExist) { - cmd.Env = append(cmd.Environ(), "DENO_NO_PACKAGE_JSON=1") - } + usePackageJson := ShouldUsePackageJsonDiscovery(entrypoint, importMap, fsys) + cmd.Env = append(cmd.Environ(), fmt.Sprintf("DENO_NO_PACKAGE_JSON=%t", !usePackageJson)) } if err := cmd.Run(); err != nil { return meta, errors.Errorf("failed to bundle function: %w", err) @@ -87,6 +85,17 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap return meta, Compress(eszipBytes, output) } +func ShouldUsePackageJsonDiscovery(entrypoint, importMap string, fsys fs.StatFS) bool { + if len(importMap) > 0 { + return false + } + packageJsonPath := filepath.Join(filepath.Dir(entrypoint), "package.json") + if _, err := fsys.Stat(packageJsonPath); errors.Is(err, os.ErrNotExist) { + return false + } + return true +} + const compressedEszipMagicID = "EZBR" func Compress(r io.Reader, w io.Writer) error { From e70f78d9243ac7e98e5d2bdc6cb7c4cb6c0cfc57 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Thu, 13 Nov 2025 16:48:55 +0800 Subject: [PATCH 8/8] chore: respect global default --- internal/functions/deploy/bundle.go | 6 ++++-- pkg/function/bundle.go | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/functions/deploy/bundle.go b/internal/functions/deploy/bundle.go index cf367dee6..0b91b5377 100644 --- a/internal/functions/deploy/bundle.go +++ b/internal/functions/deploy/bundle.go @@ -61,8 +61,10 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap } cmd = append(cmd, function.BundleFlags...) - usePackageJson := function.ShouldUsePackageJsonDiscovery(entrypoint, importMap, afero.NewIOFS(b.fsys)) - env := []string{fmt.Sprintf("DENO_NO_PACKAGE_JSON=%t", !usePackageJson)} + env := []string{} + if !function.ShouldUsePackageJsonDiscovery(entrypoint, importMap, afero.NewIOFS(b.fsys)) { + env = append(env, "DENO_NO_PACKAGE_JSON=1") + } if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" { env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry) } diff --git a/pkg/function/bundle.go b/pkg/function/bundle.go index 970a75635..04ec2c240 100644 --- a/pkg/function/bundle.go +++ b/pkg/function/bundle.go @@ -68,9 +68,8 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout - if fsys, ok := b.fsys.(fs.StatFS); ok { - usePackageJson := ShouldUsePackageJsonDiscovery(entrypoint, importMap, fsys) - cmd.Env = append(cmd.Environ(), fmt.Sprintf("DENO_NO_PACKAGE_JSON=%t", !usePackageJson)) + if fsys, ok := b.fsys.(fs.StatFS); ok && !ShouldUsePackageJsonDiscovery(entrypoint, importMap, fsys) { + cmd.Env = append(cmd.Environ(), "DENO_NO_PACKAGE_JSON=1") } if err := cmd.Run(); err != nil { return meta, errors.Errorf("failed to bundle function: %w", err)