nodejs / node-addon-api

Module for using Node-API from C++
MIT License
2.19k stars 462 forks source link

Build fails on macOS and Linux (and possibly Windows), with node release 8.11.1 and lower, for node-addon-api 1.6.0 #387

Closed bsrdjan closed 6 years ago

bsrdjan commented 6 years ago

Example:

$ cat package.json
        ...
    "dependencies": {
        "@types/bluebird": "^3.5.24",
        "bluebird": "^3.5.2",
        "node-addon-api": "^1.6.0",
        "node-pre-gyp": "^0.11.0"
    },
    "devDependencies": {
        "@babel/core": "^7.1.2",
        "@types/node": "^10.12.2",
        "acorn": "^6.0.4",
        "aws-sdk": "^2.348.0",
        "compare-versions": "^3.4.0",
        "decimal.js": "^10.0.1",
        "eslint": "^5.8.0",
        "eslint-plugin-mocha": "^5.2.0",
        "mocha": "^5.2.0",
        "node-gyp": "^3.8.0",
        "prettier-eslint": "^8.8.2",
        "random-bytes": "^1.0.0",
        "should": "^13.2.3",
        "typescript": "^3.1.6"
    }
        ...

$ node -v
v8.9.0

$ npm -v
6.4.1

$ node-pre-gyp clean configure build                                                                                                     
node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using node-pre-gyp@0.11.0
node-pre-gyp info using node@8.9.0 | darwin | x64
[node-rfc] Removing "/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57"
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.0 | darwin | x64
gyp info spawn /Users/d037732/.pyenv/shims/python2
gyp info spawn args [ '/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/d037732/src/NG-APPS/node-rfc/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/d037732/.node-gyp/8.9.0/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/d037732/.node-gyp/8.9.0',
gyp info spawn args   '-Dnode_gyp_dir=/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/d037732/.node-gyp/8.9.0/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/d037732/src/NG-APPS/node-rfc',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info ok
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/nothing/node_modules/node-addon-api/src/nothing.o
  LIBTOOL-STATIC Release/nothing.a
  CXX(target) Release/obj.target/sapnwrfc/src/node_sapnwrfc.o
In file included from ../src/node_sapnwrfc.cc:15:
In file included from ../src/client.h:21:
/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi.h:1676:33: error: unknown type name 'napi_callback_scope'
    CallbackScope(napi_env env, napi_callback_scope scope);
                                ^
/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi.h:1680:14: error: unknown type name 'napi_callback_scope'
    operator napi_callback_scope() const;
             ^
/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi.h:1687:5: error: unknown type name 'napi_callback_scope'
    napi_callback_scope _scope;
    ^
In file included from ../src/node_sapnwrfc.cc:15:
In file included from ../src/client.h:21:
In file included from /Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi.h:1780:
/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi-inl.h:3412:17: error: unknown type name 'napi_callback_scope'
  napi_env env, napi_callback_scope scope) : _env(env), _scope(scope) {
                ^
/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi-inl.h:3427:32: error: unknown type name 'napi_callback_scope'
inline CallbackScope::operator napi_callback_scope() const {
                               ^
5 errors generated.
make: *** [Release/obj.target/sapnwrfc/src/node_sapnwrfc.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack     at emitTwo (events.js:126:13)
gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
gyp ERR! System Darwin 18.2.0
gyp ERR! command "/Users/d037732/.nvm/versions/node/v8.9.0/bin/node" "/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/bin/node-gyp.js" "build" "--module=/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57/sapnwrfc.node" "--module_name=sapnwrfc" "--module_path=/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57" "--napi_version=1" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v57"
gyp ERR! cwd /Users/d037732/src/NG-APPS/node-rfc
gyp ERR! node -v v8.9.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/Users/d037732/.nvm/versions/node/v8.9.0/bin/node /Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/bin/node-gyp.js build --module=/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57/sapnwrfc.node --module_name=sapnwrfc --module_path=/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57 --napi_version=1 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v57' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at emitTwo (events.js:126:13)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:925:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
node-pre-gyp ERR! System Darwin 18.2.0
node-pre-gyp ERR! command "/Users/d037732/.nvm/versions/node/v8.9.0/bin/node" "/Users/d037732/src/NG-APPS/node-rfc/node_modules/.bin/node-pre-gyp" "clean" "configure" "build"
node-pre-gyp ERR! cwd /Users/d037732/src/NG-APPS/node-rfc
node-pre-gyp ERR! node -v v8.9.0
node-pre-gyp ERR! node-pre-gyp -v v0.11.0
node-pre-gyp ERR! not ok
Failed to execute '/Users/d037732/.nvm/versions/node/v8.9.0/bin/node /Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/bin/node-gyp.js build --module=/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57/sapnwrfc.node --module_name=sapnwrfc --module_path=/Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57 --napi_version=1 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v57' (1)

$ nvm use 8.11.2
Now using node v8.11.2 (npm v6.4.1)

$ node-pre-gyp clean configure build
node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using node-pre-gyp@0.11.0
node-pre-gyp info using node@8.11.2 | darwin | x64
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.11.2 | darwin | x64
gyp info spawn /Users/d037732/.pyenv/shims/python2
gyp info spawn args [ '/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/d037732/src/NG-APPS/node-rfc/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/d037732/.node-gyp/8.11.2/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/d037732/.node-gyp/8.11.2',
gyp info spawn args   '-Dnode_gyp_dir=/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/d037732/.node-gyp/8.11.2/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/d037732/src/NG-APPS/node-rfc',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info ok
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.11.2 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/nothing/node_modules/node-addon-api/src/nothing.o
  LIBTOOL-STATIC Release/nothing.a
  CXX(target) Release/obj.target/sapnwrfc/src/node_sapnwrfc.o
In file included from ../src/node_sapnwrfc.cc:15:
In file included from ../src/client.h:21:
/Users/d037732/src/NG-APPS/node-rfc/node_modules/node-addon-api/napi.h:1686:24: warning: private field '_async_context' is not used [-Wunused-private-field]
    napi_async_context _async_context;
                       ^
1 warning generated.
  SOLINK_MODULE(target) Release/sapnwrfc.node
  COPY /Users/d037732/src/NG-APPS/node-rfc/lib/binding/darwin-x64-node-v57/sapnwrfc.node
  TOUCH Release/obj.target/action_after_build.stamp
gyp info ok
node-pre-gyp info ok
$
mkundgol commented 6 years ago

I too have ran into this issue while installing node-ffi-napi which depends on node-addon-api.

corymickelson commented 6 years ago

Having a similar issue with 1.6.0 release

In file included from ../node_modules/node-addon-api/napi.h:1780,
                 from ../src/Reader.h:22,
                 from ../src/Reader.cc:19:
../node_modules/node-addon-api/napi-inl.h:3412:17: error: ‘napi_callback_scope’ has not been declared
   napi_env env, napi_callback_scope scope) : _env(env), _scope(scope) {
                 ^~~~~~~~~~~~~~~~~~~
../node_modules/node-addon-api/napi-inl.h: In constructor ‘Napi::CallbackScope::CallbackScope(napi_env, int)’:
../node_modules/node-addon-api/napi-inl.h:3412:57: error: class ‘Napi::CallbackScope’ does not have any field named ‘_scope’
   napi_env env, napi_callback_scope scope) : _env(env), _scope(scope) {
                                                         ^~~~~~
../node_modules/node-addon-api/napi-inl.h: In constructor ‘Napi::CallbackScope::CallbackScope(napi_env, napi_async_context)’:
../node_modules/node-addon-api/napi-inl.h:3419:41: error: ‘_scope’ was not declared in this scope
       _env, Object::New(env), context, &_scope);
                                         ^~~~~~
../node_modules/node-addon-api/napi-inl.h:3419:41: note: suggested alternative: ‘isctype’
       _env, Object::New(env), context, &_scope);
                                         ^~~~~~
                                         isctype
../node_modules/node-addon-api/napi-inl.h:3418:24: error: ‘napi_open_callback_scope’ was not declared in this scope
   napi_status status = napi_open_callback_scope(
                        ^~~~~~~~~~~~~~~~~~~~~~~~
../node_modules/node-addon-api/napi-inl.h:3418:24: note: suggested alternative: ‘napi_open_handle_scope’
   napi_status status = napi_open_callback_scope(
                        ^~~~~~~~~~~~~~~~~~~~~~~~
                        napi_open_handle_scope
../node_modules/node-addon-api/napi-inl.h: In destructor ‘virtual Napi::CallbackScope::~CallbackScope()’:
../node_modules/node-addon-api/napi-inl.h:3424:35: error: ‘_scope’ was not declared in this scope
   napi_close_callback_scope(_env, _scope);
                                   ^~~~~~
../node_modules/node-addon-api/napi-inl.h:3424:35: note: suggested alternative: ‘isctype’
   napi_close_callback_scope(_env, _scope);
                                   ^~~~~~
                                   isctype
../node_modules/node-addon-api/napi-inl.h:3424:3: error: ‘napi_close_callback_scope’ was not declared in this scope
   napi_close_callback_scope(_env, _scope);
   ^~~~~~~~~~~~~~~~~~~~~~~~~
../node_modules/node-addon-api/napi-inl.h:3424:3: note: suggested alternative: ‘napi_close_handle_scope’
   napi_close_callback_scope(_env, _scope);
   ^~~~~~~~~~~~~~~~~~~~~~~~~
   napi_close_handle_scope
../node_modules/node-addon-api/napi-inl.h: At global scope:
../node_modules/node-addon-api/napi-inl.h:3427:32: error: expected type-specifier before ‘napi_callback_scope’
 inline CallbackScope::operator napi_callback_scope() const {
                                ^~~~~~~~~~~~~~~~~~~

Getting this error with node 8.12.0, 10.9.0, and 11.1.0 Reverting node-addon-api back to 1.5.0 fixes this error.

corymickelson commented 6 years ago

Not sure if this is what is causing other peoples errors, but the one I was getting was caused by using the src/node-api.h instead of that in node itself. Removing

target_include_directories(${PROJECT_NAME}
                                               PRIVATE
              remove this--->    ${CMAKE_SOURCE_DIR}/node_modules/node-addon-api/src

is all that was needed.

ebickle commented 6 years ago

Same issue here - node.js v8.10.0, node-gyp v3.6.2, node-addon-api v1.6.0.

NickNaso commented 6 years ago

Hi everyone, sorry for this. The problem is that we added Napi::CallbackScope that use N-API function that has been released starting from Node.js v8.11.2. It works well on Node.js v8.12.0, v6.14.4, v10.x and 11.x. So I'm trying ti figure out how to solve the problem.

NickNaso commented 6 years ago

Hi @gabrielschulhof @mhdawson, I take a look a this issue and what I discovered is that napi_open_callback_scope and napi_close_callback_scope is in Node.js 8 is available starting from the version 8.11.2. These two functions are available in the last version of Node.js 6 (v6.14.4) and in all Node 10 versions. What I'm trying to do is to exclude Napi::CallbackScope where we don't have napi_open_callback_scopeandnapi_close_callback_scopefunctions. Do you have suggestions about this? Do we need to port thenapi_open_callback_scopeandnapi_close_callback_scope` in the internal version of N-API that is node-addon-api repo?

ebickle commented 6 years ago

@NickNaso What about using the NODE_MAJOR_VERSION and NODE_MINOR_VERSION #DEFINE macros and placing the code behind an #ifdef gate? I see some use of that pattern here: https://github.com/nodejs/node-addon-api/blob/master/src/node_internals.h

In a broader sense we may need to come up with a forward compatibility strategy for node-addon-api. N-API was so new that versioning differences between Node.js and the C++ wrapper weren't a concern until recently. Even if node-addon-api were to be aligned to the same major version strategy as Node.js, new minor version features will still cause compilation failures.

Probably should document the minimum version of Node.js that's needed and whenever that bumps increment the major version of node-addon-api. Node.js 8.10 is definitely going to be needed due to AWS Lambda and the way AWS (unfortunately) locks to a specific minor version of Node.

mhdawson commented 6 years ago

I think we have the required version management, we just missed guarding the new functionality with the right defines when we added the CallbackScope support. I should have caught that as part of the review.

Looking at fixing it now.

mhdawson commented 6 years ago

I believe this PR should resolve the issue: https://github.com/nodejs/node-addon-api/pull/395

@ebickle, @bsrdjan could one of you validate? I tested using the test suite, but I'd like to confirm that your re-world cases are resolved as well.

ebickle commented 6 years ago

@mhdawson I updated one of our libraries to point to that specific commit and had a team test a built with it. They reported back that it worked with no errors. Dealing with a few different versions of NPM which makes things fun, but looks good.

mhdawson commented 6 years ago

@ebickle thanks !