Skip to content

feat: fixed error on first launch of mostrix on windows, now user is guided#39

Open
arkanoider wants to merge 2 commits intomainfrom
fix-windows-launch
Open

feat: fixed error on first launch of mostrix on windows, now user is guided#39
arkanoider wants to merge 2 commits intomainfrom
fix-windows-launch

Conversation

@arkanoider
Copy link
Collaborator

@arkanoider arkanoider commented Mar 17, 2026

Summary

Improve first-run settings handling so packaged binaries (especially on Windows) bootstrap configuration reliably and fail with a clear, user-friendly message when still using placeholders.

Details

  • Embed the default settings.toml into the binary and write it to ~/.mostrix/settings.toml on first launch if the file is missing (no dependency on source-tree paths like CARGO_MANIFEST_DIR).
  • After loading settings, detect placeholder values (mostro_pubkey_hex_format, nsec1_privkey_format) and exit gracefully with an error that:
    • Shows the full path to settings.toml.
    • Instructs the user to replace placeholders with real keys before running Mostrix again.

Summary by CodeRabbit

  • New Features

    • Automatic default configuration generation on first application launch
    • Validation checks with user-friendly guidance when required settings are missing
    • Clear message on first run confirming default settings were written
  • Chores

    • Removed an obsolete ignore pattern from repository configuration

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 17, 2026

Walkthrough

Removed a .gitignore entry and refactored settings initialization: embed default TOML at compile time, write defaults to the user config on first run (with restrictive perms on Unix), add placeholder checks for key fields, and expose pub fn init_settings() -> Result<&'static Settings, anyhow::Error>.

Changes

Cohort / File(s) Summary
Gitignore
/.gitignore
Removed the .cursor/commands ignore entry.
Settings initialization
src/settings.rs
Embed default settings.toml via compile-time include; on first run write embedded defaults to ~/.{package} (Unix: 0o600); use cfg.try_deserialize() then validate placeholders for mostro_pubkey/nsec_privkey; add pub fn init_settings() and diagnostic messaging for first-run vs existing placeholder config.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant SettingsModule as Settings
    participant FS as Filesystem
    participant User

    App->>Settings: call init_settings()
    Settings->>FS: check config path exists
    alt config missing
        Settings->>FS: write embedded DEFAULT_SETTINGS_TOML (create file)
        FS-->>Settings: write OK
        Settings->>App: log "default settings written"
    else config exists
        Settings->>FS: read config
        FS-->>Settings: send contents
    end
    Settings->>Settings: deserialize via cfg.try_deserialize()
    Settings->>Settings: validate mostro_pubkey / nsec_privkey placeholders
    alt placeholders detected
        Settings->>App: return error with guidance (first-run vs existing)
    else valid
        Settings->>App: return &'static Settings
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Create settings.toml #19: Modifies the same settings module and initialization flow; likely predecessor or alternative approach to settings initialization.
  • Improve settings #20: Also touches src/settings.rs and settings fields (currencies, init flow); closely related edits to initialization and validation.

Poem

🐇 I bundled defaults snug and tight,

I wrote them out by moonlit night.
First-run checks, a careful peep—
No wandering keys, no secrets keep.
Hop on, configs now sleep sweetly.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title mentions fixing errors on first launch of mostrix on Windows and user guidance, which aligns with the main objectives of improving first-run settings handling and providing user-friendly error messages when placeholders are detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-windows-launch
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

CodeRabbit can generate a title for your PR based on the changes.

Add @coderabbitai placeholder anywhere in the title of your PR and CodeRabbit will replace it with a title based on the changes in the PR. You can change the placeholder by changing the reviews.auto_title_placeholder setting.

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

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

