Skip to content

refactor: simplify path handling in build and run commands#27

Merged
Bechma merged 2 commits intocyberfabric:mainfrom
Bechma:change-path-behaviour
Apr 7, 2026
Merged

refactor: simplify path handling in build and run commands#27
Bechma merged 2 commits intocyberfabric:mainfrom
Bechma:change-path-behaviour

Conversation

@Bechma
Copy link
Copy Markdown
Collaborator

@Bechma Bechma commented Apr 7, 2026

Summary by CodeRabbit

  • New Features

    • -p/--path is now optional and can activate a working-directory context when supplied.
  • Documentation

    • CLI docs updated to show optional -p/--path and clarify how it affects config, build, run, and generated output resolution and examples.
  • Chores

    • Bumped workspace dependency versions for cargo-generate, serde-saphyr, toml, and toml_edit.

Signed-off-by: Bechma <19294519+Bechma@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

📝 Walkthrough

Walkthrough

CLI workspace path handling was moved to argument-parse time: an optional -p/--path now validates and chdirs during parsing. Many functions and structs dropped explicit workspace path parameters and instead obtain the workspace root via a new workspace_root() helper; callers and module-parser calls were adjusted accordingly.

Changes

Cohort / File(s) Summary
Dependency Updates
Cargo.toml
Bumped workspace dependency versions: cargo-generate 0.23.7→0.23.8, serde-saphyr 0.0.22→0.0.23, toml 1.0.7→1.1.2 (serde feature kept), toml_edit 0.25.5→0.25.10.
Documentation
SKILLS.md
Documented -p/--path as optional "path activation" that changes CWD during parsing and affects resolution of -c/--config and generated output paths; updated command synopses/examples.
CLI core: arg parsing & workspace root
crates/cli/src/common.rs
PathConfigArgs.pathOption<PathBuf> with parse_and_chdir (validates & chdirs). Added workspace_root() helper. Renamed resolve_workspace_and_config()resolve_config_and_name() (drops workspace path). Removed workspace path parameters from get_config, generate_server_structure, generated_project_dir, and related helpers.
Build command
crates/cli/src/build/mod.rs
Switched to resolve_config_and_name() and updated calls to common::get_config, generate_server_structure, and generated_project_dir to omit workspace path args.
Run command & loop
crates/cli/src/run/mod.rs, crates/cli/src/run/run_loop.rs
RunLoop no longer stores path; ctor signature changed to (config_path, project_name). RunLoop computes workspace_path via workspace_root() at runtime and updated watch, dep-collection, server-generation, and config-reload flows to use computed workspace root.
Module management
crates/cli/src/config/modules/...
Removed workspace_path from ModulesContext. discover_local_modules / ListArgs::run and related helpers call get_module_name_from_crate() without a path. Test fixtures updated to use Some(PathBuf::from(".")) for optional path.
Module parser
crates/module-parser/src/metadata.rs
get_module_name_from_crate signature changed to no-arg; cargo_metadata::MetadataCommand no longer sets .current_dir(path), relying on process CWD (set by parse-time chdir).

Sequence Diagram(s)

mermaid
sequenceDiagram
participant User
participant CLI as "CLI (argparser)"
participant FS as "Filesystem (CWD)"
participant Common as "common::workspace_root()"
participant Run as "RunLoop / Build"
participant CargoMeta as "cargo metadata / module-parser"

User->>CLI: invoke command with optional -p/--path and -c/--config
CLI->>FS: parse_and_chdir(path?) — validate & chdir
CLI->>Common: resolve_config_and_name() -> returns (config_path, project_name)
Common->>FS: workspace_root() reads current_dir()
Run->>Common: get_config(config_path)
Run->>CargoMeta: get_module_name_from_crate() (runs in CWD)
Run->>Run: generate_server_structure(project_name, config_path, deps)
Run->>FS: create generated project dir under workspace_root()

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

🐰 Hop, I parsed the path and leapt,

I changed my burrow where it slept,
Now commands wander, safe and sound,
Rooted where the chdir found—
A tiny hop, the workspace set.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 19.23% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: simplifying path handling by removing workspace path arguments from build/run command signatures and relying on the process working directory instead.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Copy Markdown
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: 2

🧹 Nitpick comments (2)
Cargo.toml (1)

20-30: Use cargo add for these dependency bumps.

