feat: use packageManager from package.json as default bun version (#152)

* Initial plan

* feat: make packageManager from package.json the default bun version

Co-authored-by: xhyrom <56601352+xhyrom@users.noreply.github.com>

* test: improve cross-platform compatibility and cleanup in utils tests

Co-authored-by: xhyrom <56601352+xhyrom@users.noreply.github.com>

* docs: update README to document automatic package.json version detection

Co-authored-by: xhyrom <56601352+xhyrom@users.noreply.github.com>

* conflicts

* ci: setup-bun-from-package-json-without-specified-field

* ci: setup-bun-from-package-json-without-specified-field

* ci: no cache

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: xhyrom <56601352+xhyrom@users.noreply.github.com>
Co-authored-by: Jozef Steinhübl <contact@xhyrom.dev>
This commit is contained in:
Copilot
2026-01-05 12:45:25 +01:00
committed by GitHub
parent b02f8a8a6e
commit 563911925f
6 changed files with 181 additions and 108 deletions

View File

@@ -32,6 +32,8 @@ jobs:
- name: Setup Bun - name: Setup Bun
uses: ./ uses: ./
with:
no-cache: true
- name: Install dependencies - name: Install dependencies
run: bun install run: bun install
@@ -77,6 +79,7 @@ jobs:
id: setup_bun id: setup_bun
with: with:
bun-version: ${{ matrix.bun-version }} bun-version: ${{ matrix.bun-version }}
no-cache: true
- name: ▶️ Run Bun - name: ▶️ Run Bun
id: run_bun id: run_bun
@@ -154,6 +157,53 @@ jobs:
uses: ./ uses: ./
with: with:
bun-version-file: ${{ matrix.file.file }} bun-version-file: ${{ matrix.file.file }}
no-cache: true
- name: ⚖️ Compare versions
uses: ./.github/actions/compare-bun-version
with:
bun-version: "1.1.0"
setup-bun-from-package-json-without-specified-field:
name: setup-bun from (${{ matrix.os }}, ${{ matrix.file.name }}) without specified field
runs-on: ${{ matrix.os }}
continue-on-error: true
needs: [remove-cache, tests]
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
- windows-11-arm
file:
- name: package.json (packageManager bun@1.1.0)
file: package.json
run: |
echo "$(jq '. += {"packageManager": "bun@1.1.0"}' package.json)" > package.json
- name: package.json (packageManager yarn@bun@1.1.0)
file: package.json
run: |
echo "$(jq '. += {"packageManager": "yarn@bun@1.1.0"}' package.json)" > package.json
- name: package.json (engines bun@1.1.0)
file: package.json
run: |
echo "$(jq '.engines = {"bun": "1.1.0"}' package.json)" > package.json
steps:
- name: 📥 Checkout
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
- name: 📄 Setup file
run: ${{ matrix.file.run }}
- name: 🛠️ Setup Bun
uses: ./
with:
no-cache: true
- name: ⚖️ Compare versions - name: ⚖️ Compare versions
uses: ./.github/actions/compare-bun-version uses: ./.github/actions/compare-bun-version

View File

@@ -4,6 +4,18 @@ Download, install, and setup [Bun](https://bun.sh) in GitHub Actions.
## Usage ## Usage
```yaml
- uses: oven-sh/setup-bun@v2
```
By default, if no version is specified, the action will:
1. Check `package.json` for the `packageManager` field (e.g., `"packageManager": "bun@1.0.25"`)
2. If `packageManager` doesn't exist, check `package.json` for `engines.bun`
3. If neither exists or `package.json` is not found, use `latest`
You can also explicitly specify a version:
```yaml ```yaml
- uses: oven-sh/setup-bun@v2 - uses: oven-sh/setup-bun@v2
with: with:
@@ -66,15 +78,15 @@ If you need to override the download URL, you can use the `bun-download-url` inp
## Inputs ## Inputs
| Name | Description | Default | Examples | | Name | Description | Default | Examples |
| ------------------ | --------------------------------------------------------------------------------- | --------------------- | ------------------------------------------------ | | ------------------ | --------------------------------------------------------------------------------- | ---------------------------------------- | ------------------------------------------------ |
| `bun-version` | The version of Bun to download and install. | `latest` | `canary`, `1.0.0`, `1.0.x` | | `bun-version` | The version of Bun to download and install. | Version from `package.json`, or `latest` | `canary`, `1.0.0`, `1.0.x` |
| `bun-version-file` | The version of Bun to download and install from file. | `undefined` | `package.json`, `.bun-version`, `.tool-versions` | | `bun-version-file` | The version of Bun to download and install from file. | `undefined` | `package.json`, `.bun-version`, `.tool-versions` |
| `bun-download-url` | URL to download .zip file for Bun release | | | | `bun-download-url` | URL to download .zip file for Bun release | | |
| `registry-url` | Registry URL where some private package is stored. | `undefined` | `"https://npm.pkg.github.com/"` | | `registry-url` | Registry URL where some private package is stored. | `undefined` | `"https://npm.pkg.github.com/"` |
| `scope` | Scope for private packages. | `undefined` | `"@foo"`, `"@orgname"` | | `scope` | Scope for private packages. | `undefined` | `"@foo"`, `"@orgname"` |
| `no-cache` | Disable caching of the downloaded executable. | `false` | `true`, `false` | | `no-cache` | Disable caching of the downloaded executable. | `false` | `true`, `false` |
| `token` | Personal access token (PAT) used to fetch tags from the `oven-sh/bun` repository. | `${{ github.token }}` | `${{ secrets.GITHUB_TOKEN }}` | | `token` | Personal access token (PAT) used to fetch tags from the `oven-sh/bun` repository. | `${{ github.token }}` | `${{ secrets.GITHUB_TOKEN }}` |
## Outputs ## Outputs

186
dist/setup/index.js generated vendored

File diff suppressed because one or more lines are too long

5
package-lock.json generated
View File

@@ -6,7 +6,7 @@
"packages": { "packages": {
"": { "": {
"name": "setup-bun", "name": "setup-bun",
"version": "2.0.2", "version": "2.1.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/cache": "^4.0.0", "@actions/cache": "^4.0.0",
@@ -15,7 +15,8 @@
"@actions/glob": "^0.4.0", "@actions/glob": "^0.4.0",
"@actions/io": "^1.1.2", "@actions/io": "^1.1.2",
"@actions/tool-cache": "^2.0.1", "@actions/tool-cache": "^2.0.1",
"@iarna/toml": "^2.2.5" "@iarna/toml": "^2.2.5",
"compare-versions": "^6.1.1"
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "^1.1.13", "@types/bun": "^1.1.13",

View File

@@ -26,6 +26,7 @@ runAction({
version: version:
getInput("bun-version") || getInput("bun-version") ||
readVersionFromFile(getInput("bun-version-file")) || readVersionFromFile(getInput("bun-version-file")) ||
readVersionFromFile("package.json", true) ||
undefined, undefined,
customUrl: getInput("bun-download-url") || undefined, customUrl: getInput("bun-download-url") || undefined,
registries: registries, registries: registries,

View File

@@ -80,7 +80,10 @@ const FILE_VERSION_READERS = {
".bun-version": (content: string) => content, ".bun-version": (content: string) => content,
}; };
export function readVersionFromFile(file: string): string | undefined { export function readVersionFromFile(
file: string,
silent = false,
): string | undefined {
const cwd = process.env.GITHUB_WORKSPACE; const cwd = process.env.GITHUB_WORKSPACE;
if (!cwd) { if (!cwd) {
return; return;
@@ -96,7 +99,9 @@ export function readVersionFromFile(file: string): string | undefined {
const base = basename(file); const base = basename(file);
if (!existsSync(path)) { if (!existsSync(path)) {
warning(`File ${path} not found`); if (!silent) {
warning(`File ${path} not found`);
}
return; return;
} }
@@ -107,12 +112,16 @@ export function readVersionFromFile(file: string): string | undefined {
output = reader(readFileSync(path, "utf8"))?.trim(); output = reader(readFileSync(path, "utf8"))?.trim();
if (!output) { if (!output) {
warning(`Failed to read version from ${file}`); if (!silent) {
warning(`Failed to read version from ${file}`);
}
return; return;
} }
} catch (error) { } catch (error) {
const { message } = error as Error; const { message } = error as Error;
warning(`Failed to read ${file}: ${message}`); if (!silent) {
warning(`Failed to read ${file}: ${message}`);
}
} finally { } finally {
if (output) { if (output) {
info(`Obtained version ${output} from ${file}`); info(`Obtained version ${output} from ${file}`);