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

Bug in save under certain circumstances #12599

Open
adamnemecek opened this issue Apr 20, 2024 · 0 comments
Open

Bug in save under certain circumstances #12599

adamnemecek opened this issue Apr 20, 2024 · 0 comments
Labels
🐛 bug Something isn't working file-system Related to commands and core nushell behavior around the file system pipeline-metadata Additional information passed along that is not directly attached to the values (e.g. LSCOLORS)

Comments

@adamnemecek
Copy link

adamnemecek commented Apr 20, 2024

Describe the bug

This filter produces an incorrect output.

def rtrim [path:string] {
    open $path | lines | str trim --right | save -r -f $path
}

This filter is supposed to open a file, trim the whitespace and save the file again to the same path. This however instead deletes contents of the file. It seems that it is this particular combination of things. This filter for example works as expected.

# split on two lines
def rtrim [path:string] {
    let lines = open $path | lines 
    $lines | str trim --right | save --raw -f $path
}

This works as well (surprisingly)

# no raw
def rtrim [path:string] {
    open $path | lines | str trim --right | save -f $path
}

and so does this (unsurprisingly)

# dont overwrite
def rtrim [path:string] {
    open $path | lines | str trim --right | save -r "file.md"
}

So it seems to happen only if :

a.) it's a one liner
b.) you are overwriting the file
c.) you are are saving it as raw``

How to reproduce

1.) make file with some line endings
2.) define

def rtrim [path:string] {
    open $path | lines | str trim --right | save -r -f $path
}

3.) run rtrim "file.md"

Expected behavior

The contents of the file should not be deleted.

Screenshots

No response

Configuration

key value
version 0.92.3
branch main
commit_hash c06ef20
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.79.0-nightly (aa067fb98 2024-04-10)
rust_channel nightly-aarch64-apple-darwin
cargo_version cargo 1.79.0-nightly (74fd5bc73 2024-04-10)
build_time 2024-04-15 12:06:25 -07:00
build_rust_channel release
allocator mimalloc
features default, sqlite, trash, which
installed_plugins plist, gstat, query

Additional context

No response

@adamnemecek adamnemecek added the needs-triage An issue that hasn't had any proper look label Apr 20, 2024
IanManske added a commit that referenced this issue Apr 22, 2024
# Description
When saving to a file we currently try to check if the data source in
the pipeline metadata is the same as the file we are saving to. If so,
we create an error, since reading and writing to a file at the same time
is currently not supported/handled gracefully. However, there are still
a few instances where this error is not properly triggered, and so this
PR attempts to reduce these cases. Inspired by #12599.

# Tests + Formatting
Added a few tests.

# After Submitting
Some commands still do not properly preserve metadata (e.g., `str trim`)
and so prevent us from detecting this error.
@IanManske IanManske added 🐛 bug Something isn't working file-system Related to commands and core nushell behavior around the file system pipeline-metadata Additional information passed along that is not directly attached to the values (e.g. LSCOLORS) and removed needs-triage An issue that hasn't had any proper look labels Apr 22, 2024
IanManske added a commit that referenced this issue May 17, 2024
# Description
This PR makes some commands and areas of code preserve pipeline
metadata. This is in an attempt to make the issue described in #12599
and #9456 less likely to occur. That is, reading and writing to the same
file in a pipeline will result in an empty file. Since we preserve
metadata in more places now, there will be a higher chance that we
successfully detect this error case and abort the pipeline.
FilipAndersson245 pushed a commit to FilipAndersson245/nushell that referenced this issue May 18, 2024
# Description
This PR makes some commands and areas of code preserve pipeline
metadata. This is in an attempt to make the issue described in nushell#12599
and nushell#9456 less likely to occur. That is, reading and writing to the same
file in a pipeline will result in an empty file. Since we preserve
metadata in more places now, there will be a higher chance that we
successfully detect this error case and abort the pipeline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working file-system Related to commands and core nushell behavior around the file system pipeline-metadata Additional information passed along that is not directly attached to the values (e.g. LSCOLORS)
Projects
None yet
Development

No branches or pull requests

2 participants