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

serve <name> command errors when importing local files #1093

Closed
2 tasks done
bombillazo opened this issue May 12, 2023 · 25 comments · Fixed by #1104
Closed
2 tasks done

serve <name> command errors when importing local files #1093

bombillazo opened this issue May 12, 2023 · 25 comments · Fixed by #1104
Labels
bug Something isn't working

Comments

@bombillazo
Copy link

bombillazo commented May 12, 2023

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

When running supabase functions serve, if a function imports a local file specified in the import_map.json file that is outside the function folder, the command fails. When running an edge-function using the deno run --allow-all ./supabase/functions/my-function/index.ts command, one is able to test and run the function.

...
Download https://deno.land/x/[email protected]/lib/check_p2s.ts
Download https://deno.land/x/[email protected]/runtime/bogus.ts
Download https://deno.land/x/[email protected]/runtime/subtle_rsaes.ts
error: Module not found "file:///home/deno/lib/cors.ts".
    at file:///home/deno/functions/my-function/index.ts:9:29
2023/05/12 11:18:51 Sent Header: Host [docker]
2023/05/12 11:18:51 Sent Header: User-Agent [Go-http-client/1.1]
2023/05/12 11:18:51 Send Done
2023/05/12 11:18:51 Recv First Byte

To Reproduce

  1. Create a new edge function directory and index.ts inside the ./supabase/functions dir.
  2. Create a util file outside the new function directory. For example, inside ./supabase/lib/cors.ts.
  3. Import cors.ts inside the index.ts file.
// index.ts
import cors from '@backend/lib/cors.ts';
...
  1. Specify the path to the util file in import_map.json relative to the location of the import_map.json file
// this file is inside ./supabase
{
  "imports": {
    "@backend/": "./",
}
  1. Run supabase functions serve my-function

Expected behavior

The local edge function runs successfully as it does in deno.

Screenshots

N/A

System information

  • OS: [e.g. macOS, Windows]
  • Browser (if applies) [e.g. chrome, safari]
  • Version of supabase-js: [e.g. 2.21.0]
  • Version of supabase cli: [e.g. 1.57.3]
  • Version of Node.js: [e.g. 16]

Additional context

Add any other context about the problem here.

@sweatybridge
Copy link
Contributor

The paths specified in import_map.json are relative to supabase/functions directory. Can you try the following instead?

{
  "imports": {
    "@backend/": "../",
}

@bombillazo
Copy link
Author

@sweatybridge Ok Ill try that, where should the import_map.json file be located? Im new to Deno and I assumed import paths are relative to the import_map.json file, right now we have it inside the ./supabase directory.

@sweatybridge
Copy link
Contributor

The default location CLI loads from is supabase/functions/import_map.json. I think it's simplest to use because other locations require passing in --import-map <path> flag to the cli.

@bombillazo
Copy link
Author

I tried your suggestion but it still does not work:

error: Module not found "file:///home/lib/cors.ts".
    at file:///home/deno/functions/my-function/index.ts:9:29
2023/05/12 13:57:32 Sent Header: Host [docker]
2023/05/12 13:57:32 Sent Header: User-Agent [Go-http-client/1.1]
2023/05/12 13:57:32 Send Done
2023/05/12 13:57:32 Recv First Byte
Error: error executing command

@bombillazo
Copy link
Author

Ive been trying to figure out what these file:///home/deno/ mean and where do they come from. Our supabase dir looks like this:

/
└── supabase
    ├── functions
    │   ├── my-function
    │   │   └── index.ts
    │   └── import_map.json
    ├── libs
    │   └── cors.ts
    ├── migrations
    └── services

we are simply trying to import from libs/cors.ts into our my-function edge function.

@sweatybridge
Copy link
Contributor

We mount the functions directories into a docker container for serving locally. /home/deno is the root path within the container. Could you check the version of your CLI? supabase --version

I can't seem to reproduce on the latest version.

@bombillazo
Copy link
Author

Question: can the serve process access files outside the supabase folder? Even if they are setup correctly with import_map?

@sweatybridge
Copy link
Contributor

can the serve process access files outside the supabase folder?

Yes, we added support for this recently #1061. You can find an example in the linked PR.

@bombillazo
Copy link
Author

bombillazo commented May 12, 2023

Ok thats weird that it doesnt work for me... 😞 I am using v1.58.1, and running supabase using npx

@sweatybridge
Copy link
Contributor

Hmm could you try npx supabase -v ? There might be a different version installed via npx

@bombillazo
Copy link
Author

Now that you mention docker, let me try deleting and recreating the stack...

@bombillazo
Copy link
Author

No, still nothing, still the error. I am running with the --debug flag on to see the error. I see supabase cli download the remote deps but as soon as it starts resolving local deps, I get the error.

which container runs the edge functions?
image

@sweatybridge
Copy link
Contributor

The container is called edge-runtime but it seems like the old deno-relay container is started instead. Can you also try supabase functions serve without any args?

@bombillazo
Copy link
Author

To be clear all previous attemps have been specifying a function name:
Also, I truly appreciate the constant help! :)

So my findings:

  • supabase functions serve seems to be working! (although I commented out much code but seems like at least one local package was imported)
  • supabase functions serve --import-map ./supabase/functions/import_map.json gives this error:
Error: Error response from daemon: Duplicate mount point: /home/deno/modules/c675a10248170410cad4e4a996152aad5ad7f2a817728186567122b6394d9419
  • supabase functions serve my-function --import-map ./supabase/functions/import_map.json gives the original local module not found error...

I will continue to test with serve only but i was simply following the CLI flags in the docs.

@sweatybridge
Copy link
Contributor

Error: Error response from daemon: Duplicate mount point: /home/deno/modules/c675a10248170410cad4e4a996152aad5ad7f2a817728186567122b6394d9419

Hmm this error could come from resolving multiple modules with the same path because the base name is a hash of the original module path.

@bombillazo
Copy link
Author

bombillazo commented May 12, 2023

Success was short lived, I am getting this error when calling the functions form the edge server:

 "msg": "Error: channel closed"
serving the request with /home/deno/functions/my-function
worker thread panicked No such file or directory (os error 2)
Error: channel closed
    at async UserWorker.fetch (ext:sb_user_workers/user_workers.js:50:21)
    at async Server.<anonymous> (file:///home/deno/main/index.ts:102:16)
    at async Server.#respond (https://deno.land/[email protected]/http/server.ts:220:24)
failed to send the halt execution signal
failed to send the halt execution signal

@bombillazo
Copy link
Author

bombillazo commented May 14, 2023

@sweatybridge I can confirm what I suspected... the Deno docker container only mounts the functions folder from the supabase directory. I expected to see a lib and services folder alongside functions since I use imports from those directories and folders outside the supabase dir. Anything else inside or outside the supabase folder is not present in the container and will fail to run.

image

@sweatybridge
Copy link
Contributor

sweatybridge commented May 14, 2023

I expected to see a lib and services folder alongside functions since I use imports from those directories and folders outside the supabase dir.

Is there a /home/deno/modules directory inside your container? To avoid naming conflicts, we rename all relative imports of parent dirs to a computed hash under modules.

Your lib and services folder should be mounted under modules directory instead.

@bombillazo
Copy link
Author

bombillazo commented May 14, 2023

No, I dont see that directory, only main, fallback_import_map and flag_import_map are next to functions. Do you rename relative imports from the import_map or the source deno code files? We have it like this:

import_map.json (located inside supabase/ and passed to serve with --import-map)

    "@backend/": "./",
    "@components/": "./../src/components/",
    "@utils/": "./../src/utils/",

source code index file:

import { corsHeaders } from '@backend/lib/cors.ts';
import { supabase } from '@backend/lib/supabase.client.ts';

@sweatybridge
Copy link
Contributor

Like I mentioned before, paths in import_map are relative to supabase/functions directory. So it should be

    "@backend/": "../",
    "@components/": "../../src/components/",
    "@utils/": "../../src/utils/",

@bombillazo
Copy link
Author

bombillazo commented May 14, 2023

Ah! ok that works! I do see the modules folder now! Still having trouble with some external does now that don't download from CDN but seems like the local files issue is solved! Some notes though:

  • The folder does not appear when one runs supabase functions server <FUNCTION_NAME>
  • I still see the container image is supabase_deno_relay, I expected it would be using the newer edge-runtime container.

@sweatybridge sweatybridge changed the title serve command errors when importing local files serve <name> command errors when importing local files May 16, 2023
@sweatybridge
Copy link
Contributor

We have to keep deno relay for the time being while we iron out other issues with edge runtime. Once that's completed, we will default functions serve <name> to use the same runtime.

@ChuckJonas
Copy link

ChuckJonas commented Jul 26, 2023

@sweatybridge

This is failing in my setup:

Project Structure

root
   - common
   - react-app
   - supabase
   - functions
      - _core
         - supabaseClient.ts
      - chat
         - index.ts
      - import_map.json
   - package.json

supabase/functions/chat/index.ts

import { corsHeaders } from "core/supabaseClient.ts";

In "supabase_edge_runtime" Docker container

home/deno

home/deno
  - fallback_import_map.json
  - main
  - functions
     - _core
        - supabaseClient.ts
     - chat
        - index.ts
     - import_map.json
  - modules
      - 4b0ed4fd36b0064d333d0ebb53b49ba5d84e9b130af0ab0c15f9924364d718c6

fallback_import_map.json

{
    "imports": {
        "@supabase/supabase-js": "https://esm.sh/@supabase/supabase-js@2",
        "common/": "/home/deno/modules/4b0ed4fd36b0064d333d0ebb53b49ba5d84e9b130af0ab0c15f9924364d718c6/",
        "core/": "./_core/"
    },
    "scopes": {}
}

The deno LSP (vscode) is happy with this configuration, but when I try to invoke the functions/chat function, I get this error:

Failed to load module: "file:///home/deno/_core/supabaseClient.ts" - No such file or directory (os error 2)

It seems like thefallback_import_map.json that gets setup in the "supabase_edge_runtime" container is referencing the wrong path:

"core/": "./_core/" should be "core/": "./functions/_core/"

Also, when I try to run it with the --import-map flag, I get the Duplicate mount point error as above:

supabase functions serve --env-file .env --no-verify-jwt --import-map supabase/functions/import_map.json

-> Error response from daemon: Duplicate mount point: /home/deno/modules/4b0ed4fd36b0064d333d0ebb53b49ba5d84e9b130af0ab0c15f9924364d718c6

Update

If I update my import map to

    "core/": "./functions/_core/",

Then it works (as suspected), but this breaks the imports in my local deno LSP... Also, it goes against what was said above:

paths in import_map are relative to supabase/functions directory

So... I don't really understand what's going on...

@sagardspeed2
Copy link

I am fetching the same issue, as supabase function working in local fine, but while deploying it throws error related file not found.

any update on this ?

@inorganik
Copy link

If anyone came here with this issue, I was struggling with trying to get import_map to work, only to discover Deno stopped using import maps since 1.30:

https://docs.deno.com/runtime/manual/getting_started/configuration_file#imports-and-scopes

Since version 1.30, the deno.json configuration file acts as an import map for resolving bare specifiers.

So now you put your import aliases in the imports object in deno.json.

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

Successfully merging a pull request may close this issue.

5 participants