Skip to content

Commit

Permalink
chore(core): proxy process env setting
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jan 15, 2025
1 parent b148ccc commit e3c31b7
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 37 deletions.
18 changes: 1 addition & 17 deletions packages/nx/src/project-graph/plugins/get-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
cleanupPluginTSTranspiler,
pluginTranspilerIsRegistered,
} from './transpiler';
import { isIsolationEnabled } from './isolation/enabled';

/**
* Stuff for specified NX Plugins.
Expand Down Expand Up @@ -97,23 +98,6 @@ export function cleanupPlugins() {
* Stuff for generic loading
*/

function isIsolationEnabled() {
// Explicitly enabled, regardless of further conditions
if (process.env.NX_ISOLATE_PLUGINS === 'true') {
return true;
}
if (
// Explicitly disabled
process.env.NX_ISOLATE_PLUGINS === 'false' ||
// Isolation is disabled on WASM builds currently.
IS_WASM
) {
return false;
}
// Default value
return true;
}

const loadingMethod = isIsolationEnabled()
? loadNxPluginInIsolation
: loadNxPlugin;
Expand Down
18 changes: 18 additions & 0 deletions packages/nx/src/project-graph/plugins/isolation/enabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IS_WASM } from 'nx/src/native';

export function isIsolationEnabled() {
// Explicitly enabled, regardless of further conditions
if (process.env.NX_ISOLATE_PLUGINS === 'true') {
return true;
}
if (
// Explicitly disabled
process.env.NX_ISOLATE_PLUGINS === 'false' ||
// Isolation is disabled on WASM builds currently.
IS_WASM
) {
return false;
}
// Default value
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface PluginWorkerPreRunMessageResult {
| {
tx: string;
success: true;
mutations: NodeJS.ProcessEnv;
}
| {
success: false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function createWorkerHandler(
preRunResult: ({ tx, ...result }) => {
const { resolver, rejector } = pending.get(tx);
if (result.success) {
resolver();
resolver(result.mutations);
} else if (result.success === false) {
rejector(result.error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ const server = createServer((socket) => {
},
preRun: async ({ tx, context }) => {
try {
await plugin.preRun?.(context);
const mutations = await plugin.preRun?.(context);
return {
type: 'preRunResult',
payload: { success: true, tx },
payload: { success: true, tx, mutations },
};
} catch (e) {
return {
Expand Down
46 changes: 34 additions & 12 deletions packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
ProjectsMetadata,
} from './public-api';
import { createNodesFromFiles } from './utils';
import type { TaskResults } from '../../tasks-runner/life-cycle';
import { isIsolationEnabled } from './isolation/enabled';

export class LoadedNxPlugin {
readonly name: string;
Expand All @@ -38,7 +38,7 @@ export class LoadedNxPlugin {
graph: ProjectGraph,
context: CreateMetadataContext
) => Promise<ProjectsMetadata>;
readonly preRun?: (context: PreRunContext) => Promise<void>;
readonly preRun?: (context: PreRunContext) => Promise<NodeJS.ProcessEnv>;
readonly postRun?: (context: PostRunContext) => Promise<void>;

readonly options?: unknown;
Expand Down Expand Up @@ -114,18 +114,40 @@ export class LoadedNxPlugin {
}

if (plugin.preRun) {
this.preRun = async (context: PreRunContext) =>
this.preRun = async (context: PreRunContext) => {
const updates = {};
const originalEnv = process.env;
let revokeFn: () => void;
if (isIsolationEnabled()) {
const { proxy, revoke } = Proxy.revocable<NodeJS.ProcessEnv>(
process.env,
{
set: (target, key: string, value) => {
target[key] = value;
updates[key] = value;
return true;
},
}
);
process.env = proxy;
revokeFn = revoke;
}
plugin.preRun(this.options, context);
}

if (plugin.postRun) {
this.postRun = async (context: PostRunContext) =>
plugin.postRun(this.options, context);
if (revokeFn) {
revokeFn();
}
process.env = originalEnv;
for (const key in updates) {
process.env[key] = updates[key];
}
return updates;
};

if (plugin.postRun) {
this.postRun = async (context: PostRunContext) =>
plugin.postRun(this.options, context);
}
}
}
}

export type CreateNodesResultWithContext = CreateNodesResult & {
file: string;
pluginName: string;
};
12 changes: 10 additions & 2 deletions packages/nx/src/project-graph/plugins/run-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { PostRunContext, PreRunContext } from './public-api';
import type { LoadedNxPlugin } from './loaded-nx-plugin';
import { isIsolationEnabled } from './isolation/enabled';

export async function runPreRun(
plugins: LoadedNxPlugin[],
pluginContext: PreRunContext
) {
performance.mark(`preRun:start`);
await Promise.all(
const envs = await Promise.all(
plugins
.filter((p) => p.preRun)
.map(async (plugin) => {
performance.mark(`${plugin.name}:preRun:start`);
try {
await plugin.preRun(pluginContext);
return await plugin.preRun(pluginContext);
} finally {
performance.mark(`${plugin.name}:preRun:end`);
performance.measure(
Expand All @@ -23,6 +24,13 @@ export async function runPreRun(
}
})
);
if (isIsolationEnabled()) {
for (const env of envs) {
for (const key in env) {
process.env[key] = env[key];
}
}
}
performance.mark(`preRun:end`);
performance.measure(`preRun`, `preRun:start`, `preRun:end`);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ export async function runCommand(
projectsToRun,
currentProjectGraph,
{ nxJson },
nxArgs,
{
...nxArgs,
skipNxCache:
process.env.NX_SKIP_NX_CACHE === 'true' ||
process.env.DISABLE_NX_CACHE === 'true',
},
overrides,
initiatingProject,
extraTargetDependencies,
Expand Down
8 changes: 6 additions & 2 deletions packages/nx/src/tasks-runner/task-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export class TaskOrchestrator {
task: Task;
status: 'local-cache' | 'local-cache-kept-existing' | 'remote-cache';
}> {
task.startTime = Date.now();
const cachedResult = await this.cache.get(task);
if (!cachedResult || cachedResult.code !== 0) return null;

Expand All @@ -241,7 +240,6 @@ export class TaskOrchestrator {
if (shouldCopyOutputsFromCache) {
await this.cache.copyFilesFromCache(task.hash, cachedResult, outputs);
}
task.endTime = Date.now();
const status = cachedResult.remote
? 'remote-cache'
: shouldCopyOutputsFromCache
Expand Down Expand Up @@ -545,6 +543,10 @@ export class TaskOrchestrator {

// region Lifecycle
private async preRunSteps(tasks: Task[], metadata: TaskMetadata) {
const now = Date.now();
for (const task of tasks) {
task.startTime = now;
}
await this.options.lifeCycle.startTasks(tasks, metadata);
}

Expand All @@ -558,7 +560,9 @@ export class TaskOrchestrator {
doNotSkipCache: boolean,
{ groupId }: { groupId: number }
) {
const now = Date.now();
for (const task of tasks) {
task.endTime = now;
await this.recordOutputsHash(task);
}

Expand Down

0 comments on commit e3c31b7

Please # to comment.