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

Supabase Edge Functions - Error importing local files after upgrading to CLI v1.187.0+ #2862

Open
danablend opened this issue Nov 10, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@danablend
Copy link

Describe the bug
On Windows 11 using Docker and Supabase CLI to deploy a Supabase Edge Function, it fails to correctly import local files that are inside of the function directory (like a libs.ts file). I searched CLI versions to determine where this started, and I found that it works fine on CLI v1.186.3 - but when upgrading the CLI to version 1.187.0 or beyond, it is unable to import the local module, and can't boot the worker on Supabase.

It is perfectly reproducible on my machine, with only the CLI version upgrade to 1.187.0 and beyond causing the problem.

To Reproduce
Steps to reproduce the behavior:

  1. Under supabase/functions/test, create index.ts file
// supabase/functions/test/index.ts
import { processParams } from "./libs.ts";

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

        const processedParams = processParams({ foo });

        return new Response(
            JSON.stringify({
                success: true,
                processedParams,
            }),
            {
                headers: {
                    "Content-Type": "application/json",
                    "Access-Control-Allow-Origin": "*",
                },
            },
        );
    } catch (error) {
        return new Response(
            JSON.stringify({
                success: false,
            }),
            {
                status: 400,
                headers: {
                    "Content-Type": "application/json",
                    "Access-Control-Allow-Origin": "*",
                },
            },
        );
    }
});
  1. Under supabase/functions/test, create libs.ts file
// supabase/functions/test/libs.ts
type Params = {
    foo: string;
};

export const processParams = (params: Params) => {
    if (!params.foo) {
        throw new Error("Missing foo parameter");
    }

    return {
        foo: params.foo.toUpperCase(),
        timestamp: new Date().toISOString(),
    };
}; 
  1. Use scoop to install supabase v1.187.0
    scoop uninstall supabase && scoop install [email protected]

  2. Deploy the Supabase Edge Function
    supabase functions deploy test

  3. Make a POST request with the "foo" parameter included in the body. This will be the error response:
    {"code":"BOOT_ERROR","message":"Function failed to start (please check logs)"}

Inside of the function logs, it says this:
worker boot error: Module not found: file:///Users/danie/<redacted>/supabase/functions/test/libs.ts

  1. To fix, downgrade Supabase CLI to 1.186.3
    scoop uninstall [email protected] && scoop install [email protected]

  2. Deploy Supabase Edge Function again
    supabase functions deploy test

  3. Make a POST request with the "foo" parameter again. Now we see the correct response, and it has run successfully:
    {"success":true,"processedParams":{"foo":"BAR","timestamp":"2024-11-10T08:41:26.403Z"}}

Expected behavior
The Supabase Edge Function should be able to import local modules in the specific function's directory, using relative import syntax. E.g. import { processParams } from "./libs.ts"; should be valid and work, not causing errors. Or there should be clear guidelines on how to do it otherwise.

Screenshots
N/A

System information
Rerun the failing command with --create-ticket flag.

  • Ticket ID: [N/A (couldn't retrieve)]
  • Version of OS: [Windows 11 Pro]
  • Version of CLI: [v1.187.0 or above]
  • Version of Docker: [v25.0.3]
  • Versions of services:
        SERVICE IMAGE      │      LOCAL       │ LINKED
  ─────────────────────────┼──────────────────┼─────────
    supabase/postgres      │ 15.1.1.61        │ -
    supabase/gotrue        │ v2.151.0         │ -
    postgrest/postgrest    │ v12.2.0          │ -
    supabase/realtime      │ v2.29.15         │ -
    supabase/storage-api   │ v1.0.6           │ -
    supabase/edge-runtime  │ v1.55.0          │ -
    supabase/studio        │ 20240701-05dfbec │ -
    supabase/postgres-meta │ v0.83.2          │ -
    supabase/logflare      │ 1.4.0            │ -
    supabase/supavisor     │ 1.1.56           │ -

Additional context
I suspect this is the code where the issue was introduced:
d8761b5

In the latest version, it is here:
https://github.com/supabase/cli/blob/develop/internal/functions/deploy/bundle.go

From my understanding, it seems to work for people on MacOS or Linux. Perhaps it incorrectly maps the imports to the absolute paths in Windows, where it works fine on other OS?

Maybe this is intentional? If so, is there an explanation on how we can import local files correctly? I tried using import_maps.json and deno.json, and both of these lead to the exact same issue as described above.

Thanks!

@sweatybridge sweatybridge added the bug Something isn't working label Nov 15, 2024
@avallete
Copy link
Member

Hey there !

I've just tried this out with the latest 1.223.10 of the cli on a Windows11 machine and wasn't able to reproduce. So this might have been fixed by one of the edge-runtime update.

Could you please try again with latest version @danablend and report back ?

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

3 participants