简而言之,我通过 Homebrew 安装了 PowerShell。然后我在 VSCode 中安装了扩展(也重新启动了 VSCode)。当 VSCode 发现我正在处理 PowerShell 脚本时,它会尝试启动 PowerShell 会话。(PowerShell 集成终端)
PowerShell Integrated Console
An error occurred while starting PowerShell Editor Services:
The term 'chmod' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
at System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
at System.Management.Automation.CommandProcessorBase.Complete()
我知道这chmod
意味着某个地方的某个东西正在尝试更改某些文件权限。但我不知道它是什么。也许我可以自己更改权限,然后 PowerShell 最终会启动?
如果我查看扩展日志,我会得到以下信息:
1/20/2020 9:54:39 AM [NORMAL] - Visual Studio Code v1.41.1 64-bit
1/20/2020 9:54:39 AM [NORMAL] - PowerShell Extension v2020.1.0
1/20/2020 9:54:39 AM [NORMAL] - Operating System: MacOS 64-bit
1/20/2020 9:54:39 AM [NORMAL] - Path specified by 'powerShellExePath' setting - '' - not found, reverting to default PowerShell path.
1/20/2020 9:54:39 AM [NORMAL] - Language server starting --
1/20/2020 9:54:39 AM [NORMAL] - exe: /usr/local/bin/pwsh
1/20/2020 9:54:39 AM [NORMAL] - args: /Users/me/.vscode/extensions/ms-vscode.powershell-2020.1.0/modules/PowerShellEditorServices/Start-EditorServices.ps1 -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '2020.1.0' -AdditionalModules @('PowerShellEditorServices.VSCode') -BundledModulesPath '/Users/me/.vscode/extensions/ms-vscode.powershell-2020.1.0/modules' -EnableConsoleRepl -LogLevel 'Normal' -LogPath '/Users/me/.vscode/extensions/ms-vscode.powershell-2020.1.0/logs/1579532079-da336a1a-b41b-4ae4-8d54-c10e5e98d3fc1579532032267/EditorServices.log' -SessionDetailsPath '/Users/me/.vscode/extensions/ms-vscode.powershell-2020.1.0/sessions/PSES-VSCode-72218-307401' -FeatureFlags @()
1/20/2020 9:54:40 AM [NORMAL] - powershell.exe started, pid: 13196
1/20/2020 9:56:39 AM [NORMAL] - Language server startup failed.
1/20/2020 9:56:39 AM [ERROR] - The language service could not be started:
1/20/2020 9:56:39 AM [ERROR] - Timed out waiting for session file to appear.
1/20/2020 9:59:47 AM [NORMAL] - powershell.exe terminated or terminal UI was closed
我确实看到日志中powerShellExePath
是空白的,但我不知道该将其设置为何(或者这是否是问题所在)。
PowerShell 本身运行良好。如果我在终端中输入,pwsh path/to/file/helloWorld.ps1
它将按预期运行并打印出来。
任何想法都值得赞赏。
答案1
每次更新,我都会抓狂……幸运的是,修复很容易。
~/.vscode/extensions/ms-vscode.powershell-2020.1.0/modules/PowerShellEditorServices/Start-EditorServices.ps1
在第 272 行左右,更改
chmod $DEFAULT_USER_MODE $PipeFile
到
/bin/chmod $DEFAULT_USER_MODE $PipeFile
答案2
这种情况可能只出现在安装了基于 Windows 的配置文件脚本。
我发现,对于我使用的配置文件脚本,它最初是为 Windows 设计的,并且作为配置文件初始化的一部分,它将设置$env:PSModulePath
。但是,它将使用 Windows 路径分隔符;
,而不是:
在 macOS 上使用。因此,最后一条路径将与添加的路径合并,而最后一条路径恰好是 PowerShell VS Code 扩展的模块文件夹……
/Users/bart/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/Users/bart/.dotnet/tools/.store/powershell/7.1.4/powershell/7.1.4/tools/net5.0/any/unix/Modules:/Users/bart/.vscode/extensions/ms-vscode.powershell-2021.9.0/modules;/Users/bart/custom/path
解决方案是使用跨平台兼容的 PSModulePath 分配:
$env:PSModulePath = $env:PSModulePath + "$([System.IO.Path]::PathSeparator)$MyModulePath"