Skip to content

Conversation

@HouseinIsProgramming
Copy link
Collaborator

@HouseinIsProgramming HouseinIsProgramming commented Sep 5, 2025

Fixes #3506

Description

This PR adds a new helper function that uses require.resolve() to find the package directory instead of using root + /path-to-package.

This solves the problem where node_modules are hoisted (moved up the directory tree), causing the hardcoded path approach to fail when packages aren't in the expected location.

Before: root + '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/node_modules/package-name'
After: require.resolve('package-name') to dynamically locate the package

Also adds process.exit(0) on errors to prevent "locking the console".

Checklist

📌 Always:

  • I have set a clear title
  • My PR is small and contains a single feature
  • I have checked my own PR

👍 Most of the time:

  • I have added or updated test cases
  • I have updated the README if needed

Summary by CodeRabbit

  • New Features

    • None
  • Bug Fixes

    • Added top-level error handling for the app-creation flow with clearer non-zero exit on failure.
    • Email template copying now reports failures and stops instead of failing silently.
  • Refactor

    • Replaced hard-coded module location logic with dynamic package-resolution for improved portability.
  • Chores

    • Added a helper to robustly resolve package root directories.

@vercel
Copy link

vercel bot commented Sep 5, 2025

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

Project Deployment Preview Updated (UTC)
docs Ready Ready Preview Sep 8, 2025 0:36am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 5, 2025

Walkthrough

Replaces hard-coded node_modules lookups with a new exported helper resolvePackageRootDir(pkgName) and applies it in the create flow to locate ts-node, @vendure/core, and @vendure/email-plugin. Adds top-level Promise catch to log errors and exit on failure and explicit error logging/exit when copying email templates.

Changes

