edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

Self-hosted edge functions - Error when importing shared local files

Open neuquen opened this issue 2 years ago • 15 comments
trafficstars

Bug report

Describe the bug

Calling an edge function that imports local files from the _shared directory produces the following error:

{"msg":"InvalidWorkerCreation: worker boot error"}

Within the supabase-edge-functions container logs I see the following error:

Failed to load module: "file:///home/deno/functions/_shared/message.ts" - Invalid file path.
  Specifier: file:///home/deno/functions/_shared/message.ts

To Reproduce

  1. Follow the steps outlined under "Running Supabase" to download supabase and run using docker: https://supabase.com/docs/guides/self-hosting/docker#running-supabase
  2. Create the file /functions/_shared/message.ts:
export function message() {
  return 'Hello from Edge Functions!';
}
  1. Update /functions/hello/index.ts to import the function from that file:
import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { message } from "../_shared/message.ts";

serve(async () => {
  return new Response(
    `${message()}`,
    { headers: { "Content-Type": "application/json" } },
  )
})
  1. Run docker compose down and docker compose up to ensure the new file is present within the container.
  2. Issue an http request to the endpoint: http://localhost:8000/functions/v1/hello

Expected behavior

I'm expecting the message Hello from Edge Functions! to be displayed, but instead, I see {"msg":"InvalidWorkerCreation: worker boot error"}

System information

  • OS: Docker container on Ubuntu 22 (EC2 on AWS)

Additional context

If I move the _shared directory underneath hello: /functions/hello/_shared/, and alter the import statement within /hello/index.ts to import { message } from "./_shared/message.ts";, then it works fine.

neuquen avatar Jul 27 '23 20:07 neuquen

I have the same issue

AndriyBas avatar Aug 01 '23 06:08 AndriyBas

:tada: This issue has been resolved in version 1.8.2 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Aug 02 '23 01:08 github-actions[bot]

I have this issue too

ginsudev avatar Sep 19 '23 11:09 ginsudev

I also have this issue!

fraktalize avatar Oct 11 '23 21:10 fraktalize

@ginsudev @fraktalize Are you experiencing the issue when running via Supabase CLI? Have you tried upgrading to the latest version of the CLI?

If you're running edge-runtime directly, can you share your setup so I can try to reproduce the issue.

laktek avatar Oct 11 '23 21:10 laktek

having the same issue today, this is is not fixed yet.

drobles-atdev avatar Jan 18 '24 19:01 drobles-atdev

