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

Can't publish my Azure function that I made as part of a monorepo #40

Open
ChristianOConnor opened this issue Nov 30, 2024 · 9 comments

Comments

@ChristianOConnor
Copy link

Everything works until I get to the deploy step. First I ran this:

npx create-nx-workspace@latest

I created my monorepo which consisted of 2 typescript Azure functions and 1 C# MVC app.

I made the Azure function in question with these commands:

npx nx g @nxazure/func:init FUNCTION_DIRECTORY
npx nx g @nxazure/func:new FUNCTION_NAME --project=FUNCTION_DIRECTORY --template="HTTP trigger"

I can run and build my function locally just fine. I ran az functionapp create --resource-group RESOURCE_GROUP_NAME --consumption-plan-location LOCATION_CODE --runtime node --name FUNCTION_AZURE_NAME --storage-account STORAGE_ACCOUNT_NAME --os-type Linux to create the Azure function app in my azure portal on my actual Azure account.

My nx repo's root package.json has these scripts:

"scripts": {
    "prepare": "nx g @nx-dotnet/core:restore",
    "start:FUNCTION_DIRECTORY": "nx start FUNCTION_NAME",
    "build:FUNCTION_DIRECTORY": "nx build FUNCTION_NAME",
    "publish:FUNCTION_DIRECTORY": "nx publish FUNCTION_NAME"
  },

Build and start both work. This is my azure function's project.json file:

{
  "name": "FUNCTION_NAME",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nxazure/func:build",
      "options": {
        "typescript": true,
        "outputPath": "dist"
      }
    },
    "start": {
      "executor": "@nxazure/func:start",
      "options": {
        "port": 7071
      }
    },
    "publish": {
      "executor": "@nxazure/func:publish",
      "options": {
        "name": "FUNCTION_AZURE_NAME",
        "additionalFlags": "--typescript"
      }
    }
  },
  "tags": []
}

And I never get an error when I try to run publish, but also no function was actually uploaded:

PS path/to/this/DIRECTORY> npm run publish:FUNCTION_DIRECTORY

> @PROJECTNAME/[email protected] publish:FUNCTION_DIRECTORY
> nx publish FUNCTION_NAME


> nx run FUNCTION_NAME:publish

Compiling TypeScript files for project "FUNCTION_NAME"...
Done compiling TypeScript files for project "FUNCTION_NAME".
> @PROJECTNAME/[email protected] prepare
> nx g @nx-dotnet/core:restore
 NX  Generating @nx-dotnet/core:restore
Executing Command: dotnet "restore" "DIRECTORYDIFFERENTCSHARPPROJNAME/DIFFERENTCSHARPPROJNAMEWithappendednamestring.DIFFERENTCSHARPPROJNAME.csproj"
  Determining projects to restore...
  SOME WARNINGS ABOUT DEPRECATED C SHARP PACKAGES FOR SOME REASON
  All projects are up-to-date for restore.
Executing Command: dotnet "tool" "restore"
Restore was successful.
changed 1 package, and audited 481 packages in 4s
65 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
Getting site publishing info...
[TIMESTAMPHERE] Starting the function app deployment...
Uploading package...
Upload completed successfully.
Deployment completed successfully.
[TIMESTAMPHERE] Syncing triggers...
Functions in FUNCTION_AZURE_NAME:

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

 NX   Successfully ran target publish for project FUNCTION_NAME (26s)

View logs and investigate cache misses at https://nx.app/runs/idstringhere

PS path/to/this/DIRECTORY>

It looks like the publish command is building my C# app instead of my Azure function? As I stated previously my repo contains 2 Azure Functions and 1 C# MVC app. The publish command clearly calls out to the proper Azure Function directory. What am I doing wrong?

@AlexPshul
Copy link
Owner

Hi @ChristianOConnor ,

It seems to me like there are 2 main issues here.

  1. The restore command that is being ran by the publish command is happening probably because the publish command also runs the npm install command, which in turn, runs the prepare command that you have in your package.json file.
    I think that this has 0 consequences, other then making every npm install run slower. I don't know your exact requirements of the setup, but if it is something that you wish to avoid, you probably can just move the restore command of .NET to a different command.
  2. There are no functions loaded after you upload your function app to the cloud. Unfortunately, It is probably not something that is related to the package. This could happen for many different reasons.
    To resolve this, you could try a few options:
  • Try accessing application insights and looking at the exceptions there. It might give you an idea of why it fails.
  • Try creating your function app from the portal manually, before doing so via CLI. Maybe something was missing in your CLI command for creating the function?
  • Try using the func cli tool directly (without the @nxazure/func package) and then deploy this function app to the cloud. It should ideally work in a simple scenario. This way you can pinpoint which code fails the loading of your functions.

Sorry that I can't be more of a help here. If you find more details, I'll be happy to help you further.

@ChristianOConnor
Copy link
Author

@AlexPshul I made an example repo that illustrates my problem. I invited you as a collaborator. Please pull the code to see what I'm talking about https://github.com/ChristianOConnor/monorepo-publish-function-example . You can run the app just fine by running npm install, then npx nx start first-function. You can do a simple get request to http://localhost:7071/api/function1 to receive a "Hello world!" But when I run a publish command, look at what happens:

PS C:\Path\To\monorepo-publish-function-example> cd .\packages\first-function\
PS C:\Path\To\monorepo-publish-function-example> func azure functionapp publish MY_AZURE_FUNCTION_NAME
Getting site publishing info...
[TIMESTAMP_HERE] Starting the function app deployment...
Uploading package...
Uploading 4.99 KB [###############################################################################]
Upload completed successfully.
Deployment completed successfully.
[TIMESTAMP_HERE] Syncing triggers...
Functions in MY_AZURE_FUNCTION_NAME:
PS C:\Path\To\monorepo-publish-function-example>

As you can see, no functions were uploaded to my Azure account. Can you please help me identify the problem?

@AlexPshul
Copy link
Owner

Your repo looks like the minimum configuration, which is great for testing and making sure that the issue is not in the code, but in your cloud configuration.
Please try to create your function from the Azure Portal, instead of running the CLI command you mentioned above. This way we can eliminate any misconfiguration that might have happened while you created your function app resource via the CLI command.

You could also check the logs if you connected application insights to your function app resource.
https://learn.microsoft.com/en-us/answers/questions/764926/where-can-we-check-azure-function-startup-logs-in

@ChristianOConnor
Copy link
Author

ChristianOConnor commented Dec 5, 2024

@AlexPshul I've been testing this repo against my issue and I have realized that it's not my Azure configuration. The Azure extension scaffolds correct and operations Azure functions but @nxazure/func function scaffolded in a NX monorepo don't work. I've tried to make it as testable and replicate-able as possible. Here is a normal Azure function scaffolded with the Azure VSCode extension: https://github.com/ChristianOConnor/normalrepo-publish-function-example . Here is a monorepo with 1 Azure function scaffolded through @nxazure/func commands: https://github.com/ChristianOConnor/monorepo-publish-function-example .


To build and publish the normal Azure function:

git clone https://github.com/ChristianOConnor/normalrepo-publish-function-example.git
cd .\normalrepo-publish-function-example\
npm install
npm run build
az functionapp create --resource-group RESOURCE_GROUP_NAME --consumption-plan-location southcentralus --runtime node --name AZURE_FUNCTION_NAME --storage-account AZURE_STORAGE_ACCOUNT
func azure functionapp publish AZURE_FUNCTION_NAME

This SHOULD WORK. It worked for me when I tested it. This is what the output looks like in my powershell window.

PS C:\Path\to\normalrepo-publish-function-example> func azure functionapp publish AZURE_FUNCTION_NAME
Getting site publishing info...
[TIMESTAMP_HERE] Starting the function app deployment...
Creating archive for current directory...
Uploading 13.65 MB [##############################################################################]
Upload completed successfully.
Deployment completed successfully.
[TIMESTAMP_HERE] Syncing triggers...
Functions in AZURE_FUNCTION_NAME:
    functionWithVsCodeExtension1 - [httpTrigger]
        Invoke url: https://AZURE_FUNCTION_NAME_IN_LOWERCASE.azurewebsites.net/api/functionwithvscodeextension1

PS C:\Path\to\normalrepo-publish-function-example> 


To build and publish the @nxazure/func scaffolded function:

git clone https://github.com/ChristianOConnor/monorepo-publish-function-example
cd monorepo-publish-function-example
npm install
npx nx build first-function
cd .\packages\first-function\
az functionapp create --resource-group RESOURCE_GROUP_NAME --consumption-plan-location southcentralus --runtime node --name AZURE_FUNCTION_NAME --storage-account AZURE_STORAGE_ACCOUNT
func azure functionapp publish AZURE_FUNCTION_NAME

This WILL NOT work. It simply uploads and empty 0 function payload. This is what the output looks like in my powershell window.

PS C:\Path\to\monorepo-publish-function-example\packages\first-function> func azure functionapp publish AZURE_FUNCTION_NAME

Getting site publishing info...
[TIMESTAMP_HERE] Starting the function app deployment...
Creating archive for current directory...
Uploading 4.99 KB [###############################################################################]
Upload completed successfully.
Deployment completed successfully.
[TIMESTAMP_HERE] Syncing triggers...
Functions in AZURE_FUNCTION_NAME:
PS C:\Path\to\monorepo-publish-function-example\packages\first-function> 

As you can see, no functions are uploaded. The payload is only 4.99 KB and there is no http trigger link below Functions in AZURE_FUNCTION_NAME:



Conclusion:
The boilerplate VSCode extension Azure function builds, runs, and publishes just fine, but the boilerplate @nxazure/func Azure function on builds, and runs. It can't publish through the traditional func azure functionapp publish AZURE_FUNCTION_NAME. Are you able to figure out why this is occurring?

@AlexPshul
Copy link
Owner

Thanks for all the details and getting back to me with repos and everything. That was super helpful!
I managed to pinpoint the issue and successfully published the app.
image

Short answer:

Currently, my @nxazure/func package doesn't support the workspaces prop in the main package.json file.
Remove the prop from the main package.json file and the publish should go through.

Long answer:

To make azure functions work on Azure, the node_modules directory needs to be uploaded to the cloud alongside the source code. Without it, nothing will load. (Just like in your scenario)
To make sure that things work, during the nx publish APP_NAME -n FUNCTION_APP_NAME command, I run the npm i command.
However, due to the workspaces prop in the main package.json file, the node_modules is never created. The upload continues as planned and all the files are uploaded. But during runtime, nothing loads.
If you remove the workspaces prop, the npm i runs and creates the node_modules directory inside the app and everything works.

I hope this workaround will work for you for now.
I will keep this issue opened and update it once the new version is released.

@ChristianOConnor
Copy link
Author

@AlexPshul Oh amazing this worked for me! Thank you so much!

Copy link

This issue is gathering dust because it has been open for 30 days with no activity. Time to wake up or be swept away!

@nachevnikolay
Copy link

I want to comment of the workaround with not using workspaces:

If you remove the workspaces prop, the npm i runs and creates the node_modules directory inside the app and everything works.

I am not using workspaces in my nx project but I still encounter the issue. Is there a fix for it?

@AlexPshul
Copy link
Owner

@nachevnikolay
In most cases that I saw, this was caused by a faulty configuration of the apps and an error when the apps are being loaded in the cloud.
It could be for many different reasons.
Are you sure that all the environment variables are properly initialized on Azure?
How about the packages? Any special package that you are using in your function app?

If you are able to reproduce the issue in a repo on the side, I'd be happy to give it a go and see what's up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants