Glavin001 / atom-beautify

:mega: Help Wanted - Looking for Maintainer: https://github.com/Glavin001/atom-beautify/issues/2572 | :lipstick: Universal beautification package for Atom editor (:warning: Currently migrating to https://github.com/Unibeautify/ and have very limited bandwidth for Atom-Beautify Issues. Thank you for your patience and understanding :heart: )
http://unibeautify.com/
MIT License
1.5k stars 452 forks source link

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

Open Zelman88 opened 6 years ago

Zelman88 commented 6 years ago

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 commented 6 years ago

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

Glavin001 commented 6 years ago

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 commented 6 years ago

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 . ๐Ÿ˜ƒ

Zelman88 commented 6 years ago

@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 commented 6 years ago

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

Glavin001 commented 6 years ago

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 commented 6 years ago

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

Zelman88 commented 6 years ago

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.

Glavin001 commented 6 years ago

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

stale[bot] commented 6 years ago

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.

Intelli commented 5 years ago

Submitted Glavin001's fix since the issue still exists.

arherzf8er commented 5 years ago

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

userlond commented 5 years ago

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

wacki4 commented 5 years ago

Same issue still happening. Please include PR #2313 .

themoother commented 5 years ago

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

socialtect commented 4 years ago

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

xycofryx commented 4 years ago

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

TechRemarker commented 4 years ago

Still seeing this 2018 reported issue. Any progress?

Eroan commented 4 years ago

+1 here, same error not fixed :/

StiftungAusNachlass commented 4 years ago

+1, same error at ftp-remote-edit

SyntaxSnack commented 4 years ago

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.

dsplz commented 4 years ago

+1, same error at ftp-remote-edit

dartrax commented 4 years ago

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

kieker commented 4 years ago

+1, same error on ftp-remote-edit

eartahhj commented 4 years ago

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.

bensti22 commented 3 years ago

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)
marcel-braun commented 3 years ago
EL-S commented 2 years ago

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

socialtect commented 2 years ago

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.

EL-S commented 2 years ago

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

yampi-script commented 2 years ago

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

yampi-script commented 2 years ago

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)

quaSimba commented 2 years ago

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