Cohort / File(s) Summary
Create flow: dynamic package resolution & error handling
packages/create/src/create-vendure-app.ts
Use resolvePackageRootDir(...) to locate ts-node, @vendure/core (cli/populate and dist/index), and @vendure/email-plugin (email templates). Add top-level .catch to log errors and process.exit(1). Log and exit when email-template copy fails.
Helper utilities
packages/create/src/helpers.ts
Add and export resolvePackageRootDir(packageName: string) which resolves a package root via require.resolve(...) then ascends directories (falls back to cwd's node_modules/${packageName}), throwing descriptive errors if not found.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant CLI as create-vendure-app
    participant Resolver as resolvePackageRootDir
    participant FS as Filesystem
    participant Core as @vendure/core
    participant Email as @vendure/email-plugin
    Note over Resolver,FS: resolvePackageRootDir uses require.resolve and ascends dirs\nor falls back to cwd/node_modules

    User->>CLI: run npx @vendure/create
    CLI->>Resolver: resolvePackageRootDir('ts-node')
    Resolver->>FS: require.resolve + ascend dirs
    FS-->>Resolver: package root path
    Resolver-->>CLI: path(ts-node)

    CLI->>Resolver: resolvePackageRootDir('@vendure/core')
    Resolver->>FS: require.resolve + ascend dirs
    Resolver-->>CLI: path(@vendure/core)

    CLI->>Resolver: resolvePackageRootDir('@vendure/email-plugin')
    Resolver-->>CLI: path(email-plugin)

    CLI->>Core: require(cli/populate) & require(dist/index) via resolved paths
    CLI->>FS: copy email templates from resolved email-plugin path to project
    alt copy fails
        CLI->>User: log "Failed to copy email templates"
        CLI->>CLI: process.exit(1)
    else success
        CLI-->>User: continue scaffold
    end

    alt any unhandled error
        CLI->>User: log error
        CLI->>CLI: process.exit(1)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix create flow failure when copying email templates by resolving correct package/template paths and handling errors (#3506)

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 46eaacf and 4a36ed5.

📒 Files selected for processing (2)
  • packages/create/src/create-vendure-app.ts (4 hunks)
  • packages/create/src/helpers.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/create/src/create-vendure-app.ts
  • packages/create/src/helpers.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: publish_install (macos-latest, 24.x)
  • GitHub Check: e2e tests (24.x, postgres)
  • GitHub Check: e2e tests (24.x, sqljs)
  • GitHub Check: e2e tests (24.x, mysql)
  • GitHub Check: e2e tests (20.x, postgres)
  • GitHub Check: e2e tests (22.x, mysql)
  • GitHub Check: build (20.x)
  • GitHub Check: build (22.x)
  • GitHub Check: e2e tests (20.x, sqljs)
  • GitHub Check: e2e tests (20.x, mariadb)
  • GitHub Check: e2e tests (22.x, mariadb)
  • GitHub Check: e2e tests (22.x, postgres)
  • GitHub Check: unit tests (20.x)
  • GitHub Check: e2e tests (22.x, sqljs)
  • GitHub Check: unit tests (22.x)
  • GitHub Check: e2e tests (24.x, mariadb)
  • GitHub Check: e2e tests (20.x, mysql)
  • GitHub Check: build (24.x)
  • GitHub Check: unit tests (24.x)
  • GitHub Check: Analyze (javascript-typescript)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/create/src/create-vendure-app.ts (1)

270-271: Hoist issue remains: direct node_modules pathing for mysql/pg

These imports will fail in hoisted workspaces. Resolve by package id.

-const mysql = await import(path.join(root, 'node_modules/mysql'));
+const mysqlModule = await import('mysql');
+const mysql = (mysqlModule as any).default ?? (mysqlModule as any);
-const { Client } = await import(path.join(root, 'node_modules/pg'));
+const { Client } = await import('pg');

Also applies to: 300-301

🧹 Nitpick comments (3)
packages/create/src/create-vendure-app.ts (3)

73-76: Top-level catch + exit(1) is good

Prevents hanging on failures and surfaces an error code. Consider logging err.stack when available for easier diagnostics.


195-196: Typo in user-facing message

“inst all” → “install”.

-outro(pc.red(`Failed to inst all dependencies. Please try again.`));
+outro(pc.red(`Failed to install dependencies. Please try again.`));

301-301: Optional: simpler ts-node registration

This avoids filesystem resolution and is hoist-safe.

-require(path.join(resolveDirName('ts-node'))).register();
+require('ts-node').register();
+// or:
+// require('ts-node/register');
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between f546d22 and 25fcc2e.

📒 Files selected for processing (2)
  • packages/create/src/create-vendure-app.ts (4 hunks)
  • packages/create/src/helpers.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/create/src/create-vendure-app.ts (2)
packages/create/src/logger.ts (1)
  • log (10-24)
packages/create/src/helpers.ts (1)
  • resolveDirName (536-544)
🔇 Additional comments (1)
packages/create/src/create-vendure-app.ts (1)

27-28: Importing resolveDirName here makes sense

This aligns the file with the new helper.

Comment on lines 500 to 508
const emailPackageDirname = resolveDirName('@vendure/email-plugin');
const templateDir = path.join(emailPackageDirname, '/templates');
try {
await fs.copy(templateDir, path.join(root, 'static', 'email', 'templates'));
} catch (err: any) {
log(pc.red('Failed to copy email templates.'));
log(err);
process.exit(0);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Bug: Wrong template path and inappropriate exit(0) on error

  • Leading slash in path.join makes templateDir absolute (/templates).
  • Exiting with 0 masks failure and aborts the rest of the scaffolding.
-const emailPackageDirname = resolveDirName('@vendure/email-plugin');
-const templateDir = path.join(emailPackageDirname, '/templates');
+const emailPackageDirname = resolveDirName('@vendure/email-plugin');
+const templateDir = path.join(emailPackageDirname, 'templates');
 try {
     await fs.copy(templateDir, path.join(root, 'static', 'email', 'templates'));
 } catch (err: any) {
-    log(pc.red('Failed to copy email templates.'));
-    log(err);
-    process.exit(0);
+    log(pc.yellow('Warning: failed to copy email templates; continuing without them.'));
+    log(err?.message ?? String(err), { level: 'verbose' });
+    return;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const emailPackageDirname = resolveDirName('@vendure/email-plugin');
const templateDir = path.join(emailPackageDirname, '/templates');
try {
await fs.copy(templateDir, path.join(root, 'static', 'email', 'templates'));
} catch (err: any) {
log(pc.red('Failed to copy email templates.'));
log(err);
process.exit(0);
}
const emailPackageDirname = resolveDirName('@vendure/email-plugin');
const templateDir = path.join(emailPackageDirname, 'templates');
try {
await fs.copy(templateDir, path.join(root, 'static', 'email', 'templates'));
} catch (err: any) {
log(pc.yellow('Warning: failed to copy email templates; continuing without them.'));
log(err?.message ?? String(err), { level: 'verbose' });
return;
}
🤖 Prompt for AI Agents
In packages/create/src/create-vendure-app.ts around lines 500 to 508, the
templateDir is built with a leading slash which makes it resolve to an absolute
"/templates" path and the catch uses process.exit(0) which incorrectly signals
success; change the path construction to join the package dirname with
'templates' (no leading slash) so it points to the package's templates folder,
and in the catch log the error with context and terminate with a non-zero exit
(e.g. process.exit(1)) so failures are not masked.

@HouseinIsProgramming HouseinIsProgramming marked this pull request as draft September 5, 2025 16:27
Copy link
Member

@michaelbromley michaelbromley left a comment

Choose a reason for hiding this comment

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

2 very tiny changes please 🙏

@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 8, 2025

@michaelbromley michaelbromley merged commit fd971a5 into master Sep 8, 2025
34 of 36 checks passed
@michaelbromley michaelbromley deleted the fix-create-command branch September 8, 2025 12:38
@github-actions github-actions bot locked and limited conversation to collaborators Sep 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@vendure/create is failing with "Failed to copy email templates"

3 participants