nodejs / node-gyp

Node.js native addon build tool
MIT License
9.92k stars 1.79k forks source link

Using hyphen `-` in NODE_GYP_MODULE_NAME / `target_name` causes build errors #2653

Closed jacobq closed 6 months ago

jacobq commented 2 years ago

Hopefully this is an appropriate place to file this (if not, I am sorry; please redirect me...would it be better in nodejs/abi-stable-node?).

I recently updated a simple native node addon to use Node-API / node-addon-api and was encountering strange compile-time errors that appeared to be coming from code generated by expansion of macros like NAPI_MODULE_INIT. After troubleshooting for a while I tried changing the target_name (in bindings.gyp) to a name that did not use a hyphen (it had been of the form "foo-bar" to match the package.json name), and the problem went away. Is this a difference between NAPI and native/v8? Is it due to changes in gyp? MS Build tools? Is it documented somewhere? (last version of the code code, dated November 2019, used node.h and NODE_MODULE macro and hyphenated target_name without issue) Any clues appreciated.

The hint that helped me think of this was C2143: syntax error: missing ';' before '-', but the root cause surprised me because it didn't seem to be an issue with the older native/v8 version.

14.0.25420.1

* **Module**: Custom addon with `target_name` containing a hyphen/dash character (`-`). Can reproduce with ["hello world" example](https://github.com/nodejs/node-addon-examples/tree/main/1_hello_world/node-addon-api) by changing `target_name` to `"hello-world"` and then using `NODE_GYP_MODULE_NAME` instead of `hello` in the call to `NODE_API_MODULE`.

<details>
<summary>Boring initial output</summary>

gyp info it worked if it ends with ok gyp info using node-gyp@9.0.0 gyp info using node@16.15.0 | win32 | x64 gyp info find Python using Python version 3.8.8 found at "C:\ProgramData\Anaconda3\python.exe" gyp info find VS using VS2017 (15.9.28307.1585) found at: gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools" gyp info find VS run with --verbose for detailed information gyp info spawn C:\ProgramData\Anaconda3\python.exe gyp info spawn args [ gyp info spawn args 'C:\Users\user\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\node-gyp\gyp\gyp_main.py', gyp info spawn args 'binding.gyp', gyp info spawn args '-f', gyp info spawn args 'msvs', gyp info spawn args '-I', gyp info spawn args 'C:\Users\user\project\build\config.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\Users\user\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\node-gyp\addon.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\Users\user\AppData\Local\node-gyp\Cache\16.15.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=C:\Users\user\AppData\Local\node-gyp\Cache\16.15.0', gyp info spawn args '-Dnode_gyp_dir=C:\Users\user\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\node-gyp', gyp info spawn args '-Dnode_lib_file=C:\\Users\\user\\AppData\\Local\\node-gyp\\Cache\\16.15.0\\<(target_arch)\\node.lib', gyp info spawn args '-Dmodule_root_dir=C:\Users\user\project', 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 'C:\Users\user\project\build', gyp info spawn args '-Goutput_dir=.' gyp info spawn args ] gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe gyp info spawn args [ gyp info spawn args 'build/binding.sln', gyp info spawn args '/clp:Verbosity=minimal', gyp info spawn args '/nologo', gyp info spawn args '/p:Configuration=Release;Platform=x64' gyp info spawn args ]


</details>

<details><summary>Build errors (interesting part):</summary>

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch. addon.cc c:\users\user\project\addon.cc(130): error C2143: syntax error: missing ';' before '-' [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): warning C4229: anachronism used: modifiers on data are ignored [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): error C2182: '_register_foo': illegal use of type 'void' [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): error C2143: syntax error: missing ')' before '-' [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): error C2165: 'left-side modifier': cannot modify pointers to data [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): error C2059: syntax error: ')' [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): error C2086: 'int _register_foo': redefinition [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): note: see declaration of '_register_foo' c:\users\user\project\addon.cc(130): error C2143: syntax error: missing ';' before '{' [C:\Users\user\project\build\foo-bar.vcxproj] c:\users\user\project\addon.cc(130): error C2447: '{': missing function header (old-style formal list?) [C:\Users\user\project\build\foo-bar.vcxproj] gyp ERR! build error gyp ERR! stack Error: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (C:\Users\user\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\node-gyp\lib\build.js:194:23) gyp ERR! stack at ChildProcess.emit (node:events:527:28) gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) gyp ERR! System Windows_NT 10.0.22000 gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\user\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "rebuild" gyp ERR! cwd C:\Users\user\project gyp ERR! node -v v16.15.0 gyp ERR! node-gyp -v v9.0.0 gyp ERR! not ok



</details>
arthot commented 2 years ago

It's not just Windows. I have the same error on Mac OS.

Output

> verify-sdk@0.0.1 build
> prebuildify --napi --target 14.17.4

gyp info it worked if it ends with ok
gyp info using node-gyp@7.1.2
gyp info using node@14.17.4 | darwin | arm64
gyp info find Python using Python version 3.9.13 found at "/opt/homebrew/opt/python@3.9/bin/python3.9"
gyp info spawn /opt/homebrew/opt/python@3.9/bin/python3.9
gyp info spawn args [
gyp info spawn args   '/Users/artem/.nvm/versions/node/v14.17.4/lib/node_modules/npm/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/artem/evt/verify-sdk-wrapper/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/artem/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4',
gyp info spawn args   '-Dnode_gyp_dir=/Users/artem/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/artem/evt/verify-sdk-wrapper',
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 spawn args ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/verify-sdk/native/sdk.o
../native/sdk.c:138:16: error: variable has incomplete type 'void'
/*napi_value*/ NAPI_MODULE_INIT(/*napi_env env, napi_value exports*/) {
               ^
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:106:3: note: expanded from macro 'NAPI_MODULE_INIT'
  NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER)          \
  ^
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:92:3: note: expanded from macro 'NAPI_MODULE'
  NAPI_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
  ^
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:71:17: note: expanded from macro 'NAPI_MODULE_X'
    NAPI_C_CTOR(_register_ ## modname) {                              \
                ^
:418:1: note: expanded from here
_register_verify
^
../native/sdk.c:138:16: error: expected ';' after top level declarator
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:106:15: note: expanded from macro 'NAPI_MODULE_INIT'
  NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER)          \
              ^
:1:36: note: expanded from here
#define NODE_GYP_MODULE_NAME verify-sdk
                                   ^
../native/sdk.c:138:16: error: variable has incomplete type 'void'
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:106:3: note: expanded from macro 'NAPI_MODULE_INIT'
  NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER)          \
  ^
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:92:3: note: expanded from macro 'NAPI_MODULE'
  NAPI_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
  ^
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:71:17: note: expanded from macro 'NAPI_MODULE_X'
    NAPI_C_CTOR(_register_ ## modname) {                              \
                ^
:418:1: note: expanded from here
_register_verify
^
../native/sdk.c:138:16: error: expected ';' after top level declarator
/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node/14.17.4/include/node/node_api.h:106:15: note: expanded from macro 'NAPI_MODULE_INIT'
  NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER)          \
              ^
:1:36: note: expanded from here
#define NODE_GYP_MODULE_NAME verify-sdk
                                   ^
4 errors generated.
make: *** [Release/obj.target/verify-sdk/native/sdk.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/Users/artem/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
gyp ERR! System Darwin 21.5.0
gyp ERR! command "/Users/artem/.nvm/versions/node/v14.17.4/bin/node" "/Users/artem/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--target=14.17.4" "--devdir=/var/folders/ry/vn519xvd2zqbx1tv7fnmpzr40000gp/T/prebuildify/node" "--arch=arm64" "--release"
gyp ERR! cwd /Users/artem/evt/verify-sdk-wrapper
gyp ERR! node -v v14.17.4
gyp ERR! node-gyp -v v7.1.2
gyp ERR! not ok
node-gyp exited with 1
buffcode commented 1 year ago

This should either be fixed or result in an compilation error, mentioning an invalid target name.

StefanStojanovic commented 6 months ago

This issue seems no longer relevant as it uses a no longer supported version of Node and has been stale for a some time. If there are no objections, I will close this issue next Mon-Tue. In case something similar happens again please open a new issue.

truedat101 commented 3 weeks ago

Seems still relevant. In fact, the problem is also with the target_name having a "." in the name. So special character messes up the expansion of NODE_MODULE

../nTPLaddon/binding.cc:129:1: note: in expansion of macro ‘NODE_MODULE’
  129 | NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

which was called nTPL.native

truedat101 commented 3 weeks ago

I'm verifying this using node v20.17.0 on linux.