Skip to content

Commit

Permalink
chore: resolved review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aryamohanan committed Aug 16, 2024
1 parent f15fdf6 commit b7a9df6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions packages/shared-metrics/src/util/nativeModuleRetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,28 +183,33 @@ function copyPrecompiled(opts, loaderEmitter, callback) {
.then(() => {
// See below for the reason why we append 'precompiled' to the path.
const targetDir = path.join(opts.nativeModulePath, 'precompiled');
const sourceDir = path.join(os.tmpdir(), opts.nativeModuleName);
logger.debug(
`Copying the precompiled build for ${opts.nativeModuleName} ${label} from ${sourceDir} to ${targetDir}.`
);

fse
.copy(path.join(os.tmpdir(), opts.nativeModuleName), targetDir)
.copy(sourceDir, targetDir, { dereference: true })
.then(() => {
// We have unpacked and copied the correct precompiled native addon. The next attempt to require the
// dependency should work.
//
// However, we must not use any of the paths from which Node.js has tried to load the module before (that
// is, node_modules/${opts.nativeModuleName}). Node.js' module loading infrastructure
// (lib/internal/modules/cjs/loader.js and lib/internal/modules/package_json_reader.js) have built-in
// caching on multiple levels (for example, package.json locations and package.json contents). If Node.js
// has tried unsuccessfully to load a module or read a package.json from a particular path,
// it will remember and not try to load anything from that path again (a `false` will be
// put into the cache for that cache key). Instead, we force a new path, by adding precompiled
// to the module path and use the absolute path to the module to load it.
logger.debug(`Successfully copied the precompiled build for ${opts.nativeModuleName} ${label}.`);
opts.loadFrom = targetDir;
callback(true);
})
.catch(error => {
logger.warn(`Copying the precompiled build for ${opts.nativeModuleName} ${label} failed.`, error);
callback(false);
});
// We have unpacked and copied the correct precompiled native addon. The next attempt to require the
// dependency should work.
//
// However, we must not use any of the paths from which Node.js has tried to load the module before (that
// is, node_modules/${opts.nativeModuleName}). Node.js' module loading infrastructure
// (lib/internal/modules/cjs/loader.js and lib/internal/modules/package_json_reader.js) have built-in
// caching on multiple levels (for example, package.json locations and package.json contents). If Node.js
// has tried unsuccessfully to load a module or read a package.json from a particular path,
// it will remember and not try to load anything from that path again (a `false` will be
// put into the cache for that cache key). Instead, we force a new path, by adding precompiled
// to the module path and use the absolute path to the module to load it.
})
.catch(tarErr => {
logger.warn(`Unpacking the precompiled build for ${opts.nativeModuleName} ${label} failed.`, tarErr);
Expand Down

0 comments on commit b7a9df6

Please sign in to comment.