Skip to content

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

Open
@djulien

Description

@djulien

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    gypIssues and PRs related to the GYP tool and .gyp build files

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions