Skip to content

Commit

Permalink
👌 Improve error handling when CONDA environ variable isn't set (#434)
Browse files Browse the repository at this point in the history
This action uses the preinstalled `miniconda` and determines its installation base path reading the `CONDA` environ variable.
Up until `macOS-12` this worked for all runner images, however `macOS-13` and `macOS-14` (current `macos-latest`) do not install `miniconda` and workflows fail with:

```
Error: The "path" argument must be of type string. Received undefined
```
See #432 

With this change, the error will be:
```
Error: Could not determine conda base path, it seams conda is not installed.
```
On any system without the `CONDA` environ variable set.
And for macos runner images > 12:
```
 Error: Could not determine conda base path, it seams conda is not installed.
  MacOS images newer than "macos-12" (i.e. "macOS-latest") are known to be incompatible with this action due to a missing miniconda installation.
  See: #432
```
  • Loading branch information
s-weigand authored May 17, 2024
1 parent e05c107 commit 74fc5b1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
needs: release
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- name: Setup conda v1
uses: s-weigand/setup-conda@v1
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
CONDA_CHANNELS: 'defaults'
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Prepare tests
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:
ENV_PYTHON: 3.8
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Prepare tests
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:
ENV_PYTHON: 3.8
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Prepare tests
Expand Down Expand Up @@ -174,7 +174,7 @@ jobs:
needs: jest-tests
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Prepare tests
Expand Down Expand Up @@ -212,7 +212,7 @@ jobs:
needs: jest-tests
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Prepare tests
Expand Down Expand Up @@ -250,7 +250,7 @@ jobs:
needs: jest-tests
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Prepare tests
Expand Down Expand Up @@ -286,7 +286,7 @@ jobs:
needs: jest-tests
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
steps:
- uses: actions/checkout@v4
- name: Download built dist
Expand Down Expand Up @@ -325,7 +325,7 @@ jobs:
PYPY_TEST: true
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, windows-latest, macOS-12]
pypy-ver: ['pypy3.7']
include:
- os: ubuntu-latest
Expand Down Expand Up @@ -354,5 +354,5 @@ jobs:
- name: Run tests
run: |
pypy -m ensurepip
pypy -m pip install -q pytest
pypy -m pip install --trusted-host pypi.python.org -q pytest
pypy -m pytest -v integrationtests/test_python_version.py
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
This action adds the [`conda`](https://conda.io/projects/conda/en/latest/user-guide/tasks/index.html)
command from the on the worker preinstalled miniconda version to the known shell commands.

> [!CAUTION]
> This action [is known to currently not work with macOS runner-images newer than `macOS-12` (i.e. `macOS-latest`)](https://github.com/s-weigand/setup-conda/issues/432).
## Inputs

| Name | Requirement | Default | Description |
Expand Down
49 changes: 48 additions & 1 deletion __tests__/conda_actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { EOL } from 'os'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { parseActivationScriptOutput } from '../src/conda_actions'
import {
parseActivationScriptOutput,
addCondaToPath,
} from '../src/conda_actions'

describe('Parse env activation output', () => {
it('Parse linux activation', async () => {
Expand Down Expand Up @@ -65,3 +69,46 @@ describe('Parse env activation output', () => {
expect(envVars['CONDA_PYTHON_EXE']).toBe('C:\\Miniconda\\python.exe')
})
})

const testConfig = {
activate_conda: false,
update_conda: false,
python_version: '',
conda_channels: [],
os: 'linux',
}

describe("Throw error if CONDA env var isn't set", () => {
const OLD_ENV = process.env

beforeEach(() => {
process.env = {}
})

afterAll(() => {
process.env = OLD_ENV
})

it.each(['linux', 'win32', 'darwin'])(
`General error %p`,
async (os: string) => {
await expect(addCondaToPath({ ...testConfig, os })).rejects.toThrow(
'Could not determine conda base path, it seams conda is not installed.',
)
},
)

it('MacOs > 12 error', async () => {
process.env.ImageOS = 'macos13'
await expect(
addCondaToPath({ ...testConfig, os: 'darwin' }),
).rejects.toThrow(
[
'Could not determine conda base path, it seams conda is not installed.',
'MacOS images newer than "macos-12" (i.e. "macOS-latest") are known to be ' +
'incompatible with this action due to a missing miniconda installation.',
'See: https://github.com/s-weigand/setup-conda/issues/432',
].join(EOL),
)
})
})
27 changes: 23 additions & 4 deletions src/conda_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,29 @@ const add_bin_dir = (python_dist_dir: string, config: ConfigObject): void => {
*
* @param config Configuration of the action
*/
const addCondaToPath = async (config: ConfigObject): Promise<void> => {
export const addCondaToPath = async (config: ConfigObject): Promise<void> => {
startGroup('Adding conda path to PATH')
console.log(`${process.env.CONDA}`)
const conda_base_path = process.env.CONDA as string
console.log('The CONDA env var is:', process.env.CONDA)
const conda_base_path = process.env.CONDA
let errorMessageAppendix: string[] = []
if (conda_base_path === undefined) {
if (config.os === 'darwin' && process.env.ImageOS !== undefined) {
const macImageVersion = Number(process.env.ImageOS.replace('macos', ''))
if (macImageVersion > 12) {
errorMessageAppendix = [
'MacOS images newer than "macos-12" (i.e. "macOS-latest") are known to be ' +
'incompatible with this action due to a missing miniconda installation.',
'See: https://github.com/s-weigand/setup-conda/issues/432',
]
}
}
throw new Error(
[
'Could not determine conda base path, it seams conda is not installed.',
...errorMessageAppendix,
].join(EOL),
)
}
sane_add_path(conda_base_path)
add_bin_dir(conda_base_path, config)
endGroup()
Expand Down Expand Up @@ -250,7 +269,7 @@ const chown_conda_macOs = async (config: ConfigObject): Promise<void> => {
*/
const update_conda = async (config: ConfigObject): Promise<void> => {
if (config.update_conda) {
startGroup('Updateing conda:')
startGroup('Updating conda:')
await exec('conda', [
'update',
'-y',
Expand Down

0 comments on commit 74fc5b1

Please sign in to comment.