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

feat(sozo): add full workspace and accept non-dojo targets #2207

Merged
merged 13 commits into from
Jul 25, 2024

Conversation

glihm
Copy link
Collaborator

@glihm glihm commented Jul 24, 2024

Description

With this PR, sozo will now accept again to build any Cairo code, even without the [[target.dojo]]. The dojo metadata (which contains the namespace configuration) is then a default configuration, as the namespace is not expected to be used in such situations.

Summary by CodeRabbit

  • New Features

    • Added package filtering options for build and test commands, allowing users to specify which packages to build or test.
    • Enhanced feedback during the compilation process with improved logging and user messages.
  • Bug Fixes

    • Improved handling of namespace configurations based on target environment, ensuring relevant settings are applied.
  • Refactor

    • Streamlined metadata extraction processes for packages, enhancing clarity and maintainability.
  • Documentation

    • Updated documentation to reflect new fields and functionalities for build and test commands.
  • Chores

    • Updated dependency revisions in configuration files to incorporate recent improvements.

Copy link

coderabbitai bot commented Jul 24, 2024

Walkthrough

Ohayo, sensei! The recent updates significantly enhance package management in the build and test processes, introducing optional package filtering for improved control. The BuildArgs and TestArgs structs now allow users to specify which packages to manage. Additionally, updates in logging and metadata extraction streamline operations, enhancing usability and clarity throughout the compilation and configuration workflows.

Changes

Files Change Summary
bin/sozo/src/commands/build.rs, bin/sozo/src/commands/test.rs Introduced packages: Option<PackagesFilter> to BuildArgs and TestArgs, enabling package filtering.
crates/dojo-core/Scarb.toml, crates/dojo-lang/Cargo.toml Updated dojo_plugin dependency revision and added scarb-ui to the workspace.
crates/dojo-lang/src/plugin.rs, crates/dojo-lang/src/utils.rs Enhanced generate_code and get_namespace_config with conditional logic and improved debug logging.
crates/dojo-world/src/metadata.rs Replaced build_artifact_from_filename with dojo_metadata_from_package for focused metadata extraction.
crates/dojo-lang/src/scarb_internal/mod.rs Improved compile_workspace with regex for cleaner unit names and enhanced logging during compilation.
examples/game-lib/*/Scarb.toml, examples/game-lib/*/src/lib.cairo Simplified configurations by removing unnecessary namespace declarations and dojo target sections.
examples/spawn-and-move/Scarb.toml Added new dependency for origami_token from a specified local path.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BuildArgs
    participant DojoCompiler
    participant TestArgs
    participant Metadata

    User->>BuildArgs: Specify packages to build
    BuildArgs->>DojoCompiler: Run build with specified packages
    DojoCompiler-->>User: Log compilation process

    User->>TestArgs: Specify packages to test
    TestArgs->>Metadata: Retrieve package metadata
    Metadata-->>User: Provide test results
Loading

Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8280817 and c1d0aa5.

