Skip to content

Conversation

@dsherret
Copy link
Contributor

@dsherret dsherret commented Jan 2, 2025

Creates a Sys trait and puts any system related functionality behind it.

This has several benefits:

  1. Enables in-memory testing of this crate in the future (for now, I've only implemented a default RealSys implementation).
  2. Allows consumers of this crate to not depend on env_home, rustix, and winsafe if they already have code or other dependencies for doing this.
    • Allows changing the behaviour of tilde expansion (for example, previously this crate relied on using winapi to get the home directory on Windows, but now it uses env_home, which only looks at the environment variables)
  3. Opens the possibility to using this crate in wasm32-unknown-unknown as someone can provide their own which::Sys implementation.

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Jan 2, 2025

I like this but can we adjust the presentation a bit? I'd consider this a feature for advanced users. So we should move the README example into the generated rustdocs. Additionally let's not flatten the sys module in our exports, just make the module public.

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Jan 2, 2025

Also if you haven't already you should use your new trait in a proof of concept implementation for your own use case. It'd be a shame if we needed to revise the trait after this was already published.

@dsherret
Copy link
Contributor Author

dsherret commented Jan 2, 2025

Also if you haven't already you should use your new trait in a proof of concept implementation for your own use case. It'd be a shame if we needed to revise the trait after this was already published.

Yes, that would be good. I'll look into doing that later. Going to convert this PR to draft until I find the time.

@dsherret dsherret marked this pull request as draft January 2, 2025 16:23
@dsherret
Copy link
Contributor Author

This PR should be good now. I tried it out in deno_task_shell, which has its own in-memory representation of the environment and in one of Deno's crates that needs to compile to wasm32-unknown-unknown.

Copy link
Collaborator

@Xaeroxe Xaeroxe left a comment

Choose a reason for hiding this comment

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

This looks pretty good, with one exception which is that we aren't running CI tests for the new --no-default-features configuration. Indeed, the command cargo test --no-default-features doesn't currently pass.

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented May 29, 2025

Testing without a sys implementation seems hard, but maybe we can do it in terms of an in-memory sys implementation.

@dsherret
Copy link
Contributor Author

Yeah I’ll do it tomorrow.

pub bins: Vec<PathBuf>,
}
#[cfg(feature = "real-sys")]
mod real_sys {
Copy link
Contributor Author

@dsherret dsherret Jun 4, 2025

Choose a reason for hiding this comment

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

Note: No changes, just nested in this module in order to conditionally compile this. Scroll down to mod in_memory for new stuff (though the diff is terrible... might be better to just view the file)

actual.display()
);
}
fn with_entry_mut(&mut self, path: impl AsRef<Path>) -> Option<&mut DirectoryEntry> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I lazily used AI for this and some other methods, but it seems to be working ok and this is just test code.

fn tilde_path() {
let mut sys = InMemorySys::new();
sys.set_home_dir("/home/user/");
sys.set_env_var("PATH", "/dir/:~/sub/");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm actually surprised this crate has this behaviour, but it's nice it can be disabled now by overriding the home_dir implementation to return None.

@dsherret dsherret requested a review from Xaeroxe June 4, 2025 01:13
Copy link
Collaborator

@Xaeroxe Xaeroxe left a comment

Choose a reason for hiding this comment

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

Overall looks good, with a couple nitpicks. In addition to the comment given, please don't fully qualify types like std::collection::HashSet, I'd prefer to add those to an import within the mod declarations.

@dsherret
Copy link
Contributor Author

dsherret commented Jun 4, 2025

In addition to the comment given, please don't fully qualify types like std::collection::HashSet, I'd prefer to add those to an import within the mod declarations.

Can you point out where these are? I scrolled through the code and didn't see anything that stood out.

(not sure this is a good use of time tbh)

@dsherret dsherret requested a review from Xaeroxe June 4, 2025 01:32
#[inline]
pub(crate) fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
#[allow(clippy::disallowed_methods)] // ok, sys implementation
std::fs::canonicalize(path)
Copy link
Contributor Author

@dsherret dsherret Jun 4, 2025

Choose a reason for hiding this comment

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

Ah, maybe these ones? This is done to prevent having to have #[cfg...] on use statements, which starts to get unwieldy IMO (also makes things annoying when removing/chaning sections of code because then clippy errors only happen depending on how the compilation is)

@dsherret dsherret requested a review from Xaeroxe June 4, 2025 01:44
@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Jun 4, 2025

This branch has merge conflicts that need to be resolved

@dsherret
Copy link
Contributor Author

dsherret commented Jun 4, 2025

It doesn't seem to? Did you want me to rebase and squash or something?

image

> git merge master
Already up to date.

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Jun 4, 2025

I see, okay, my button was set to "rebase and merge" but the other merge options work fine.

@Xaeroxe Xaeroxe merged commit a0a6daf into harryfei:master Jun 4, 2025
17 checks passed
@dsherret dsherret deleted the feat_add_sys_trait branch June 4, 2025 13:11
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.

2 participants