github / github/local-action

[Bug] `--pre` and `--post` documented as taking paths relative to "action directory", but it is relative to the `cwd` instead

Open
#271 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
451
Forks
28
PR merge metrics
No merged PRs in 30d

Description

Given the following directory structure:

```
.../repo
├── dist
│ ├── generic
│ │ └── index.cjs
├── generic
│ └── action.yml
├── package.json
└── src
├── generic
│ ├── index.ts
│ └── main.ts
└── main.ts
```

for example, using https://github.com/actions/cache's sub-actions `save` and `restore`. Importantly, there is no `action.yml` in the root directory of the repository, and this is by design! (Imagine in the future not just `generic/action.yml` but other things like `video/action.yml` or `docker/action.yml`?)

If I run from `.` to valid file paths, I get an error as expected, the entry points should be relative to the action directory:

```bash
❯ pwd
.../repo
❯ npx @github/local-action ./generic ./src/generic/main.ts .env --post ./src/generic/main.ts
error: command-argument value './src/generic/main.ts' is invalid for argument 'entrypoint'. Entrypoint does not exist: ./src/generic/main.ts
```

So, relative to `./generic`'s `action.yml`, the `src/...` things are in `../src`, right? **I still get an error!**

```bash
❯ pwd
.../repo
❯ npx @github/local-action ./generic ../src/generic/main.ts .env --post ../src/generic/main.ts
error: option '--post ' argument '../src/generic/main.ts' is invalid. POST entrypoint does not exist: ../src/generic/main.ts
```

It seems that you must make the main entry point correctly relative, but the `--post` entry-point should be relative to the current working directory?

```bash
❯ pwd
.../repo
❯ npx @github/local-action ./generic ../src/generic/main.ts .env --post ./src/generic/main.ts
_ _ _ ____ _
/ \ ___| |_(_) ___ _ __ | _ \ ___| |__ _ _ __ _ __ _ ___ _ __
/ _ \ / __| __| |/ _ \| '_ \ | | | |/ _ \ '_ \| | | |/ _` |/ _` |/ _ \ '__|
/ ___ \ (__| |_| | (_) | | | | | |_| | __/ |_) | |_| | (_| | (_| | __/ |
/_/ \_\___|\__|_|\___/|_| |_| |____/ \___|_.__/ \__,_|\__, |\__, |\___|_|
|___/ |___/
================================================================================
Configuration
================================================================================

┌─────────┬────────────────────┬────────────────────────────────┐
│ (index) │ Field │ Value │
├─────────┼────────────────────┼────────────────────────────────┤
│ 0 │ 'Action Path' │ '.../repo/generic' │
│ 1 │ 'Entrypoint' │ '.../repo/src/generic/main.ts' │
│ 2 │ 'Post Entrypoint' │ '.../repo/src/generic/main.ts' │
│ 3 │ 'Environment File' │ '.../repo/.env' │
└─────────┴────────────────────┴────────────────────────────────┘

# Correct execution...
```

**So, the comment in the documentation about `--pre` and `--post` being relative to the "action directory" is incorrect and misleading!** It seems they are relative to the current working directory instead:

```bash
❯ pwd
.../repo
❯ mkdir test
❯ cd test
❯ pwd
.../repo/test
❯ npx @github/local-action ../generic ../src/generic/main.ts ../.env --post ../src/generic/main.ts
_ _ _ ____ _
/ \ ___| |_(_) ___ _ __ | _ \ ___| |__ _ _ __ _ __ _ ___ _ __
/ _ \ / __| __| |/ _ \| '_ \ | | | |/ _ \ '_ \| | | |/ _` |/ _` |/ _ \ '__|
/ ___ \ (__| |_| | (_) | | | | | |_| | __/ |_) | |_| | (_| | (_| | __/ |
/_/ \_\___|\__|_|\___/|_| |_| |____/ \___|_.__/ \__,_|\__, |\__, |\___|_|
|___/ |___/
================================================================================
Configuration
================================================================================

┌─────────┬────────────────────┬────────────────────────────────┐
│ (index) │ Field │ Value │
├─────────┼────────────────────┼────────────────────────────────┤
│ 0 │ 'Action Path' │ '.../repo/generic' │
│ 1 │ 'Entrypoint' │ '.../repo/src/generic/main.ts' │
│ 2 │ 'Post Entrypoint' │ '.../repo/src/generic/main.ts' │
│ 3 │ 'Environment File' │ '.../repo/.env' │
└─────────┴────────────────────┴────────────────────────────────┘

# Correct execution...

❯ cd ../..
❯ pwd
...
❯ npm i @actions/artifact @actions/cache @actions/core @actions/github @github/local-action
❯ npx @github/local-action repo/generic ../src/generic/main.ts repo/.env --post repo/src/generic/main.ts
_ _ _ ____ _
/ \ ___| |_(_) ___ _ __ | _ \ ___| |__ _ _ __ _ __ _ ___ _ __
/ _ \ / __| __| |/ _ \| '_ \ | | | |/ _ \ '_ \| | | |/ _` |/ _` |/ _ \ '__|
/ ___ \ (__| |_| | (_) | | | | | |_| | __/ |_) | |_| | (_| | (_| | __/ |
/_/ \_\___|\__|_|\___/|_| |_| |____/ \___|_.__/ \__,_|\__, |\__, |\___|_|
|___/ |___/
================================================================================
Configuration
================================================================================

┌─────────┬────────────────────┬────────────────────────────────┐
│ (index) │ Field │ Value │
├─────────┼────────────────────┼────────────────────────────────┤
│ 0 │ 'Action Path' │ 'repo/generic' │
│ 1 │ 'Entrypoint' │ 'repo/src/generic/main.ts' │
│ 2 │ 'Post Entrypoint' │ 'repo/src/generic/main.ts' │
│ 3 │ 'Environment File' │ 'repo/.env' │
└─────────┴────────────────────┴────────────────────────────────┘
```

which looks like another correct execution, but the behaviour is, in fact, incorrect, if someone has safety checks in the `post` run related to state passing from `main` to `post`.

Here is a minimal reproducer for `src/generic/main.ts`:

```typescript
import * as core from "@actions/core";

const isPostActionChecked = {
inMain: false,
inPost: false,
};

export async function run(): Promise {
const isPostAction = !!core.getState("isPostAction");
if (!isPostAction) {
core.info("In main...");
if (!isPostActionChecked.inMain) {
isPostActionChecked.inMain = true;
} else {
throw new Error("isPostAction() already checked in 'main' run");
}

core.saveState("isPostAction", "true");
} else {
core.info("In post...");
if (!isPostActionChecked.inPost) {
isPostActionChecked.inPost = true;
} else {
throw new Error("isPostAction() already checked in 'post' run");
}
}
}
```

```bash
# Executing from OUTSIDE the repository, correct paths and incorrect behaviour:
❯ npx @github/local-action repo/generic ../src/generic/main.ts repo/.env --post repo/src/generic/main.ts
================================================================================
Running Action
================================================================================

In main...
::save-state name=isPostAction::true

================================================================================
Running Post Step
================================================================================

In main...
.../repo/src/generic/main.ts:15
throw new Error("isPostAction() already checked in 'main' run");
^

Error: isPostAction() already checked in 'main' run
```

```bash
# Execution of the same code from INSIDE the repository, correct paths and correct behaviour...
❯ npx @github/local-action ./generic ../src/generic/main.ts ./.env --post ./src/generic/main.ts
================================================================================
Running Action
================================================================================

::info::In main...

================================================================================
Running Post Step
================================================================================

::info::In post...

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.