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

Created on 16 Feb 2018  路  26Comments  路  Source: Glavin001/atom-beautify

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?

bug pr-available windows

Most helpful comment

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

All 26 comments

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

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

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

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

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

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!

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.

Submitted Glavin001's fix since the issue still exists.

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

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

Same issue still happening. Please include PR #2313 .

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

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

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

Still seeing this 2018 reported issue. Any progress?

+1 here, same error not fixed :/

+1, same error at ftp-remote-edit

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.

+1, same error at ftp-remote-edit

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

+1, same error on ftp-remote-edit

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.

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)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

physcocode picture physcocode  路  3Comments

kuzyn picture kuzyn  路  3Comments

callmeyesh picture callmeyesh  路  3Comments

CorentinAndre picture CorentinAndre  路  4Comments

jcollum picture jcollum  路  4Comments