Add nullglob functionality to nu-glob#9307
Add nullglob functionality to nu-glob#9307casuallyblue wants to merge 4 commits intonushell:mainfrom casuallyblue:main
Conversation
|
Hi, thanks for your contribution, I wonder is Would you please add a relative test case for relative commands you changed? |
I think that is likely the issue that is causing that, and I've found that I need to fix my pull request because its acting on the pattern as a whole and it should be working by path segment instead.
I'm not quite sure on how to do test cases since this patch works on the results from actually running the glob patterns against a filesystem. Do you have any pointers on how I could do this from a test environment? |
|
Cool! Thanks for working on this! I'm definitely hoping for a fix for To answer your question about tests, you can search for glob in our tests and find hits like this for nushell/crates/nu-command/tests/commands/ls.rs Lines 174 to 206 in 3005fe1 and globnushell/crates/nu-command/tests/commands/glob.rs Lines 6 to 22 in 3005fe1 Just note that the glob command does not use the nu-glob crate, it uses wax just to give some different functionality (although we'd like to integrate them back together someday)
|
|
@casually-blue are you going to be able to have time for this? |
|
I've been a bit busy the beginning of this week but I've got some time today to work on it. |
|
Initially I was thinking that it would be possible to just check if there were no matches for the pattern as a whole but I've realized that nullglob needs to actually be implemented so it gets checked for each path segment which requires a bit more understanding of how the glob library is actually working internally. I should have something soon but if anyone who knows a bit more about it has any pointers that would be greatly appreciated. |
|
Thanks for your help, we appreciate the dedication to making nushell better! |
|
After some more looking at how the code is working it looks like this would not actually fix the problem with ls, cp, etc., in a real way. Their main problem when dealing with folders with those names seems to be that they aren't escaping the pattern when they use their current working directory. While nullglob would probably fix them for simple cases, they would probably break again if any part of a path had a file that matched the glob. |
|
The tricky part is supporting both cases. Sometimes you want Our previous approach was to totally remove |
Yeah, I'm talking about the relative path that ls is putting in when it canonicalizes the path. The current version will treat characters in that as part of the glob when they are meant to be read literally. |
|
any ideas for a fix? |
|
Looking at the code I got it somewhat working by just escaping the filename and then converting back to a PathBuf for pushing the rest of the glob, but that both breaks the prefix calculation for removing the relative parts of the path and isn't really a good idea because then the PathBufs that ls uses in its code aren't valid paths anymore since escaping is its own thing. |
|
I may be close with #9416, if you're interested in testing, please let me know how it goes. |
|
closing in favor of #9416 (which is pretty hacky. would love to have a better fix still) |
# Description This PR is trying to allow you to have `[blah]` in your path and yet still have `ls` work. This is done by trying to separate the path from the pattern to be searched for. It may still need more work. I've tested it with: - mkdir "[test]" - cd "[test]" - ls Related to #9307 Hopefully fixes #9232 # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
Description
This pull request fixes an issue where files with square brackets in their names would be treated as a glob pattern rather than the filename, even if there were no matches to the glob pattern. This is similar to bash nullglob.
User-Facing Changes
Glob would now use the literal glob pattern as a result if there were no other results from matching