Please regenerate the cargo-generate, serde-saphyr, toml, and toml_edit updates via cargo add instead of hand-editing the manifest. That keeps the dependency and lockfile updates in the same reproducible workflow. As per coding guidelines, Cargo.toml: Always prefer cargo add over manually editing Cargo.toml for managing Rust dependencies.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Cargo.toml` around lines 20 - 30, Revert the manual version edits in
Cargo.toml and instead update the specified crates using cargo add so the
manifest and Cargo.lock stay consistent: run cargo add for cargo-generate
(targeting 0.23.8), serde-saphyr (0.0.23), toml (1.1.2 with serde feature), and
toml_edit (0.25.10), then verify the entries for cargo-generate, serde-saphyr,
toml and toml_edit in Cargo.toml are the versions added and commit the resulting
Cargo.toml and Cargo.lock changes together.
crates/cli/src/run/run_loop.rs (1)

33-37: Use the resolved workspace root consistently in this method.

Line 33 already captures workspace_path, but Line 34 and Line 37 immediately call helpers that resolve current_dir() again. Passing workspace_path through here would keep watch-mode state explicit and avoid future drift if anything mutates the process CWD.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/cli/src/run/run_loop.rs` around lines 33 - 37, The method captures
workspace_path but then calls helpers that re-resolve the process CWD; update
the three calls to use the resolved workspace_path consistently: pass
&workspace_path into common::get_config (so it resolves config relative to
workspace_path), into common::generate_server_structure(&self.project_name,
&self.config_path, &dependencies, &workspace_path) (or adjust its signature to
accept workspace_path) and into
common::generated_project_dir(&self.project_name, &workspace_path) (and change
that function signature if needed); ensure dependencies created via
create_dependencies() also resolve any paths using the same workspace_path
rather than calling current_dir().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/cli/src/common.rs`:
- Around line 19-20: PathConfigArgs::path currently becomes inconsistent because
parse_and_chdir() returns a PathBuf but later code canonicalizes only
self.config (so relative config paths are resolved against the ambient CWD after
parse_and_chdir() has already changed it), and a relative -p value becomes stale
after the chdir; fix by making parse_and_chdir() return both a
canonical/absolute path (or canonicalize its return) and ensure the code that
canonicalizes config uses the path from PathConfigArgs::path as the base (i.e.,
canonicalize config relative to self.path before any chdir), or alternatively
move the chdir until after both self.path and self.config are canonicalized;
update references to parse_and_chdir, PathConfigArgs::path, and the block that
canonicalizes self.config so the struct remains self-consistent and relative
paths are resolved against the intended directory.

In `@SKILLS.md`:
- Around line 65-69: Update SKILLS.md to make the -p/--path flag documentation
consistent: change the description for the `config mod list` example that
currently says it “defaults to .” so it no longer asserts a default, and update
the quick-reference block where `-p <workspace>` is shown as required (lines
rendering `-p <workspace>`) to show it as optional (e.g., `[-p <workspace>]` or
`-p, --path <PATH>` with the same “optional” wording). Ensure all occurrences of
the `-p, --path <PATH>` flag text match the shared section's wording that it is
optional and has no default so the `config mod list` and quick-reference entries
are consistent with the top-level `-p` description.

---

Nitpick comments:
In `@Cargo.toml`:
- Around line 20-30: Revert the manual version edits in Cargo.toml and instead
update the specified crates using cargo add so the manifest and Cargo.lock stay
consistent: run cargo add for cargo-generate (targeting 0.23.8), serde-saphyr
(0.0.23), toml (1.1.2 with serde feature), and toml_edit (0.25.10), then verify
the entries for cargo-generate, serde-saphyr, toml and toml_edit in Cargo.toml
are the versions added and commit the resulting Cargo.toml and Cargo.lock
changes together.

In `@crates/cli/src/run/run_loop.rs`:
- Around line 33-37: The method captures workspace_path but then calls helpers
that re-resolve the process CWD; update the three calls to use the resolved
workspace_path consistently: pass &workspace_path into common::get_config (so it
resolves config relative to workspace_path), into
common::generate_server_structure(&self.project_name, &self.config_path,
&dependencies, &workspace_path) (or adjust its signature to accept
workspace_path) and into common::generated_project_dir(&self.project_name,
&workspace_path) (and change that function signature if needed); ensure
dependencies created via create_dependencies() also resolve any paths using the
same workspace_path rather than calling current_dir().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 83254423-c3a1-44bc-be2a-01098766bb10

📥 Commits

Reviewing files that changed from the base of the PR and between 8898f18 and b5439b4.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (9)
  • Cargo.toml
  • SKILLS.md
  • crates/cli/src/build/mod.rs
  • crates/cli/src/common.rs
  • crates/cli/src/config/modules/add.rs
  • crates/cli/src/config/modules/list.rs
  • crates/cli/src/config/modules/mod.rs
  • crates/cli/src/run/mod.rs
  • crates/cli/src/run/run_loop.rs
💤 Files with no reviewable changes (1)
  • crates/cli/src/config/modules/mod.rs

Comment on lines +19 to +20
#[arg(short = 'p', long, value_parser = parse_and_chdir)]
pub path: Option<PathBuf>,
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.

⚠️ Potential issue | 🟡 Minor

PathConfigArgs::path no longer affects config resolution.

Line 40 now canonicalizes only self.config, so any in-process caller that constructs PathConfigArgs { path, config } directly resolves a relative config against the ambient CWD instead of path. Also, if -p was relative, the PathBuf returned from parse_and_chdir() is stale as soon as Line 33 changes the CWD.

💡 One way to keep the struct self-consistent
 fn parse_and_chdir(s: &str) -> Result<PathBuf, String> {
-    let path = PathBuf::from(s);
+    let path = PathBuf::from(s)
+        .canonicalize()
+        .map_err(|e| format!("failed to access {s}: {e}"))?;

     if !path.is_dir() {
         return Err(format!("not a directory: {}", path.display()));
     }

     env::set_current_dir(&path)
         .map_err(|e| format!("failed to change directory to {}: {e}", path.display()))?;

     Ok(path)
 }

 impl PathConfigArgs {
     pub fn resolve_config(&self) -> anyhow::Result<PathBuf> {
-        self.config
+        let config_path = if self.config.is_absolute() {
+            self.config.clone()
+        } else if let Some(path) = &self.path {
+            path.join(&self.config)
+        } else {
+            workspace_root()?.join(&self.config)
+        };
+
+        config_path
             .canonicalize()
             .context("can't canonicalize config")
     }
 }

Also applies to: 26-37, 40-44

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/cli/src/common.rs` around lines 19 - 20, PathConfigArgs::path
currently becomes inconsistent because parse_and_chdir() returns a PathBuf but
later code canonicalizes only self.config (so relative config paths are resolved
against the ambient CWD after parse_and_chdir() has already changed it), and a
relative -p value becomes stale after the chdir; fix by making parse_and_chdir()
return both a canonical/absolute path (or canonicalize its return) and ensure
the code that canonicalizes config uses the path from PathConfigArgs::path as
the base (i.e., canonicalize config relative to self.path before any chdir), or
alternatively move the chdir until after both self.path and self.config are
canonicalized; update references to parse_and_chdir, PathConfigArgs::path, and
the block that canonicalizes self.config so the struct remains self-consistent
and relative paths are resolved against the intended directory.

