supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.09k stars 212 forks source link

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

Open danablend opened 2 weeks ago

danablend commented 2 weeks ago

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": "*",
            },
        },
    );
}

});


2. 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(),
};

};


3. Use scoop to install supabase v1.187.0
`scoop uninstall supabase && scoop install supabase@1.187.0`

4. Deploy the Supabase Edge Function
`supabase functions deploy test`

5. 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`

6. To fix, downgrade Supabase CLI to 1.186.3
`scoop uninstall supabase@1.187.0 && scoop install supabase@1.186.3`

7. Deploy Supabase Edge Function again
`supabase functions deploy test`

8. 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:
https://github.com/supabase/cli/commit/d8761b5620cb99f98c178918df3ebd8180cf5560

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!
avallete commented 5 days ago

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 ?