Skip to content

Error getting BOM file from poetry #448

Closed

Description

@JCHacking

I have a project in which I use poetry, and I want to be able to obtain the BOM file with the poetry dependencies.

When I run the following command: cyclonedx-py --poetry --output bom.xml

I get the following error:

Traceback (most recent call last):
  File "C:\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\jcmencia\PycharmProjects\QA-Pruebas\py_requests\venv\Scripts\cyclonedx-py.exe\__main__.py", line 7, in <module>     
  File "C:\Users\jcmencia\PycharmProjects\QA-Pruebas\py_requests\venv\lib\site-packages\cyclonedx_py\client.py", line 307, in main   
    CycloneDxCmd(args).execute()
  File "C:\Users\jcmencia\PycharmProjects\QA-Pruebas\py_requests\venv\lib\site-packages\cyclonedx_py\client.py", line 147, in execute
    output = self.get_output()
  File "C:\Users\jcmencia\PycharmProjects\QA-Pruebas\py_requests\venv\lib\site-packages\cyclonedx_py\client.py", line 84, in get_output
    parser = self._get_input_parser()
  File "C:\Users\jcmencia\PycharmProjects\QA-Pruebas\py_requests\venv\lib\site-packages\cyclonedx_py\client.py", line 282, in _get_input_parser
    input_data = input_data_fh.read()
  File "C:\Python310\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 6935: character maps to <undefined>

The poetry.lock file is in UTF-8 format and with LF line endings and I use Windows with PyCharm

If I edit in the client.py file the following line:

self._arguments.input_source = open(os.path.join(current_directory, 'poetry.lock'), 'r')

And I add the encoding parameter:

self._arguments.input_source = open(os.path.join(current_directory, 'poetry.lock'), 'r', encoding="utf-8")

The command works perfectly for me.

Is there any configuration I can do to make it use UTF-8 encoding instead of opting for cp1252? Or any solution for this problem? Everything I have tried has not worked.
Thanks in advance!

Activity

  1. changed the title [-]Error getting file from poetry[/-] [+]Error getting BOM file from poetry[/+] on Nov 30, 2022
  2. JCHacking commented on Nov 30, 2022

    @JCHacking
    ContributorAuthor

    Could a parameter be added to the command like --utf-8 to add that extra parameter to open?

    So if my application requires not to use PYTHONUTF8 I can use this library.

  3. jkowalleck commented on Dec 1, 2022

    @jkowalleck
    Member

    my idea to the solution:
    i would prefer to have char-detect, rather than hardcoding the char-encoding or having a user to decide.
    Need to think about it and need to prepare reproducible envs for testing, first.

  4. JCHacking commented on Oct 12, 2023

    @JCHacking
    ContributorAuthor

    Hi @jkowalleck
    I have opened a pull request #596 with the solution to this (Autodetecting characters). I have tested it on my Windows system and it works correctly so I think it is solved.
    Regards!!!

  5. jkowalleck commented on Oct 12, 2023

    @jkowalleck
    Member

    oh, great.
    I will see if I can provide a CT/CI env to reproduce the issue, so that i can use it as a regression test env

  6. jkowalleck commented on Oct 12, 2023

    @jkowalleck
    Member

    re #448 (comment)

    Need to think about it and need to prepare reproducible envs for testing, first.

    re #448 (comment)

    I will see if I can provide a CT/CI env to reproduce the issue, so that i can use it as a regression test env

    @JCHacking could you help me reproducing the issue?
    maybe provide the input file that caused your issue?

  7. JCHacking commented on Oct 12, 2023

    @JCHacking
    ContributorAuthor

    @jkowalleck
    The problem is not in the file. I will try to explain it as best I can.

    The Python open function seems to work differently on Windows systems than on Unix systems, by modifying the default encoding with which you open it.

    • UNIX = UTF-8
    • Windows = Windows-1252

    What this causes is that in Windows systems when you try to open the poetry.lock file you will find a character that does not exist in Windows-1252.

    The applied solution skips this encoding, since when opening it in byte mode it does not have to encode it.
    Then when we evaluate the content and determine the encoding, is when we pass it to string (In python 3 the strings already work in UTF-8, so it will always be in the same format).

    Now, to be able to put a test of this scenario there is a problem, and is that each character that there is in Windows-1252 exists in UTF-8, so you can not put a file with X character "rare" and make it fail without this solution, because the container of the test (I understand that they are made with github actions and that probably is a linux container) will open the file as UTF-8.

    In this scenario I can think of 2 possible solutions:

    • That this test runs in a windows container (I don't know if this would be possible).
    • A test which with a mock of the open method I pass encoding=cp2152 (Windows-1252 = cp1252) to simulate that it opens it as a Windows system would do it.

    If you need me to investigate to apply the second solution I would have no problem (the first one would cost me more because I don't know how github actions work, since I haven't worked with it yet).

  8. jkowalleck commented on Oct 19, 2023

    @jkowalleck
    Member

    Discussion:

    The more I read about the issue, the more I think it is a bad idea to use dynamic encoding detection for files that are written by machines and read by machines.
    This would include: Pipfile.lock, poetry.lock
    This would not include requirements.txt

    Since we must handle one of the files file, then why not also handle the others.
    Your approach appears reasonable, but might get optimized: only char-detect where needed.

  9. JCHacking commented on Oct 19, 2023

    @JCHacking
    ContributorAuthor

    In this way, by validating the encoding of each file we could also allow the client to modify one of the locks manually and without its editor realizing it, change the encoding.
    This is rare but it could happen. Thus, we also allow that any poetry or library decides the encoding of its files, since as we auto-detect it, it would not cause any problems with those changes.

    I think it will help us avoid having to make absurd changes due to external factors.

  10. jkowalleck commented on Oct 19, 2023

    @jkowalleck
    Member

    we could also allow the client to modify one of the locks manually and without its editor realizing it, change the encoding.

    this is no intended use case of lock files. If they modify it, they need to keep the standard encoding.
    We do not want to find excuses for any implementation. Rather than implementing as use cases appear.

    for requirement.txt it often happens that they are written manually. Here, I totally see your point.

  11. added a commit that references this issue on Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions