oven-sh / setup-bun

Set up your GitHub Actions workflow with a specific version of Bun
MIT License
455 stars 37 forks source link

[BUG]: Cannot redefine key #107

Open carlosmfreitas2409 opened 1 week ago

carlosmfreitas2409 commented 1 week ago

Hello!

I have the following step (exactly as it says in the README):

- name: Setup bun
  uses: oven-sh/setup-bun@v2
  with:
    bun-version: 1.1.26
    registry-url: https://npm.pkg.github.com/
    scope: '@my-org'

- name: Install dependencies
  env:
    BUN_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  run: bun install --frozen-lockfile

And when running the workflow, the following error occurs: Image

The bunfig.toml was created automatically by the action and looks like this:

[install.scopes]
'@my-org' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/"}

There is an issue with this problem (#33), but it is closed and without a solution.

Yurchishin commented 1 week ago

Fix Summary

I resolved an issue related to package installation in the CI/CD pipeline. Here's how:

  1. Avoid committing .npmrc
    Remove or avoid committing the .npmrc file. For reasons I couldn't fully determine, having .npmrc with bunfig.toml in the repository caused configuration issues on the CI/CD side.

  2. Commit a custom bunfig.toml file
    To enable local package installation, create and commit a bunfig.toml file with the following content and commit it:

    [install.scopes]
    '@my-org' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com" }
  3. Set up a .env file for local development
    Ensure you have a .env file locally to define the BUN_AUTH_TOKEN for authentication:

    BUN_AUTH_TOKEN=******
  4. Update your CI/CD workflow
    Use the following configuration in your GitHub Actions workflow to install dependencies:

    - name: Install Bun
     uses: oven-sh/setup-bun@v2
    - name: Install Dependencies
     run: bun install --frozen-lockfile
     env:
       GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Optional
       NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Optional
       BUN_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
xhyrom commented 1 week ago

We should probably create some sort of merge mechanic like in https://github.com/oven-sh/setup-bun/pull/39

xhyrom commented 1 week ago

Hi @carlosmfreitas2409 and @Yurchishin, could you please test my PR fixing this issue? I think oven-sh/setup-bun@feat/multiple-registries or oven-sh/setup-bun@7ac6e6faeade3b75e9bff88d66e9200170e311cd should work :)

carlosmfreitas2409 commented 1 week ago

Hi @Yurchishin and @xhyrom

First of all, thanks for the answers guys!

To update you guys. I didn't have any bunfig.toml in my repository, however, in the workflow I'm using setup-bun, there's another step later (changesets) that needs to update some files in the repository and then pushes those changes, creating a new pull request.

Since the Bun action creates the bunfig.toml file, the later step was committing the config file to the PR (and I merged).

So the first time, when bunfig.toml hadn't been merged, the install step worked without any problems. And after merged, the install step started giving the redefine key error.


Even though I was committing the file that is being created from the action, I think this should not happen, right? (I could be wrong)

Because theoretically, if I create the file manually, it will have the same content/code that the action created and the same error would happen again (I haven't tested this).

So the temporary solution I had was to delete the bunfig.toml file after the install deps step and it works fine.

I will test if your PR resolves it in a few hours @xhyrom, thank you so much!

xhyrom commented 1 week ago

Thanks, I wonder that we should probably also handle this case by removing the bunfig at the end / reverthing the content of the file 🤔

carlosmfreitas2409 commented 1 week ago

Yes, I believe it would be important. However, it would not solve all cases (including this one).

Let's suppose that I wanted to publish a lib on the Github Registry and for that I need to use the registry-url. I will need bunfig.toml until the publish script runs.

  1. Setup bun (with registry-url)
  2. Install dependencies
  3. Run publish (need the registry-url)
  4. Post-setup bun (remove bunfig)

In the above step, it will work.

However, if between these steps we are using something that commits, bunfig will still be committed because the post-setup will not have been run yet. Example:

  1. Setup bun (with registry-url)
  2. Install dependencies
  3. Commit and push auto version changes (still have the bunfig)
  4. Run publish (need the registry-url)
  5. Post-setup bun (remove bunfig)

https://github.com/oven-sh/setup-bun/blob/8f1bc2eeb376efbcaee0c5f579dee0d2f81582c6/src/action.ts#L46-L48

Maybe, instead of writing the bunfig file with process.cwd, there is some way to create it in temporarily directory? I think Github provide a RUNNER_TEMP and HOME env.

(I think this also refers to #99)

xhyrom commented 6 days ago

Ohh, yes, you're right about this one, I'll see what we can do.

csoylu commented 4 days ago

A question not related to this project but Bun: shouldn't Bun have a environment variable for registry auth like npm has?

xhyrom commented 4 days ago

A question not related to this project but Bun: shouldn't Bun have a environment variable for registry auth like npm has?

As you said, this is not related to setup-bun. Create an issue on oven-sh/bun or ask on discord server :)