atom-beautify icon indicating copy to clipboard operation
atom-beautify copied to clipboard

Cannot read property 'split' of undefined when using php-cs-fixer or phpcbf with atom beautify

Open Zelman88 opened this issue 7 years ago • 33 comments

On all my computers I've got a problem with beautifying php files with atom beautify. Both php-cs-fixer and phpcbf give me an error:

Cannot read property 'split' of undefined
Hide Stack Trace
TypeError: Cannot read property 'split' of undefined
    at PHPCSFixer.module.exports.Beautifier.findFile (file:///C:/Users/adamz/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:138:24)
    at PHPCSFixer.module.exports.PHPCSFixer.beautify (file:///C:/Users/adamz/.atom/packages/atom-beautify/src/beautifiers/php-cs-fixer.coffee:65:39)
    at file:///C:/Users/adamz/.atom/packages/atom-beautify/src/beautifiers/index.coffee:355:28
    at tryCatcher (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:582:21)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at Promise._resolveCallback (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:432:57)
    at Promise._settlePromiseFromHandler (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:524:17)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at PromiseArray._resolve (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise_array.js:126:19)
    at PromiseArray._promiseFulfilled (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise_array.js:144:14)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:574:26)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:582:21)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:582:21)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at Promise._resolveCallback (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:432:57)
    at Promise._settlePromiseFromHandler (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:524:17)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at Promise._resolveCallback (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:432:57)
    at Promise._settlePromiseFromHandler (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:524:17)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:638:18)
    at PromiseArray._resolve (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise_array.js:126:19)
    at PromiseArray._promiseFulfilled (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise_array.js:144:14)
    at Promise._settlePromise (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:574:26)
    at Promise._settlePromise0 (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:143:10)
    at Async.drainQueues (C:\Users\adamz\.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:17:14)
    at process._tickCallback (internal/process/next_tick.js:103:7)

Searched this problem on google, but no luck. Anybody had similiar problem?

Zelman88 avatar Feb 16 '18 14:02 Zelman88

My debug file: https://gist.github.com/Zelman88/b181bad08649ccd4ca43baea63c693b0

Zelman88 avatar Feb 16 '18 14:02 Zelman88

Thank you for your debug file.

There appears to be a bug when calling https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/beautifier.coffee#L138

  ###
  Find file
  ###
  findFile: (startDir, fileNames) ->
    throw new Error "Specify file names to find." unless arguments.length
    unless fileNames instanceof Array
      fileNames = [fileNames]
    startDir = startDir.split(path.sep)
    while startDir.length
      currentDir = startDir.join(path.sep)
      for fileName in fileNames
        filePath = path.join(currentDir, fileName)
        try
          fs.accessSync(filePath, fs.R_OK)
          return filePath
      startDir.pop()
    return null

It is likely one or both of these lines: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L61-L65

Glavin001 avatar Feb 16 '18 15:02 Glavin001

Investigating your logs closer:

