Closed bhuynhdev closed 10 months ago
The detection of the executable was reliant on the information provided in this pull request: https://github.com/bitst0rm-pub/Formatter/pull/36 Prettier seems like to love dancing in the past, from bin-prettier.js, to prettier, now prettier.cmd, ...
I do not have windows, You could test this please:
In modules > formatter_prettier.py > line 17:
from EXECUTABLES = ['prettier', 'bin-prettier.js']
change to EXECUTABLES = ['prettier.cmd', 'prettier', 'bin-prettier.js']
It is correct, local first, then global prioritizing, therefore the error.
Adding "prettier.cmd" nor "prettier.ps1" did not fix it
Actually seems like it's not that the executable path is wrong; the actual issue seems to be that Formatter is trying to run these executable using Node.exe
whereas those executable files in the .bin
folder seems to be bash/Powershell/CMD script that is supposed to be run standalone
The referenced issue from VSCodeVim seems to have come to same conclusion and have made the changes to run the prettier path directly without the node
interpreter
ok thanks. We must adjust so for windows:
`EXECUTABLES = ['prettier.cmd', 'prettier', 'bin-prettier.js']`
...
...
def get_cmd(self):
executable = common.get_executable(self.view, self.uid, ['prettier.cmd'], runtime_type='node')
cmd = [executable]
if not executable:
cmd = common.get_head_cmd(self.view, self.uid, INTERPRETERS, EXECUTABLES, runtime_type='node')
if not cmd:
return None
Thanks a lot. Using common.IS_WINDOWS
is an awwesome idea. I used
# formatter_prettier.py
def get_cmd(self):
cmd = []
if common.IS_WINDOWS:
executable = common.get_executable(self.view, self.uid, EXECUTABLES, runtime_type='node')
cmd = [executable]
else:
cmd = common.get_head_cmd(self.view, self.uid, INTERPRETERS, EXECUTABLES, runtime_type='node')
if not cmd:
return None
And also no need for prettier.cmd
in the EXECUTABLES
list.
Result:
[DEBUG] Current arguments: ['C:\\Users\\users\\code-project\\node_modules\\.bin\\prettier', '--no-config', '--stdin-filepath', 'C:\\Users\\users\\code-project\\src\\HomePage.jsx']
▋[Formatter](MainThread:main.py#L349): [DEBUG] Replacing text ...
▋[Formatter](Thread-1:main.py#L295): [DEBUG] Formatting successful. 🎉😃🍰
It can be seen that after the changes, Formatter now runs the prettier
bin file directly without the node.EXE
; therefore, it is able to format the file
aha your log: on windows Prettier does not seem to usenode.exe
directly. so the final patch would be:
# formatter_prettier.py
EXECUTABLES = ['prettier', 'bin-prettier.js'] # no changes
...
def get_cmd(self):
if common.IS_WINDOWS:
executable = common.get_executable(self.view, self.uid, EXECUTABLES, runtime_type='node')
cmd = [executable]
else:
cmd = common.get_head_cmd(self.view, self.uid, INTERPRETERS, EXECUTABLES, runtime_type='node')
if not cmd:
return None
i will release this later, thanks.
Actually my bad. There is this case where there is no local executable found --> Formatter defaults to the global user executable --> The Global prettier executable is a .cjs
file --> Now it needs the node
interpreter
Sorry for the mistake
New proposed changes:
# formatter_prettier.py
def get_cmd(self):
if common.IS_WINDOWS:
executable = common.get_executable(
self.view, self.uid, EXECUTABLES, runtime_type="node"
)
if not executable.endswith("js"):
# If the executable is a standalone script, then just run it
cmd = [executable]
else: # Else, it is a JS file then it needs an interpreter
cmd = common.get_head_cmd(
self.view, self.uid, INTERPRETERS, EXECUTABLES, runtime_type="node"
)
else:
cmd = common.get_head_cmd(
self.view, self.uid, INTERPRETERS, EXECUTABLES, runtime_type="node"
)
if not cmd:
return None
On unix it does not have .cjs
, your patch works well on unix. It looks awesome to catch all cases, could you pls file a push request so i can merged it. Thanks.
The Formatter plugin seems to be able to detect local executables from the "node_modules/.bin" folder, but it seems to be using the wrong file on Windows.
Specifically, it is using
node_modules\.bin\prettier
instead ofnode_modules\.bin\prettier.cmd
(reference: https://github.com/VSCodeVim/Vim/issues/2649), thus leading to issue:I have specified a global prettier executable with the
executable_path
settings, but it seems to be ignored, and Formatter keeps prioritizing using the local executable, which leads to the above error