Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 31, 2025

The argv field was already implemented in PreTasksExecutionContext and PostTasksExecutionContext but lacked tests and documentation for plugin authors to discover and use it.

Changes

  • Added unit tests (packages/nx/src/daemon/server/handle-tasks-execution-hooks.spec.ts) validating that argv flows correctly through hook handlers for different command patterns (direct, affected, run-many)

  • Enhanced existing documentation in astro-docs/src/content/docs/extending-nx/task-running-lifecycle.mdoc with a new section covering:

    • Context property definitions showing the argv field
    • Examples showing how to detect command types (direct execution, affected, run-many)
    • Example demonstrating conditional analytics based on the original command
    • Common command patterns reference
    • Best practices for defensive argv parsing

Usage

import type { NxPlugin, PostTasksExecutionContext } from '@nx/devkit';

export const myPlugin: NxPlugin = {
  name: 'my-plugin',
  
  postTasksExecution: async (options, context: PostTasksExecutionContext) => {
    // Distinguish between nx build my-app vs nx affected -t build
    if (context.argv.includes('affected')) {
      console.log('Running in affected mode');
    }
  }
};

Fixes https://linear.app/nxdev/issue/NXC-3382/add-contextargv-to-task-execution-hook-contexts

Original prompt

Issue Title: Add context.argv to task execution hook contexts
Issue Description: Expose the original CLI arguments on the plugin worker so hooks can distinguish how execution was started (e.g., nx build nx-api vs nx affected -t build). Proposal: include the invoking argv on the hook context (e.g., context.argv).
Fixes https://linear.app/nxdev/issue/NXC-3382/add-contextargv-to-task-execution-hook-contexts

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
https://github.com/nrwl/nx

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
Aha! that worked - so you can tell it to assign to copilot instead of "me and copilot"

Comment by User d484ef82-7f7d-4a95-be09-9d82ca3905dc:
📋 I wasn't able to determine which GitHub repository to work in.

I think it's one of these, but can you tell me which one is right?

Comment by User :
Created issue NXC-3382

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
This comment thread is synced to a corresponding thread in Slack. All replies are displayed in both locations.

Comment by User :
This thread is for an agent session with githubcopilot.

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
@linear make a ticket and assign it to copilot

Comment by User f5ae6d50-28e9-4ee7-ad51-3da8208d5914:
Makes sense to me

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
Sure, context.argv?

Comment by User f5ae6d50-28e9-4ee7-ad51-3da8208d5914:
We can add them as argv?

Comment by User f5ae6d50-28e9-4ee7-ad51-3da8208d5914:
Yeah they would run on the plugin worker so it's not there

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
@jason we could add originalArgv to the contexts?

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
Eh, probably not... they run on the plugin worker

Comment by User 4215f3ef-50bd-4f09-85a0-b489c88057b6:
Yeah, I don't think the hooks know.... I'd be curious if process.argv would just have that info though

Comment by User 439b15a6-827b-4258-971a-d86133ad59de:
payfit does

Comment by User 74901385-a023-4825-8470-fe68b1b55664:
I can’t see anything about that in the docs - so I would assume the hooks are agnostic to how the tasks were triggered?

Comment by User 74901385-a023-4825-8470-fe68b1b55664:
so they’re asking is there’s a way to tell the difference between nx build nx-api or nx affected -t build in the task hook?

Comment by User 74901385-a023-4825-8470-fe68b1b55664:

I’ve been playing around with the Task Execution Hooks, specifically the postTasksExecution hook, and I think it will be really useful for me to grab some detailed metrics for our specific use cases.
What I feel like it’s missing is a way to see what command actually started the task execution, whether it was a specific target or an affected command. As long as it was a specific target, I think the tasks are sorted in order so the last taskResult will probably be the actual target of the command but for affected it seems a bit more random what the last result will be.
Is there a way to know exactly which command kicked off the ‘task execution’?


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel
Copy link

vercel bot commented Oct 31, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
nx-dev Ready Ready Preview Oct 31, 2025 5:54pm
@netlify
Copy link