2018-02-16T14:19:22.108Z - error: [beautifiers\index.coffee]  TypeError: Cannot read property 'split' of undefined
    at PHPCSFixer.module.exports.Beautifier.findFile (file:///C:/Users/adamz/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:138:24)
    at PHPCSFixer.module.exports.PHPCSFixer.beautify (file:///C:/Users/adamz/.atom/packages/atom-beautify/src/beautifiers/php-cs-fixer.coffee:65:39)
    at file:///C:/Users/adamz/.atom/packages/atom-beautify/src/beautifiers/index.coffee:355:28

It is coming from https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L65

      options.cs_fixer_config_file = @findFile(atom.project.getPaths()[0], configFiles)

Specifically

atom.project.getPaths()[0]

Have you opened a project in Atom? I wonder if a workaround is to ensure you have a project folder opened.

Pull Requests welcome to fix this! Please let me know if you need any help making a Pull Request to Atom-Beautify, @Zelman88 . 😃

Glavin001 avatar Feb 16 '18 15:02 Glavin001

@Glavin001 I'm using https://atom.io/packages/ftp-remote-edit and not opening any project. Indeed when I open project locally in Atom beautify with php-cs-fixer works both locally and remote. Is there any possibility that it would work without opening project locally? I prefer to work directly on my servers with ftp connection.

Zelman88 avatar Feb 16 '18 16:02 Zelman88

also other beautifiers like HTML or CSS don't have this problem - they work with ftp-remote-edit without opening project locally

Zelman88 avatar Feb 16 '18 16:02 Zelman88

It is distinctly a bug with PHP-CS-Fixer beautifier integration in Atom-Beautify.

To fix this I recommend making a Pull Request fixing the code here: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L63-L65

    # Try again to find a config file in the project root
    if not options.cs_fixer_config_file
      options.cs_fixer_config_file = @findFile(atom.project.getPaths()[0], configFiles)

It could be something like changing to:

    if not options.cs_fixer_config_file and atom.project.getPaths()[0]

However, you will need to test this fix for yourself.

cc @szeck87

Glavin001 avatar Feb 16 '18 17:02 Glavin001

Same goes for PHPCBF: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/phpcbf.coffee#L45

Glavin001 avatar Feb 16 '18 17:02 Glavin001

Changed to this: ``if not options.cs_fixer_config_file and atom.project.getPaths()[0]"

and now it works 👍

Maybe someone more familiar with github could make a Pull Request? I don't have any experience with this.

Zelman88 avatar Feb 16 '18 17:02 Zelman88

@Zelman88 : Awesome to hear it is working now for you!

Maybe someone more familiar with github could make a Pull Request? I don't have any experience with this.

I would love to help you become a contributor to this project, especially since you discovered this bug 😃 .

You can use GitHub's file editing interface directly! No need to clone the Git repository. Go to these links:

  • PHP-CS-Fixer: https://github.com/Glavin001/atom-beautify/edit/master/src/beautifiers/php-cs-fixer.coffee
  • PHPCBF: https://github.com/Glavin001/atom-beautify/edit/master/src/beautifiers/phpcbf.coffee

Make the changes which you found worked for you.

Here are some other quick reads to help you: https://help.github.com/articles/creating-a-pull-request/

It would be wonderful to help you get started contributing on GitHub! Let @szeck87 or I know if you need a hand with anything -- it should not take long 👍 . Thanks in advance!

Glavin001 avatar Feb 16 '18 17:02 Glavin001

This issue has been automatically marked as stale because it has not had recent activity. If this is still an issue, please add a comment. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 17 '18 18:04 stale[bot]

Submitted Glavin001's fix since the issue still exists.

Intelli avatar Dec 11 '18 05:12 Intelli

Still facing same issues on fresh Atom with ftp-remote-edit without a local project.

arherzf8er avatar Feb 14 '19 11:02 arherzf8er

Same error, any php source. Why PR https://github.com/Glavin001/atom-beautify/pull/2313 is ignored?

userlond avatar May 13 '19 03:05 userlond

Same issue still happening. Please include PR #2313 .

wacki4 avatar Jun 10 '19 13:06 wacki4

Ditto... ftp-remote-edit coughing up the same error still

themoother avatar Jul 17 '19 23:07 themoother

I am getting the same 'split' error. I tired a new pull and the line updates mentioned above and not luck. Any suggestions?

socialtect avatar Sep 25 '19 16:09 socialtect

Same issue here, Tried Glavin001 fix (https://github.com/Glavin001/atom-beautify/issues/2044#issuecomment-366295528) to no avail.

xycofryx avatar Oct 07 '19 06:10 xycofryx

Still seeing this 2018 reported issue. Any progress?

TechRemarker avatar Feb 17 '20 14:02 TechRemarker

+1 here, same error not fixed :/

Eroan avatar Feb 25 '20 13:02 Eroan

+1, same error at ftp-remote-edit

StiftungAusNachlass avatar Feb 25 '20 18:02 StiftungAusNachlass

Same error using Atom Beautify on OpenSUSE Tumbleweed with PHP 7.4.3. Not sure what I can do to set-up automatic PHP formatting in Atom.

cortexorca avatar Mar 08 '20 06:03 cortexorca

+1, same error at ftp-remote-edit

dsplz avatar Mar 28 '20 09:03 dsplz

Same error here. Just checking out if Atom would help me in php development. Seems not so.

dartrax avatar May 11 '20 11:05 dartrax

+1, same error on ftp-remote-edit

kieker avatar Jul 14 '20 13:07 kieker

I have been having this issue for a long time, and still have it in atom 1.4.9 on Debian when not opening a project or project folder.

I fixed it as reported above https://github.com/Glavin001/atom-beautify/issues/2044#issuecomment-366295528

Looking at the stack trace I find the same rows as other users:

Cannot read property 'split' of undefined TypeError: Cannot read property 'split' of undefined at PHPCSFixer.module.exports.Beautifier.findFile (/home/user_name/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:138:25) at PHPCSFixer.module.exports.PHPCSFixer.beautify (/home/user_name/.atom/packages/atom-beautify/src/beautifiers/php-cs-fixer.coffee:65:39) [...]

so the problem arises when php-cs-fixer.coffee (line 65) calls the function findFile (located in beautifier.coffee, line 134). This function tries to do the following at line 138:

startDir = startDir.split(path.sep)

so since startDir results as "undefined", I can guess that the row at line 65 in php-cs-fixer.coffee is passing an empty first argument here:

options.cs_fixer_config_file = @findFile(atom.project.getPaths()[0], configFiles)

At this point I tried the fix suggested above and it seems to be working for me:

in /home/user_name/.atom/packages/atom-beautify/src/beautifiers/php-cs-fixer.coffe at line 64:

change this row: if not options.cs_fixer_config_file

into this: if not options.cs_fixer_config_file and atom.project.getPaths()[0]

then close and reopen atom if needed.

this seems to be working, I can guess that the reason is that if there is not a cs_fixer_config_file if you do not open a project, but atom can get a path (i didn't check how that works), so the "findFile" function will have a value to give to "startDir" and the "split" function won't fail anymore.

The problem is probably somewhere else to be honest, I don't think this fix should be "permanent", maybe it also depends on local php/atom configurations. As mentioned this does not happen when opening a project in atom.

eartahhj avatar Jul 23 '20 10:07 eartahhj

i change this line at two places if not options.cs_fixer_config_file

# Find a config file in the working directory if a custom one was not provided
if not options.cs_fixer_config_file and atom.project.getPaths()[0]
options.cs_fixer_config_file = if context? and context.filePath? then @findFile(path.dirname(context.filePath), configFiles

# Try again to find a config file in the project root
if not options.cs_fixer_config_file and atom.project.getPaths()[0] 
  options.cs_fixer_config_file = @findFile(atom.project.getPaths()[0], configFiles)

bensti22 avatar Jan 06 '21 20:01 bensti22

  • 1 Same error while editing remote files with FileZilla.

marcel-braun avatar Apr 15 '21 15:04 marcel-braun

I'm still getting this error with remote ftp editing too

EL-S avatar Aug 03 '21 23:08 EL-S

I gave up trying to use this.

On Aug 3, 2021, at 4:48 PM, EL-S @.***> wrote:

 I'm still getting this error with remote ftp editing too

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

socialtect avatar Aug 03 '21 23:08 socialtect

i change this line at two places if not options.cs_fixer_config_file

# Find a config file in the working directory if a custom one was not provided
if not options.cs_fixer_config_file and atom.project.getPaths()[0]
options.cs_fixer_config_file = if context? and context.filePath? then @findFile(path.dirname(context.filePath), configFiles

# Try again to find a config file in the project root
if not options.cs_fixer_config_file and atom.project.getPaths()[0] 
  options.cs_fixer_config_file = @findFile(atom.project.getPaths()[0], configFiles)

This fix worked for me

EL-S avatar Aug 04 '21 00:08 EL-S

It would be good to put the corrected file because I still can't use it in php

yampi-script avatar Sep 21 '21 22:09 yampi-script

TypeError: Cannot read property 'split' of undefined at PHPCSFixer.module.exports.Beautifier.findFile (file:///C:/Users/Rodrigo Glz/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:138:25) at PHPCSFixer.module.exports.PHPCSFixer.beautify (file:///C:/Users/Rodrigo Glz/.atom/packages/atom-beautify/src/beautifiers/php-cs-fixer.coffee:65:39) at file:///C:/Users/Rodrigo Glz/.atom/packages/atom-beautify/src/beautifiers/index.coffee:363:28 at tryCatcher (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\util.js:16:23) at Promise._settlePromiseFromHandler (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:547:31) at Promise._settlePromise (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:604:18) at Promise._settlePromiseCtx (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\promise.js:641:10) at _drainQueueStep (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:97:12) at _drainQueue (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:86:9) at Async._drainQueues (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:102:5) at Async.drainQueues (C:\Users\Rodrigo Glz.atom\packages\atom-beautify\node_modules\bluebird\js\release\async.js:15:14)

yampi-script avatar Sep 21 '21 23:09 yampi-script

@Glavin001, I see two pull requests pending since some time now. One implements the fix in the way that was discussed here. Is there a chance that this bug could be fixed with a coming update?

quaSimba avatar Jun 22 '22 16:06 quaSimba