microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
149 stars 35 forks source link

Activating extension 'ms-python.black-formatter' failed: e.replace is not a function. #443

Closed Alex-Schaefer closed 6 months ago

Alex-Schaefer commented 8 months ago

I'm encountering an unusual issue where I'm unable to use the Black formatter extension on my home PC due to an activation failure, despite it functioning correctly on my work PC.

Both systems are Windows 10 and utilize Python 3.10, chosen specifically for compatibility with an embedded system I am working on. I have enabled Settings Sync to ensure uniform settings and extensions across both PCs. Unfortunately, due to the extension's failure to activate, I am unable to provide any logs from Black itself.

The issue was identified through the Developer Tools console in Visual Studio Code, with the following output captured:

INFO Placing extension(s) ms-python.black-formatter, ms-python.isort, ms-python.python, ms-python.vscode-pylance, ms-toolsai.jupyter, ms-toolsai.jupyter-renderers on a separate extension host.
log.ts:427  WARN [twxs.cmake]: Cannot register 'cmake.cmakePath'. This property is already registered.
log.ts:417  INFO Started local extension host with pid 2056.
log.ts:417  INFO Started local extension host with pid 16260.
mainThreadExtensionService.ts:113 Activating extension 'ms-python.black-formatter' failed: e.replace is not a function.
$onExtensionActivationError @ mainThreadExtensionService.ts:113
log.ts:417  INFO [perf] Render performance baseline is 12ms
log.ts:427  WARN [cmake-tools]: Couldn't find message for key cmake-tools.configuration.cmake.options.advanced.statusBarVisibility.variant.description.

Attempting to downgrade the Black formatter extension to the last version released in 2023 did not resolve the issue.

I'm at a loss regarding additional information that might be relevant to resolving this problem, but I'm willing to provide further details upon request. Any guidance or suggestions on how to fix this activation failure would be greatly appreciated.

karthiknadig commented 8 months ago

Did this start happening with 1.86.0?

karthiknadig commented 8 months ago

You can also look for a call stack of failure inside the extension by either running Developer: show running extensions command that will list details of all the extensions vs code tried to activate.

Alex-Schaefer commented 8 months ago

Did this start happening with 1.86.0?

~~I don't think so. It has to be something with the new home PC setup. I just updated the work PC to VSC 1.86.0 with Black v2024.0.0 and everything still works.~~

I'll check in the evening what Developer: show running extensions returns on the home PC, at work every extension is activated successfully. And there is not "e.replace is not a function" error.

Alex-Schaefer commented 8 months ago

Well, now after a PC reboot, I have the same problem at the work PC, too. Updating VSC and Black worked initially, but now after the reboot, it's broken.

Activating extension 'ms-python.black-formatter' failed: e.replace is not a function.
$onExtensionActivationError @ mainThreadExtensionService.ts:113

Developer: show running extensions shows every Extension except Black Formatter is activated.

For Black Formatter it's stuck at "Activating..."

Those are my extensions shown by code --list-extensions

adpyke.codesnap
christian-kohler.path-intellisense
cschlosser.doxdocgen
donjayamanne.python-environment-manager
edonet.vscode-command-runner
github.codespaces
github.copilot
github.copilot-labs
github.vscode-pull-request-github
jerrygoyal.shortcut-menu-bar
kevinrose.vsc-python-indent
mcu-debug.debug-tracker-vscode
ms-ceintl.vscode-language-pack-de
ms-python.black-formatter
ms-python.debugpy
ms-python.isort
ms-python.pylint
ms-python.python
ms-python.vscode-pylance
ms-toolsai.jupyter
ms-toolsai.jupyter-keymap
ms-toolsai.jupyter-renderers
ms-toolsai.vscode-jupyter-cell-tags
ms-toolsai.vscode-jupyter-slideshow
ms-vscode-remote.remote-containers
ms-vscode-remote.remote-ssh
ms-vscode-remote.remote-ssh-edit
ms-vscode-remote.remote-wsl
ms-vscode.cmake-tools
ms-vscode.cpptools
ms-vscode.cpptools-extension-pack
ms-vscode.cpptools-themes
ms-vscode.remote-explorer
pdconsec.vscode-print
rubberduck.rubberduck-vscode
snappify.snappify
twxs.cmake
zixuanwang.linkerscript

I didn't find more information about why the Black Formatter extension won't get activated, besides the mentioned e.replace error message.

karthiknadig commented 8 months ago

Can you try extension host logs or developer tools (Help > toggle developer tools)? and share the logs.

Alex-Schaefer commented 8 months ago

In exthost.log I found this:

2024-02-05 19:04:25.064 [error] Activating extension ms-python.black-formatter failed due to an error:
2024-02-05 19:04:25.064 [error] TypeError: e.replace is not a function
    at c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.0.0\dist\extension.js:1:57261
    at Array.map (<anonymous>)
    at a (c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.0.0\dist\extension.js:1:57229)
    at l (c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.0.0\dist\extension.js:1:58101)
    at c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.0.0\dist\extension.js:1:58443
    at Array.map (<anonymous>)
    at t.getExtensionSettings (c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.0.0\dist\extension.js:1:58435)
    at t.activate (c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.0.0\dist\extension.js:1:66047)
    at z.ib (c:\Users\Alex\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:156:13232)
    at z.hb (c:\Users\Alex\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:156:12946)
    at c:\Users\Alex\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:156:11081
    at async c.n (c:\Users\Alex\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:140:6256)
    at async c.m (c:\Users\Alex\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:140:6219)
    at async c.l (c:\Users\Alex\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:140:5676)
