Skip to content

Commit a6c3392

Browse files
committed
fix: pyenv init on linux
1 parent c4fd56d commit a6c3392

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/resources/python/pyenv/global-parameter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getPty, ParameterSetting, SpawnStatus, StatefulParameter } from '@codifycli/plugin-core';
22

3-
import { PyenvConfig } from './pyenv.js';
3+
import { PYENV_INIT_INLINE, PyenvConfig } from './pyenv.js';
44

55
export class PyenvGlobalParameter extends StatefulParameter<PyenvConfig, string>{
66

@@ -13,7 +13,7 @@ export class PyenvGlobalParameter extends StatefulParameter<PyenvConfig, string>
1313
override async refresh(): Promise<null | string> {
1414
const $ = getPty();
1515

16-
const { data, status } = await $.spawnSafe('pyenv global')
16+
const { data, status } = await $.spawnSafe(`${PYENV_INIT_INLINE} pyenv global`, { interactive: true })
1717
if (status === SpawnStatus.ERROR) {
1818
return null;
1919
}
@@ -23,16 +23,16 @@ export class PyenvGlobalParameter extends StatefulParameter<PyenvConfig, string>
2323

2424
override async add(valueToAdd: string): Promise<void> {
2525
const $ = getPty();
26-
await $.spawn(`pyenv global ${valueToAdd}`, { interactive: true })
26+
await $.spawn(`${PYENV_INIT_INLINE} pyenv global ${valueToAdd}`, { interactive: true })
2727
}
2828

2929
override async modify(newValue: string): Promise<void> {
3030
const $ = getPty();
31-
await $.spawn(`pyenv global ${newValue}`, { interactive: true })
31+
await $.spawn(`${PYENV_INIT_INLINE} pyenv global ${newValue}`, { interactive: true })
3232
}
3333

3434
override async remove(): Promise<void> {
3535
const $ = getPty();
36-
await $.spawn('pyenv global system', { interactive: true })
36+
await $.spawn(`${PYENV_INIT_INLINE} pyenv global system`, { interactive: true })
3737
}
3838
}

src/resources/python/pyenv/pyenv.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export interface PyenvConfig extends ResourceConfig {
1515
// TODO: Add option here to use homebrew to install instead. Default to true. Maybe add option to set default values to resource config.
1616
}
1717

18+
// pyenv is a real binary on PATH (unlike nvm, which is a sourced shell function), so every
19+
// command that invokes it must set up PATH inline rather than assuming a prior interactive
20+
// shell already sourced the rc lines added by addPyenvInitialization().
21+
export const PYENV_INIT_INLINE = 'export PYENV_ROOT="$HOME/.pyenv"; [ -d "$PYENV_ROOT/bin" ] && export PATH="$PYENV_ROOT/bin:$PATH"; eval "$(pyenv init -)" 2>/dev/null;';
22+
1823
const defaultConfig: Partial<PyenvConfig> = {
1924
pythonVersions: [],
2025
}
@@ -60,7 +65,7 @@ export class PyenvResource extends Resource<PyenvConfig> {
6065
override async refresh(): Promise<Partial<PyenvConfig> | null> {
6166
const $ = getPty();
6267

63-
const pyenvVersion = await $.spawnSafe('pyenv --version')
68+
const pyenvVersion = await $.spawnSafe(`${PYENV_INIT_INLINE} pyenv --version`, { interactive: true })
6469
if (pyenvVersion.status === SpawnStatus.ERROR) {
6570
return null
6671
}
@@ -101,7 +106,7 @@ export class PyenvResource extends Resource<PyenvConfig> {
101106
override async destroy(): Promise<void> {
102107
const $ = getPty();
103108

104-
await $.spawn('rm -rf $(pyenv root)', { interactive: true });
109+
await $.spawn(`${PYENV_INIT_INLINE} rm -rf $(pyenv root)`, { interactive: true });
105110
await $.spawn('rm -rf $HOME/.pyenv');
106111

107112
await FileUtils.removeLineFromStartupFile('export PYENV_ROOT="$HOME/.pyenv"')
@@ -120,7 +125,7 @@ export class PyenvResource extends Resource<PyenvConfig> {
120125
// TODO: Need to support bash in addition to zsh here
121126
private async isValidInstall(): Promise<boolean> {
122127
const $ = getPty();
123-
const { data: doctor } = await $.spawnSafe('pyenv doctor', { interactive: true })
128+
const { data: doctor } = await $.spawnSafe(`${PYENV_INIT_INLINE} pyenv doctor`, { interactive: true })
124129
return doctor.includes('Congratulations! You are ready to build pythons!');
125130
}
126131
}

src/resources/python/pyenv/python-versions-parameter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ArrayStatefulParameter, getPty, SpawnStatus } from '@codifycli/plugin-core';
22

3-
import { PyenvConfig } from './pyenv.js';
3+
import { PYENV_INIT_INLINE, PyenvConfig } from './pyenv.js';
44

55
export class PythonVersionsParameter extends ArrayStatefulParameter<PyenvConfig, string> {
66
override async refresh(desired: string[]): Promise<string[] | null> {
77
const $ = getPty();
88

9-
const { data } = await $.spawnSafe('pyenv versions --bare --skip-aliases --skip-envs')
9+
const { data } = await $.spawnSafe(`${PYENV_INIT_INLINE} pyenv versions --bare --skip-aliases --skip-envs`, { interactive: true })
1010

1111
const versions = data.split(/\n/)
1212
.map((l) => l.trim())
@@ -16,7 +16,7 @@ export class PythonVersionsParameter extends ArrayStatefulParameter<PyenvConfig,
1616
// reason behind this is that pyenv does special version processing during installs. For ex: specifying
1717
// pyenv install 3 will install the latest version 3.12.2
1818
for (const desiredVersion of desired ?? []) {
19-
const { status, data } = await $.spawnSafe(`pyenv latest ${desiredVersion}`);
19+
const { status, data } = await $.spawnSafe(`${PYENV_INIT_INLINE} pyenv latest ${desiredVersion}`, { interactive: true });
2020

2121
if (status !== SpawnStatus.SUCCESS) {
2222
continue;
@@ -35,11 +35,11 @@ export class PythonVersionsParameter extends ArrayStatefulParameter<PyenvConfig,
3535

3636
override async addItem(version: string): Promise<void> {
3737
const $ = getPty();
38-
await $.spawn(`pyenv install ${version} -s`, { interactive: true });
38+
await $.spawn(`${PYENV_INIT_INLINE} pyenv install ${version} -s`, { interactive: true });
3939
}
4040

4141
override async removeItem(version: string): Promise<void> {
4242
const $ = getPty();
43-
await $.spawn(`pyenv uninstall ${version} -f`, { interactive: true });
43+
await $.spawn(`${PYENV_INIT_INLINE} pyenv uninstall ${version} -f`, { interactive: true });
4444
}
4545
}

0 commit comments

Comments
 (0)