Files selected for processing (2)
  • crates/dojo-lang/src/compiler.rs (3 hunks)
  • crates/dojo-lang/src/utils.rs (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/dojo-lang/src/utils.rs
Additional comments not posted (2)
crates/dojo-lang/src/compiler.rs (2)

108-108: Ohayo, sensei! Great addition of trace logging.

The trace logging statement will help in monitoring the compilation process.


Line range hint 407-412:
Ohayo, sensei! The changes to the function call look good.

The changes ensure that the correct directories are passed for writing manifests and ABIs.


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@glihm glihm requested a review from lambda-0x July 24, 2024 06:02
@glihm
Copy link
Collaborator Author

glihm commented Jul 24, 2024

Missing the package filter on test to be completed.

Copy link

@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: 0

Outside diff range, codebase verification and nitpick comments (3)
crates/dojo-lang/src/utils.rs (2)

18-21: Consider removing commented-out debug lines or using a logging mechanism.

The debug comments are currently commented out. It would be better to either remove them or use a logging mechanism to control verbosity.

-    // Super verbose print, but useful to get the CfgSet.
-    // debug!(cfg_set = ?db.cfg_set(), crates = ?db.crates(), "Retrieving namespace configuration.");
+    tracing::debug!(cfg_set = ?db.cfg_set(), crates = ?db.crates(), "Retrieving namespace configuration.");

42-55: Consider removing commented-out debug lines or using a logging mechanism.

The debug comments are currently commented out. It would be better to either remove them or use a logging mechanism to control verbosity.

-        // Very verbose.
-        // tracing::debug!(config_path = %config_path.to_string_lossy(), "Reading Scarb.toml file for namespace config.");
+        tracing::debug!(config_path = %config_path.to_string_lossy(), "Reading Scarb.toml file for namespace config.");
bin/sozo/src/commands/test.rs (1)

65-67: Incomplete implementation of the packages field in TestArgs struct.

The packages field is correctly declared and documented, but its usage is not fully implemented. There is a TODO comment indicating the need for further work. Please complete the implementation, possibly using the implementation in build.rs as a reference.

  • bin/sozo/src/commands/test.rs: Lines with TODO comment for packages field.
Analysis chain

New field packages in TestArgs struct is well-implemented.

The new field allows users to specify which packages to test. The documentation is clear and the implementation is consistent with the existing fields.

Ensure that the new field is utilized correctly in the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new `packages` field in the `TestArgs` struct.

# Test: Search for the usage of the `packages` field. Expect: Correct usage in the codebase.
rg --type rust -A 5 $'packages'

Length of output: 11926

Copy link

@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: 2

Outside diff range, codebase verification and nitpick comments (5)
crates/dojo-world/src/metadata.rs (5)

86-87: Ohayo, sensei! Add a brief doc comment for the new function.

Adding a doc comment will improve readability and provide context for future developers.

/// Extracts Dojo metadata from a specified package.

95-96: Ohayo, sensei! Improve the error message for clarity.

The error message can be more descriptive to help users understand the issue better.

- return Err(anyhow::anyhow!("[lib] package cannot have [[target.dojo]]."));
+ return Err(anyhow::anyhow!("Package with [lib] target cannot also have [[target.dojo]] target."));

121-122: Ohayo, sensei! Add a comment to explain the purpose of metadata_artifacts_load.

Adding a comment here will help future developers understand why this function is called.

// Load additional artifacts metadata for the dojo.

128-129: Ohayo, sensei! Add a brief doc comment for the function.

Adding a doc comment will improve readability and provide context for future developers.

/// Extracts Dojo metadata from the workspace.

137-154: Ohayo, sensei! Improve error handling for multiple dojo packages.

The error message can be more descriptive to help users understand the issue better.

- Err(anyhow::anyhow!(
-     "Multiple packages with dojo target found in workspace. Please specify a package \
-      using --package option or maybe one of them must be declared as a [lib]."
- )),
+ Err(anyhow::anyhow!(
+     "Multiple packages with dojo target found in workspace. Please specify a package using the --package option or ensure only one package has the dojo target."
+ )),

Comment on lines +125 to +126
Ok(dojo_metadata)
}
Copy link

Choose a reason for hiding this comment

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

Ohayo, sensei! Consider logging the collected metadata at a higher log level.

Using info level instead of trace might be more appropriate for logging collected metadata.

- tracing::trace!(target: LOG_TARGET, ?dojo_metadata);
+ tracing::info!(target: LOG_TARGET, ?dojo_metadata, "Collected Dojo metadata from package.");

Committable suggestion was skipped due to low confidence.