…ndency

Signed-off-by: Bechma <19294519+Bechma@users.noreply.github.com>
Copy link
Copy Markdown
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/module-parser/src/metadata.rs`:
- Around line 25-29: The function get_module_name_from_crate() currently relies
on the process CWD for cargo metadata; change it to accept a workspace path
parameter (e.g., get_module_name_from_crate(workspace: &Path) ->
anyhow::Result<HashMap<String, ConfigModule>>) and call
cargo_metadata::MetadataCommand::new().manifest_path(workspace.join("Cargo.toml"))
(or set current_dir with .current_dir(workspace)) before .exec(); then update
all callers to pass the resolved workspace path from the config loader so
metadata is always queried against the intended workspace rather than the
process CWD.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9f49a435-592f-4c5f-a997-3ae328ed630f

📥 Commits

Reviewing files that changed from the base of the PR and between b5439b4 and 5ffc540.

📒 Files selected for processing (5)
  • SKILLS.md
  • crates/cli/src/common.rs
  • crates/cli/src/config/modules/add.rs
  • crates/cli/src/config/modules/list.rs
  • crates/module-parser/src/metadata.rs
✅ Files skipped from review due to trivial changes (3)
  • crates/cli/src/config/modules/list.rs
  • crates/cli/src/config/modules/add.rs
  • SKILLS.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/cli/src/common.rs

@Bechma Bechma merged commit be04e67 into cyberfabric:main Apr 7, 2026
2 checks passed
@Bechma Bechma deleted the change-path-behaviour branch April 7, 2026 19:28
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