Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError while using "mox init" #186

Closed
krakovia-evm opened this issue Jan 25, 2025 · 7 comments
Closed

UnicodeEncodeError while using "mox init" #186

krakovia-evm opened this issue Jan 25, 2025 · 7 comments

Comments

@krakovia-evm
Copy link

i've installed moccasin in an isolated environment with uv

uv init --python 3.11
uv venv --python 3.11
uv add moccasin

when i launch
mox init my_project

i get

Running init command...
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\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Scripts\mox.exe\__main__.py", line 8, in <module>
  File "C:\Users\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Lib\site-packages\moccasin\__init__.py", line 8, in main
    __main__.main(sys.argv[1:])
  File "C:\Users\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Lib\site-packages\moccasin\__main__.py", line 58, in main
    import_module(f"moccasin.commands.{command_to_run}").main(args)
  File "C:\Users\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Lib\site-packages\moccasin\commands\init.py", line 30, in main
    path: Path = new_project(
                 ^^^^^^^^^^^^
  File "C:\Users\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Lib\site-packages\moccasin\commands\init.py", line 62, in new_project
    _create_files(project_path, vscode=vscode, pyproject=pyproject)
  File "C:\Users\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Lib\site-packages\moccasin\commands\init.py", line 84, in _create_files
    _write_file(project_path.joinpath(README_PATH), README_MD_SRC)
  File "C:\Users\shadow\Desktop\PROJS\CradleStudios\CradleVesting_mocassin\.venv\Lib\site-packages\moccasin\commands\init.py", line 104, in _write_file
    fp.write(contents)
  File "C:\Users\shadow\AppData\Roaming\uv\python\cpython-3.11.9-windows-x86_64-none\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f40d' in position 22: character maps to <undefined>

in file .venv\Lib\site-packages\moccasin\commands\init.py, row 103, i changed

with path.open("w") as fp:
to
with path.open("w", encoding="utf-8") as fp:

and now the command works fine =)

@s3bc40
Copy link
Contributor

s3bc40 commented Jan 25, 2025

Hey!

Thanks for pointing it out 👌

I suppose the env was activated since we see that it takes moccasin from your venv/Lib so all good.

And it seems you are on Windows, I'll try to reproduce for science on my windows.

No WSL ?

I'll come back after testing

@s3bc40
Copy link
Contributor

s3bc40 commented Jan 25, 2025

I have tested, and I do have the same error by doing it on Windows with the same pattern as you.

And yes, the following solution is working:

def _write_file(path: Path, contents: str, overwrite: bool = False) -> None:
    if not path.exists() or overwrite:
-        with path.open("w") as fp:
+        with path.open("w", encoding="utf-8") as fp:
            fp.write(contents)

Just out of curiosity, Is there any motivation to install moccasin this way and not with uv tool intall moccasin to get it inside its own virtual environment and get it globally?

You could then do mox init and get your my_projects directly, but maybe you are adding in an existing project.

@PatrickAlphaC is it something to fix for Windows users or is there something I missed?

EDIT: maybe I'll need to double-check on Linux afterward

@krakovia-evm
Copy link
Author

I have tested, and I do have the same error by doing it on Windows with the same pattern as you.

And yes, the following solution is working:

def _write_file(path: Path, contents: str, overwrite: bool = False) -> None:
if not path.exists() or overwrite:

  •    with path.open("w") as fp:
    
  •    with path.open("w", encoding="utf-8") as fp:
          fp.write(contents)
    

Just out of curiosity, Is there any motivation to install moccasin this way and not with uv tool intall moccasin to get it inside its own virtual environment and get it globally?

You could then do mox init and get your my_projects directly, but maybe you are adding in an existing project.

@PatrickAlphaC is it something to fix for Windows users or is there something I missed?

EDIT: maybe I'll need to double-check on Linux afterward

WSL gave me strange network issues in the past so don't use it anymore on my main device.
About uv tool intall moccasin , you're right i should use it!

@PatrickAlphaC
Copy link
Member

Yes, uv add moccasin should work; thanks for reporting and looking into this...

Wow thanks @s3bc40! Wondering... Are you interested in making that PR with the fix? I think maybe we should also add a windows test maybe in the tests folder, and add a windows specific test in the github actions.

I don't love supporting windows, since that opens up a can of worms, but you should be able to at least install!

@s3bc40
Copy link
Contributor

s3bc40 commented Jan 25, 2025

I am not a big fan of windows for coding too, aha. But I think I can just check if everything is fine with the current tests.

And if I see something off, I'll dig into this and make specific tests (while learning how to do so).

I'll make a PR when I'll reach something!

@s3bc40
Copy link
Contributor

s3bc40 commented Jan 27, 2025

Here is my PR: #191

I have added other fixes while trying to run mox on Windows, and test again on my Linux (feels way better!).

After a few days trying to run the tests by installing moccasin locally on Windows, I think it will be a great idea to create a specific test suit for Windows.

And the best would be to be capable of installing the repo on Windows and run the equivalent of what we already have on Linux or MacOs.

But it will be a huge work, because it means to deal with the Windows permission system, which is kind of a nightmare to handle. Especially the subprocess and the AppData\Local\Temp privileges. I tried everything (running GitBash as admin, runas, tweaking the dir privilege), nothing was working or if it was I encountered a new permission wall.

Maybe with someone with more XP it would be smoother. But for now I have fixed some features that didn't run on Windows.

PatrickAlphaC pushed a commit that referenced this issue Jan 29, 2025
…arser (#191)

* fix: windows issues on mox cli command and utils argparser

- `init.py`: windows error `(UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f40d')` when the param `encoding="utf-8" for `open` is not set
- `compile.py`: windows does not support `fork` method for `multiprocessing.set_start_method()`, hence changed to "spawn"
- `vars.py`: added general constant `IS_WINDOWS` for future use and avoid any duplicates
- fixed utils CLI command running an error when no arguments passed + fixed some var typo

* fix: format with ruff reorder import
@PatrickAlphaC
Copy link
Member

This should be fixed now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants