Extra characters appearing in Flask JavaScript files

Something very random has happened during my development and I can’t for the life of me figure out what has gone wrong or why.

Im building an application in Flask as well as using Docker. Everything has been running fine up until now.

  • Is there any official Docker images for Hadoop?
  • how to run nginx docker container with custom config?
  • Secure Logging drivers with Docker?
  • How to improve Kubernetes security especially inter-Pods?
  • kubernetes hpa cannot get cpu consumption
  • Jenkins trigger on-demand slaves in dockers
  • If I have a JavaScript file that if I modify Flask loads it in with some random characters in it at the end of the file. Or to be more specific,

    If I have a file with

    alert('random text');
    

    And I load the page, the JS will do what you would expect. But then if I add any extra characters to the file,

    alert('randomer text');
    

    I get 2 random characters at the end of the file. Both look like

    If I was to delete characters from the original file, say

    alert('random');
    

    I would get a file with 5 characters missing from the end, so it would look like

    alert('random te
    

    As you can see very bizarre.

    When running Flask locally everything works fine its only when I load everything into Docker that this starts to happen, so I have to assume its something with Docker & Flask/Python.

    The Flask app is started with Gunicorn using default settings, again this has not been an issue in the past.

    My Docker environment is run with

    docker run -t -i -d --name pv -v $PWD:/app/code -p 5000:5000 -p 5555:5555 --link redis_db:redis_db my_image
    

    Again this hasn’t changed in the last month.

    This project is 2+ months old and has been using Docker right from the start with no issues at all.

    The only thing that has happened that I can recall since this error has started is that I had to do a Hard GIT Reset to move back one commit.

    Things I have already tried.

    • Using a different IDE to see if it was doing something strange with encoding.

    • Declaring the JavaScript file as utf-8 when loading it in.

    • Loading in different browsers. Chrome throws “Uncaught SyntaxError: Unexpected
      token ILLEGAL”. And other browsers do similar.

    • Using an older Docker file.

    • Loading the script from a different view template.

    Honestly I’m at a loss. Any help would be appreciated.

    Edit

    To add to this I only now seem to get the error when running the application using Gunicorn.

  • Failed to load OSETUP.dll when installing Excel in Windows docker container
  • Clarify docker-tag and docker-push command
  • Docker daemon fails
  • Installing cassandra on RHEL using docker
  • how to get container name from inside? docker.io
  • Invalid value “zookeeper” for flag -a: valid streams are STDIN, STDOUT and STDERR
  • One Solution collect form web for “Extra characters appearing in Flask JavaScript files”

    You gave the actual byte values as: (239, 191, 189)

    This is U+FFFD, the unicode replacement character:

    http://www.fileformat.info/info/unicode/char/0fffd/index.htm

    My guess is you’ve written a bad character to the file that your editor is incapable of displaying or editing (as you explained by the weird behavior when adding/removing characters at the end of the file).

    I would forget about docker, javascript, IDEs, browsers, etc. and just simply look at the raw bytes of the file and replace them if necessary. You can use python to do this:

    >>> data = open('something.js').read()
    
    # view the last 8 bytes of the file
    >>> data[-8:]
    ...
    
    # if you see the last 3 bytes of the file are the bad 239, 191, 189 sequence,
    # (displayed as "\xef\xbf\xbd" in python), then chop them off:
    >>> open('something.js', 'w').write(data[:-3])
    
    # or if they occur somewhere else,
    # use some other string processing to replace them..
    >>> open('something.js', 'w').write(data.replace("\xef\xbf\xbd", ""))
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.