isomorphic-git/isomorphic-git

"TypeError: Cannot read property '0' of undefined" during clone using Parcel

raoulmillais opened this issue · 8 comments

I am following the browser example and I get an error on this line in GitCommit

Uncaught (in promise) TypeError: Cannot read property '0' of undefined

If I npm link a clone of the isomorphic-git repo and log out the headers variable it is an array of strings. Have I stumbled on a bug?

These are the headers:

["tree f796b167fe72560d513ddab9321141dd539d6d47", "parent 0793e27", "author William Hilton wmhilton@gmail.com 1542241548 -0500", "committer GitHub noreply@github.com 1542241548 -0500", "gpgsig -----BEGIN PGP SIGNATURE-----", " ", " wsBcBAABCAAQBQJb7L0MCRBK7hj4Ov3rIwAAdHIIAKS41E6rLOz3acKlh5ZJ25VV", " JHrD5yPb/I+bjQVz9kR2+nVTfaDaciHfu+3mVHgM90b8hstzY36cEq5RaaMrrwzH", " Z1XeFG81kezc3tG/TWIwbE2xQ8S+hwO/rBZJ7814PuM10/CpUSx7qfsgCjRXTKJi", " iTkDuv+yrlHeg+Ixja3CrrAtlBlGCJB0H8RndOr7QcTmtSNZBkyNv39egaR7WjNM", " 5JbYDEAcX1BnuthiN0IQhBtXsert25DoAqb4C/GJv9GeAYGCo7/5ReinvyTy28Gp", " gO1uDojhOkvnuOKztMwcn/HgfUGKDB/eFnRccRehXi9LcXswqc6U3x8R0C1QLxg=", " =TMTS", " -----END PGP SIGNATURE-----", " "]

adding a console.log statement inside the loop over the normalised headers and the problem goes away ¯\_(ツ)_/¯

Hmm... that's interesting. I haven't seen that error before! I don't have enough information to reproduce the error yet. What example are you running exactly? And what browser were you using?

Thanks for getting back to me @wmhilton. Sorry I was tired last night after spending half a day trying to get this working and failed to give you all the information you need to help! I should have mentioned that I am using parcel.js (with babel configured for babel-preset-env) to bundle an index file which contains almost exactly what is in the browser tutorial. I have created a minimal gist that reproduces the issue

npm install && npm start if you clone the gist

I am running:

  • MacOSX Mojave 10.14.1
  • Latest chrome (Version 70.0.3538.110 (Official Build) (64-bit)

Awesome! I'll investigate this today. I have a sneaking suspicion (given that adding a console.log "fixed" it) that it may be a bug in the transpiler (parcel, or babel, or uglify) but if I can reproduce it and figure out how to fix it without adding a console.log that'll be ideal!

This is officially really weird. AFAICT it's probably a bug in parcel related to the infamous babel "regenerator-runtime". It seems to be transpiling my simple "for (h of hs)" loop into something else. I just can't figure out what.

Aha. Firefox shows the actual error and the transpiled JS that parcel's created:

Unhandled promise rejection TypeError: "_e10 is undefined"
        }, {
          key: "parseHeaders",
          value: function parseHeaders() {
            var t = c.justHeaders(this._commit).split("\n"),
                e = [];
            var _iteratorNormalCompletion13 = true;
            var _didIteratorError13 = false;
            var _iteratorError13 = undefined;

            try {
              for (var _iterator13 = t[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
                var _r7 = _step13.value;
                " " === _r7[0] ? e[e.length - 1] += "\n" + _r7.slice(1) : e.push(_r7);
              }
            } catch (err) {
              _didIteratorError13 = true;
              _iteratorError13 = err;
            } finally {
              try {
                if (!_iteratorNormalCompletion13 && _iterator13.return != null) {
                  _iterator13.return();
                }
              } finally {
                if (_didIteratorError13) {
                  throw _iteratorError13;
                }
              }
            }

            var r = {
              parent: []
            };

            for (var _i2 = 0; _i2 < e.length; _i2++) {
  /*HERE*/    var _t12 = _e10[_i2];

              var _e10 = _t12.slice(0, _t12.indexOf(" ")),
                  _n4 = _t12.slice(_t12.indexOf(" ") + 1);

              Array.isArray(r[_e10]) ? r[_e10].push(_n4) : r[_e10] = _n4;
            }

            return r.author && (r.author = Object(u.a)(r.author)), r.committer && (r.committer = Object(u.a)(r.committer)), r;
          }
        }, {

Hmm.

Well, you might not like this answer but I found one potential "solution". Problem goes away if you add this to your package.json so parcel doesn't transpile so aggressively:

  "browserslist": [
    "last 2 Chrome versions"
  ]

With some experimenting, I found even this works if you want to be more conservative:

  "browserslist": [
    "> 3%"
  ]

Parcel defaults to > 0.25% "(Meaning, support every browser that has 0.25% or more of the total amount of active web users)" which is probably an unrealistic support goal for an application using isomorphic-git anyway.

I know this is not super satisfying - I too am now curious what I did that causes Babel (or something) to lose track of the scope of hs - it looks to me like _e10 should in fact be e. But let me know if this solution works for you and I can close this ticket, or if we need to find another solution.

I'm renaming the issue to make it more Googleable. I doubt you'll be the last person to run into this weird error.

Awesome. Thanks for your help @wmhilton Your solution works well and is fine for my needs.