Skip to content

Makefile generator creates misplaced build artifacts for dependencies outside of binding.gyp root dir #325

@clavin

Description

@clavin

A binding.gyp with dependencies that live outside of its directory will create misplaced artifacts with the makefile generator (usually, on mac & linux). The dependency will be resolved to a relative path with parent directory segments (../../*) and those get joined as-is to the build directory (./build/../../*), resulting in build files outside of the build directory. This result may confuse other tooling too, unfortunately.

For example, the following code in the makefile generator produces a path like ./build/../../node_modules/node-addon-api/node_addon_api.target.mk:

if options.generator_output:
output_file = os.path.join(
options.depth, options.generator_output, base_path, base_name
)

Motivating example

I have a multi-workspace project set up like this:

  • super/ (root)
    • package.json (defines "workspaces")
    • workspaces/
      • native/
        • package.json
        • binding.gyp

super has no dependencies, and native only depends on node-gyp and node-addon-api. Note that in this setup, npm hoists node-addon-gyp to the root node_modules/.

node-addon-api recommends adding it as a dependency in binding.gyp:

# super/workspaces/native/binding.gyp
{
    "targets": [{
        "target_name": "native",
        "sources": ["module.cc"],
        "dependencies": ["<!(node -p \"require('node-addon-api').targets\"):node_addon_api"]
    }]
}

After running node-gyp rebuild, we end up with some mysterious build artifacts:

  • super/ (root)
    • node_modules/ ... (real, created by npm)
    • workspaces/
      • node_modules/ (not real, created by gyp)
        • node-addon-api/
          • node_addon_api_except_all.target.mk
          • node_addon_api_except.target.mk
          • node_addon_api_maybe.target.mk
          • node_addon_api.target.mk
          • node_addon_api.Makefile

A real issue that this causes is npm list failing:

$ npm ls

super@0.0.1 /[...]/super
├─┬ native@0.0.1 -> ./workspaces/native
│ ├── bindings@1.5.0
│ ├── node-addon-api@ invalid: "^8.5.0" from workspaces/native
│ └── node-gyp@12.1.0
└── node-addon-api@8.5.0 extraneous

npm error code ELSPROBLEMS
npm error extraneous: node-addon-api@8.5.0 /[...]/node_modules/node-addon-api
npm error invalid: node-addon-api@ /[...]/workspaces/node_modules/node-addon-api

# exit code 1

We also see some misplaced files within the build directory too:

  • super/workspaces/native/build/
    • node_modules/ (should be within Release/obj.target/)
      • node-addon-api/
        • node_addon_api.stamp
    • Release/ ...

I haven't found these artifacts to cause issues, but I am highlighting them because they share the same root cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions