作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在Windows上始终遇到以下错误(本地和Github Actions内部)。我在Mac OS和Linux上执行而不是时出现此错误。
Error: Custom { kind: Other, error: PathError { path: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\.tmpi45reT", err: Os { code: 32, kind: Other, message: "The process cannot access the file because it is being used by another process." } } }
代码基本上是使用
tempfile
crate 创建一个临时目录,然后在该目录中执行一堆
File::create
(以及其他一些可能不相关的东西)。写入所有文件后,将对每个文件进行显式
drop
ped,而对目录句柄进行显式
close
d。这是代码的粗略版本:
use fs::File;
use std::env;
use std::fs;
use std::io::Write as _;
use tempfile::tempdir;
fn main() -> std::io::Result<()> {
let dir = tempdir()?;
let dir_path = dir.path();
let pkg_path = dir_path.join("package.json");
let mut pkg_json = File::create(pkg_path)?;
pkg_json.write_all(
r#"
{
"name": "testpackage"
}
"#
.as_bytes(),
)?;
let _a = File::create(dir_path.join("yarn.lock"))?;
let _b = File::create(dir_path.join(".gitignore"))?;
env::set_current_dir(&dir)?;
drop(pkg_json);
drop(_a);
drop(_b);
dir.close()?;
Ok(())
}
这是集成测试的一部分,其中每个测试都执行与上面的代码相似的操作。
最佳答案
我发现原因是在将当前目录手动设置为tmp目录后,没有将当前目录改回原来的位置。
关于windows - 在临时目录中创建文件时的“The process cannot access the file because it is being used by another process”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63684982/
我是一名优秀的程序员,十分优秀!