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

Support Custom GitHub Action use of GITHUB_ENV #47

Open
rlespinasse opened this issue Mar 28, 2023 · 10 comments
Open

Support Custom GitHub Action use of GITHUB_ENV #47

rlespinasse opened this issue Mar 28, 2023 · 10 comments
Labels
enhancement New feature or request

Comments

@rlespinasse
Copy link

Is your feature request related to a problem? Please describe.
Some GitHub Actions use GITHUB_ENV to set up custom environment variables.
When used inside a workflow, use ${{ env.CUSTOM_VARIABLE }} is flagged as Context access might be invalid.

Describe the solution you'd like
Enable the custom GitHub Actions developer to define statically or dynamically the generated environment variables (like for the action output) and use it in this plugin to remove false-positive.

Additional context

This workflow is flagged with the issue

name: "Context access might be invalid"
on: push
jobs:
  context-access-might-be-invalid:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup environment variables
        uses: rlespinasse/github-slug-action@v4

      - name: Print custom environment variables
        run: echo ${{ env.GITHUB_REF_SLUG }}

And produces
Capture d’écran 2023-03-28 à 20 15 36

@rlespinasse rlespinasse added the enhancement New feature or request label Mar 28, 2023
@rlespinasse rlespinasse changed the title Support custom GitHub Action use of GITHUB_ENV Support Custom GitHub Action use of GITHUB_ENV Mar 28, 2023
@kristof-mattei
Copy link

Another example:

# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Something

on:
  pull_request:
    branches:
      - main

jobs:
  job1:
    name: Job1
    runs-on: ubuntu-latest
    outputs:
      code: ${{ steps.filter.outputs.code }}

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Check if we actually made changes
        uses: dorny/paths-filter@v2
        id: filter
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          filters: .github/file-filters.yml

      - shell: bash
        run: |
          echo ${{ fromJSON(env.SOME_ENV) }}

image

@rdhar
Copy link

rdhar commented Mar 29, 2023

Same here; additions to GITHUB_ENV return "Context access might be invalid: REF" warning.
This is true for both expressions (i.e., if: env.REF) as well as environment variable interpolation (i.e., ${{ env.REF }}).

Here's a workflow snippet with the same warning on every env.REF line, despite these environment variables being initialised in the previous step.

steps:
  - name: Add environment variable
    run: echo REF=${string/--*/} >> $GITHUB_ENV

  - name: Reference environment variable
    if: env.REF != 'default' # Context access might be invalid: REF
    uses: actions/github-script@v6
    with:
      script: return "${{ env.REF }}" # Context access might be invalid: REF

Thank you.

@briantist
Copy link

I understand this warning in that there is probably no way for the extension to determine that the var will exist at runtime, so it will never be able to definitively know when to warn or not.

So I think what we need is a way to tell the extension not to warn on certain vars, perhaps even scoped.

Probably something comment based like other linters use., maybe a noqa type of thing.

name: "Context access might be invalid"
on: push
jobs:
  context-access-might-be-invalid:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup environment variables
        uses: rlespinasse/github-slug-action@v4

      # noqa: runtime-reference GITHUB_REF_SLUG
      - name: Print custom environment variables
        run: echo ${{ env.GITHUB_REF_SLUG }}

Scoping would be neat, like somehow applying the noqa and then disabling it, so that you can mark the area where it should not apply instead of having it never validate that variable again in the whole workflow.


Workaround

This can be worked around by explicitly adding the variable to an env block anywhere before the reference in the workflow:

name: "Context access might be invalid"
on: push
env:
  GITHUB_REF_SLUG: ""

jobs:
  context-access-might-be-invalid:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup environment variables
        uses: rlespinasse/github-slug-action@v4

      - name: Print custom environment variables
        run: echo ${{ env.GITHUB_REF_SLUG }}

This is not a great workaround imo, because it masks the real issue, and means littering the workflow with useless references. Worse, it makes it more difficult to understand the workflow.


There are also some errors with the variable flagging (it misses some!) which I'll raise in another issue.

@tylerclark
Copy link

Fixed in next release: #61 (comment)

@felipesu19
Copy link
Collaborator

Considering its supposedly to be fixed , I'm closing!

@brianjmurrell
Copy link

Fixed in next release: #61 (comment)

#61 is an issue, not a PR so it can it fix anything itself. Perhaps what you mean is that actions/languageservices#15 fixes #61, but that doesn't seem entirely relevant here. Short-circuit evaluation is not even mentioned in this issue (but is mentioned in #61).

So does #61 (actually actions/languageservices#15) really fix all of the problems described in this issue? actions/languageservices#15 only mentions the problem with short-circuit operators (which is not even described in this issue, but is described in #61)?

@felipesu19
Copy link
Collaborator

Fixed in next release: #61 (comment)

#61 is an issue, not a PR so it can it fix anything itself. Perhaps what you mean is that actions/languageservices#15 fixes #61, but that doesn't seem entirely relevant here. Short-circuit evaluation is not even mentioned in this issue (but is mentioned in #61).

So does #61 (actually actions/languageservices#15) really fix all of the problems described in this issue? actions/languageservices#15 only mentions the problem with short-circuit operators (which is not even described in this issue, but is described in #61)?

You are correct and my closing of this was too hasty. Re-opening and re-evaluating. Sorry about that.

@carry0987
Copy link

Any updates for this issue ?

@Aetherinox
Copy link

Is this a confirmed bug others are getting? I loaded up visual studio code today to work on workflows, and I have this error everywhere, yet it's pulling info fine on Github.

@jakoch
Copy link

jakoch commented Jul 12, 2024

This is an upstream issue of the LSP, right? actions/languageservices#60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog 🗒
Development

No branches or pull requests

10 participants