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

Function names cannot contain dashes. #1735

Closed
Spencer-Easton opened this issue Oct 18, 2019 · 11 comments
Closed

Function names cannot contain dashes. #1735

Spencer-Easton opened this issue Oct 18, 2019 · 11 comments

Comments

@Spencer-Easton
Copy link

[REQUIRED] Environment info

firebase-tools: 7.6.0

Platform: Windows WSL 1

[REQUIRED] Test case

Provide a function name that contains a '-' The emulator will error out with an error similar to:
Error: Function name "DriveActivity-getActivity" is invalid. Function names cannot contain dashes.

The function emulator does allow for '.' in the name but those are invalid in function names.

[REQUIRED] Steps to reproduce

Create a function with a '-' in the name.
run firebase functions:shell

[REQUIRED] Expected behavior

According to the Functions UI in the cloud console:

Name must start with a letter followed by up to 62 letters, numbers, or hyphens, and cannot end with a hyphen

[REQUIRED] Actual behavior

The function name errors out the functions emulator.
Error: Function name "DriveActivity-getActivity" is invalid. Function names cannot contain dashes.

@samtstern
Copy link
Contributor

@joehan or @laurenzlong do you know which is correct? Can function names contain dashes?

@eghernqvist
Copy link

Unsure if this is related, but running 7.6.1 I can't emulate my functions with hyphens in the name either, but with a different error thrown.
Log:

➜  functions git:(develop) ✗ firebase functions:shell -p 5004
✔  functions: Emulator started at http://localhost:5004
i  functions: Loaded functions: auth-createUser, auth-deleteUser, verifyUser
$ firebase > auth-createUser()
Thrown:
ReferenceError: auth is not defined
$ firebase > verifyUser
[Function: bound ]

@samtstern
Copy link
Contributor

@eghernqvist I think what's happening there is it's trying to do the mathematical operation auth - createUser() and failing to find auth.

This is not legal JavaScript:

function auth-createUser() {
  return 1;
}

If you try to run that you will get:

Thrown:
function auth-createUser() { return 1; }
             ^

SyntaxError: Unexpected token -

However Google Cloud Functions does allow dashes in function names because they are not literal JavaScript functions. Can you show how you're defining your auth-createUser function?

@eghernqvist
Copy link

@samtstern I've grouped my functions in the way the Firebase CLI docs recommend grouping functions.

I'm using the emulator instead to get around this, but a small tip or note in the linked docs that shell testing requires your GC Functions to be JavaScript function compatibly named would probably help prevent others from falling into this pitfall!

@samtstern
Copy link
Contributor

@eghernqvist if we can't find a way to fix this behavior we will definitely add a documentation note. Can you show how you're grouping your functions though? I thought you'd get functions named auth.createUser() not auth-createUser() but maybe my understanding is wrong.

@eghernqvist
Copy link

@samtstern Re-read the docs and it does seem like getting auth.createUser() is the intended result. Might have something to do with my setup using babel for import?

Basically my setup is this:

auth.js

const createUser = functions.auth.user()
  .onCreate(() => {...})

export default { createUser }

index.js the function deploy source

import auth from './auth'
export { auth }

Tried with

import auth from './auth'
export default { auth }
// results in `default-auth-api` function name

Also tried changing it to:

import authApi from './auth'
export const auth = authApi
// results in function named `auth-api`, still

@gugahoi
Copy link

gugahoi commented Feb 10, 2020

I think the info line in functions:shell is misleading. It actually produces the correct namespace despite showing the names at the top. For me, it says: Loaded functions: blah-get however I can simply type the below to invoke it:

// firebase functions:shell
blah.get({})
// index.js
exports.blah = {
    get: functions.https.onCall(...)
}

@samtstern
Copy link
Contributor

Hmmm yeah there's definitely some inconsistent stuff going on here. I tried with these functions:

exports.myGroup = {
    first: functions.https.onRequest(async (request, response) => {
        response.json({ name: "first" });
    }),
    second: functions.https.onRequest(async (request, response) => {
        response.json({ name: "second" });
    }),
}

When I run functions:shell I see:

i  functions: Loaded functions: myGroup-first, myGroup-second

However if I actually want to run the functions in the shell I need to do myGroup.first() and myGroup.second().

When I deploy (no args) I see this:

✔  functions[myGroup-second(us-central1)]: Successful create operation. 
Function URL (myGroup-second): https://us-central1-fir-dumpster.cloudfunctions.net/myGroup-second
✔  functions[myGroup-first(us-central1)]: Successful create operation. 
Function URL (myGroup-first): https://us-central1-fir-dumpster.cloudfunctions.net/myGroup-first

So in URL form they use the - in the name. However if I want to deploy just one of the functions with --only I need to do this:

$ firebase deploy --only functions:myGroup.first

If I use the - form, the deploy command does not work.

@samtstern
Copy link
Contributor

Ok I have sent out #1972 to address the confusion.

Here's what's going on (I asked someone who has been around Cloud Functions longer than I have):

  • GCF allows - in function names, but we don't in the Firebase CLI (explained below)
  • When you group functions using exports.group = { functionA: ..., functionB: ... } we wanted to make the process intuitive and Javascript-y. So we use . notation to refer to those functions wherever possible. You do --deploy only:group.functionA, etc.
  • Because . is not a valid name in a GCF function, when we deploy we replace the . with a - so that in the URL that function would be called group-functionA.
  • In the functions:shell we wanted to reflect your code, so the function is defined as group.functionA. However the log message in functions:shell was showing the deployed name, which is wrong.

So I hope that makes sense! I do agree it's confusing but changing this decision now would be hugely disruptive for a small benefit. So I hope that #1972 reduces confusion.

@samtstern
Copy link
Contributor

#1972 is merged so going to close this as I think that's as much as we can do to reduce this confusion!

@tmk1991
Copy link

tmk1991 commented Dec 25, 2021

Still experiencing this firebase-tools 9.23.3

leomp12 added a commit to ecomplus/cloud-commerce that referenced this issue Aug 31, 2022

Verified

This commit was signed with the committer’s verified signature.
darkowlzz Sunny
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants