From 7625956b2088c38e6e4a2a328539be5142e3b7c1 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 11 Apr 2023 23:26:28 +0800 Subject: [PATCH] fix(core): only use native hasher if available (#16197) --- packages/nx/src/hasher/file-hasher.ts | 5 +++-- packages/nx/src/hasher/native-file-hasher.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/hasher/file-hasher.ts b/packages/nx/src/hasher/file-hasher.ts index e78c1698a518d..2a619259fe2de 100644 --- a/packages/nx/src/hasher/file-hasher.ts +++ b/packages/nx/src/hasher/file-hasher.ts @@ -10,8 +10,9 @@ import { NativeFileHasher } from './native-file-hasher'; function createFileHasher(): FileHasherBase { try { if ( - !process.env.NX_NON_NATIVE_HASHER || - process.env.NX_NON_NATIVE_HASHER != 'true' + (!process.env.NX_NON_NATIVE_HASHER || + process.env.NX_NON_NATIVE_HASHER != 'true') && + NativeFileHasher.available() ) { return new NativeFileHasher(); } diff --git a/packages/nx/src/hasher/native-file-hasher.ts b/packages/nx/src/hasher/native-file-hasher.ts index 9e5c86e6f7033..48862472bb83e 100644 --- a/packages/nx/src/hasher/native-file-hasher.ts +++ b/packages/nx/src/hasher/native-file-hasher.ts @@ -3,6 +3,15 @@ import { performance } from 'perf_hooks'; import { workspaceRoot } from '../utils/app-root'; export class NativeFileHasher extends FileHasherBase { + static available() { + try { + require('../native'); + return true; + } catch { + return false; + } + } + async init(): Promise { performance.mark('init hashing:start'); // Import as needed. There is also an issue running unit tests in Nx repo if this is a top-level import.