WSL2中使用BenchmarkDotNet报错:访问/mnt/c/DumpStack.log.tmp被拒绝
WSL2下运行BenchmarkDotNet的权限问题解决
问题场景
学习《Stylish F#: Crafting Elegant Functional Code for .NET and .NET Core》性能章节时,编写了如下F#基准测试代码:
open BenchmarkDotNet.Running open BenchmarkDotNet.Attributes module Dummy = let slowFunction () = System.Threading.Thread.Sleep 100 99 let fastFunction () = System.Threading.Thread.Sleep 10 99 module Harness = [<MemoryDiagnoser>] type Harness() = [<Benchmark>] member _.Old() = Dummy.slowFunction () [<Benchmark>] member _.New() = Dummy.fastFunction () [<EntryPoint>] let run _ = BenchmarkRunner.Run<Harness.Harness>() |> printfn "%A" 0
在WSL2的Ubuntu 20.04环境下执行dotnet run -c release命令时,出现以下错误:
// Validating benchmarks: // ***** BenchmarkRunner: Start ***** // ***** Found 2 benchmark(s) in total ***** // ***** Building 1 exe(s) in Parallel: Start ***** // ***** Done, took 00:00:00 (0.13 sec) ***** // Found 2 benchmarks: // Harness.Old: DefaultJob // Harness.New: DefaultJob // Generate Exception: Access to the path '/mnt/c/DumpStack.log.tmp' is denied. // BenchmarkDotNet has failed to build the auto-generated boilerplate code. // It can be found in /mnt/c/data/dev/fsharp-lab/Stylish-FSharp6/bin/release/net6.0/c456e559-bdf2-4a24-858d-b423379f42c9 // Please follow the troubleshooting guide: https://benchmarkdotnet.org/articles/guides/troubleshooting.html // ***** BenchmarkRunner: Finish *****
针对推测的WSL2用户无法访问Windows 11中DumpStack.log.tmp文件的问题,解答如下:
1. 是否可以授权访问该文件(可能会降低安全性)?
不建议直接对DumpStack.log.tmp授权。该文件是Windows系统生成的临时调试日志,修改其权限会打破WSL2与Windows的文件权限隔离机制,带来安全风险——比如WSL内进程可能意外篡改系统级文件,影响Windows系统稳定性。且即使临时修改权限,后续系统更新或文件重建后权限会重置,无法彻底解决问题。
2. BenchmarkDotNet是否支持在WSL2环境中运行?
完全支持。出现问题的核心原因是项目放在了Windows挂载目录(/mnt/c/...)下,WSL2访问Windows文件系统存在权限兼容性问题。解决方法是将项目迁移到WSL2原生Linux文件系统(如~/dev/目录),BenchmarkDotNet即可正常生成和访问临时文件,避免权限错误。
内容的提问来源于stack exchange,提问作者Digital Stoic
相关产品推荐
相关产品推荐

