Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can only run the codemod on a file once #195

Open
MarcoKramer opened this issue Nov 20, 2019 · 12 comments
Open

Can only run the codemod on a file once #195

MarcoKramer opened this issue Nov 20, 2019 · 12 comments

Comments

@MarcoKramer
Copy link

MarcoKramer commented Nov 20, 2019

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)

Copy link
Member

rwjblue commented Nov 20, 2019

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?

@MarcoKramer
Copy link
Author

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

Copy link
Member

rwjblue commented Nov 22, 2019

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

@basz
Copy link

basz commented Dec 4, 2019

might be related to : #104 (comment)

@MichalBryxi
Copy link

MichalBryxi commented Dec 28, 2019

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

@st-h
Copy link

st-h commented Dec 30, 2019

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.

@stukalin
Copy link

stukalin commented Jan 22, 2020

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
Copy link

stukalin commented Jan 23, 2020

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 babel/babel#9657

@IgorKvasn
Copy link

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...

@kcarmonamurphy
Copy link
Contributor

@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!

@nfagerlund
Copy link

nfagerlund commented Jun 22, 2021

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: react-static/react-static#875

@runspired
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants