deadc0de6 / dotdrop

Save your dotfiles once, deploy them everywhere
https://dotdrop.readthedocs.io
GNU General Public License v3.0
1.77k stars 105 forks source link

[bug] `application/javascript` files treated as binary file #396

Closed acourtneybrown closed 1 year ago

acourtneybrown commented 1 year ago

Dotdrop version (and git commit if run from source): v1.13.0 Using dotdrop: from Homebrew

Describe the bug

The _handle_file method in templategen.py treats files with mime-type application/javascript as a binary file, thus only copying it directly vs. running the file through the templating engine. I'm uncertain if this recent bug is due to a change in dotdrop or the underlying magic library, as my setup had previously worked with version of dotdrop prior to 1.13.0_1 (from Homebrew), but now I see the same incorrect behavior with 1.13.0 & 1.12.13

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a dotfile with media-type application/javascript which also has template directives & has the template property either true or inherited true.
  2. Run dotdrop install on the file
  3. Notice that the resulting file still contains the template directives
  4. See error

Expected behavior

The file should be run through the templating engine & the template directives should not appear in the resulting output file.

Additional information

From acourtneybrown/dotfiles:

  f_finicky.js:
    src: finicky.js
    dst: ~/.finicky.js

which uses finicky.js (from the same repo) containing template directives like:

    {%@@ if personal @@%}
    {
      match: [
        finicky.matchHostnames([
          "govzw.com",

          /nytimes.com/,
          /wsj.com/,
        ]),
        /^https:\/\/github.com\/{{@@ github_account @@}}\/.*$/,
      ],
      browser: "Safari"
    },
    {
      match: ({ opener }) => {
        // finicky.log(opener.bundleId);
        return opener.bundleId
          && (opener.bundleId === "com.agilebits.onepassword7" || opener.bundleId === "com.1password.1password")
      },
      browser: "Safari"
    },
    {%@@ endif @@%}

Dotdrop's execution with the debug logs (--verbose)

$ dotdrop install --verbose f_finicky.js
...

contains output showing the misidentification of the application/javascript file as not text:

[DEBUG][dotdrop.templategen._handle_file] using "magic" for filetype identification
[DEBUG][dotdrop.templategen._handle_file] filetype "/Users/adambrown/.dotfiles/dotfiles/finicky.js": application/javascript
[DEBUG][dotdrop.templategen._handle_file] is text "/Users/adambrown/.dotfiles/dotfiles/finicky.js": False
deadc0de6 commented 1 year ago

Thanks for reporting this. Dotdrop will handle files as text as long as their mime type contains text, json or empty.

I tried running the finicky.js file through file and python-magic with different outputs depending on OS/versions. Most of time text/plain is returned but I also got application/javascript.

Even though application/javascript is obsolete (and thus shouldn't happen), I will add an exception for the following mime types so that those files will get handle as text:

deadc0de6 commented 1 year ago

The above commit should fix it for you. Could you test dotdrop from the master branch and let me know if that does it for you?

acourtneybrown commented 1 year ago

Yeah, the change above ensures that dotdrop processes the Javascript file through the template engine. Thanks for the quick help.

deadc0de6 commented 1 year ago

@acourtneybrown awesome!

Thanks for your help. I'll release a new version on pypi ASAP and will ping you when done.

deadc0de6 commented 1 year ago

@acourtneybrown new version 1.13.2 is available on pypi.