Skip to content
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

fix(cli): bogus validation error when merging imported enums #1861

Merged
merged 2 commits into from
Nov 15, 2024
Merged

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 14, 2024

fixes #1849

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request primarily involve modifications to the loadDocument function in cli-util.ts, focusing on how imported documents are removed and introducing a new validation step post-merge of imported declarations. The assignableToAttributeParam function has been removed from utils.ts, affecting type assignment validation. Additionally, a new regression test has been added to ensure the proper handling of enums in imported schemas, specifically addressing an issue related to default values.

Changes

File Path Change Summary
packages/schema/src/cli/cli-util.ts - Modified loadDocument to change document deletion logic and added validationAfterImportMerge.
packages/schema/src/language-server/validator/utils.ts - Removed assignableToAttributeParam function.
tests/regression/tests/issue-1849.test.ts - Added a new regression test for issue #1849 regarding enum resolution in imported schemas.

Assessment against linked issues

Objective Addressed Explanation
"Reference not resolved" error when using an enum from an imported schema in @default (#1849)

Possibly related PRs


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e88448e and 423ba6c.

📒 Files selected for processing (1)
  • packages/schema/src/language-server/validator/utils.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/schema/src/language-server/validator/utils.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 1

🧹 Outside diff range and nitpick comments (1)
tests/regression/tests/issue-1849.test.ts (1)

4-23: Enhance test coverage with negative test cases.

While the current test verifies the fix for using imported enums as default values, consider adding negative test cases to ensure proper validation errors are thrown for invalid scenarios.

Consider adding these test cases:

it('should fail with invalid enum value', async () => {
    await expect(loadSchema(
        `schema.zmodel
        import './enum'
        
        model Post {
          id Int @id
          status Status @default(INVALID_VALUE)
        }

        ${FILE_SPLITTER}enum.zmodel
        enum Status { 
          PENDING
          PUBLISHED
        }`,
        { provider: 'postgresql', pushDb: false }
    )).rejects.toThrow();
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85a8d5d and e88448e.

📒 Files selected for processing (3)
  • packages/schema/src/cli/cli-util.ts (1 hunks)
  • packages/schema/src/language-server/validator/utils.ts (0 hunks)
  • tests/regression/tests/issue-1849.test.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/schema/src/language-server/validator/utils.ts
🔇 Additional comments (4)
tests/regression/tests/issue-1849.test.ts (2)

21-21: Configuration matches reported environment.

The test configuration correctly uses PostgreSQL as specified in issue #1849, with pushDb: false for isolation.


6-20: Verify schema structure matches the issue report.

The test schema correctly reproduces the scenario from issue #1849 where an imported enum is used as a default value.

Let's verify this matches the original issue:

✅ Verification successful

Test case structure is valid for verifying enum imports and defaults

The test case correctly verifies:

  • Import of an enum from a separate file using relative import
  • Usage of the imported enum as a field type
  • Setting a default value from the imported enum
  • Proper separation of schema files using FILE_SPLITTER

The test structure is appropriate for regression testing of enum imports and default values, even though we couldn't access the original issue. The test follows ZenStack's schema testing patterns and includes all necessary components to verify the functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the test schema matches the issue report
# Expected: Find similar schema structure in the issue description

gh issue view 1849 --json body | jq -r '.body' | grep -A 10 "schema.zmodel"

Length of output: 73


Script:

#!/bin/bash
# Let's try searching for the issue number in the test file and surrounding context
rg -B 5 -A 5 "1849" tests/regression/tests/issue-1849.test.ts

Length of output: 289


Script:

#!/bin/bash
# Let's check the entire test file content to understand the test case better
cat tests/regression/tests/issue-1849.test.ts

Length of output: 620


Script:

#!/bin/bash
# Let's search for any related test files or documentation about importing enums
rg -g "*.md" -g "*.test.ts" -i "import.*enum" --multiline

Length of output: 280

packages/schema/src/cli/cli-util.ts (2)

108-111: Verify the fix addresses the original issue.

The changes modify document cleanup and add validation, but we should verify they fully address the "Reference not resolved: Status" error mentioned in issue #1849.

#!/bin/bash
# Description: Verify the changes fix the enum reference resolution issue

# Search for test cases covering imported enum default values
rg -l "enum.*Status.*@default" tests/

# Check for any remaining "Reference not resolved" error messages in the codebase
rg "Reference not resolved.*Status"

# Look for similar patterns that might be affected
ast-grep --pattern 'enum $enumName {
  $$$
}

model $modelName {
  $$$
  $field $type @default($enumValue)
  $$$
}'

108-109: Review the impact of the new document cleanup approach.

The change from using DocumentBuilder.update to a two-step cleanup process (direct deletion + index removal) might affect how imported declarations are handled. While this approach is more explicit, we should verify it doesn't introduce side effects for imported enum references.

✅ Verification successful

Document cleanup change is safe and consistent

The new two-step cleanup approach (using deleteDocument and IndexManager.remove) is a direct replacement for the previous DocumentBuilder.update pattern. Both approaches aim to clean up imported documents, but the new implementation is more explicit in handling both the document and its index. The search results show that:

  1. The old pattern used DocumentBuilder.update([], [doc.uri]) to remove documents
  2. The new approach properly handles both document cleanup and index removal in a coordinated way
  3. No other parts of the codebase rely on the specific cleanup mechanism

This change maintains the same cleanup semantics while being more explicit about the operations being performed. The modification is an implementation detail that doesn't affect how imported declarations (including enums) are processed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any references to imported enums in the codebase
# that might be affected by the new document cleanup approach

# Search for enum imports and their usage patterns
rg -A 5 "import.*enum" 

# Search for @default directives using enum values
rg -A 5 "@default.*enum"

Length of output: 195


Script:

#!/bin/bash
# Let's try a different approach to verify the document cleanup impact

# 1. Find all references to DocumentBuilder.update to understand the old pattern
rg -A 3 "DocumentBuilder\.update"

# 2. Check for any existing usage of deleteDocument and IndexManager.remove
rg -A 3 "deleteDocument|IndexManager\.remove"

# 3. Look for import-related code patterns
ast-grep --pattern 'import { $_ } from $_'

# 4. Search for enum validation or processing logic
rg -A 5 "validateEnum|processEnum|enum.*validation"

# 5. Check for any test cases related to enum imports
fd -e test.ts -e spec.ts -X rg -l "enum.*import"

Length of output: 1052

packages/schema/src/cli/cli-util.ts Show resolved Hide resolved
@ymc9 ymc9 merged commit ad07053 into dev Nov 15, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/issue-1849 branch November 15, 2024 00:53
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.

1 participant