Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use cache for Deno functions: "Could not set the Deno root directory". #428

Open
2 tasks done
digisomni opened this issue Oct 24, 2024 · 1 comment
Open
2 tasks done
Labels
bug Something isn't working

Comments

@digisomni
Copy link

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

[Error] Error bundling script https://raw.githubusercontent.com/vircadia/vircadia-world-sdk-ts/refs/heads/master/seed/babylon/seed.ts: Error: Could not set the Deno root directory
    at assert (https://jsr.io/@deno/cache-dir/0.8.0/util.ts:5:11)
    at new DenoDir (https://jsr.io/@deno/cache-dir/0.8.0/deno_dir.ts:11:5)
    at createCache (https://jsr.io/@deno/cache-dir/0.8.0/mod.ts:42:19)
    at bundle (https://jsr.io/@deno/emit/0.45.0/mod.ts:61:19)
    at file:///Users/aurora/Documents/Projects/Vircadia/vircadia-world-server/modules/supabase/app/supabase/functions/bundle/index.ts:41:31
    at Array.map (<anonymous>)
    at Object.handler (file:///Users/aurora/Documents/Projects/Vircadia/vircadia-world-server/modules/supabase/app/supabase/functions/bundle/index.ts:34:52)
    at eventLoopTick (ext:core/01_core.js:168:7)
    at async respond (ext:sb_core_main_js/js/http.js:163:14)

To Reproduce

Serve this function and call it with a .ts script to bundle:

import { bundle } from "jsr:@deno/emit";

// Set reasonable limits for bundling operations
const BUNDLE_TIMEOUT = 60000; // 60 seconds
const MAX_URLS = 50; // Maximum number of URLs to process

Deno.serve(async (req) => {
    try {
        const { urls } = await req.json();

        // Validate input
        if (!Array.isArray(urls)) {
            return new Response(
                JSON.stringify({ error: "URLs must be provided as an array" }),
                {
                    status: 400,
                    headers: { "Content-Type": "application/json" },
                },
            );
        }

        // Enforce URL limit
        if (urls.length > MAX_URLS) {
            return new Response(
                JSON.stringify({
                    error: `Cannot process more than ${MAX_URLS} URLs at once`,
                }),
                {
                    status: 400,
                    headers: { "Content-Type": "application/json" },
                },
            );
        }

        // Process URLs with timeout protection
        const compiledScripts = await Promise.all(
            urls.map(async (url: string) => {
                try {
                    // Create a timeout promise
                    const timeoutPromise = new Promise((_, reject) => {
                        setTimeout(
                            () =>
                                reject(
                                    new Error(`Bundling timed out for ${url}`),
                                ),
                            BUNDLE_TIMEOUT,
                        );
                    });

                    // Bundle with timeout
                    const bundlePromise = bundle(new URL(url), {
                        allowRemote: true,
                    });

                    const result = await Promise.race([
                        bundlePromise,
                        timeoutPromise,
                    ]);

                    return {
                        url,
                        code: result.code,
                        size: result.code.length,
                    };
                } catch (error) {
                    console.error(`Error bundling script ${url}:`, error);
                    return {
                        url,
                        error: `Bundling error: ${error.message}`,
                        stack: Deno.env.get("ENVIRONMENT") === "development"
                            ? error.stack
                            : undefined,
                    };
                }
            }),
        );

        return new Response(
            JSON.stringify({
                scripts: compiledScripts,
                timestamp: new Date().toISOString(),
            }),
            {
                headers: {
                    "Content-Type": "application/json",
                    "Cache-Control": "public, max-age=3600",
                },
            },
        );
    } catch (error) {
        console.error("Server error:", error);
        return new Response(
            JSON.stringify({
                error: error.message,
                stack: Deno.env.get("ENVIRONMENT") === "development"
                    ? error.stack
                    : undefined,
            }),
            {
                status: 500,
                headers: { "Content-Type": "application/json" },
            },
        );
    }
});

Expected behavior

The Edge Runtime needs to (if not already) allow for Edge Functions to use a temporary cache in order to operate nominally.

System information

  • OS: macOS (Supabase CLI 1.207.9)
@digisomni digisomni added the bug Something isn't working label Oct 24, 2024
@digisomni digisomni changed the title Cannot use cache for Deno functions "Could not set the Deno root directory". Cannot use cache for Deno functions: "Could not set the Deno root directory". Oct 24, 2024
@kallebysantos
Copy link
Contributor

Hi @digisomni,
If I understood it well you probably is looking for something like the new Ephemeral Storage feature.
So your case you may should invoke bundle() like that:

// Bundle with Ephemeral Storage
const bundlePromise = bundle(new URL(url), {
    allowRemote: true,
    cacheRoot: '/tmp'
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants