RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.85k stars 81 forks source link

Cannot install CLI on windows #974

Open MrPupik opened 3 months ago

MrPupik commented 3 months ago

Bug description

When starting a new project in windows, python -m prisma ... fails when trying to install prisma CLI (I suspect it fails using npm)

How to reproduce

Start a new empty project on windows 11 and attempt generating the client. Error:

prismaPythonSample  .venv ❯ python -m prisma         
This command is only intended to be invoked internally. Please run the following instead:
prisma <command>
e.g.
prisma generate
prismaPythonSample  .venv ❯ python -m prisma generate
YYYYYYYYYYYYYY
['generate']
Installing Prisma CLI
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\__main__.py", line 6, in <module>
    cli.main()
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\cli.py", line 39, in main
    sys.exit(prisma.run(args[1:]))
             ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\prisma.py", line 35, in run
    entrypoint = ensure_cached().entrypoint
                 ^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\prisma.py", line 88, in ensure_cached
    proc = npm.run(
           ^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\_proxy.py", line 19, in __getattr__
    return getattr(self.__get_proxied__(), attr)
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\_proxy.py", line 38, in __get_proxied__
    self.__proxied = proxied = self.__load__()
                               ^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\_node.py", line 406, in __load__
    return resolve(self.target)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\_node.py", line 281, in resolve
    return NodeBinaryStrategy.resolve(target)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\_node.py", line 149, in resolve
    path = _get_global_binary(target)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\_node.py", line 333, in _get_global_binary
    if not _should_use_binary(target=target, path=path):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\_node.py", line 355, in _should_use_binary
    version = _get_binary_version(target, path)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\dev\prismaPythonSample\.venv\Lib\site-packages\prisma\cli\_node.py", line 377, in _get_binary_version
    proc = subprocess.run(
           ^^^^^^^^^^^^^^^
  File "C:\Users\itayn\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\itayn\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\itayn\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
RobertCraigie commented 2 months ago

@MrPupik can you share a full stack trace? It looks like your error is cut off.

NotLegish commented 1 week ago

@MrPupik can you share a full stack trace? It looks like your error is cut off.

Hey. I think I may be experiencing the same issue. Here is my stacktrace with the logging enabled. The error seems to be [WinError 193] %1 ei ole kelvollinen Win32-sovellus which roughly translates to %1 is not a valid Win32-application The file at C:\Program Files\nodejs\npm seems to be a bash script. Let me know if you need more information.

(.venv) PS D:\koodia\test prisma> powershell -Command { $env:PRISMA_PY_DEBUG="1"; prisma db push }          
[DEBUG  ] prisma.cli.prisma: Running prisma command with args: ['db', 'push']
Installing Prisma CLI
[DEBUG  ] prisma.cli._node: Checking if nodejs-bin is installed
[DEBUG  ] prisma.cli._node: Checking for global target binary: npm
[DEBUG  ] prisma.cli._node: Found global binary at: C:\Program Files\nodejs\npm
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\koodia\test prisma\.venv\scripts\prisma.exe\__main__.py", line 7, in <module>
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\cli.py", line 37, in main
    sys.exit(prisma.run(args[1:]))
             ^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\prisma.py", line 35, in run
    entrypoint = ensure_cached().entrypoint
                 ^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\prisma.py", line 88, in ensure_cached
    proc = npm.run(
           ^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\_proxy.py", line 19, in __getattr__
    return getattr(self.__get_proxied__(), attr)
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\_proxy.py", line 38, in __get_proxied__
    self.__proxied = proxied = self.__load__()
                               ^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\_node.py", line 406, in __load__
    return resolve(self.target)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\_node.py", line 281, in resolve
    return NodeBinaryStrategy.resolve(target)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\_node.py", line 149, in resolve
    path = _get_global_binary(target)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\_node.py", line 333, in _get_global_binary
    if not _should_use_binary(target=target, path=path):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\_node.py", line 355, in _should_use_binary
    version = _get_binary_version(target, path)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\koodia\test prisma\.venv\Lib\site-packages\prisma\cli\_node.py", line 377, in _get_binary_version
    proc = subprocess.run(
           ^^^^^^^^^^^^^^^
  File "C:\Users\Omistaja\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Omistaja\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Omistaja\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [WinError 193] %1 ei ole kelvollinen Win32-sovellus

I am quite new to github issues, hopefully I did not cause any trouble or if I did let me know what I should have done differently.

MrPupik commented 1 week ago

@RobertCraigie

Sorry I missed your previous message, but I'm pretty sure @NotLegish's error is the same as mine. Will confirm later

RobertCraigie commented 1 week ago

The file at C:\Program Files\nodejs\npm seems to be a bash script.

that seems quite odd that it's a bash script on windows... how did you install Node.js?

does running npm work for you?

NotLegish commented 1 week ago

The file at C:\Program Files\nodejs\npm seems to be a bash script.

that seems quite odd that it's a bash script on windows... how did you install Node.js?

does running npm work for you?

That might be a typo. Regardless the way I assumed it was bash/batch script was only based on a comment header as I am not able to tell the difference between such languages.

I do not remember exactly how I installed node.js, but it was likely via an msi installer. While I believe I can run npm fine I will get back to you later and confirm this.

NotLegish commented 6 days ago

The file at C:\Program Files\nodejs\npm seems to be a bash script.

that seems quite odd that it's a bash script on windows... how did you install Node.js?

does running npm work for you?

Running npm does work. Powershells Get-Command returns C:\Program Files\nodejs\npm.cmd instead of the C:\Program Files\nodejs\npm that prisma finds.

Regarding the file at C:\Program Files\nodejs\npm the comment header indeed seems to indicate it being a bash script. I have attached the contents of both the npm and npm.cmd file for you to confirm.

C:\Program Files\nodejs\npm

#!/usr/bin/env bash

# This is used by the Node.js installer, which expects the cygwin/mingw
# shell script to already be present in the npm dependency folder.

(set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix

basedir=`dirname "$0"`

case `uname` in
  *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ `uname` = 'Linux' ] && type wslpath &>/dev/null ; then
  IS_WSL="true"
fi

function no_node_dir {
  # if this didn't work, then everything else below will fail
  echo "Could not determine Node.js install directory" >&2
  exit 1
}

NODE_EXE="$basedir/node.exe"
if ! [ -x "$NODE_EXE" ]; then
  NODE_EXE="$basedir/node"
fi
if ! [ -x "$NODE_EXE" ]; then
  NODE_EXE=node
fi

# this path is passed to node.exe, so it needs to match whatever
# kind of paths Node.js thinks it's using, typically win32 paths.
CLI_BASEDIR="$("$NODE_EXE" -p 'require("path").dirname(process.execPath)' 2> /dev/null)"
if [ $? -ne 0 ]; then
  # this fails under WSL 1 so add an additional message. we also suppress stderr above
  # because the actual error raised is not helpful. in WSL 1 node.exe cannot handle
  # output redirection properly. See https://github.com/microsoft/WSL/issues/2370
  if [ "$IS_WSL" == "true" ]; then
    echo "WSL 1 is not supported. Please upgrade to WSL 2 or above." >&2
  fi
  no_node_dir
fi
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
if [ $? -ne 0 ]; then
  no_node_dir
fi
NPM_PREFIX_NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js"

# a path that will fail -f test on any posix bash
NPM_WSL_PATH="/.."

# WSL can run Windows binaries, so we have to give it the win32 path
# however, WSL bash tests against posix paths, so we need to construct that
# to know if npm is installed globally.
if [ "$IS_WSL" == "true" ]; then
  NPM_WSL_PATH=`wslpath "$NPM_PREFIX_NPM_CLI_JS"`
fi
if [ -f "$NPM_PREFIX_NPM_CLI_JS" ] || [ -f "$NPM_WSL_PATH" ]; then
  NPM_CLI_JS="$NPM_PREFIX_NPM_CLI_JS"
fi

"$NODE_EXE" "$NPM_CLI_JS" "$@"

C:\Program Files\nodejs\npm.cmd

:: Created by npm, please don't edit manually.
@ECHO OFF

SETLOCAL

SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
  SET "NODE_EXE=node"
)

SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
  SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
  SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)

"%NODE_EXE%" "%NPM_CLI_JS%" %*