use-private-action icon indicating copy to clipboard operation
use-private-action copied to clipboard

Finally statement executed before awaited async function call is done

Open slabruyere opened this issue 5 years ago • 2 comments

Hi,

I'm having a weird issue with a Node.js action using [email protected]. In order to troubleshoot this issue, I imported use-private-action so I am using it as a local action. I have only added a line before main.ts L182 in the finally statement to have a log console.log('deleteFolder') before deleteFolder(tempFolderName);.

My action is in index.js, which looks like this:

const core = require('@actions/core');
const exec = require('@actions/exec');

async function getFolderName() {
  const forbiddenFolders = [
    '.',
    './.github',
    './.git',
    './test',
    './src',
    './dist',
    './node_modules'
  ];

  let folders = [];

  try {
    await exec.exec('find',
                    ['.', '-maxdepth', '1', '-type', 'd'], {
                      listeners: {
                        stdline: (data) => {
                          folders.push(data);
                        }
                      }
                    });
  } catch (err) {
    core.setFailed('Could not retrieve temp directory name.');
  }
  
  const possibleFolders =  folders.filter(folder => {
    return forbiddenFolders.indexOf(folder) < 0;
  });

  return possibleFolders[0];
}

async function run() {

  const tempFolderName = await getFolderName();

  try {
    await exec.exec('python', [`${tempFolderName}/my-script.py`]);
  } catch (error) {
    core.setFailed(error.message);
  }

}

module.exports = run;

run()

Commented extract from the logs:

Run ./bagbyte/use-private-action # notice I'm using a local version
  with:
    private-action: xx/yy/zz@master
    private-action-token: ***
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.7.6/x64
git clone ***github.com/xx/yy.git tm29hikcp7o
Cloning into 'tm29hikcp7o'...
git checkout -f --detach master
HEAD is now at aaabbb Commit message.
Your branch is up to date with 'origin/master'.
cat tm29hikcp7o/zz/action.yml
[...]
runs:
  using: 'node12'
  main: 'index.js'
pwd
/home/runner/work/yy/yy
find . -maxdepth 1 -type d
deleteFolder  # so my temporary folder gets deleted before I can actually use it
.
./tm29hikcp7o
python ./tm29hikcp7o/my-script.py
python: can't open file './tm29hikcp7o/my-script.py': [Errno 2] No such file or directory
##[error]The process 'python' failed with exit code 2
##[error]Node run failed with exit code 1

I'm puzzled because the structure of your code should ensure that the action has been wholly executed before running the deleteFolder instruction.

   try {
        // Execute the action
        await executeAction(actionFileFolder);
    } catch (err) {
        core.setFailed(err);
    } finally {
        // Cleanup
        deleteFolder(tempFolderName);
    }
}

I'm wondering if this has anything to do with the ts compile step.

Do you have an idea of why I'm getting this strange behaviour? I may be missing a big error right in the middle of my face here, so thanks for your help.

As a side note PR#5 would be a workaround for that specific case, but not for the more generic ones.

Cheers,

slabruyere avatar Feb 06 '20 05:02 slabruyere

Hi, I accidentally merged your PR so I had to revert it back.

Can you describe your scenario and what are you trying to achieve? In your PR you are substituting the temp folder with a know one, and then, you are deleting it in the same way the temp folder is deleted, so I'm not sure what is the difference between the two scenarios.

bagbyte avatar May 12 '20 15:05 bagbyte

Hi,

No worries for merging and having to revert.

Well, my understanding was that because I have to perform a find to know where my code has been cloned (rather than knowing ahead of time), the finally statement is executed before my async function is finished. Hence the folder is being deleted before I got to use the code in there.

Does that make sense?

slabruyere avatar May 12 '20 23:05 slabruyere