nx/e2e/utils/get-env-info.ts
Craigory Coppola a0e8f83672
fix(core): move plugin worker to socket (#26558)
<!-- 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
Plugin isolation communicates with workers via built-in node IPC with
forked processes. When doing this, the parent process will not exit
until the child process has exited, in case more messages would be sent.
This requires an explicit call to shut down the plugin workers.

We set this up as a `process.on('exit')` listener, to shutdown the
workers whenever the main Nx process dies. This is "fine", but requires
explicit calls to `process.exit` as node won't exit on its own
otherwise.

## Expected Behavior
To allow plugin workers to clean themselves up on exit, but not require
explicit `process.exit` calls, we need to detach them from the main
process and call `unref`. This only works when IPC is not being used. As
such, we need a different way to communicate with the worker.

This PR updates the communication method to mirror the daemon, and
communicate over a socket. Additionally, this PR enables isolation
during the Nx repo's E2E tests.

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

Fixes #
2024-06-26 17:31:10 -04:00

177 lines
4.4 KiB
TypeScript

import { readJsonFile, workspaceRoot } from '@nx/devkit';
import { execSync } from 'child_process';
import { existsSync } from 'fs-extra';
import { join } from 'path';
import { dirSync } from 'tmp';
import * as isCI from 'is-ci';
import { PackageManager } from 'nx/src/utils/package-manager';
import { tmpProjPath } from './create-project-utils';
import { e2eConsoleLogger } from './log-utils';
export const isWindows = require('is-windows');
export function getPublishedVersion(): string {
process.env.PUBLISHED_VERSION =
process.env.PUBLISHED_VERSION ||
// read version of built nx package
readJsonFile(join(workspaceRoot, `./build/packages/nx/package.json`))
.version ||
// fallback to latest if built nx package is missing
'latest';
return process.env.PUBLISHED_VERSION as string;
}
export function detectPackageManager(dir: string = ''): PackageManager {
return existsSync(join(dir, 'bun.lockb'))
? 'bun'
: existsSync(join(dir, 'yarn.lock'))
? 'yarn'
: existsSync(join(dir, 'pnpm-lock.yaml')) ||
existsSync(join(dir, 'pnpm-workspace.yaml'))
? 'pnpm'
: 'npm';
}
export function isNotWindows() {
return !isWindows();
}
export function isOSX() {
return process.platform === 'darwin';
}
export function isAndroid() {
return (
process.platform === 'linux' &&
process.env.ANDROID_HOME &&
process.env.ANDROID_SDK_ROOT
);
}
export const e2eRoot = isCI
? dirSync({ prefix: 'nx-e2e-' }).name
: '/tmp/nx-e2e';
export function isVerbose() {
return (
process.env.NX_VERBOSE_LOGGING === 'true' ||
process.argv.includes('--verbose')
);
}
export function isVerboseE2ERun() {
return process.env.NX_E2E_VERBOSE_LOGGING === 'true' || isVerbose();
}
export const e2eCwd = `${e2eRoot}/nx`;
export function getSelectedPackageManager(): 'npm' | 'yarn' | 'pnpm' | 'bun' {
return (process.env.SELECTED_PM as 'npm' | 'yarn' | 'pnpm' | 'bun') || 'npm';
}
export function getNpmMajorVersion(): string | undefined {
try {
const [npmMajorVersion] = execSync(`npm -v`).toString().split('.');
return npmMajorVersion;
} catch {
return undefined;
}
}
export function getYarnMajorVersion(path: string): string | undefined {
try {
// this fails if path is not yet created
const [yarnMajorVersion] = execSync(`yarn -v`, {
cwd: path,
encoding: 'utf-8',
}).split('.');
return yarnMajorVersion;
} catch {
try {
const [yarnMajorVersion] = execSync(`yarn -v`, {
encoding: 'utf-8',
}).split('.');
return yarnMajorVersion;
} catch {
return undefined;
}
}
}
export function getLatestLernaVersion(): string {
const lernaVersion = execSync(`npm view lerna version`, {
encoding: 'utf-8',
}).trim();
return lernaVersion;
}
export const packageManagerLockFile = {
npm: 'package-lock.json',
yarn: 'yarn.lock',
pnpm: 'pnpm-lock.yaml',
bun: 'bun.lockb',
};
export function ensureCypressInstallation() {
let cypressVerified = true;
try {
const r = execSync('npx cypress verify', {
stdio: isVerbose() ? 'inherit' : 'pipe',
encoding: 'utf-8',
cwd: tmpProjPath(),
});
if (r.indexOf('Verified Cypress!') === -1) {
cypressVerified = false;
}
} catch {
cypressVerified = false;
} finally {
if (!cypressVerified) {
e2eConsoleLogger('Cypress was not verified. Installing Cypress now.');
execSync('npx cypress install', {
stdio: isVerbose() ? 'inherit' : 'pipe',
encoding: 'utf-8',
cwd: tmpProjPath(),
});
}
}
}
export function ensurePlaywrightBrowsersInstallation() {
const playwrightInstallArgs =
process.env.PLAYWRIGHT_INSTALL_ARGS || '--with-deps';
execSync(`npx playwright install ${playwrightInstallArgs}`, {
stdio: isVerbose() ? 'inherit' : 'pipe',
encoding: 'utf-8',
cwd: tmpProjPath(),
});
e2eConsoleLogger(
`Playwright browsers ${execSync('npx playwright --version')
.toString()
.trim()} installed.`
);
}
export function getStrippedEnvironmentVariables() {
return Object.fromEntries(
Object.entries(process.env).filter(([key, value]) => {
if (key.startsWith('NX_E2E_')) {
return true;
}
const allowedKeys = ['NX_ADD_PLUGINS', 'NX_ISOLATE_PLUGINS'];
if (key.startsWith('NX_') && !allowedKeys.includes(key)) {
return false;
}
if (key === 'JEST_WORKER_ID') {
return false;
}
return true;
})
);
}