SAP-samples / cloud-cap-samples

This project contains sample applications for SAP Cloud Application Programming Model.
https://cap.cloud.sap/
Apache License 2.0
482 stars 500 forks source link

"ERR! Could not install from "../common" as it does not contain a package.json file." for cf push -f gen/srv #53

Closed Sygyzmundovych closed 4 years ago

Sygyzmundovych commented 4 years ago

I cloned the repo and did cds watch bookshop. Opened http://localhost:4004 -- all good.

Then followed only https://cap.cloud.sap/docs/advanced/deploy-to-cloud to deploy to the Bookshop example to my HDI in SCP Trial.

But in step https://cap.cloud.sap/docs/advanced/deploy-to-cloud#deploy-using-cf-push the command

cf push -f gen/srv --random-route

failed:

$ cf push -f gen/srv --random-route
Pushing from manifest to org a109df31trial / space dev as abc.xyz@sap.com...
Using manifest file /Users/i000000/Projects/cloud-cap-samples/bookshop/gen/srv/manifest.yaml
Getting app info...
Updating app with these attributes...
  name:                bookshop-srv
  path:                /Users/i000000/Projects/cloud-cap-samples/bookshop/gen/srv
  disk quota:          1G
  health check type:   port
  instances:           1
  memory:              128M
  stack:               cflinuxfs3
  services:
    bookshop-db-hdi-container
  routes:
    bookshop-srv-terrific-civet-rc.cfapps.eu10.hana.ondemand.com

Updating app bookshop-srv...
Mapping routes...
Comparing local files to remote cache...
Packaging files to upload...
Uploading files...
 1.86 KiB / 1.86 KiB [============================================================================================================] 100.00% 1s

Waiting for API to complete processing files...

Staging app and tracing logs...
          Using default npm version: 6.13.4
   -----> Installing yarn 1.21.1
          Copy [/tmp/buildpacks/5dbe64bf3d3c2193d7e1ab2679366053/dependencies/527e472b80235f08997eeb3ddca77906/yarn-1.21.1-any-stack-fd04cba1.tgz]
          Installed yarn 1.21.1
   -----> Creating runtime environment
          PRO TIP: It is recommended to vendor the application's Node.js dependencies
          Visit http://docs.cloudfoundry.org/buildpacks/node/index.html#vendoring
          NODE_ENV=production
          NODE_HOME=/tmp/contents187127754/deps/0/node
          NODE_MODULES_CACHE=true
          NODE_VERBOSE=false
          NPM_CONFIG_LOGLEVEL=error
          NPM_CONFIG_PRODUCTION=true
   -----> Building dependencies
          Installing node modules (package.json)
   npm ERR! code ENOLOCAL
   npm ERR! Could not install from "../common" as it does not contain a package.json file.
   npm ERR! A complete log of this run can be found in:
   npm ERR!     /tmp/cache/final/.npm/_logs/2020-04-09T15_09_18_422Z-debug.log
          **ERROR** Unable to build dependencies: exit status 1
   Failed to compile droplet: Failed to run all supply scripts: exit status 14
   Exit status 223
   Cell e7a50627-16cd-4ddf-b175-9b42fd470c14 stopping instance 05f23b5f-19e4-444c-a647-b9b41624342f
   Cell e7a50627-16cd-4ddf-b175-9b42fd470c14 destroying container for instance 05f23b5f-19e4-444c-a647-b9b41624342f
Error staging application: App staging failed in the buildpack compile phase
FAILED

Indeed, there is no folder common under the gen, while the gen/srv/package.json has it as a dependency:

{
  "name": "@capire/bookshop",
  "version": "1.0.0",
  "description": "A simple self-contained bookshop service.",
  "dependencies": {
    "@capire/common": "../common",
    "@sap/cds": "^3.33.1",
    "express": "^4.17.1"
  },
  "scripts": {
    "genres": "cds serve test/genres.cds",
    "start": "cds run",
    "watch": "cds watch"
  },
  "cds": {
    "requires": {
      "db": {
        "kind": "sql"
      }
    }
  }
}
Sygyzmundovych commented 4 years ago

And while cf push -f gen/db finished Ok, I see that tables SAP_COMMON_CURRENCIES and SAP_COMMON_CURRENCIES_TEXTS in my HDI schema are empty. I assume they should be populated from common/data?

chgeo commented 4 years ago

Indeed, there is no folder common under the gen, while the gen/srv/package.json has it as a dependency:

The models from ../common are already compiled into the effective model file in gen/srv/csn.json, so this dependency is actually not needed on CF. However, as the node.js buildpack always does an npm install, this fails. Not sure here about the proper solution...

ChristianAyotteRizing commented 4 years ago

Hi @chgeo , is there any update on this? I have been getting the exact same error as @Sygyzmundovych

I was able to get past the initial error by removing the ../common dependency (and adding @sap/hana-client to the /bookshop app.), but then when I try to /browse Books, I get:

2020-04-28T20:44:12.99-0700 [APP/PROC/WEB/0] OUT > READ Books { '$count': 'true', '$skip': '0', '$top': '30' } 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR [2020-04-29T03:44:13.089Z | ERROR | 1503087]: Cannot read property 'schema' of undefined 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR [INTERNAL ERROR] TypeError: Cannot read property 'schema' of undefined 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at Promise (/home/vcap/deps/0/node_modules/@sap/cds-hana/lib/client/Client.js:93:31) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at new Promise () 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at Client.connect (/home/vcap/deps/0/node_modules/@sap/cds-hana/lib/client/Client.js:66:12) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at SQLDatabase._testConnection (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/connect/Service.js:298:18) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at SQLDatabase._getAndCheckCredentials (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/connect/Service.js:280:17) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at SQLDatabase._createPool (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/connect/Service.js:227:24) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at SQLDatabase.acquire (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/connect/Service.js:147:49) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at SQLDatabase.run (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/connect/Service.js:370:31) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at OdataContext.context.run.args [as run] (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/services/Service.js:911:23) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR at Object.context [as handler] (/home/vcap/deps/0/node_modules/@sap/cds-services/lib/services/handlers/onRead.js:40:18) 2020-04-28T20:44:13.09-0700 [APP/PROC/WEB/0] ERR Please report this error. 2020-04-28T20:44:13.19-0700 [RTR/2] OUT bookshop-srv-talkative-hyrax-zu.cfapps.us10.hana.ondemand.com - [2020-04-29T03:44:12.684068766Z] "POST /browse/$batch HTTP/1.1" 502 356 67 "https://bookshop-srv-talkative-hyrax-zu.cfapps.us10.hana.ondemand.com/$fiori-preview/?service=CatalogService&entity=Books" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36" "-" "10.0.137.25:61022" x_forwarded_for:"-" x_forwarded_proto:"https" vcap_request_id:"8d05dd7c-e4a7-4269-5154-10df72a1bd78" response_time:0.506885 gorouter_time:0.000148 app_id:"24fd0be8-fc03-4cc6-b829-4a4c14fc766e" app_index:"0" x_correlationid:"-" tenantid:"-" x_scp_request_id:"9f663b48-d663-4b18-8d23-eec09e7428d1-5EA8F803-E104CB" x_cf_app_instance:"-" x_b3_traceid:"8bdb15b32903c8cd" x_b3_spanid:"8bdb15b32903c8cd" x_b3_parentspanid:"-" b3:"8bdb15b32903c8cd-8bdb15b32903c8cd" 2020-04-28T20:44:13.19-0700 [RTR/2] OUT 2020-04-28T20:44:13.19-0700 [APP/PROC/WEB/0] ERR npm ERR! code ELIFECYCLE 2020-04-28T20:44:13.19-0700 [APP/PROC/WEB/0] ERR npm ERR! errno 1 2020-04-28T20:44:13.28-0700 [APP/PROC/WEB/0] ERR npm ERR! @capire/bookshop@1.0.0 start: cds run 2020-04-28T20:44:13.28-0700 [APP/PROC/WEB/0] ERR npm ERR! Exit status 1 2020-04-28T20:44:13.28-0700 [APP/PROC/WEB/0] ERR npm ERR! 2020-04-28T20:44:13.28-0700 [APP/PROC/WEB/0] ERR npm ERR! Failed at the @capire/bookshop@1.0.0 start script. 2020-04-28T20:44:13.28-0700 [APP/PROC/WEB/0] ERR npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 2020-04-28T20:44:13.29-0700 [APP/PROC/WEB/0] ERR npm ERR! A complete log of this run can be found in: 2020-04-28T20:44:13.29-0700 [APP/PROC/WEB/0] ERR npm ERR! /home/vcap/app/.npm/_logs/2020-04-29T03_44_13_288Z-debug.log 2020-04-28T20:44:13.41-0700 [APP/PROC/WEB/0] OUT Exit status 1

