Azure / static-web-apps-cli

Azure Static Web Apps CLI ✨
https://aka.ms/swa/cli-local-development
MIT License
597 stars 117 forks source link

Unable to test api functions locally #723

Closed smolattack closed 1 year ago

smolattack commented 1 year ago

Are you accessing the CLI from the default port :4280 ?

Make sure you are accessing the URL printed in the console when running swa start!

ℹ️ NOTE: Make sure to enable debug logs when running any swa commands using --verbose=silly

Describe the bug When locally developing, the api is not proxied which means you can't just call fetch("api/whatever").

Deploying the app and accessing it from the public URL, seems fine.

To Reproduce Steps to reproduce the behavior:

  1. pnpm create vite (choose react and typescript when prompted)
  2. run swa and accept the defaults
  3. in VS Code F1 -> Azure Static Web App Create Http Trigger and choose typescript/v4
  4. create a post function
  5. edit App.tsx to call above function
  6. run swa start or swa start --api-location api
  7. Access http://localhost:5173/ as per output of above command
  8. try to invoke the function defined in App.tsx
  9. If the request is method is post, you should see a 404. If the request method is get, you will see a response from the vite server

Expected behavior Should be able to call api endpoints locally on the same socket as the client app.

Screenshots

swa --print-config

Welcome to Azure Static Web Apps CLI (1.1.3)

Using configuration "my-project" from file:
  /home/me/code/my-project/swa-cli.config.json

Options: 
 - port: 4280
 - host: localhost
 - apiPort: 7071
 - dataApiPort: 5000
 - appLocation: my-project-client
 - apiLocation: api
 - dataApiLocation: <undefined>
 - outputLocation: dist
 - swaConfigLocation: <undefined>
 - ssl: false
 - sslCert: <undefined>
 - sslKey: <undefined>
 - appBuildCommand: pnpm run build
 - apiBuildCommand: pnpm run build
 - run: pnpm run dev
 - verbose: log
 - devserverTimeout: 60
 - open: false
 - githubActionWorkflowLocation: <undefined>
 - appDevserverUrl: http://localhost:5173
 - apiDevserverUrl: <undefined>
 - env: preview
 - appName: my-project-swa
 - dryRun: false
 - apiLanguage: node
 - apiVersion: 18
 - dataApiDevserverUrl: <undefined>
 - subscriptionId: ab0d4fc7-2a52-4efd-a643-ddefeedfff15
 - resourceGroup: my-project-swa-rg
 - tenantId: 8923a56d-0f80-41be-b555-e1fb4e143509
 - clientId: <undefined>
 - clientSecret: <undefined>
 - useKeychain: true
 - clearCredentials: false
 - folderName: swa-db-connections
 - config: swa-cli.config.json
 - printConfig: true

swa start --verbose=silly   

Welcome to Azure Static Web Apps CLI (1.1.3)

Getting config file options from swa-cli.config.json...
Changed directory to /home/me/code/my-project
Using configuration "my-project" from file:
  /home/me/code/my-project/swa-cli.config.json

***********************************************************************
* WARNING: This emulator may not match the cloud environment exactly. *
* Always deploy and test your app in Azure.                           *
***********************************************************************

Checking if localhost:4280 is accepting TCP connections...
Port 4280 is available. Use it.
Resolved port number: 4280
appDevserverUrl provided, we will try connect to dev server at dist
Api Folder found: /home/me/code/my-project/api
Trying to read workflow config with values:
 - appLocation: /home/me/code/my-project/my-project-client
 - outputLocation: http://localhost:5173
 - apiLocation: /home/me/code/my-project/api
No workflow config folder found at /home/me/code/my-project/my-project-client/.github/workflows
Validating user workflow config (BEFORE):
 - appLocation: /home/me/code/my-project/my-project-client
 - outputLocation: http://localhost:5173
 - apiLocation: /home/me/code/my-project/api
Validating user workflow config (AFTER):
 - appLocation: /home/me/code/my-project/my-project-client
 - apiLocation: /home/me/code/my-project/api
 - outputLocation: http://localhost:5173
 - dataApiLocation: <undefined>
User workflow config:
 - appLocation: /home/me/code/my-project/my-project-client
 - apiLocation: /home/me/code/my-project/api
 - outputLocation: http://localhost:5173
 - dataApiLocation: <undefined>
Running echo 'No Data API found'. Skipping
Starting the SWA emulator with the following configuration:
- ssl:
  - 0: false
  - 1: <undefined>
  - 2: <undefined>
- env:
  - SWA_RUNTIME_CONFIG_LOCATION: /home/me/code/my-project/my-project-client
  - SWA_RUNTIME_WORKFLOW_LOCATION: <undefined>
  - SWA_CLI_DEBUG: silly
  - SWA_CLI_API_PORT: 7071
  - SWA_CLI_APP_LOCATION: /home/me/code/my-project/my-project-client
  - SWA_CLI_OUTPUT_LOCATION: http://localhost:5173
  - SWA_CLI_API_LOCATION: /home/me/code/my-project/api
  - SWA_CLI_DATA_API_LOCATION: <undefined>
  - SWA_CLI_DATA_API_PORT: undefined
  - SWA_CLI_HOST: localhost
  - SWA_CLI_PORT: 4280
  - SWA_CLI_APP_SSL: false
  - SWA_CLI_APP_SSL_CERT: <undefined>
  - SWA_CLI_APP_SSL_KEY: <undefined>
  - SWA_CLI_STARTUP_COMMAND: pnpm run dev
  - SWA_CLI_VERSION: 1.1.3
  - SWA_CLI_SERVER_TIMEOUT: 60
  - SWA_CLI_OPEN_BROWSER: false
- commands:
  - swa: node "/home/me/.local/share/pnpm/global/5/.pnpm/@azure+static-web-apps-cli@1.1.3/node_modules/@azure/static-web-apps-cli/dist/msha/server.js"
  - api: cd "/home/me/code/my-project/api" && "func" start --cors "*" --port 7071 
  - dataApi: <undefined>
  - run: cd "/home/me/code/my-project/my-project-client" && pnpm run dev
