Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Apparently my package.json is invalid when trying to add my own package

Discussion in 'Package Manager' started by profanicus, Sep 27, 2021.

  1. profanicus

    profanicus

    Joined:
    Nov 23, 2009
    Posts:
    295
    This is probably something simple, but it's fairly new territory for me so maybe someone can help me out? :)

    What I'm trying to do is import a package from GitHub that I've created. It's behind SSL.
    In my project I open Package Manager and 'add package from Git URL'.

    It downloads and unzips the files into the PackageCache folder, creating a directory called '@
    981766cfbd', which I believe is the hash for the latest commit. Is this correct?

    Then it fails with:

    The file [/Users/profanicus/UnityProjects/invaders/Library/PackageCache/@981766cfbd/package.json] is not valid JSON: Unexpected token in JSON at position 0 [NotFound]


    The below is copy/pasted from that package.json at the PackageCache location shown in the error above:

    {
    "name": "com.profanicus.game-tools",
    "displayName": "GameTools",
    "description": "Collection of shared scripts",
    "version": "1.0.0"
    }


    If I try 'add package from disk' using that same folder, it fails with the same error. So what is wrong there? Sorry if it's blindingly obvious...
     
  2. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    481
    Hi @profanicus,

    Your approach looks right to me. I managed to reproduce your issue by forcing a text editor to save the file as UTF-8 with BOM. A BOM is a Byte Order Mark, a special invisible character that can be added at the beginning of text files in some character encodings. You should be able to open the file in a text editor and re-save the file without the BOM. For instance, with Visual Studio code, you could do this by following similar steps as illustrated on this screen capture (but choosing UTF-8 rather than UTF-8 with BOM as the screen capture shows).

    After you save the file without the BOM, you need to commit the file back to your Git repository and push the change, then you should be able to add your package a Git-based dependency as you initially tried.
     
    steril likes this.
  3. profanicus

    profanicus

    Joined:
    Nov 23, 2009
    Posts:
    295
    Thanks @maximeb_unity, that worked! Fantastic! :D

    I originally made the json file in Rider I think. I guess in future I just use notepad, or remember to be more mindful of the file encoding.
     
  4. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    481
    Hey @profanicus, I'm glad that fixed it! To be honest, I was surprised this was a problem, UTF-8 is rarely used with BOM because it's not actually useful for that encoding, so most software save UTF-8 without BOM. Maybe you can configure rider to save files in UTF-8 without BOM?

    Anyway, since this issue could happen to anyone, and anyone could be just as easily confused as to what the issue is, I logged an issue in our bug tracker.
     
  5. profanicus

    profanicus

    Joined:
    Nov 23, 2009
    Posts:
    295
    Thanks @maximeb_unity, I found the relevant setting in Rider. It defaults to "BOM for new UTF-8 files: with BOM under Windows, no BOM otherwise"

    According to the wiki page that setting links to, "Microsoft compilers and interpreters, and many pieces of software on Microsoft Windows such as Notepad treat the BOM as a required magic number".
     
    maximeb_unity likes this.