Skip to content

fix(plugin): handle directory paths correctly in executor and generator generators #31856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AgentEnder
Copy link
Member

Fixes path handling issue in @nx/plugin generators where the last folder name in the path was being ignored.

The executor and generator generators were not creating subdirectories as expected when provided with directory paths. This fix detects when the path looks like a directory path and adjusts the directory to include the artifact name as a subdirectory.

Changes:

  • Modified normalizeOptions in both executor and generator generators to detect directory paths
  • Added logic to create subdirectories when the path segment matches the artifact name
  • Added comprehensive tests to verify the fix works correctly
  • Ensures backward compatibility with existing file path usage

Fixes #31803
Fixes #31776

Generated with Claude Code

…or generators

The executor and generator generators were not creating subdirectories as expected when provided with directory paths. 

For example, when using `--path=nx-tools/azure/src/executors/artifact-upload`, users expected files to be created in `nx-tools/azure/src/executors/artifact-upload/` directory, but they were being created directly in `nx-tools/azure/src/executors/`.

This fix detects when the path looks like a directory path (doesn't end with an explicit filename) and adjusts the directory to include the artifact name as a subdirectory.

Changes:
- Modified normalizeOptions in both executor and generator generators to detect directory paths
- Added logic to create subdirectories when the path segment matches the artifact name
- Added comprehensive tests to verify the fix works correctly
- Ensures backward compatibility with existing file path usage

Fixes #31803

Co-authored-by: Craigory Coppola <[email protected]>
@AgentEnder AgentEnder requested a review from a team as a code owner July 8, 2025 14:35
@AgentEnder AgentEnder requested a review from FrozenPandaz July 8, 2025 14:35
Copy link

vercel bot commented Jul 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview Jul 8, 2025 3:55pm

Copy link
Contributor

nx-cloud bot commented Jul 8, 2025

🤖 Nx Cloud AI Fix Available

Fix generation in progress

Status: Fix generation not started • View Fix ↗


View your CI Pipeline Execution ↗ for commit 9a4f69a

Command Status Duration Result
nx run-many -t check-imports check-commit check... ❌ Failed 15s View ↗
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 48s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 2s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 4s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 3s View ↗
nx documentation ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-08 15:50:15 UTC

Comment on lines 178 to 181
// If the last segment doesn't contain a file extension and matches the artifact name,
// it's likely a directory path, so we should create a subdirectory
if (!lastSegment.includes('.') && lastSegment === name) {
directory = joinPathFragments(initialDirectory, name);
Copy link
Contributor

Choose a reason for hiding this comment

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

The current directory path detection logic has a limitation when artifact names contain periods. The check !lastSegment.includes('.') incorrectly assumes any segment with a period is a file, but artifact names themselves can legitimately contain periods.

Consider implementing a more robust detection method, such as:

  1. Checking for known file extensions (.ts, .js, etc.) rather than any period
  2. Using a pattern matching approach that identifies the artifact name at the end of the path
  3. Adding explicit directory/file path detection logic

This would prevent incorrect subdirectory creation when using artifact names that contain periods but are intended as directory names.

Suggested change
// If the last segment doesn't contain a file extension and matches the artifact name,
// it's likely a directory path, so we should create a subdirectory
if (!lastSegment.includes('.') && lastSegment === name) {
directory = joinPathFragments(initialDirectory, name);
// If the last segment doesn't end with a known file extension and matches the artifact name,
// it's likely a directory path, so we should create a subdirectory
const knownFileExtensions = ['.ts', '.js', '.jsx', '.tsx'];
const hasKnownFileExtension = knownFileExtensions.some(ext => lastSegment.endsWith(ext));
if (!hasKnownFileExtension && lastSegment === name) {
directory = joinPathFragments(initialDirectory, name);

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Member Author

Choose a reason for hiding this comment

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

@claude implement this fix in both generators

Copy link
Contributor

@claude claude bot Jul 8, 2025

Choose a reason for hiding this comment

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

Claude finished @AgentEnder's task —— View job


Enhanced Directory Path Detection Implementation Complete ✅

Successfully implemented the fix for directory path detection in both generators as requested.

Tasks:

  • Read current implementation in both generators
  • Understand the problematic path detection logic
  • Implement enhanced logic using known file extensions
  • Apply fix to both executor and generator generators
  • Test the changes
  • Commit the improvements

Analysis:
Both generators had the same issue at line 180 (executor.ts) and line 59 (generator.ts). The current logic !lastSegment.includes('.') incorrectly treated any segment with a period as a file, but artifact names can legitimately contain periods.

Fix Applied:
✅ Replaced the dot check with proper file extension detection using known extensions: .ts, .js, .jsx, .tsx.
✅ Updated both packages/plugin/src/generators/executor/executor.ts and packages/plugin/src/generators/generator/generator.ts
✅ Tests are passing successfully
✅ Code has been formatted with prettier
✅ Changes committed to the PR branch

What was changed:

The logic now uses:

const knownFileExtensions = ['.ts', '.js', '.jsx', '.tsx'];
const hasKnownFileExtension = knownFileExtensions.some((ext) =>
  lastSegment.endsWith(ext)
);
if (!hasKnownFileExtension && lastSegment === name) {
  directory = joinPathFragments(initialDirectory, name);
}

This enhancement properly detects directory paths by checking for known file extensions rather than any period, resolving the issue where artifact names containing periods were incorrectly treated as files.

Commit: 4faeb08

Replace simple dot check with proper file extension detection using known extensions (.ts, .js, .jsx, .tsx) to correctly handle artifact names containing periods.

This resolves the issue where artifact names like "my.artifact" were incorrectly treated as files instead of directory paths.

Co-authored-by: Craigory Coppola <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nx generate @nx/plugin:executor ignores last folder name in given path nx g @nx/plugin:generator does not create last folder in path as expected
1 participant