drahnr / cargo-spellcheck

Checks all your documentation for spelling and grammar mistakes with hunspell and a nlprule based checker for grammar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: failed to rename file (Access is denied. (os error 5))

zohnannor opened this issue · comments

Describe the bug

To Reproduce

Steps to reproduce the behavior:

  1. Run $env:RUST_BACKTRACE="full"; cargo spellcheck fix
  2. Complete the steps (go through all errors reported)
  3. It tries to save temp file and then replace the original with it
  4. Error:
       0: failed to rename file from E:\my_path\.spellcheck.tmpd1c18dca-90a0-4552-8397-6b052802e37a to 
    \\?\E:\my_path\src\lib.rs
       1: Access is denied. (os error 5)
    
    Location:
       /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\result.rs:1911
      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
         1: onig_get_start_by_callout_args<unknown>
            at <unknown source file>:<unknown line>
         2: onig_get_start_by_callout_args<unknown>
            at <unknown source file>:<unknown line>
         3: onig_get_start_by_callout_args<unknown>
            at <unknown source file>:<unknown line>
         4: onig_get_start_by_callout_args<unknown>
            at <unknown source file>:<unknown line>
         5: store_pointer<unknown>
            at <unknown source file>:<unknown line>
         6: store_pointer<unknown>
            at <unknown source file>:<unknown line>
         7: store_pointer<unknown>
            at <unknown source file>:<unknown line>
         8: store_pointer<unknown>
            at <unknown source file>:<unknown line>
         9: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        10: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        11: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        12: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        13: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        14: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        15: onig_get_syntax_op2<unknown>
            at <unknown source file>:<unknown line>
        16: store_pointer<unknown>
            at <unknown source file>:<unknown line>
        17: onig_unicode_define_user_property<unknown>
            at <unknown source file>:<unknown line>
        18: BaseThreadInitThunk<unknown>
            at <unknown source file>:<unknown line>
        19: RtlUserThreadStart<unknown>
            at <unknown source file>:<unknown line>
      
      Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
    

Expected behavior

The process ends without errors and replaces my old file with the new one.

Please complete the following information:

  • System: Win10
  • Obtained: cargo install --locked cargo-spellcheck
  • Version: cargo-spellcheck 0.9.6

Additional context
My terminal is ran under Administrator privileges

Is your directory a special thing, i.e. a shared network folder or such? Can you replace the the file manually? I don't posses a windows machine to test this unfortunately, the CI passes when cross compiling and running wine as emulator.

os error 5 on windows means access denied, so that's a hint, but I am not sure how much I can help there.

Can you replace the the file manually?

Yep, I can delete my old file, rename .spellcheck.tmp<UUID> and place it where it needs to be.

Is your directory a special thing,

No, just a directory I created for my projects awhile ago. No shared network, nothing special.

Here is the steps to reproduce from creating the project to witnessing the error:

PS C:\User\Username\> E:
PS E:\> mkdir my_path
PS E:\> cd .\my_path\
PS E:\my_path> ca new spell
     Created binary (application) `spell` package
PS E:\my_path> cd spell
PS E:\my_path> # I've edited my main.rs file
PS E:\my_path\spell> bat .\src\main.rs
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: .\src\main.rs
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ /// Spelilng erorr;
   2   │ struct S;
   3   │
   4   │ fn main() {
   5   │     println!("Hello, world!");
   6   │ }
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
PS E:\my_path\spell> $env:RUST_BACKTRACE=1; ca spellcheck fix
error: spellcheck(Hunspell)
  --> \\?\E:\my_path\spell\src\main.rs:1
   |
 1 |  Spelilng erorr;
   |  ^^^^^^^^

(1/2) Apply this suggestion [y,n,q,a,d,j,e,?]?


   Spelunking
 » Spelling
   `Spelilng`
error: spellcheck(Hunspell)
  --> \\?\E:\my_path\spell\src\main.rs:1
   |
 1 |  Spelilng erorr;
   |           ^^^^^

(2/2) Apply this suggestion [y,n,q,a,d,j,e,?]?


   err
 » error
   `erorr`
Error:
   0: failed to rename file from E:\my_path\spell\.spellcheck.tmpf29a673f-4c65-448a-ab33-2fe5bb5cda05 to \\?\E:\my_path\spell\src\main.rs
   1: Access is denied. (os error 5)

Location:
   /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\result.rs:1911

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: onig_get_start_by_callout_args<unknown>
      at <unknown source file>:<unknown line>
   2: onig_get_start_by_callout_args<unknown>
      at <unknown source file>:<unknown line>
   3: onig_get_start_by_callout_args<unknown>
      at <unknown source file>:<unknown line>
   4: onig_get_start_by_callout_args<unknown>
      at <unknown source file>:<unknown line>
   5: store_pointer<unknown>
      at <unknown source file>:<unknown line>
   6: store_pointer<unknown>
      at <unknown source file>:<unknown line>
   7: store_pointer<unknown>
      at <unknown source file>:<unknown line>
   8: store_pointer<unknown>
      at <unknown source file>:<unknown line>
   9: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  10: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  11: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  12: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  13: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  14: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  15: onig_get_syntax_op2<unknown>
      at <unknown source file>:<unknown line>
  16: store_pointer<unknown>
      at <unknown source file>:<unknown line>
  17: onig_unicode_define_user_property<unknown>
      at <unknown source file>:<unknown line>
  18: BaseThreadInitThunk<unknown>
      at <unknown source file>:<unknown line>
  19: RtlUserThreadStart<unknown>
      at <unknown source file>:<unknown line>

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
Run with RUST_BACKTRACE=full to include source snippets.
PS E:\my_path\spell>

That is indeed strange, nothing seems to prevent cargo-spellcheck from renaming the file. I'll try to elaborate on this issue more.

Hey, I did a little bit of research. The reason this error happens is: you can't rename file if it is not closed.

in this lines:

let wr = fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(&tmp)?;
let mut writer = std::io::BufWriter::with_capacity(1024, wr);
let mut content = String::with_capacity(2e6 as usize);
reader.get_mut().read_to_string(&mut content)?;
{
let th = crate::TinHat::on();
apply_patches(
bandaids.into_iter().map(|x| Patch::from(x)),
content.as_str(), // FIXME for efficiency, correct_lines should integrate with `BufRead` instead of a `String` buffer
&mut writer,
)?;
writer.flush()?;
fs::rename(tmp, path)?;
// Writing for this file is done, unblock the signal handler.
drop(th);
}
Ok(())
}

wr is not closed, but it's renamed to another file.

Thank you, I'll fix this for the next release :)

I thought the fix would be simple: just drop writer before renaming so it would close the file but no, the the error is still present.

May I open a PR when I'll resolve this? I've spent whole evening on this, debugging and reading sources and I really want to help fix it 😃 Moreover, this error happens on my machine (for some weird reason) so I can easily test the fix locally.

Ofc! Feel free to open a PR at any time! I am more than happy to aid where I can :)

Hint: it might be enough to explicitly drop the writer and wr in

, but that's just a wild guess, technically it should be auto dropped in there iiuc