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

"node-gyp rebuild" fails with node addon api if module name contains special chars #36916

Open
djulien opened this issue Jan 13, 2021 · 1 comment
Labels
gyp Issues and PRs related to the GYP tool and .gyp build files

Comments

@djulien
Copy link

djulien commented Jan 13, 2021

What steps will reproduce the bug?

"node-gyp rebuild" on a C++ node addon that contains special chars within target_name in binding.gyp

(snip from binding.gyp)
"target_name": "my-addon", #this will fail
"target_name": "my_addon", #no errors

How often does it reproduce? Is there a required condition?

100% reproducible with conditions specified

What is the expected behavior?

successful node addon rebuild

What do you see instead?

(snip from build output)
: error: expected initializer before ‘-’ token
/home/dj/.cache/node-gyp/12.18.2/include/node/node_api.h:53:15: note: in definition of macro ‘NAPI_C_CTOR’
53 | static void fn(void) attribute((constructor));
| ^~

(snip)
: error: expected initializer before ‘-’ token
/home/dj/.cache/node-gyp/12.18.2/include/node/node_api.h:54:15: note: in definition of macro ‘NAPI_C_CTOR’
54 | static void fn(void)
| ^~
/home/dj/.cache/node-gyp/12.18.2/include/node/node_api.h:75:3: note: in expansion of macro ‘NAPI_MODULE_X’
75 | NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
| ^~~~~~~~~~~~~

Additional information

Problem exists within the definition of macro NAPI_MODULE_X within ~/.nvm/versions/node/v12.18.2/include/node/node_api.h
Root cause is that the module name is being used as a C++ symbol when it doesn't need to be.

To fix, change NAPI_MODULE_X at line 57 of ~/.nvm/versions/node/v12.18.2/include/node/node_api.h
from:
#define NAPI_MODULE_X(modname, regfunc, priv, flags)
EXTERN_C_START
static napi_module _module =
{
NAPI_MODULE_VERSION,
flags,
FILE,
regfunc,
#modname,
priv,
{0},
};
NAPI_C_CTOR(register ## modname) {
napi_module_register(&_module);
}
EXTERN_C_END

to (see "CHANGED HERE"):
#define NAPI_MODULE_X(modname, regfunc, priv, flags)
EXTERN_C_START
static napi_module _module =
{
NAPI_MODULE_VERSION,
flags,
FILE,
regfunc,
#modname,
priv,
{0},
};
NAPI_C_CTOR(register ## regfunc /*modname CHANGED HERE*/) {
napi_module_register(&_module);
}
EXTERN_C_END

This change will use regfunc instead of modname in the token pasting for the C ctor name.

@djulien djulien changed the title node-gyp rebuild fails with node addon api if module name contains special chars "node-gyp rebuild" fails with node addon api if module name contains special chars Jan 13, 2021
@RaisinTen RaisinTen added the gyp Issues and PRs related to the GYP tool and .gyp build files label Jan 14, 2021
@gireeshpunathil
Copy link
Member

@nodejs/node-gyp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gyp Issues and PRs related to the GYP tool and .gyp build files
Projects
None yet
Development

No branches or pull requests

3 participants