foreshadow / atom-python-run

A simple atom package. Press one key to run your python code in atom.
https://atom.io/packages/atom-python-run
MIT License
44 stars 32 forks source link

Press F5 to run code and get BufferExceptionTypeError then can not run the code successfully #67

Closed FIRECONTER closed 6 years ago

FIRECONTER commented 6 years ago

I use the latest version. But I meet such error:

atom-python-run: BufferExceptionTypeError: Cannot read property 'then' of undefined: You failed to create the file before attempting to execute it. You must create the file first and then execute it after it has been created.

I have checked that the error comes from the function saveAndRun in atom-python-run.js file

atom.workspace.getActiveTextEditor().save().then(() => { run({ 'command': atom.config.get(atom-python-run.${key}Command), 'pause': atom.config.get(atom-python-run.${key}Pause), }); });

FIRECONTER commented 6 years ago

if I choose an earlier version such as V0.91, it works well

ghost commented 6 years ago

Please describe steps to reproduce this error in detail. Step by step, from beginning to end, how do you trigger the error?

neederhow commented 6 years ago

I meet this error too. I just open a .py file and press f5\f6 and the error message popup

atom-python-run: BufferExceptionTypeError: Cannot read property 'then' of undefined: You failed to create the file before attempting to execute it. You must create the file first and then execute it after it has been created.

ghost commented 6 years ago

For any users/developers experiencing similar issues posting a comment; please consider the following questions:

To verify what @FIRECONTER originally stated, this block causes this issue.

atom.workspace.getActiveTextEditor().save().then(() => {
    run({
        'command': atom.config.get(`atom-python-run.${key}Command`),
        'pause': atom.config.get(`atom-python-run.${key}Pause`),
    });
});

From what I can tell, this is because save().then() acts as a Promise object which expects something to be returned.

The error TypeError: Cannot read property 'then' of undefined reflects that it expects the function saveAndRun() to return something signifying that the promise has been resolved.

Because nothing is returned, the try...catch statement catches this exception. The exception is mangled by the text that is added to error using atom.notifications.addError().

For a whole picture, this is what it looks like now.

function saveAndRun(key) {
    try {
        console.log(`${key} pressed`)
        atom.workspace.getActiveTextEditor().save().then(() => {
            run({
                'command': atom.config.get(`atom-python-run.${key}Command`),
                'pause': atom.config.get(`atom-python-run.${key}Pause`),
            });
        });
    } catch(error) {
        atom.notifications.addError(`atom-python-run: \`BufferException${error}\`: You failed to create the file before attempting to execute it. You must create the file first and then execute it after it has been created.`);
        return;
    }
}

In theory, since we expect a Promise to be returned, we could just say return save.then(run()) and clean up the error message.

Because I don't know exactly what causes the issue, I can't be sure. I haven't been able to reproduce this error at all. I need in depth, step by step, information so I can reproduce it. Although, if someone figured that out, I'm sure they would be able to surmise a fix for it.

For now, It could be changed to this instead.

function saveAndRun(key) {
    try {
        console.log(`${key} pressed`);
        // save() returns a promise
        let save = atom.workspace.getActiveTextEditor().save();
        // Atom expects that promise to be resolved or rejected
        return save.then(() => {
            run({
                'command': atom.config.get(`atom-python-run.${key}Command`),
                'pause': atom.config.get(`atom-python-run.${key}Pause`),
            });
        });
    } catch(error) {
        // the promise failed and the exception was caught
        console.log(`atom-python-run: saveAndRun: ${error}`);
        atom.notifications.addError(`atom-python-run: \`${error}\`.`);
        // nothing to do, so we return undefined here
        return;
    }
}

Again, I can't guarantee that this will work. I would need someone to test this, see if it works, and then report the results here.

ghost commented 6 years ago

I pushed a patch to the repo for this (and another related) issue. You can test the patch by uninstalling atom-python-run, cloning the repo using git, and then linking the cloned repo to atom.

$ apm uninstall atom-python-run
$ git clone https://github.com/foreshadow/atom-python-run.git
$ cd atom-python-run
$ apm link .
$ atom

You can test the patch, see if you can reproduce the error, and report back here. You can tweak the patch to fix it as well if it doesn't create the desired side-effect.

To undo the dev repo, you can do the same in reverse.

$ cd atom-python-run
$ apm unlink .
$ apm install atom-python-run
$ cd ..
$ rm -rf atom-python-run

You can find additional information in the wiki.

Look here for help on how to access and use the Console Log.

You can see here that the Console Log and cp Log are different logging mechanisms.

Feel free to express if you're confused and/or need additional clarification. If you've found a fix, please post it here, make a pull request, or create a fork with an implemented fix.

foreshadow commented 6 years ago

Fixed in v0.9.6