Comment on lines +99 to +111
let project_metadata = match package.manifest.metadata.dojo() {
Ok(m) => Ok(m),
Err(e) => {
if is_lib || !is_dojo {
Ok(ProjectMetadata::default())
} else {
Err(anyhow::anyhow!(
"In manifest {} [dojo] package must have [[target.dojo]]: {}.",
ws.manifest_path(),
e
))
}
}
Copy link

Choose a reason for hiding this comment

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

Ohayo, sensei! Simplify the error handling logic.

The error handling logic can be simplified by using map_err to transform the error.

- let project_metadata = match package.manifest.metadata.dojo() {
-     Ok(m) => Ok(m),
-     Err(e) => {
-         if is_lib || !is_dojo {
-             Ok(ProjectMetadata::default())
-         } else {
-             Err(anyhow::anyhow!(
-                 "In manifest {} [dojo] package must have [[target.dojo]]: {}.",
-                 ws.manifest_path(),
-                 e
-             ))
-         }
-     }
- }?;
+ let project_metadata = package.manifest.metadata.dojo().map_err(|e| {
+     if is_lib || !is_dojo {
+         anyhow::anyhow!("In manifest {} [dojo] package must have [[target.dojo]]: {}.", ws.manifest_path(), e)
+     } else {
+         ProjectMetadata::default()
+     }
+ })?;
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
let project_metadata = match package.manifest.metadata.dojo() {
Ok(m) => Ok(m),
Err(e) => {
if is_lib || !is_dojo {
Ok(ProjectMetadata::default())
} else {
Err(anyhow::anyhow!(
"In manifest {} [dojo] package must have [[target.dojo]]: {}.",
ws.manifest_path(),
e
))
}
}
let project_metadata = package.manifest.metadata.dojo().map_err(|e| {
if is_lib || !is_dojo {
ProjectMetadata::default()
} else {
anyhow::anyhow!("In manifest {} [dojo] package must have [[target.dojo]]: {}.", ws.manifest_path(), e)
}
})?;

Copy link

codecov bot commented Jul 25, 2024

Codecov Report

Attention: Patch coverage is 77.88462% with 69 lines in your changes missing coverage. Please review.

Project coverage is 68.95%. Comparing base (a35ddcb) to head (c1d0aa5).
Report is 1 commits behind head on main.

Files Patch % Lines
crates/dojo-lang/src/scarb_internal/mod.rs 81.73% 21 Missing ⚠️
crates/dojo-world/src/metadata.rs 84.29% 19 Missing ⚠️
crates/dojo-lang/src/plugin.rs 14.28% 12 Missing ⚠️
bin/sozo/src/commands/test.rs 0.00% 8 Missing ⚠️
crates/dojo-lang/src/utils.rs 60.00% 8 Missing ⚠️
bin/sozo/src/commands/build.rs 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2207      +/-   ##
==========================================
- Coverage   69.06%   68.95%   -0.12%     
==========================================
  Files         339      339              
  Lines       44052    44288     +236     
==========================================
+ Hits        30426    30539     +113     
- Misses      13626    13749     +123     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@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

Comment on lines +101 to +105
// TODO: if we want to output the manifests at the package level,
// we must iterate on the ws members, to find the location of the
// sole package with the `dojo` target.
// In this case, we can use this path to output the manifests.

Copy link

Choose a reason for hiding this comment

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

Ohayo, sensei! Acknowledge the TODO comment.

The TODO comment indicates a potential future enhancement to output manifests at the package level.

Would you like me to create a GitHub issue to track this task, or provide assistance in implementing this feature?

Comment on lines +357 to +367
// Ensure `contracts` dir exist event if no contracts are compiled
// to avoid errors when loading manifests.
let base_contracts_dir = base_manifests_dir.join(CONTRACTS_DIR);
let base_contracts_abis_dir = base_abis_dir.join(CONTRACTS_DIR);
if !base_contracts_dir.exists() {
std::fs::create_dir_all(&base_contracts_dir)?;
}
if !base_contracts_abis_dir.exists() {
std::fs::create_dir_all(&base_contracts_abis_dir)?;
}

Copy link

Choose a reason for hiding this comment

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

Ohayo, sensei! Nice addition of directory creation logic.

Ensuring the contracts directory exists even if no contracts are compiled prevents potential errors when loading manifests. Consider using fs::create_dir_all once for both directories.

-  if !base_contracts_dir.exists() {
-      std::fs::create_dir_all(&base_contracts_dir)?;
-  }
-  if !base_contracts_abis_dir.exists() {
-      std::fs::create_dir_all(&base_contracts_abis_dir)?;
-  }
+  [base_contracts_dir, base_contracts_abis_dir].iter().try_for_each(|dir| {
+      if !dir.exists() {
+          std::fs::create_dir_all(dir)
+      } else {
+          Ok(())
+      }
+  })?;
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
// Ensure `contracts` dir exist event if no contracts are compiled
// to avoid errors when loading manifests.
let base_contracts_dir = base_manifests_dir.join(CONTRACTS_DIR);
let base_contracts_abis_dir = base_abis_dir.join(CONTRACTS_DIR);
if !base_contracts_dir.exists() {
std::fs::create_dir_all(&base_contracts_dir)?;
}
if !base_contracts_abis_dir.exists() {
std::fs::create_dir_all(&base_contracts_abis_dir)?;
}
// Ensure `contracts` dir exist event if no contracts are compiled
// to avoid errors when loading manifests.
let base_contracts_dir = base_manifests_dir.join(CONTRACTS_DIR);
let base_contracts_abis_dir = base_abis_dir.join(CONTRACTS_DIR);
[base_contracts_dir, base_contracts_abis_dir].iter().try_for_each(|dir| {
if !dir.exists() {
std::fs::create_dir_all(dir)
} else {
Ok(())
}
})?;

Comment on lines +394 to +404
// Ensure `models` dir exist event if no models are compiled
// to avoid errors when loading manifests.
let base_models_dir = base_manifests_dir.join(MODELS_DIR);
let base_models_abis_dir = base_abis_dir.join(MODELS_DIR);
if !base_models_dir.exists() {
std::fs::create_dir_all(&base_models_dir)?;
}
if !base_models_abis_dir.exists() {
std::fs::create_dir_all(&base_models_abis_dir)?;
}

Copy link

Choose a reason for hiding this comment

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

Ohayo, sensei! Nice addition of directory creation logic.

Ensuring the models directory exists even if no models are compiled prevents potential errors when loading manifests. Consider using fs::create_dir_all once for both directories.

-  if !base_models_dir.exists() {
-      std::fs::create_dir_all(&base_models_dir)?;
-  }
-  if !base_models_abis_dir.exists() {
-      std::fs::create_dir_all(&base_models_abis_dir)?;
-  }
+  [base_models_dir, base_models_abis_dir].iter().try_for_each(|dir| {
+      if !dir.exists() {
+          std::fs::create_dir_all(dir)
+      } else {
+          Ok(())
+      }
+  })?;
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
// Ensure `models` dir exist event if no models are compiled
// to avoid errors when loading manifests.
let base_models_dir = base_manifests_dir.join(MODELS_DIR);
let base_models_abis_dir = base_abis_dir.join(MODELS_DIR);
if !base_models_dir.exists() {
std::fs::create_dir_all(&base_models_dir)?;
}
if !base_models_abis_dir.exists() {
std::fs::create_dir_all(&base_models_abis_dir)?;
}
// Ensure `models` dir exist event if no models are compiled
// to avoid errors when loading manifests.
let base_models_dir = base_manifests_dir.join(MODELS_DIR);
let base_models_abis_dir = base_abis_dir.join(MODELS_DIR);
[base_models_dir, base_models_abis_dir].iter().try_for_each(|dir| {
if !dir.exists() {
std::fs::create_dir_all(dir)
} else {
Ok(())
}
})?;

@glihm glihm merged commit 5ee2f05 into dojoengine:main Jul 25, 2024
14 of 15 checks passed
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