ember-angle-brackets-codemod icon indicating copy to clipboard operation
ember-angle-brackets-codemod copied to clipboard

Can only run the codemod on a file once

Open MarcoKramer opened this issue 4 years ago • 12 comments

When I run the codemod on a file the conversion to the angle brackets syntax works fine. But when I discard the changes and try to run the codemod again I get this error:

/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/core/lib/transformation/index.js:79
    throw e;
    ^

TypeError: /usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/safe-buffer/index.js: Cannot read property 'from' of undefined
    at Converter.toBase64 (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/convert-source-map/index.js:61:28)
    at Converter.toComment (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/convert-source-map/index.js:65:21)
    at generateCode (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/core/lib/transformation/file/generate.js:78:76)
    at runSync (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/core/lib/transformation/index.js:68:33)
    at transformSync (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/core/lib/transform.js:43:38)
    at Object.transform (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/core/lib/transform.js:22:38)
    at compile (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/register/lib/node.js:75:20)
    at compileHook (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/@babel/register/lib/node.js:104:12)
    at Module._compile (/usr/lib/node_modules/ember-angle-brackets-codemod/node_modules/pirates/lib/index.js:93:29)
    at Module._extensions..js (internal/modules/cjs/loader.js:973:10) {
  code: 'BABEL_GENERATE_ERROR'
}

Do you know what's causing this?

(I'm running WSL2 with Ubuntu 18.04 on Windows 10 build 19023)

MarcoKramer avatar Nov 20 '19 14:11 MarcoKramer

Yikes, this is pretty odd. I've not run into this myself, do you know if it is specific to your project or does it happen even on other projects?

rwjblue avatar Nov 20 '19 23:11 rwjblue

I investigated a bit more and it only happens if I install the codemod globally. Guess I just should not do that then. 😁

MarcoKramer avatar Nov 22 '19 12:11 MarcoKramer

That is good info though, should help us reproduce...

rwjblue avatar Nov 22 '19 15:11 rwjblue

might be related to : https://github.com/ember-codemods/ember-angle-brackets-codemod/issues/104#issuecomment-552417508

basz avatar Dec 04 '19 14:12 basz

Same issue. Different codemod - ember-native-class-codemod, but the same problem: Fails when codemod is installed globally.

❯ ember-native-class-codemod http://localhost:4200 ./addon/components/interval-picker.js
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/core/lib/transformation/index.js:79
    throw e;
    ^

TypeError: ${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/safe-buffer/index.js: Cannot read property 'from' of undefined
    at Converter.toBase64 (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/convert-source-map/index.js:62:28)
    at Converter.toComment (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/convert-source-map/index.js:66:21)
    at generateCode (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/core/lib/transformation/file/generate.js:78:76)
    at runSync (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/core/lib/transformation/index.js:68:33)
    at transformSync (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/core/lib/transform.js:43:38)
    at Object.transform (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/core/lib/transform.js:22:38)
    at compile (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/register/lib/node.js:75:20)
    at compileHook (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/@babel/register/lib/node.js:104:12)
    at Module._compile (${HOME}/.volta/tools/image/packages/ember-native-class-codemod/1.1.0/node_modules/pirates/lib/index.js:93:29)
    at Module._extensions..js (internal/modules/cjs/loader.js:1159:10) {
  code: 'BABEL_GENERATE_ERROR'
}
All done.
Results:
0 errors
0 unmodified
0 skipped
0 ok
Time elapsed: 2.336seconds

While running via npx seems to work:

❯ npx ember-native-class-codemod http://localhost:4200 ./addon/components/interval-picker.js
Downloading Chromium r686378 - 110.2 Mb [====================] 100% 0.0s
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
All done.
Results:
0 errors
1 unmodified
0 skipped
0 ok
Time elapsed: 7.327seconds

My env:

❯ volta --version
0.6.3

❯ node --version
v10.18.0

❯ npm --version
6.13.4

Source code of given component: https://gist.github.com/MichalBryxi/ba67cb77bd94c6b973f62676152aa175

MichalBryxi avatar Dec 28 '19 10:12 MichalBryxi

I've been running into the same thing after it has been recommended to me that I should install the codemods locally in order to not have to download and install chromium each time I run a codemod (which is very time consuming). It seems like that the error message changes a little when installing via yarn or npm (I was trying quite a few things as I first suspected a local issue) I have also noticed this issue with other codemods like native-class or no-implicit-this. Removing the globally installed codemod fixes the issues, but brings up the chromium download issue again.

st-h avatar Dec 30 '19 13:12 st-h

Same thing here with the native-class codemod.

UPD I've managed to pin it down a bit. So the issue happens when the jscodeshift makes the require('@babel/register')({...}) call. It works ok for the 1st time but fails on consecutive runs.

In theory we can omit this @babel/register call by passing the --no-babel flag in the codemod-cli, but that's hardly a proper fix. What would you say @rwjblue? Anyways I'll try to drill it a bit more and see if that's anyhow could be a purely jscodeshift issue.

stukalin avatar Jan 22 '20 11:01 stukalin

Found a very weird solution: the problem disappeared when I installed the jscodeshift globally. Note, that this didn't work under Windows :-(

npm i -g jscodeshift.

I guess I'd stop digging here until I face it again. BTW, found the related issue on babel itself https://github.com/babel/babel/issues/9657

stukalin avatar Jan 23 '20 06:01 stukalin

I encountered the same problem and did a bit of debugging

I was able to monkey patch it somehow: I cloned the codemod repo, installed dependencies and then manually altered index.js in convert-source-map dependency:

before (https://github.com/thlorenz/convert-source-map/blob/9e9a7a9b652c30878c9c9aa591d861a9fdf61a7e/index.js#L61)

Converter.prototype.toBase64 = function () {
  var json = this.toJSON();
  return SafeBuffer.Buffer.from(json, 'utf8').toString('base64');
};

after

Converter.prototype.toBase64 = function () {
  var json = this.toJSON();
  return SafeBuffer.from(json, 'utf8').toString('base64'); //<--------------------- this line is different
};

After this change, everything works fine...

IgorKvasn avatar Feb 23 '21 09:02 IgorKvasn

@IgorKvasn Thank you for your suggestion. In order to get this addon working locally, I had to use Buffer instead of SafeBuffer. So just this one change:

return Buffer.from(json, 'utf8').toString('base64'); //<--------------------- this line is different and everything now works!

kcarmonamurphy avatar Mar 24 '21 18:03 kcarmonamurphy

This bug is unbelievably haunted.

From safe-buffer/index.js:

if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
  module.exports = buffer
} else {
  // Copy properties from require('buffer')
  copyProps(buffer, exports)
  exports.Buffer = SafeBuffer
}

The problem seems to start from the first branch, where the native Buffer API is good to go so it just re-exports it.

  • If you console.log(module.exports) right after that assignment, it'll display the real contents of require('buffer'), like you'd expect, including the .Buffer prop in question.
  • But if you go to the call site in convert-source-map/index.js and console.log(SafeBuffer), it logs an empty object?!

👻

This behaved the same way in several old node versions; I stopped testing when I got to the 10.x LTS. I wonder if Babel is doing something really weird with things that do require('buffer')? The node docs do say "The Buffer class is within the global scope, making it unlikely that one would need to ever use require('buffer').Buffer."

As for why the codemod works right once and then never again, ??!?!

I hate this.

Related art: https://github.com/react-static/react-static/issues/875

nfagerlund avatar Jun 22 '21 00:06 nfagerlund

I suspect the issue here at least partially relates to a failed parse due to invalid syntax. Once the failed parse has occurred I think something happens to the require cache that screws the other runs.

runspired avatar Jan 10 '22 19:01 runspired