karthiknadig commented 8 months ago

@Alex-Schaefer This is very helpful thanks.

karthiknadig commented 8 months ago

@Alex-Schaefer Would it be possible to share your settings? also, are you formatting a file directly without opening a workspace or do you have a workspace. It seems like the issue with resolving settings, specifically the current working directory.

karthiknadig commented 8 months ago

Can you try with this build? https://github.com/microsoft/vscode-black-formatter/actions/runs/7792001706/artifacts/1221986877

You can use "Install from VSIX..." command to install it.

Alex-Schaefer commented 8 months ago

Wow, that was quick. With this build, Black Formatter is activated.

But there is still a problem, formatting won't work, indeed because of my settings: my provided args are the problem:

2024-02-06 05:25:47.722 [info] Name: Black Formatter
2024-02-06 05:25:47.722 [info] Module: black
2024-02-06 05:25:47.722 [info] Default formatter is set to ms-python.black-formatter for workspace c:\Users\Alex\X\Y\Z
2024-02-06 05:25:47.722 [info] Python extension loading
2024-02-06 05:25:47.722 [info] Waiting for interpreter from python extension.
2024-02-06 05:25:47.722 [info] No interpreter found from setting black-formatter.interpreter
2024-02-06 05:25:47.722 [info] Getting interpreter from ms-python.python extension for workspace c:\Users\Alex\X\Y\Z
2024-02-06 05:25:47.722 [info] Python extension loaded
2024-02-06 05:25:47.722 [info] Interpreter from ms-python.python extension for c:\Users\Alex\X\Y\Z: c:\Users\Alex\X\Y\Z\.venv\Scripts\python.exe
2024-02-06 05:25:47.722 [info] No interpreter found from setting black-formatter.interpreter
2024-02-06 05:25:47.722 [info] Getting interpreter from ms-python.python extension for workspace c:\Users\Alex\X\Y\Z
2024-02-06 05:25:47.722 [info] Interpreter from ms-python.python extension for c:\Users\Alex\X\Y\Z: c:\Users\Alex\X\Y\Z\.venv\Scripts\python.exe
2024-02-06 05:25:47.722 [info] Server run command: c:\Users\Alex\X\Y\Z\.venv\Scripts\python.exe c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.1.7792001706-dev\bundled\tool\lsp_server.py
2024-02-06 05:25:47.722 [info] Server: Start requested.
2024-02-06 05:25:48.342 [info] CWD Server: c:\Users\Alex\X\Y\Z
2024-02-06 05:25:48.342 [info] Settings used to run Server:
[
    {
        "cwd": "c:\\Users\\Alex\\X\\Y\\Z",
        "workspace": "file:///c%3A/Users/Alex/X/Y/Z",
        "args": [
            [
                "--line-length",
                "119"
            ]
        ],
        "path": [],
        "interpreter": [
            "c:\\Users\\Alex\\X\\Y\\Z\\.venv\\Scripts\\python.exe"
        ],
        "importStrategy": "useBundled",
        "showNotifications": "off"
    }
]

2024-02-06 05:25:48.342 [info] Global settings:
{
    "cwd": "C:\\Users\\Alex\\AppData\\Local\\Programs\\Microsoft VS Code",
    "workspace": "C:\\Users\\Alex\\AppData\\Local\\Programs\\Microsoft VS Code",
    "args": [],
    "path": [],
    "interpreter": [],
    "importStrategy": "useBundled",
    "showNotifications": "off"
}

2024-02-06 05:25:48.342 [info] sys.path used to run Server:
   c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.1.7792001706-dev\bundled\libs
   c:\Users\Alex\.vscode\extensions\ms-python.black-formatter-2024.1.7792001706-dev\bundled\tool
   C:\Program Files\Python310\python310.zip
   C:\Program Files\Python310\DLLs
   C:\Program Files\Python310\lib
   C:\Program Files\Python310
   c:\Users\Alex\X\Y\Z\.venv
   c:\Users\Alex\X\Y\Z\.venv\lib\site-packages
2024-02-06 05:25:48.359 [info] c:\Users\Alex\X\Y\Z\.venv\Scripts\python.exe -m black --version
2024-02-06 05:25:48.359 [info] CWD formatter: c:\Users\Alex\X\Y\Z
2024-02-06 05:25:48.773 [info] Version info for formatter running for C:\Users\Alex\X\Y\Z:
black, 24.1.1 (compiled: no)
Python (CPython) 3.10.11

2024-02-06 05:25:48.774 [info] SUPPORTED black>=22.3.0
FOUND black==24.1.1

2024-02-06 05:26:29.142 [info] [Error - 5:26:29 AM] Request textDocument/formatting failed.
2024-02-06 05:26:29.142 [info]   Message: TypeError: sequence item 3: expected str instance, list found
  Code: -32603 
[object Object]
2024-02-06 05:28:01.917 [info] [Error - 5:28:01 AM] Request textDocument/formatting failed.
2024-02-06 05:28:01.917 [info]   Message: TypeError: sequence item 3: expected str instance, list found
  Code: -32603 
[object Object]

Since I added those settings lately, but didn't think about it, this was the root cause for the problem. Not a new version etc, but my wrong args.

With those corrected settings, everything works as intended:

    "black-formatter.args": [
        "--line-length", "119"
    ],

Now, the latest release version of Black Formatter works, too. Successful activated and formatting works.

So this is resolved for me, thank you very much!

Maybe it would be a good idea to check the settings before applying them, so that there is a more clear error, saying something is wrong with the settings?