same issue :(

jb-thery avatar Feb 12 '24 13:02 jb-thery

Also same issue; just updated the CLI.

supabase-edge-runtime-1.33.5

thewebbeckons avatar Feb 12 '24 15:02 thewebbeckons

@jb-thery @thewebbeckons Are you trying to import from _shared directory in supabase/functions? What's the exact error message you get? Also, it would be handy if you can share the import statements in your function so I can try to reproduce it.

laktek avatar Feb 13 '24 00:02 laktek

@laktek Seems like mine was something different just reporting the same error message. Can disregard my comment; everything is resolved on my end.

thewebbeckons avatar Feb 14 '24 02:02 thewebbeckons

I have the same issue. Running 1.38.0. _shared is in the functions folder

Once I moved it into the folder where the function resides, the issue got resolved.

Here is the detailed error:


Download https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/class-transformer.js?dts
Uncaught SyntaxError: Invalid or unexpected token
    at file:///home/deno/functions/_shared/models/person-form-answer-option.ts:3:3
An error has occured
InvalidWorkerCreation: worker boot error
    at async UserWorker.create (ext:sb_user_workers/user_workers.js:141:15)
    at async Server.<anonymous> (file:///home/deno/main/index.ts:129:20)
    at async #respond (https://deno.land/[email protected]/http/server.ts:220:18) {
  name: "InvalidWorkerCreation"
}

Here is excerpt from my person-form-answer-option.ts:

import {
  Expose,
} from "https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/class-transformer.js?dts";

export class PersonFormAnswerOption {
  @Expose()
  public id!: string;

antonio-petrov avatar Mar 08 '24 15:03 antonio-petrov

having similar issues

rookledookle avatar Apr 29 '24 14:04 rookledookle

I am having the same issue also. I use the same folder setup as per https://supabase.com/docs/guides/functions/quickstart#organizing-your-edge-functions

My folder structure is as so:

  • supabase
    • functions
      • _shared
        • admin.ts
      • my-handler-1
        • index.ts
      • my-handler-2
        • index.ts
      • import_map.json

My import_map.json looks like: { "imports": { "@supabase": "npm:@supabase/[email protected]", "@admin": "./_shared/admin.ts", } }

If I import in either of the functions index.ts files something from @admin e.g. import { myFunction } from "@admin";

My IDE (VSCode) is happy, but I get the following runtime error when the function is invoked:

Using supabase-edge-runtime-1.45.2 (compatible with Deno v1.40.3) serving the request with /home/deno/functions/my-handler-1 serving the request with /home/deno/functions/my-handler-1 failed to load 'file:///home/deno/_shared/admin.ts': Module not found "file:///home/deno/_shared/admin.ts". An error has occured InvalidWorkerCreation: worker boot error failed to load 'file:///home/deno/_shared/admin.ts': Module not found "file:///home/deno/_shared/admin.ts". at async UserWorker.create (ext:sb_user_workers/user_workers.js:141:15) at async Object.handler (file:///home/deno/main/index.ts:147:22) at async handleHttp (ext:sb_core_main_js/js/http.js:112:15) { name: "InvalidWorkerCreation" }

I have tired running the function by specifying the import_map.json file as an arg, by config.toml and also without specifying anything about an import_map.json file and I can't get it to work.

e,g. npx supabase functions serve my-handler-1 --env-file ./.env --import-map ./supabase/functions/import_map.json

Only by moving the admin.ts file into the my-handler-1 folder makes the import work, however this obviously makes it unable to be shared to my-handler-2/index.ts

Has anyone else been able to share a local module between 2 functions successfully? If so, could you please post an example.

questicles avatar May 09 '24 04:05 questicles

Same issue, please fix it or suggest solution please.

s-borah avatar May 14 '24 18:05 s-borah

Not sure if this issue is directly related, but I get this error when importing json file locally using the debugger:

Using supabase-edge-runtime-1.54.6 (compatible with Deno v1.43.0)
serving the request with /{~~~masked~~~}/supabase/functions/create_notification
Debugger listening on ws://0.0.0.0:8083/ws/b1c0f068-52c0-4f8c-81fe-24c19aa84c30
Visit chrome://inspect to connect to the debugger.
Worker is waiting for debugger to connect.
Uncaught SyntaxError: Unexpected non-whitespace character after JSON at position 89 (line 3 column 2)
An error has occured
InvalidWorkerCreation: worker boot error Uncaught SyntaxError: Unexpected non-whitespace character after JSON at position 89 (line 3 column 2)
    at async UserWorker.create (ext:sb_user_workers/user_workers.js:155:15)
    at async Object.handler (file:///home/deno/index.ts:147:22)
    at async respond (ext:sb_core_main_js/js/http.js:163:14) {
  name: "InvalidWorkerCreation"
}

using this code:

import serviceAccount from "../test.json" with { type: "json" };

with this test file

{
  "project_id": "projectid", "private_key": "abcd", "client_email": "[email protected]"
}

Seems like something do with the end of file character (line 3 column 2)... but there's nothing there... i have tried adding an extra line or deleting any whitespace after it, still fires the same issue.

This only occurs when using the debugger, or the ''--inspect" flag.

seadynamic8 avatar Jun 20 '24 15:06 seadynamic8

same issue here

ophelie-gaudin avatar Nov 05 '24 10:11 ophelie-gaudin