netlify bot commented Oct 31, 2025

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit f764814
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/6904f6edf7294c00084c3bd6
😎 Deploy Preview https://deploy-preview-33322--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI and others added 3 commits October 31, 2025 17:25
Co-authored-by: AgentEnder <6933928+AgentEnder@users.noreply.github.com>
Co-authored-by: AgentEnder <6933928+AgentEnder@users.noreply.github.com>
Co-authored-by: AgentEnder <6933928+AgentEnder@users.noreply.github.com>
Copilot AI changed the title [WIP] Add context.argv to task execution hook contexts Oct 31, 2025
Copilot AI requested a review from AgentEnder October 31, 2025 17:29
Copilot finished work on behalf of AgentEnder October 31, 2025 17:29
@AgentEnder
Copy link
Member

@copilot Remove the markdown docs file and add it in the appropriate place in the astro-docs project instead. The docs should cover example usages, such as determining what the original command being ran by the user is, and be located alongside existing docs for the hooks.

…cle guide

Co-authored-by: AgentEnder <6933928+AgentEnder@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 31, 2025

@copilot Remove the markdown docs file and add it in the appropriate place in the astro-docs project instead. The docs should cover example usages, such as determining what the original command being ...

Done in f764814. Removed the root-level markdown file and added the documentation to astro-docs/src/content/docs/extending-nx/task-running-lifecycle.mdoc alongside existing hook documentation. The new section includes:

  • Context property definitions showing argv
  • Example showing how to detect command types (direct, affected, run-many)
  • Example demonstrating conditional analytics based on the original command
  • Common command patterns reference
  • Best practices for defensive argv parsing
Copilot finished work on behalf of AgentEnder October 31, 2025 17:51
@AgentEnder AgentEnder changed the title Document and test argv in task execution hook contexts Oct 31, 2025
@AgentEnder AgentEnder marked this pull request as ready for review October 31, 2025 20:47
@AgentEnder AgentEnder requested review from a team as code owners October 31, 2025 20:47
@nx-cloud
Copy link
Contributor

nx-cloud bot commented Oct 31, 2025

View your CI Pipeline Execution ↗ for commit f764814

Command Status Duration Result
nx affected --targets=lint,test,test-kt,build,e... ❌ Failed 6m 22s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 1m 57s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 10s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 2s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2025-10-31 21:03:41 UTC

Copy link
Contributor

@nx-cloud nx-cloud bot left a comment

Choose a reason for hiding this comment

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

Nx Cloud is proposing a fix for your failed CI:

These changes fix the formatting error by running Prettier on the task-running-lifecycle.mdoc file. The documentation content added in this PR was not formatted before committing, causing the astro-docs:format check to fail. Running npx prettier --write on the file ensures it meets the project's code style standards.

We verified this fix by re-running astro-docs:format.

Suggested Fix changes
diff --git a/astro-docs/src/content/docs/extending-nx/task-running-lifecycle.mdoc b/astro-docs/src/content/docs/extending-nx/task-running-lifecycle.mdoc
index 9a3969b015..b593889cca 100644
--- a/astro-docs/src/content/docs/extending-nx/task-running-lifecycle.mdoc
+++ b/astro-docs/src/content/docs/extending-nx/task-running-lifecycle.mdoc
@@ -169,14 +169,16 @@ function isAffectedCommand(argv) {
 }
 
 function getTargetName(argv) {
-  const targetIndex = argv.findIndex(arg => arg === '-t' || arg === '--target');
+  const targetIndex = argv.findIndex(
+    (arg) => arg === '-t' || arg === '--target'
+  );
   return targetIndex !== -1 ? argv[targetIndex + 1] : undefined;
 }
 
 export async function postTasksExecution(options, context) {
   const isAffected = isAffectedCommand(context.argv);
   const target = getTargetName(context.argv);
-  
+
   // Send analytics with command context
   await sendAnalytics({
     executionId: context.id,
@@ -203,7 +205,9 @@ When working with `context.argv`, parse it defensively:
 const target = context.argv[2];
 
 // Good - searches for the flag
-const targetIndex = context.argv.findIndex(arg => arg === '-t' || arg === '--target');
+const targetIndex = context.argv.findIndex(
+  (arg) => arg === '-t' || arg === '--target'
+);
 const target = targetIndex !== -1 ? context.argv[targetIndex + 1] : undefined;
 ​`​`​`
 

Apply fix via Nx Cloud  Reject fix via Nx Cloud

Or Apply changes locally with:

npx nx-cloud apply-locally 9bVB-pd98

Apply fix locally with your editor ↗  View interactive diff ↗


🎓 To learn more about Self Healing CI, please visit nx.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants