Skip to content

feat: Add -d/--directory option to serverpod generate command - #4131

Merged
SandPod merged 6 commits into
mainfrom
copilot/add-directory-option-to-generate
Nov 3, 2025
Merged

SandPod merged 6 commits into
mainfrom
copilot/add-directory-option-to-generate

Conversation

Copilot AI commented Oct 30, 2025 •

Copy link
Copy Markdown
Contributor

Adds directory specification to serverpod generate, enabling code generation from any location without changing working directory. Useful for monorepo setups and CI/CD pipelines.

Changes

  • Command option: Added --directory/-d parameter to GenerateOption enum with empty string default
  • Path resolution: Updated file path construction to use config.serverPackageDirectoryPathParts instead of relative paths for consistency when directory is specified
  • Config loading: Pass directory parameter to GeneratorConfig.load(), which already supported this parameter
  • Tests: Added integration tests for error handling with invalid directories and E2E tests to validate generating from different directories

Testing

  • Integration tests: Created in tools/serverpod_cli/test/integration/generate/ for error handling scenarios
  • E2E tests: Added in tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart that validate:
    • Generating a project from a different directory using absolute path with -d option
    • Generating a project from a different directory using relative path with -d option
    • Error handling when the specified directory doesn't exist

Usage

# From any directory
serverpod generate -d path/to/project_server

# Backward compatible - current directory still default
cd project_server && serverpod generate

Fixes #4095

Original prompt

This section details on the original issue you should resolve

<issue_title>Add -d/--directory option to serverpod generate command</issue_title>
<issue_description>## Description
The serverpod generate command should support a -d or --directory option to allow users to specify which directory to generate code for, rather than always operating on the current directory.

Affected Files

  • tools/serverpod_cli/lib/src/commands/generate.dart:50

Context

// TODO: add a -d option to select the directory
GeneratorConfig config;
try {
  config = await GeneratorConfig.load();

Suggested Implementation

  • Add a --directory or -d command-line option to the generate command
  • Update GeneratorConfig.load() to accept an optional directory parameter
  • Update command help text and documentation
  • Add tests for directory specification
  • Handle edge cases (non-existent directories, permission issues, etc.)

Impact

Improved CLI usability, especially for monorepo setups or CI/CD pipelines

Related

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Summary by CodeRabbit

  • New Features

    • Added -d directory option to the generate command, enabling code generation from custom project directories
  • Tests

    • Added integration and end-to-end test coverage for directory-specific code generation scenarios

@coderabbitai

coderabbitai Bot commented Oct 30, 2025 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This PR adds a -d/--directory command-line option to the Serverpod CLI generate command, enabling users to specify target directories for code generation. Implementation includes a new CLI option in GenerateOption, passing the directory parameter to GeneratorConfig.load(), updating file path resolutions for pubspec files relative to server/client packages, and comprehensive integration and end-to-end tests.

Changes

Cohort / File(s) Change Summary
Core Implementation
tools/serverpod_cli/lib/src/commands/generate.dart
Added -d/--directory StringOption to GenerateOption; updated GeneratorConfig.load() call to accept directory parameter; adjusted pubspec.yaml and pubspec.lock discovery to resolve relative to server/client package directory paths instead of root.
Integration Tests
tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
New integration test validating GeneratorConfig.load behavior: verifies ServerpodProjectNotFoundException when target directory does not exist or lacks Serverpod dependency; creates temporary directories with minimal pubspec.yaml files.
End-to-End Tests
tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
New e2e test validating serverpod generate -d option from different directory contexts; verifies CLI installation, project creation, code generation with absolute/relative paths, generated file presence, and error handling for non-existent directories.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Path resolution logic in generate.dart: Verify that pubspec.yaml and pubspec.lock are correctly resolved relative to server/client package directories across different directory inputs
  • Integration test expectations: Confirm error messages and exception types match actual GeneratorConfig.load() behavior
  • E2E test setup and assertions: Review temporary directory handling, CLI installation process, and file generation validation logic across multiple path scenarios

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "feat: Add -d/--directory option to serverpod generate command" clearly and concisely describes the main change in the changeset. The title accurately summarizes the core feature being added—a new command-line option for directory specification—and is specific enough that a developer scanning the git history would immediately understand the purpose of this change. The title follows conventional commit conventions with the "feat:" prefix and avoids vague or generic terminology.
Linked Issues Check ✅ Passed The pull request successfully implements the core coding requirements from linked issue #4095. The implementation adds the --directory/-d CLI option to the generate command with proper flag configuration [#4095], updates the code to pass the directory parameter to GeneratorConfig.load() [#4095], and updates file path resolution to use config.serverPackageDirectoryPathParts for consistent path handling [#4095]. Comprehensive tests are included: integration tests validate error handling for non-existent directories and missing dependencies [#4095], and E2E tests verify generation from different directory contexts with both absolute and relative paths [#4095]. All major coding objectives from the linked issue are addressed.
Out of Scope Changes Check ✅ Passed All changes in this pull request are directly scoped to implementing the -d/--directory feature requested in issue #4095. The modified generate.dart file implements the core feature, while the two new test files (integration tests and E2E tests) validate the functionality and edge cases. There are no unrelated modifications to other systems, no refactoring outside the feature scope, and no changes to unrelated components. All changes directly support the single feature objective.
Description Check ✅ Passed The pull request description provides substantive information covering what is being changed (adding a -d/--directory parameter), why it matters (enables generation from any location for monorepo and CI/CD scenarios), the specific changes made (with implementation details), testing strategy, and usage examples. The description explicitly fixes issue #4095 as required. While the pre-launch checklist from the template is not explicitly included in the description, the description contains the core required information about the change, its rationale, and linked issues, making it substantially complete.

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

Copilot AI changed the title [WIP] Add -d/--directory option to serverpod generate command feat: Add -d/--directory option to serverpod generate command Oct 30, 2025
Copilot AI requested a review from SandPod October 30, 2025 14:14
@SandPod

SandPod commented Nov 2, 2025

Copy link
Copy Markdown
Contributor

@copilot Please add a test for this in tests/serverpod_cli_e2e_test/test. I want at least one test that validates that I can generate a project from another directory than the serverpod server.

Copilot AI commented Nov 2, 2025 •

Copy link
Copy Markdown
Contributor Author

@copilot Please add a test for this in tests/serverpod_cli_e2e_test/test. I want at least one test that validates that I can generate a project from another directory than the serverpod server.

Added E2E tests in tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart that validate:

  • Generating a project from a different directory using absolute path with -d option
  • Generating a project from a different directory using relative path with -d option
  • Error handling when the specified directory doesn't exist

All tests are passing. (5f2dd90)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.mixpanel.com
    • Triggering command: dart /home/REDACTED/work/serverpod/serverpod/tools/serverpod_cli/./.dart_tool/pub/bin/serverpod_cli/serverpod_cli.dart-3.9.0.snapshot version (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@SandPod
SandPod marked this pull request as ready for review November 2, 2025 17:23
@SandPod

SandPod commented Nov 2, 2025

Copy link
Copy Markdown
Contributor

@coderabbitai review

@SandPod
SandPod requested a review from a team November 2, 2025 17:24
@coderabbitai

coderabbitai Bot commented Nov 2, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart (1)

73-80: Remove unnecessary kill() call on already-completed process.

The process's exit code is already awaited in the setUp (line 66), so the process has completed by the time tearDown runs. Calling kill() on a completed process is unnecessary.

Apply this diff:

     tearDown(() async {
-      createProcess.kill();
       // Clean up the created project
       var projectDir = Directory(path.join(tempPath, projectName));
       if (projectDir.existsSync()) {
         projectDir.deleteSync(recursive: true);
       }
     });
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 048ea0d and 5f2dd90.

📒 Files selected for processing (3)
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart (1 hunks)
  • tools/serverpod_cli/lib/src/commands/generate.dart (3 hunks)
  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.dart: Format all Dart code using dart format . before committing
Code must pass Dart analyzer in strict mode (util/run_tests_analyze or melos lint_strict)
Use descriptive variable and function names

Files:

  • tools/serverpod_cli/lib/src/commands/generate.dart
  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
**/lib/**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Remove unnecessary comments from production code

Files:

  • tools/serverpod_cli/lib/src/commands/generate.dart
**/*_test.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*_test.dart: Write test descriptions using the Given/When/Then pattern
Organize tests with passing/success cases first, group related cases using group(), then follow with error cases

Files:

  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
🧠 Learnings (6)
📓 Common learnings
Learnt from: tp
Repo: serverpod/serverpod PR: 3917
File: modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/analysis_options.yaml:3-6
Timestamp: 2025-08-25T19:46:58.073Z
Learning: In Serverpod, test tool paths like `test/integration/test_tools/serverpod_test_tools.dart` are configured per project in the "generator.yaml" file and managed by a generator system, so they don't need to be updated manually across all files at once.
📚 Learning: 2025-08-25T19:46:58.073Z
Learnt from: tp
Repo: serverpod/serverpod PR: 3917
File: modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/analysis_options.yaml:3-6
Timestamp: 2025-08-25T19:46:58.073Z
Learning: In Serverpod, test tool paths like `test/integration/test_tools/serverpod_test_tools.dart` are configured per project in the "generator.yaml" file and managed by a generator system, so they don't need to be updated manually across all files at once.

Applied to files:

  • tools/serverpod_cli/lib/src/commands/generate.dart
  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
📚 Learning: 2025-10-15T11:44:29.873Z
Learnt from: CR
Repo: serverpod/serverpod PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-15T11:44:29.873Z
Learning: Applies to **/*_test.dart : Organize tests with passing/success cases first, group related cases using group(), then follow with error cases

Applied to files:

  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
📚 Learning: 2025-07-02T16:07:50.947Z
Learnt from: tp
Repo: serverpod/serverpod PR: 3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".

Applied to files:

  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
📚 Learning: 2025-06-27T08:39:36.440Z
Learnt from: tp
Repo: serverpod/serverpod PR: 3721
File: util/pub_get_all:0-0
Timestamp: 2025-06-27T08:39:36.440Z
Learning: In the Serverpod project, `dart pub get` commands work fine on Flutter packages both locally and in CI environments, even when the package has `flutter: sdk: flutter` dependencies in pubspec.yaml. The technical constraint of Flutter SDK dependencies causing `dart pub` failures may not apply in all environments or recent Dart versions.

Applied to files:

  • tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart
  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
📚 Learning: 2025-10-15T11:44:29.873Z
Learnt from: CR
Repo: serverpod/serverpod PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-15T11:44:29.873Z
Learning: Applies to **/*_test.dart : Write test descriptions using the Given/When/Then pattern

Applied to files:

  • tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart
⏰ 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). (10)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
  • GitHub Check: Migration e2e tests (3.29.0)
🔇 Additional comments (7)
tests/serverpod_cli_e2e_test/test/generate_with_directory_test.dart (1)

43-266: Well-structured E2E tests with good coverage.

The test suite properly follows the Given/When/Then pattern and organizes tests with passing cases first, then error cases. The tests provide comprehensive coverage of absolute paths, relative paths, and error scenarios for the new -d option.

tools/serverpod_cli/lib/src/commands/generate.dart (4)

28-34: Clean implementation of the directory option.

The new directory option is well-defined with appropriate defaults, short flag, and clear help text. The empty string default allows the existing behavior (current directory) to be preserved.


56-60: LGTM!

The directory parameter is correctly extracted from the command configuration and passed to GeneratorConfig.load().


66-69: Correct path resolution for server pubspec file.

The updated path construction using config.serverPackageDirectoryPathParts properly resolves the pubspec.yaml location relative to the server package directory, supporting the new directory option.


97-100: Consistent path resolution for pubspec.lock.

The path construction mirrors the approach used for pubspec.yaml, ensuring consistent behavior for both files when using the directory option.

tools/serverpod_cli/test/integration/generate/generate_with_directory_test.dart (2)

12-27: Well-structured error case test.

The test properly validates the error handling when GeneratorConfig.load is called with a non-existent directory, using Given/When/Then pattern and checking both exception type and message.


29-63: Comprehensive validation of missing dependency scenario.

The test creates a realistic setup with server and client directories containing pubspec files without Serverpod dependencies, then properly validates the exception type and error message.

@nielsenko
nielsenko self-requested a review November 3, 2025 14:58

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some comments on the test setup, but most importantly I believe the error message needs to reference the directory passed with -d/--directory.

@SandPod
SandPod requested a review from nielsenko November 3, 2025 16:15

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🚀

@SandPod
SandPod merged commit 5a895a2 into main Nov 3, 2025
@SandPod
SandPod deleted the copilot/add-directory-option-to-generate branch November 3, 2025 16:38
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 3, 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.

Add -d/--directory option to serverpod generate command

3 participants