chgeo commented 4 years ago

was able to get past the initial error by removing the ../common dependency

Yes, that's a workaround for the missing dependency

and adding @sap/hana-client to the /bookshop app

Good!

Cannot read property 'schema' of undefined

You can check the application log where it says connect to... to which DB it wants to connect. You maybe read undefined there, which indicates a configuration error. I remember the current version has a bug where the kind:sql in the package.json is not properly resolved to hana on Cloud Foundry, which would explain the issue. A fixed version of @sap/cds will be published today, so you might wait a bit, or replace the sql with hana in package.json, and then build and deploy again.

chgeo commented 4 years ago

The new version 3.34.1 of @sap/cds is published. Please use this one with the sql configuration.

ChristianAyotteRizing commented 4 years ago

Thanks for your help so far @chgeo - I'm no longer seeing the "Cannot read property 'schema' of undefined" error when trying to read from /browse/Books. However, I am seeing a consistent problem with reading entities from the CDS API in nodejs: that is, in all exported service implementations, cds.entities and/or db.entities (after connecting to database) show as undefined at runtime. I'm seeing this in capsamples as well.

For testing, I added the following log step to bookshop/gen/srv/srv/cat-service.js then deployed to cf. (service is created by 'cf cs hana hdi-shared bookshop-db-hdi-container')

const cds = require('@sap/cds')
module.exports = async function (){

  const db = await cds.connect.to('db') // connect to database service
  const { Books } = db.entities         // get reflected definitions

// vvvvvvvv below line added
  console.log(`[cat-service] Fetched Books entity ${JSON.stringify(Books)}`)
...

Now, cds.entities works fine in a local run (cds watch) and I can effectively create new books and orders, but I see in the logs (cf logs bookshop-srv --recent) the following

... 2020-04-29T07:49:56.26-0700 [APP/PROC/WEB/0] OUT [cds] - serving AdminService { at: '/admin' } 2020-04-29T07:49:58.56-0700 [APP/PROC/WEB/0] OUT [cds] - serving CatalogService { at: '/browse', impl: 'srv/cat-service.js' } 2020-04-29T07:49:58.56-0700 [APP/PROC/WEB/0] OUT [cat-service] Fetched Books entity undefined 2020-04-29T07:49:58.66-0700 [APP/PROC/WEB/0] OUT [cds] - launched in: 18695.134ms 2020-04-29T07:49:58.66-0700 [APP/PROC/WEB/0] OUT [cds] - server listening on { url: 'http://localhost:8080' } ...

ChristianAyotteRizing commented 4 years ago

Also, is there documentation for how to deploy the fiori/app contents to cloud-foundry? Should we use html5-app-deployer?

ChristianAyotteRizing commented 4 years ago

After a bit of digging I found the service implementations (cat-service.js, admin-service.js and orders-service.js) work when deployed to CF if the API call to cds.entities explicitly includes the namespace.

i.e.,

const db = await cds.connect.to('db') // connect to database service
const { Books } = db.entities('sap.capire.bookshop')         // get reflected definitions

or

const { Books } = cds.entities('sap.capire.bookshop')

Is this the correct approach?

danjoa commented 4 years ago

Both are equivalent (assumed in the second case primary db has been connected, which usually happens on bootstrapping). cds acts as a shortcut to connected primary db, which is accessible thru cds.db and will be exactly the cached service object you get from subsequent invocations of await cds.connect.to('db')

chgeo commented 4 years ago

db.entities issue was also reported in https://answers.sap.com/questions/13051405/cap-different-behavior-when-deploying-to-cf-and-ha.html

gregorwolf commented 4 years ago

I was able to fix this issue for the local run with sqlite by adding:

"model": "db"

to the cds.requires.db section in the package.json

chgeo commented 4 years ago

was able to get past the initial error by removing the ../common dependency

Yes, that's a workaround for the missing dependency

The issue with the ../common dependency is fixed with the latest @sap/cds-dk 3.0.0. cds build now removes such file dependencies in the generated gen/srv/package.json.

chgeo commented 4 years ago

Also, with latest @sap/cds 4.1.9, the disfunctional cds.entities is fixed.