Skip to content

Commit

Permalink
feat(core): add hasher implementation info in nx report (#16261)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored Apr 13, 2023
1 parent 9658fb1 commit cfaf649
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
16 changes: 13 additions & 3 deletions packages/nx/src/command-line/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {
import { gt, valid } from 'semver';
import { findInstalledPlugins } from '../utils/plugins/installed-plugins';
import { getNxRequirePaths } from '../utils/installation-directory';
import {
getHashingImplementation,
HasherImplementation,
} from '../utils/get-hashing-implementation';

const nxPackageJson = readJsonFile<typeof import('../../package.json')>(
join(__dirname, '../../package.json')
Expand Down Expand Up @@ -59,12 +63,14 @@ export async function reportHandler() {
packageVersionsWeCareAbout,
outOfSyncPackageGroup,
projectGraphError,
currentHasherImplementation,
} = await getReportData();

const bodyLines = [
`Node : ${process.versions.node}`,
`OS : ${process.platform} ${process.arch}`,
`${pm.padEnd(5)}: ${pmVersion}`,
`Node : ${process.versions.node}`,
`OS : ${process.platform} ${process.arch}`,
`${pm.padEnd(7)}: ${pmVersion}`,
`Hasher : ${currentHasherImplementation}`,
``,
];

Expand Down Expand Up @@ -142,6 +148,7 @@ export interface ReportData {
migrateTarget: string;
};
projectGraphError?: Error | null;
currentHasherImplementation: HasherImplementation;
}

export async function getReportData(): Promise<ReportData> {
Expand Down Expand Up @@ -172,6 +179,8 @@ export async function getReportData(): Promise<ReportData> {

const outOfSyncPackageGroup = findMisalignedPackagesForPackage(nxPackageJson);

const currentHasherImplementation = getHashingImplementation();

return {
pm,
pmVersion,
Expand All @@ -180,6 +189,7 @@ export async function getReportData(): Promise<ReportData> {
packageVersionsWeCareAbout,
outOfSyncPackageGroup,
projectGraphError,
currentHasherImplementation,
};
}

Expand Down
31 changes: 8 additions & 23 deletions packages/nx/src/hasher/file-hasher.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
import { GitBasedFileHasher } from './git-based-file-hasher';
import { workspaceRoot } from '../utils/workspace-root';
import { NodeBasedFileHasher } from './node-based-file-hasher';
import { FileHasherBase } from './file-hasher-base';
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join } from 'path';
import { NativeFileHasher } from './native-file-hasher';
import {
HasherImplementation,
getHashingImplementation,
} from '../utils/get-hashing-implementation';

function createFileHasher(): FileHasherBase {
try {
if (
(!process.env.NX_NON_NATIVE_HASHER ||
process.env.NX_NON_NATIVE_HASHER != 'true') &&
NativeFileHasher.available()
) {
switch (getHashingImplementation()) {
case HasherImplementation.Native:
return new NativeFileHasher();
}

execSync('git rev-parse --is-inside-work-tree', {
stdio: 'ignore',
windowsHide: true,
});

// we don't use git based hasher when the repo uses git submodules
if (!existsSync(join(workspaceRoot, '.git', 'modules'))) {
case HasherImplementation.Git:
return new GitBasedFileHasher();
} else {
case HasherImplementation.Node:
return new NodeBasedFileHasher();
}
} catch {
return new NodeBasedFileHasher();
}
}

Expand Down
38 changes: 38 additions & 0 deletions packages/nx/src/utils/get-hashing-implementation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NativeFileHasher } from '../hasher/native-file-hasher';
import { workspaceRoot } from './workspace-root';

import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join } from 'path';

export enum HasherImplementation {
Native = 'Native',
Git = 'Git',
Node = 'Node',
}

export function getHashingImplementation() {
try {
if (
(!process.env.NX_NON_NATIVE_HASHER ||
process.env.NX_NON_NATIVE_HASHER != 'true') &&
NativeFileHasher.available()
) {
return HasherImplementation.Native;
}

execSync('git rev-parse --is-inside-work-tree', {
stdio: 'ignore',
windowsHide: true,
});

// we don't use git based hasher when the repo uses git submodules
if (!existsSync(join(workspaceRoot, '.git', 'modules'))) {
return HasherImplementation.Git;
} else {
return HasherImplementation.Node;
}
} catch {
return HasherImplementation.Node;
}
}

1 comment on commit cfaf649

@vercel
Copy link

@vercel vercel bot commented on cfaf649 Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.