Skip to content

Commit

Permalink
fix(core): several powerpack fixes (#28088)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

* There is no flag in `nx.json` to enable the db cache
* Typings for powerpack are `any`
* Powerpack errors are not shown in `nx report`
* 9999 workspaces show up when user purchases unlimited license
* There is no indication from caching that license has expired.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

* There is an `enableDbCache` flag for `nx.json`. It'll probably be
changed soon.
* Typings for powerpack come from the powerpack packages
* Powerpack errors are shown in nx report
* "an unlimited number of workspaces" shows up when user purchases
unlimited license
* There is an indication when licenses expire.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit 4081901)
  • Loading branch information
FrozenPandaz committed Sep 25, 2024
1 parent 5ac5544 commit a0ceabe
Show file tree
Hide file tree
Showing 12 changed files with 1,554 additions and 41 deletions.
9 changes: 9 additions & 0 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Nx.json configuration
- [cli](../../devkit/documents/NxJsonConfiguration#cli): Object
- [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase): string
- [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string
- [enableDbCache](../../devkit/documents/NxJsonConfiguration#enabledbcache): boolean
- [extends](../../devkit/documents/NxJsonConfiguration#extends): string
- [generators](../../devkit/documents/NxJsonConfiguration#generators): Object
- [implicitDependencies](../../devkit/documents/NxJsonConfiguration#implicitdependencies): ImplicitDependencyEntry<T>
Expand Down Expand Up @@ -98,6 +99,14 @@ will be used. Convenient for small workspaces with one main application.

---

### enableDbCache

`Optional` **enableDbCache**: `boolean`

Enable the new experimental db based cache

---

### extends

`Optional` **extends**: `string`
Expand Down
13 changes: 13 additions & 0 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ProjectsConfigurations or NxJsonConfiguration
- [cli](../../devkit/documents/Workspace#cli): Object
- [defaultBase](../../devkit/documents/Workspace#defaultbase): string
- [defaultProject](../../devkit/documents/Workspace#defaultproject): string
- [enableDbCache](../../devkit/documents/Workspace#enabledbcache): boolean
- [extends](../../devkit/documents/Workspace#extends): string
- [generators](../../devkit/documents/Workspace#generators): Object
- [implicitDependencies](../../devkit/documents/Workspace#implicitdependencies): ImplicitDependencyEntry<string[] | "\*">
Expand Down Expand Up @@ -118,6 +119,18 @@ will be used. Convenient for small workspaces with one main application.

---

### enableDbCache

`Optional` **enableDbCache**: `boolean`

Enable the new experimental db based cache

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[enableDbCache](../../devkit/documents/NxJsonConfiguration#enabledbcache)

---

### extends

`Optional` **extends**: `string`
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
"@nx/js": "19.8.0-beta.2",
"@nx/next": "19.8.0-beta.2",
"@nx/playwright": "19.8.0-beta.2",
"@nx/powerpack-license": "0.0.2-alpha.4",
"@nx/powerpack-s3-cache": "0.0.2-alpha.4",
"@nx/powerpack-shared-fs-cache": "0.0.2-alpha.4",
"@nx/react": "19.8.0-beta.2",
"@nx/storybook": "19.8.0-beta.2",
"@nx/vite": "19.8.0-beta.2",
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"@nx/nx-linux-arm64-musl",
"@nx/nx-linux-arm-gnueabihf",
"@nx/nx-win32-arm64-msvc",
"@nx/nx-freebsd-x64"
"@nx/nx-freebsd-x64",
"@nx/powerpack-license"
]
}
]
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/adapter/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const allowedWorkspaceExtensions = [
'useInferencePlugins',
'neverConnectToCloud',
'sync',
'enableDbCache',
] as const;

if (!patched) {
Expand Down
27 changes: 23 additions & 4 deletions packages/nx/src/command-line/report/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { getNxRequirePaths } from '../../utils/installation-directory';
import { NxJsonConfiguration, readNxJson } from '../../config/nx-json';
import { ProjectGraph } from '../../config/project-graph';
import { ProjectGraphError } from '../../project-graph/error-types';
import { getPowerpackLicenseInformation } from '../../utils/powerpack';
import {
getPowerpackLicenseInformation,
NxPowerpackNotInstalledError,
} from '../../utils/powerpack';
import type { PowerpackLicense } from '@nx/powerpack-license';

const nxPackageJson = readJsonFile<typeof import('../../../package.json')>(
join(__dirname, '../../../package.json')
Expand Down Expand Up @@ -61,6 +65,7 @@ export async function reportHandler() {
pm,
pmVersion,
powerpackLicense,
powerpackError,
localPlugins,
powerpackPlugins,
communityPlugins,
Expand Down Expand Up @@ -93,6 +98,7 @@ export async function reportHandler() {
});

if (powerpackLicense) {
bodyLines.push('');
bodyLines.push(LINE_SEPARATOR);
bodyLines.push(chalk.green('Nx Powerpack'));

Expand Down Expand Up @@ -122,6 +128,13 @@ export async function reportHandler() {
)}`
);
}
bodyLines.push('');
} else if (powerpackError) {
bodyLines.push('');
bodyLines.push(chalk.red('Nx Powerpack'));
bodyLines.push(LINE_SEPARATOR);
bodyLines.push(powerpackError.message);
bodyLines.push('');
}

if (registeredPlugins.length) {
Expand Down Expand Up @@ -183,8 +196,8 @@ export async function reportHandler() {
export interface ReportData {
pm: PackageManager;
pmVersion: string;
// TODO(@FrozenPandaz): Provide the right type here.
powerpackLicense: any | null;
powerpackLicense: PowerpackLicense | null;
powerpackError: Error | null;
powerpackPlugins: PackageJson[];
localPlugins: string[];
communityPlugins: PackageJson[];
Expand Down Expand Up @@ -234,13 +247,19 @@ export async function getReportData(): Promise<ReportData> {
const native = isNativeAvailable();

let powerpackLicense = null;
let powerpackError = null;
try {
powerpackLicense = await getPowerpackLicenseInformation();
} catch {}
} catch (e) {
if (!(e instanceof NxPowerpackNotInstalledError)) {
powerpackError = e;
}
}

return {
pm,
powerpackLicense,
powerpackError,
powerpackPlugins,
pmVersion,
localPlugins,
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
* Configuration for the `nx sync` command.
*/
sync?: NxSyncConfiguration;

/**
* Enable the new experimental db based cache
*/
enableDbCache?: boolean;
}

export type PluginConfiguration = string | ExpandedPluginConfiguration;
Expand Down
33 changes: 16 additions & 17 deletions packages/nx/src/tasks-runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { machineId } from 'node-machine-id';
import { NxCache, CachedResult as NativeCacheResult } from '../native';
import { getDbConnection } from '../utils/db-connection';
import { isNxCloudUsed } from '../utils/nx-cloud-utils';
import { readNxJson } from '../config/nx-json';
import { NxJsonConfiguration, readNxJson } from '../config/nx-json';
import { verifyOrUpdateNxCloudClient } from '../nx-cloud/update-manager';
import { getCloudOptions } from '../nx-cloud/utilities/get-cloud-options';
import { isCI } from '../utils/is-ci';
Expand All @@ -28,9 +28,12 @@ export type CachedResult = {
};
export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult };

export function getCache(options: DefaultTasksRunnerOptions) {
export function getCache(
nxJson: NxJsonConfiguration,
options: DefaultTasksRunnerOptions
) {
return process.env.NX_DISABLE_DB !== 'true' &&
process.env.NX_DB_CACHE === 'true'
(nxJson.enableDbCache === true || process.env.NX_DB_CACHE === 'true')
? new DbCache({
// Remove this in Nx 21
nxCloudRemoteCache: isNxCloudUsed(readNxJson())
Expand Down Expand Up @@ -155,26 +158,22 @@ export class DbCache {
}
}

private async getPowerpackS3Cache(): Promise<RemoteCacheV2 | null> {
try {
const { getRemoteCache } = await import(
this.resolvePackage('@nx/powerpack-s3-cache')
);
return getRemoteCache();
} catch {
return null;
}
private getPowerpackS3Cache(): Promise<RemoteCacheV2 | null> {
return this.getPowerpackCache('@nx/powerpack-s3-cache');
}

private getPowerpackSharedCache(): Promise<RemoteCacheV2 | null> {
return this.getPowerpackCache('@nx/powerpack-shared-fs-cache');
}

private async getPowerpackSharedCache(): Promise<RemoteCacheV2 | null> {
private async getPowerpackCache(pkg: string): Promise<RemoteCacheV2 | null> {
let getRemoteCache = null;
try {
const { getRemoteCache } = await import(
this.resolvePackage('@nx/powerpack-shared-fs-cache')
);
return getRemoteCache();
getRemoteCache = (await import(this.resolvePackage(pkg))).getRemoteCache;
} catch {
return null;
}
return getRemoteCache();
}

private resolvePackage(pkg: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/tasks-runner/default-tasks-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ async function runAllTasks(
context.initiatingProject,
context.projectGraph,
context.taskGraph,
context.nxJson,
options,
context.nxArgs?.nxBail,
context.daemon,
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/tasks-runner/task-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import {
import { workspaceRoot } from '../utils/workspace-root';
import { output } from '../utils/output';
import { combineOptionsForExecutor } from '../utils/params';
import { NxJsonConfiguration } from '../config/nx-json';

export class TaskOrchestrator {
private cache = getCache(this.options);
private cache = getCache(this.nxJson, this.options);
private forkedProcessTaskRunner = new ForkedProcessTaskRunner(this.options);

private tasksSchedule = new TasksSchedule(
Expand Down Expand Up @@ -68,6 +69,7 @@ export class TaskOrchestrator {
private readonly initiatingProject: string | undefined,
private readonly projectGraph: ProjectGraph,
private readonly taskGraph: TaskGraph,
private readonly nxJson: NxJsonConfiguration,
private readonly options: DefaultTasksRunnerOptions,
private readonly bail: boolean,
private readonly daemon: DaemonClient,
Expand Down
11 changes: 5 additions & 6 deletions packages/nx/src/utils/powerpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ export async function printPowerpackLicense() {
logger.log(
`Nx Powerpack Licensed to ${organizationName} for ${seatCount} user${
seatCount > 1 ? '' : 's'
} in ${workspaceCount} workspace${workspaceCount > 1 ? '' : 's'}`
} in ${
workspaceCount === 9999 ? 'an unlimited number of' : workspaceCount
} workspace${workspaceCount > 1 ? '' : 's'}`
);
} catch {}
}

export async function getPowerpackLicenseInformation() {
try {
const { getPowerpackLicenseInformation } = (await import(
// @ts-ignore
'@nx/powerpack-license'
// TODO(@FrozenPandaz): Provide the right type here.
)) as any;
// )) as typeof import('@nx/powerpack-license');
)) as typeof import('@nx/powerpack-license');
return getPowerpackLicenseInformation(workspaceRoot);
} catch (e) {
if ('code' in e && e.code === 'ERR_MODULE_NOT_FOUND') {
if ('code' in e && e.code === 'MODULE_NOT_FOUND') {
throw new NxPowerpackNotInstalledError(e);
}
throw e;
Expand Down
Loading

0 comments on commit a0ceabe

Please sign in to comment.