[swa] No staticwebapp.config.json found in current project
[swa] Validating dev server config:
[swa]  - url: http://localhost:5173
[swa]  - timeout: 60
[swa] Checking if localhost:5173 is accepting TCP connections...
[swa] - Waiting for http://localhost:5173 to be ready
[api] 
Azure Functions Core Tools
[api] Core Tools Version:       4.0.3971 Commit hash: d0775d487c93ebd49e9c1166d5c3c01f3c76eaaf  (64-bit)
[api] Function Runtime Version: 4.0.1.16815
[api] 
[run] 
[run] > my-project-client@0.0.0 dev /home/me/code/my-project/my-project-client
[run] > vite
[run] 
[run] 2023-07-12T01:27:38.571Z vite:config bundled config file loaded in 137.21ms
[run] 2023-07-12T01:27:38.582Z vite:config using resolved config: {
[run]   plugins: [
[run]     'vite:optimized-deps',
[run]     'vite:watch-package-data',
[run]     'vite:pre-alias',
[run]     'alias',
[run]     'vite:react-babel',
[run]     'vite:react-refresh',
[run]     'vite:modulepreload-polyfill',
[run]     'vite:resolve',
[run]     'vite:html-inline-proxy',
[run]     'vite:css',
[run]     'vite:esbuild',
[run]     'vite:json',
[run]     'vite:wasm-helper',
[run]     'vite:worker',
[run]     'vite:asset',
[run]     'vite:wasm-fallback',
[run]     'vite:define',
[run]     'vite:css-post',
[run]     'vite:worker-import-meta-url',
[run]     'vite:asset-import-meta-url',
[run]     'vite:dynamic-import-vars',
[run]     'vite:import-glob',
[run]     'vite:client-inject',
[run]     'vite:import-analysis'
[run]   ],
[run]   optimizeDeps: {
[run]     disabled: 'build',
[run]     force: undefined,
[run]     include: [ 'react', 'react/jsx-dev-runtime' ],
[run]     esbuildOptions: { preserveSymlinks: false }
[run]   },
[run]   server: {
[run]     preTransformRequests: true,
[run]     sourcemapIgnoreList: [Function: isInNodeModules],
[run]     middlewareMode: false,
[run]     fs: { strict: true, allow: [Array], deny: [Array] }
[run]   },
[run]   esbuild: { jsxDev: true, jsx: 'automatic', jsxImportSource: undefined },
[run]   build: {
[run]     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
[run]     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
[run]     outDir: 'dist',
[run]     assetsDir: 'assets',
[run]     assetsInlineLimit: 4096,
[run]     cssCodeSplit: true,
[run]     sourcemap: false,
[run]     rollupOptions: { onwarn: [Function: onwarn] },
[run]     minify: 'esbuild',
[run]     terserOptions: {},
[run]     write: true,
[run]     emptyOutDir: null,
[run]     copyPublicDir: true,
[run]     manifest: false,
[run]     lib: false,
[run]     ssr: false,
[run]     ssrManifest: false,
[run]     ssrEmitAssets: false,
[run]     reportCompressedSize: true,
[run]     chunkSizeWarningLimit: 500,
[run]     watch: null,
[run]     commonjsOptions: { include: [Array], extensions: [Array] },
[run]     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
[run]     modulePreload: { polyfill: true },
[run]     cssMinify: true
[run]   },
[run]   resolve: {
[run]     mainFields: [ 'module', 'jsnext:main', 'jsnext' ],
[run]     browserField: true,
[run]     conditions: [],
[run]     extensions: [
[run]       '.mjs',  '.js',
[run]       '.mts',  '.ts',
[run]       '.jsx',  '.tsx',
[run]       '.json'
[run]     ],
[run]     dedupe: [ 'react', 'react-dom' ],
[run]     preserveSymlinks: false,
[run]     alias: [ [Object], [Object] ]
[run]   },
[run]   configFile: '/home/me/code/my-project/my-project-client/vite.config.ts',
[run]   configFileDependencies: [ '/home/me/code/my-project/my-project-client/vite.config.ts' ],
[run]   inlineConfig: {
[run]     root: undefined,
[run]     base: undefined,
[run]     mode: undefined,
[run]     configFile: undefined,
[run]     logLevel: undefined,
[run]     clearScreen: undefined,
[run]     optimizeDeps: { force: undefined },
[run]     server: {}
[run]   },
[run]   root: '/home/me/code/my-project/my-project-client',
[run]   base: '/',
[run]   rawBase: '/',
[run]   publicDir: '/home/me/code/my-project/my-project-client/public',
[run]   cacheDir: '/home/me/code/my-project/my-project-client/node_modules/.vite',
[run]   command: 'serve',
[run]   mode: 'development',
[run]   ssr: {
[run]     format: 'esm',
[run]     target: 'node',
[run]     optimizeDeps: { disabled: true, esbuildOptions: [Object] }
[run]   },
[run]   isWorker: false,
[run]   mainConfig: null,
[run]   isProduction: false,
[run]   css: undefined,
[run]   preview: {
[run]     port: undefined,
[run]     strictPort: undefined,
[run]     host: undefined,
[run]     https: undefined,
[run]     open: undefined,
[run]     proxy: undefined,
[run]     cors: undefined,
[run]     headers: undefined
[run]   },
[run]   envDir: '/home/me/code/my-project/my-project-client',
[run]   env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
[run]   assetsInclude: [Function: assetsInclude],
[run]   logger: {
[run]     hasWarned: false,
[run]     info: [Function: info],
[run]     warn: [Function: warn],
[run]     warnOnce: [Function: warnOnce],
[run]     error: [Function: error],
[run]     clearScreen: [Function: clearScreen],
[run]     hasErrorLogged: [Function: hasErrorLogged]
[run]   },
[run]   packageCache: Map(1) {
[run]     'fnpd_/home/me/code/my-project/my-project-client' => {
[run]       dir: '/home/me/code/my-project/my-project-client',
[run]       data: [Object],
[run]       hasSideEffects: [Function: hasSideEffects],
[run]       webResolvedImports: {},
[run]       nodeResolvedImports: {},
[run]       setResolvedCache: [Function: setResolvedCache],
[run]       getResolvedCache: [Function: getResolvedCache]
[run]     },
[run]     set: [Function (anonymous)]
[run]   },
[run]   createResolver: [Function: createResolver],
[run]   worker: {
[run]     format: 'iife',
[run]     plugins: [
[run]       'vite:optimized-deps',
[run]       'vite:watch-package-data',
[run]       'vite:pre-alias',
[run]       'alias',
[run]       'vite:modulepreload-polyfill',
[run]       'vite:resolve',
[run]       'vite:html-inline-proxy',
[run]       'vite:css',
[run]       'vite:esbuild',
[run]       'vite:json',
[run]       'vite:wasm-helper',
[run]       'vite:worker',
[run]       'vite:asset',
[run]       'vite:wasm-fallback',
[run]       'vite:define',
[run]       'vite:css-post',
[run]       'vite:worker-import-meta-url',
[run]       'vite:asset-import-meta-url',
[run]       'vite:dynamic-import-vars',
[run]       'vite:import-glob',
[run]       'vite:client-inject',
[run]       'vite:import-analysis'
[run]     ],
[run]     rollupOptions: {},
[run]     getSortedPlugins: [Function: getSortedPlugins],
[run]     getSortedPluginHooks: [Function: getSortedPluginHooks]
[run]   },
[run]   appType: 'spa',
[run]   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
[run]   getSortedPlugins: [Function: getSortedPlugins],
[run]   getSortedPluginHooks: [Function: getSortedPluginHooks]
[run] }
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteTimeMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / corsMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use /__open-in-editor launchEditorMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteHMRPingMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteServePublicMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteTransformMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteServeRawFsMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteServeStaticMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteHtmlFallbackMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteIndexHtmlMiddleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / vite404Middleware
[run] Wed, 12 Jul 2023 01:27:38 GMT connect:dispatcher use / viteErrorMiddleware
[run] 2023-07-12T01:27:38.597Z vite:esbuild 20.77ms tsconfck init /home/me/code/my-project/my-project-client
[run] 2023-07-12T01:27:38.599Z vite:deps Hash is consistent. Skipping. Use --force to override.
[run] 
[run]   VITE v4.4.3  ready in 265 ms
[run] 
[run]   ➜  Local:   http://localhost:5173/
[run]   ➜  Network: use --host to expose
[api] [2023-07-12T01:27:39.146Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[api] For detailed output, run func with --verbose flag.
[swa] Could not connect to tcp:[::1]:5173
[api] [2023-07-12T01:27:44.102Z] Host lock lease acquired by instance ID '000000000000000000000000861DE9B7'.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

manekinekko commented 1 year ago

Hi @smolattack it looks like you are using the default SWA CLI port which is 4280, so you would need to access your application on http://localhost:4280, and not http://localhost:5173.

diberry commented 1 year ago

@manekineko - Is it possible the CLI runtime could be improved to understand the issue and suggest the fix in the logs or in the response?

smolattack commented 1 year ago

Hi @smolattack it looks like you are using the default SWA CLI port which is 4280, so you would need to access your application on http://localhost:4280, and not http://localhost:5173.

There's noting on port 4280. I've pretty much given up on local dev and just publish my app for testing. I don't expect this to be fixed on Linux so I'm closing this.