Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement Action in TypeScript #45

Merged
merged 6 commits into from
Feb 20, 2024
Merged

Re-implement Action in TypeScript #45

merged 6 commits into from
Feb 20, 2024

Conversation

pbrisbin
Copy link
Member

@pbrisbin pbrisbin commented Feb 10, 2024

We generally prefer TypeScript for actions, and given that this action is almost entirely just using @actions/cache and invoking stack (which can be done better with @actions/exec), it makes sense to use TypeScript even though it's "simple".

This brings the following benefits to users:

  • We can now properly group our output and log details such that the workflows are much easier to debug (e.g. for caching behaviors). @actions/exec also makes command invocations logged better, incorporating rerun-with-debug, etc.

  • Some of the changed behaviors (described below) are bug-fixes

This brings the following benefits to maintainers:

  • The complexity of conditional jobs and cache-key handling (among other things) is much easier to handle in TypeScript than shell
  • We now have a nice little set of unit tests across key functionality
  • We can now set all of stack path as outputs without having to maintain the list in action.yml

The following behaviors were changed along the way:

  • We no longer skip the Build step on a primary-key hit

    This is still (I think) fine (and preferred) to do on the Dependencies step, since there are no artifacts useful outside of the build produced in that step. However, if you are relying on the Build step to produce executables (for example), skipping it on a cache-hit means no executable and that will fail.

  • We are very careful to not install GHC pre-caching

    See Don't install ghc before restoring dependencies #33. There are some caveats being discussed there, and this PR implements what's described.

  • We run stack upgrade before getting started

    And added a no-stack-upgrade input to disable that if desired. Note that Stack v2.15 (newer than currently on the runners) is required to address the above, which is why I added this.


@pbrisbin pbrisbin changed the title pb/typescript Re-implement Action in TypeScript Feb 12, 2024
package.json Outdated Show resolved Hide resolved
src/with-cache.ts Outdated Show resolved Hide resolved
@pbrisbin pbrisbin requested a review from stackptr February 12, 2024 15:08
@pbrisbin pbrisbin marked this pull request as ready for review February 12, 2024 15:08
@@ -0,0 +1,22 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handy to define -- is there any tool in particular that motivated this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most everything boilerplate here was copied from grep-action, which originally used our typescript-action-template repo and has since grabbed some stuff from this official template. 🤷

action.yml Outdated Show resolved Hide resolved
src/parse-stack-path.test.ts Show resolved Hide resolved
src/stack-cli.ts Outdated Show resolved Hide resolved
src/with-cache.ts Outdated Show resolved Hide resolved
@pbrisbin
Copy link
Member Author

🎉 this looks ready to go

  1. I plan to keep this in @v5 as there are no user-facing breaks
  2. I plan to test this branch in our megarepo project for a few days first

Let me know if anyone disagrees with (1).

@pbrisbin
Copy link
Member Author

Heh, welp, I found one issue:

stack-build-arguments: |
  --ghc-options "$STACK_GHC_OPTIONS"

This no longer works, because the value is not passed to a shell, it's given to exec. Any variables like this won't be expanded. Using exec , and removing the shell intermediary is kind of an important benefit of this whole approach, so I wonder what we should do.

  1. Say it's not supported

    I think that

    stack-build-arguments: |
      --ghc-options "${{ env.STACK_GHC_OPTIONS }}"

    Would work the same, but I haven't tested it. It's possible env isn't available when with arguments are processed.

  2. Support it by doing exec("sh", ["-c", "..."])

    This would be much uglier to escape (add bug risk) and make the build logs less clear.

  3. Support it by doing our own envsubst thing

    I dunno, this doesn't seem that bad?

@pbrisbin
Copy link
Member Author

I did (3) 🤷

Copy link
Member

@stackptr stackptr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

envsubst.ts seems like it has the necessary test cases. The third-party library I found for this (please no leftpad jokes) is a little more complicated but doesn't seem to cover any particular use-case that is missing here.

I might find it surprising that an action implemented its own env substitution, though, and would expect an underlying sh instead. I understand why that more complicated approach would have a bug risk, but could you elaborate on the impact to build logs?

Despite that question, I'm fine with this approach as-is.

@pbrisbin
Copy link
Member Author

pbrisbin commented Feb 16, 2024

could you elaborate on the impact to build logs?

It's minor, but right now the build logs may show:

% /usr/bin/stack build --test
stack logs...

Or

% /usr/bin/stack build --test
Error: /usr/bin/stack exited 1

And now they would show,

% /bin/sh -c /usr/bin/stack build --test
stack logs...

And

% /bin/sh /usr/bin/stack build --test
Error: /bin/sh exited 1

Which is just a little potentially confusing if you were expecting stack, especially the error case -- if that's all you looked at.

We generally prefer TypeScript for actions, and since this is almost
entirely doing things available as libraries (`@actions/cache` and
`@actions/exec`), it will be easy to port.

Once in TypeScript, we can continue to extend and fix in a more
comfortable language.
- Fix `cacheSaveAlways` behavior
- Prefer positive conditional
- Use `jest.spyOn`
- Encapsulate `getStackDirectories`
- Parse and cache stack programs too
- Track `--resolver` and use that instead of yaml resolver in cache key
- Don't test so many resolvers
- Remove `nightly` examples, we unit test that now
- Remove explicit resolver cache prefixes, we fixed that in the action
- Fix reading `LOCALAPPDATA`
- Replace environment variables in shellwords inputs
@pbrisbin pbrisbin merged commit df847eb into main Feb 20, 2024
18 checks passed
@pbrisbin pbrisbin deleted the pb/typescript branch February 20, 2024 16:34
@pbrisbin pbrisbin restored the pb/typescript branch February 20, 2024 16:34
@pbrisbin
Copy link
Member Author

FYI I restored the branch for now, until I update our test repositories to go back to @v5, please don't clean it up yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants