Skip to content

Commit baa2600

Browse files
authored
Add automatic toolchain version detection (#299)
1 parent f406a0a commit baa2600

File tree

8 files changed

+120
-50
lines changed

8 files changed

+120
-50
lines changed

.github/workflows/test-latest.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,34 @@ jobs:
5353
version: latest
5454
- run: rye sync
5555
working-directory: __tests__/fixtures/rye-project
56+
57+
test-latest-automatic-toolchain:
58+
runs-on: ${{ matrix.os }}
59+
strategy:
60+
matrix:
61+
os: [ubuntu-latest, macos-latest, macos-14, oracle-aarch64]
62+
steps:
63+
- uses: actions/checkout@v4
64+
- name: Determine expected python version
65+
id: determine-expected-python-version
66+
run: |
67+
EXPECTED_PYTHON_VERSION=$(cat .python-version)
68+
echo "EXPECTED_PYTHON_VERSION=$EXPECTED_PYTHON_VERSION" >> "$GITHUB_OUTPUT"
69+
working-directory: __tests__/fixtures/rye-project
70+
- name: Setup rye
71+
uses: ./
72+
with:
73+
version: latest
74+
working-directory: __tests__/fixtures/rye-project
75+
- run: rye sync
76+
working-directory: __tests__/fixtures/rye-project
77+
- name: Check python version
78+
run: |
79+
ACTUAL_PYTHON_VERSION=$(ls $RYE_HOME/py)
80+
if [ "$ACTUAL_PYTHON_VERSION" != "$EXPECTED_PYTHON_VERSION" ];
81+
then
82+
echo "$ACTUAL_PYTHON_VERSION is not the same as $EXPECTED_PYTHON_VERSION"
83+
exit 1
84+
fi
85+
env:
86+
EXPECTED_PYTHON_VERSION: ${{ steps.determine-expected-python-version.outputs.EXPECTED_PYTHON_VERSION }}

dist/save-cache/index.js

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/save-cache/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup/index.js

Lines changed: 30 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/restore-cache.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import {getArch} from './utils'
1010

1111
export const STATE_CACHE_KEY = 'cache-key'
1212
export const STATE_CACHE_MATCHED_KEY = 'cache-matched-key'
13-
export const workingDirInput = core.getInput('working-directory')
14-
export const workingDir = workingDirInput ? `${path.sep}${workingDirInput}` : ''
15-
export const venvPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}${path.sep}.venv`
13+
const workingDirInput = core.getInput('working-directory')
14+
const workingDir = workingDirInput ? `${path.sep}${workingDirInput}` : ''
15+
export const WORKING_DIR_PATH = `${process.env['GITHUB_WORKSPACE']}${workingDir}`
16+
export const VENV_PATH = `${process.env['GITHUB_WORKSPACE']}${workingDir}${path.sep}.venv`
1617
const CACHE_VERSION = '5'
1718
const cacheLocalStoragePath =
1819
`${core.getInput('cache-local-storage-path')}` || ''
@@ -34,7 +35,7 @@ export async function restoreCache(
3435
try {
3536
matchedKey = cacheLocalStoragePath
3637
? await restoreCacheLocal(cacheKey)
37-
: await cache.restoreCache([venvPath], cacheKey)
38+
: await cache.restoreCache([VENV_PATH], cacheKey)
3839
} catch (err) {
3940
const message = (err as Error).message
4041
core.warning(message)
@@ -73,7 +74,7 @@ function handleMatchResult(
7374

7475
const venvPathMatch = doesCachedVenvPathMatchCurrentVenvPath()
7576
if (!venvPathMatch) {
76-
fs.rmSync(venvPath, {recursive: true})
77+
fs.rmSync(VENV_PATH, {recursive: true})
7778
core.setOutput('cache-hit', false)
7879
return
7980
}
@@ -86,12 +87,12 @@ function handleMatchResult(
8687
}
8788

8889
function doesCachedVenvPathMatchCurrentVenvPath(): boolean {
89-
const ryeVenvPath = `${venvPath}${path.sep}rye-venv.json`
90+
const ryeVenvPath = `${VENV_PATH}${path.sep}rye-venv.json`
9091
const ryeVenv = JSON.parse(fs.readFileSync(ryeVenvPath, 'utf8'))
9192
core.info(
92-
`Checking if the cached .venv matches the current path: ${venvPath}`
93+
`Checking if the cached .venv matches the current path: ${VENV_PATH}`
9394
)
94-
if (ryeVenv.venv_path !== venvPath) {
95+
if (ryeVenv.venv_path !== VENV_PATH) {
9596
core.warning(
9697
`The .venv in the cache cannot be used because it is from another location: ${ryeVenv.venv_path}`
9798
)
@@ -105,7 +106,7 @@ async function restoreCacheLocal(
105106
): Promise<string | undefined> {
106107
const storedCache = `${cacheLocalStoragePath}${path.sep}${primaryKey}`
107108
if (await exists(storedCache)) {
108-
await cp(`${storedCache}${path.sep}.venv`, venvPath, {
109+
await cp(`${storedCache}${path.sep}.venv`, VENV_PATH, {
109110
recursive: true
110111
})
111112
return primaryKey

src/save-cache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from 'path'
55
import {
66
STATE_CACHE_MATCHED_KEY,
77
STATE_CACHE_KEY,
8-
venvPath
8+
VENV_PATH
99
} from './restore-cache'
1010

1111
const enableCache = core.getInput('enable-cache') === 'true'
@@ -36,18 +36,18 @@ async function saveCache(): Promise<void> {
3636
core.info(`Cache hit occurred on key ${cacheKey}, not saving .venv.`)
3737
return
3838
}
39-
core.info(`Saving .venv path: ${venvPath}`)
39+
core.info(`Saving .venv path: ${VENV_PATH}`)
4040
cacheLocalStoragePath
4141
? await saveCacheLocal(cacheKey)
42-
: await cache.saveCache([venvPath], cacheKey)
42+
: await cache.saveCache([VENV_PATH], cacheKey)
4343

4444
core.info(`.venv saved with the key: ${cacheKey}`)
4545
}
4646

4747
async function saveCacheLocal(cacheKey: string): Promise<void> {
4848
const targetPath = `${cacheLocalStoragePath}${path.sep}${cacheKey}`
4949
await io.mkdirP(targetPath)
50-
await io.cp(venvPath, `${targetPath}${path.sep}.venv`, {
50+
await io.cp(VENV_PATH, `${targetPath}${path.sep}.venv`, {
5151
recursive: true
5252
})
5353
}

src/setup-rye.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as io from '@actions/io'
55
import * as path from 'path'
66
import * as fs from 'fs'
77
import {downloadVersion, tryGetFromCache} from './download/download-version'
8-
import {restoreCache} from './restore-cache'
8+
import {restoreCache, WORKING_DIR_PATH} from './restore-cache'
99
import {
1010
Architecture,
1111
EARLIEST_VERSION_WITH_NO_MODIFY_PATHSUPPORT,
@@ -103,13 +103,18 @@ async function installRye(
103103
core.debug(`Created temporary directory ${tempDir}`)
104104
// Cache first to get the correct path
105105
const cachedPath = await tc.cacheDir(tempDir, toolsCacheName, version, arch)
106+
const toolchainVersion = await determineToolchainVersion()
107+
const env = toolchainVersion
108+
? {
109+
...process.env,
110+
RYE_HOME: cachedPath,
111+
RYE_TOOLCHAIN_VERSION: toolchainVersion
112+
}
113+
: {...process.env, RYE_HOME: cachedPath}
106114
const options: exec.ExecOptions = {
107115
cwd: cachedPath,
108116
silent: !core.isDebug(),
109-
env: {
110-
...process.env,
111-
RYE_HOME: cachedPath
112-
}
117+
env: env
113118
}
114119
core.info(`Installing Rye into ${cachedPath}`)
115120
const execArgs = ['self', 'install', '--yes']
@@ -122,6 +127,22 @@ async function installRye(
122127
return cachedPath
123128
}
124129

130+
async function determineToolchainVersion(): Promise<string | void> {
131+
const pythonVersionFile = `${WORKING_DIR_PATH}${path.sep}.python-version`
132+
if (fs.existsSync(pythonVersionFile)) {
133+
const toolchainVersion = await fs.promises.readFile(
134+
pythonVersionFile,
135+
'utf8'
136+
)
137+
core.info(`Determined RYE_TOOLCHAIN_VERSION: ${toolchainVersion.trim()}`)
138+
return toolchainVersion.trim()
139+
}
140+
core.warning(
141+
`No .python-version file found, using default RYE_TOOLCHAIN_VERSION`
142+
)
143+
return
144+
}
145+
125146
async function createConfigBackup(installedPath: string): Promise<void> {
126147
if (fs.existsSync(`${installedPath}${path.sep}${RYE_CONFIG_TOML}`)) {
127148
await io.cp(

0 commit comments

Comments
 (0)