node-gyp icon indicating copy to clipboard operation
node-gyp copied to clipboard

Non-deterministic temporary files left in node_modules

Open alexeagle opened this issue 2 years ago • 1 comments

A Makefile is generated during compilation, for example the ffi-napi package leaves a file node_modules/ffi-napi/build/Makefile on the disk.

This file contains an absolute path on the disk, which is non-deterministic: it depends on the setup of the machine where the installation takes place.

As a result, build systems which do incremental re-build/re-test based on distributed caches (such as Bazel) will have a cache miss when this file gets included in the inputs.

As a workaround, Bazel users must delete the build folder from offending packages in a postinstall step to avoid re-building everything that depends on that package.

 "scripts": {
    "// ": "we must delete non-deterministic outputs from compiling native packages, or they bust the bazel cache",
    "postinstall": "rm -rf node_modules/{deasync,iltorb,node-sass}/build node_modules/node-gyp/gyp/pylib/gyp/generator/__pycache__",
}

Instead I think it would be better for node-gyp to clean up its temporary files. (Perhaps a flag can be added for users who want to inspect those files to debug a native compilation)

alexeagle avatar Feb 14 '22 21:02 alexeagle

I'm buildng on Docker, so the paths are always the same, but the order of some of the dependencies for the Makefile target differs from one build to the next (this example is from better-sqlite3):

< Makefile: $(srcdir)/binding.gyp $(srcdir)/deps/defines.gypi $(srcdir)/../../../../local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi $(srcdir)/build/config.gypi $(srcdir)/deps/sqlite3.gyp $(srcdir)/../../../../../root/.cache/node-gyp/18.16.1/include/node/common.gypi $(srcdir)/deps/common.gypi
---
> Makefile: $(srcdir)/build/config.gypi $(srcdir)/deps/sqlite3.gyp $(srcdir)/deps/defines.gypi $(srcdir)/binding.gyp $(srcdir)/../../../../local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi $(srcdir)/../../../../../root/.cache/node-gyp/18.16.1/include/node/common.gypi $(srcdir)/deps/common.gypi

So even keeping your paths the same doesn't make this deterministic.

jwalton avatar Jul 18 '23 20:07 jwalton