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

Cannot set property to of [object String] which has only a getter #752

Closed
ilex0208 opened this issue Jul 15, 2017 · 15 comments
Closed

Cannot set property to of [object String] which has only a getter #752

ilex0208 opened this issue Jul 15, 2017 · 15 comments

Comments

@ilex0208
Copy link

@ilex0208 ilex0208 commented Jul 15, 2017

Node version (or tell us if you're using electron or some other framework):6.11

ShellJS version (the most recent version/Github branch you see the bug on):0.7.8

Operating system:windows10

Description of the bug:Cannot set property to of [object String] which has only a getter

Example ShellJS command to reproduce the error: shelljs.rm

gulp.task('clean', () => {
  if (fs.existsSync(resolveCwd('lib'))) {
    shelljs.rm('-rf', resolveCwd('lib'));
  }
});

the error details is:
pipeMethods: [ 'cat','exec','grep', 'head', 'sed','sort','tail', 'to', 'toEnd', 'uniq' ]
cmd cat
cmd exec
cmd grep
cmd head
cmd sed
cmd sort
cmd tail
cmd to
error TypeError: Cannot set property to of [object String] which has only a getter
cmd toEnd
cmd uniq

that is to say, the 'to' method cannot set property!

@ilex0208
Copy link
Author

@ilex0208 ilex0208 commented Jul 15, 2017

When I used the version less than or equal to 0.6.0, it ran successfully

@nfischer
Copy link
Member

@nfischer nfischer commented Jul 15, 2017

Why are you trying to set the .to property? I don't even see where that's happening in your code.

Full stack trace?

@ilex0208
Copy link
Author

@ilex0208 ilex0208 commented Jul 15, 2017

I don't know why. I just used the code above in gulp.
The exception occurs in shelljs/src/common.js at line 151.
Source code:
pipeMethods.forEach(function (cmd) { that[cmd] = shellMethods[cmd].bind(that); });

@nfischer
Copy link
Member

@nfischer nfischer commented Jul 17, 2017

@ilex0208

I have no problems:

// gulpfile.js
var fs = require('fs');
var path = require('path');

var gulp = require('gulp');
var shelljs = require('shelljs');

gulp.task('clean', () => {
  if (fs.existsSync(path.resolve('lib'))) {
    shelljs.rm('-rf', path.resolve('lib'));
  }
});
$ ls -d lib/ &>/dev/null && echo 'The folder exists'
The folder exists
$ gulp clean
[21:57:20] Using gulpfile ~/foobar/gulpfile.js
[21:57:20] Starting 'clean'...
[21:57:20] Finished 'clean' after 6.33 ms
$ ls -d lib/ &>/dev/null || echo 'The folder was deleted'
The folder was deleted

Node v6.7.0
Gulp 3.9.1
ShellJS 0.7.8

@nfischer
Copy link
Member

@nfischer nfischer commented Jul 17, 2017

Can you try to repro with my steps?

@ilex0208
Copy link
Author

@ilex0208 ilex0208 commented Jul 22, 2017

I'm sorry that I can't reply you in time due to too many affairs to deal with recently.

Create a NPM module, directory is as follows

ptool/
  bin/
    s-tool.js
  index.js
  package.json

s-tool.js

    #!/usr/bin/env node

    const program = require('commander');

    program.on('--help', () => {
      console.log('  Usage:');
      console.log('  clean');
    });

    program.parse(process.argv);
    const task = program.args[0];

    if (!task) {
      program.help();
    } else {
      const gulp = require('gulp');
      require('../index');
      gulp.start(task);
    }

index.js

    const path = require('path');
    const fs = require('fs-extra');
    const shelljs = require('shelljs');

    const gulp = require('gulp');

    function resolveCwd() {
      const args = [].slice.call(arguments, 0);
      args.unshift(process.cwd());
      return path.join.apply(path, args);
    };

    // clean
    gulp.task('clean', () => {
      if (fs.existsSync(resolveCwd('lib'))) {
        shelljs.rm('-rf', resolveCwd('lib'));
      }
    });

    // other task ...

package.json

    {
        "name": "s-tool",
        "version": "1.0.0",
        "bin": {
            "s-tool": "./bin/s-tool.js"
        },
        "scripts": {
            "pub": "npm publish"
        },
        "license": "ISC",
        "dependencies": {
            "commander": "~2.9.0",
            "shelljs": "~0.7.8",
            "fs-extra": "^0.26.7",
            "gulp": "^3.9.1"
        }
    }

useage:

$ npm install -g s-tool

$ cd workspace
$ s-tool clean

If the folder lib exists, the error occurs.

However, when I used the version less than or equal to 0.6.0, it ran successfully

@nfischer
Copy link
Member

@nfischer nfischer commented Jul 22, 2017

I still have no repro on Linux.

Please provide a full stack trace from when you reproduce.

Also, please give a repro case using no other modules (only shelljs). I don't know if those other modules are tampering with us.

@nfischer
Copy link
Member

@nfischer nfischer commented Oct 26, 2017

@ilex0208 any update?

@nfischer
Copy link
Member

@nfischer nfischer commented May 10, 2018

Closing due to lack of response. Please ping this thread again if you can provide more information.

@zombieJ
Copy link

@zombieJ zombieJ commented Dec 20, 2018

I found some package modify prototype of String makes pipeMethods add failed failed.

@nfischer
Copy link
Member

@nfischer nfischer commented Dec 20, 2018

@zombieJ we do not modify the prototype unless you import with shelljs/global (which we no longer recommend).

@zombieJ
Copy link

@zombieJ zombieJ commented Dec 20, 2018

hi @nfischer ,
I do not mean shelljs change that. I mean other package change the prototype of String makes that[cmd] = shellMethods[cmd].bind(that) throw Cannot set property to of [object String] which has only a getter error.

@nfischer
Copy link
Member

@nfischer nfischer commented Dec 28, 2018

@zombieJ ah, thanks for explaining. I would argue this is a bug in the other package, since packages should not modify global prototypes.

Regardless, perhaps we can work around this with #651.

@ahungrynoob
Copy link

@ahungrynoob ahungrynoob commented Mar 29, 2019

hi @nfischer ,
I do not mean shelljs change that. I mean other package change the prototype of String makes that[cmd] = shellMethods[cmd].bind(that) throw Cannot set property to of [object String] which has only a getter error.

The package you mean is colorful, right? https://github.com/lepture/colorful

@nfischer
Copy link
Member

@nfischer nfischer commented Apr 22, 2019

I don't believe there's anything for us to address here, kindly raise this issue with the colorful module.

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

No branches or pull requests

4 participants