Inline comments:
In `@src/settings.rs`:
- Around line 145-148: The current user-facing message always says a default
settings.toml "has been created" using path_display even when the file already
existed; adjust the control flow that emits that message to check the actual
file creation outcome (e.g., the Result or bool returned from the create/write
call or using path.exists()) and branch: if the write actually created the file
emit the existing "Default settings.toml has been created at {}..." message,
otherwise emit a different message (e.g., "Default settings.toml already exists
at {}. Please edit it to replace placeholder values...") while keeping the
existing fail-fast check logic intact; update the code that constructs/emits the
message that currently uses path_display to use this conditional wording.
- Around line 119-123: When creating the default settings.toml (the block using
hidden_file and DEFAULT_SETTINGS_TOML in src/settings.rs), replace fs::write()
on Unix with opening the file via std::fs::OpenOptions and setting the file mode
to 0o600 (use std::os::unix::fs::OpenOptionsExt::set_mode) before writing the
contents, preserving the existing error mapping (map_err) and the println; keep
the existing fs::write() fallback for non-Unix targets (cfg(unix) /
cfg(not(unix))) so Windows behavior is unchanged. Ensure you reference
hidden_file and DEFAULT_SETTINGS_TOML and write the full contents to the
OpenOptions-created file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 398212af-75c1-40f6-9447-a6b087c58ba2

📥 Commits

Reviewing files that changed from the base of the PR and between aff84ee and 83f10c4.

📒 Files selected for processing (2)
  • .gitignore
  • src/settings.rs

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.

🧹 Nitpick comments (1)
src/settings.rs (1)

198-202: Consider using restrictive permissions in save_settings on Unix for consistency.

While fs::write() preserves existing file permissions when truncating, applying explicit 0o600 mode here would provide defense-in-depth if the file were ever deleted and recreated by this function.

Optional hardening
     let toml_string = toml::to_string_pretty(settings)
         .map_err(|e| anyhow::anyhow!("Failed to serialize settings: {}", e))?;
 
-    fs::write(&hidden_file, toml_string)
-        .map_err(|e| anyhow::anyhow!("Failed to write settings file: {}", e))?;
+    #[cfg(unix)]
+    {
+        use std::io::Write;
+        use std::os::unix::fs::OpenOptionsExt;
+        let mut file = fs::OpenOptions::new()
+            .write(true)
+            .create(true)
+            .truncate(true)
+            .mode(0o600)
+            .open(&hidden_file)
+            .map_err(|e| anyhow::anyhow!("Failed to write settings file: {}", e))?;
+        file.write_all(toml_string.as_bytes())
+            .map_err(|e| anyhow::anyhow!("Failed to write settings file: {}", e))?;
+    }
+    #[cfg(not(unix))]
+    {
+        fs::write(&hidden_file, toml_string)
+            .map_err(|e| anyhow::anyhow!("Failed to write settings file: {}", e))?;
+    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/settings.rs` around lines 198 - 202, In save_settings replace the
fs::write(&hidden_file, toml_string) call with opening the file via
std::fs::OpenOptions (use
OpenOptions::new().create(true).write(true).truncate(true)) and write the
toml_string via write_all, and on Unix set explicit permissions by calling
OpenOptionsExt::mode(0o600) (import std::os::unix::fs::OpenOptionsExt) so the
created file gets restrictive 0o600 permissions; keep the same error mapping for
write failures and use the same hidden_file and toml_string variables.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/settings.rs`:
- Around line 198-202: In save_settings replace the fs::write(&hidden_file,
toml_string) call with opening the file via std::fs::OpenOptions (use
OpenOptions::new().create(true).write(true).truncate(true)) and write the
toml_string via write_all, and on Unix set explicit permissions by calling
OpenOptionsExt::mode(0o600) (import std::os::unix::fs::OpenOptionsExt) so the
created file gets restrictive 0o600 permissions; keep the same error mapping for
write failures and use the same hidden_file and toml_string variables.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7556458d-6176-453e-a6b2-1d5cffa3f688

📥 Commits

Reviewing files that changed from the base of the PR and between 83f10c4 and ce671c9.

📒 Files selected for processing (1)
  • src/settings.rs

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