Skip to content

Commit 34522aa

Browse files
committed
Avoid env alias when capturing environment
Fixes #17
1 parent 6c96f84 commit 34522aa

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import defaultShell from 'default-shell';
55

66
const args = [
77
'-ilc',
8-
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit',
8+
// Use the command builtin to avoid shell aliases or functions named `env`.
9+
'echo -n "_SHELL_ENV_DELIMITER_"; command env; echo -n "_SHELL_ENV_DELIMITER_"; exit',
910
];
1011

1112
const env = {

test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
13
import test from 'ava';
24
import {shellEnv, shellEnvSync} from './index.js';
35

@@ -17,6 +19,32 @@ test('sync', t => {
1719
t.is(env.ZSH_TMUX_AUTOSTART, 'false');
1820
});
1921

22+
test('sync - ignores env alias', async t => {
23+
const temporaryDirectoryPrefix = path.join(process.cwd(), 'temporary-shell-env-');
24+
const temporaryDirectory = await fs.mkdtemp(temporaryDirectoryPrefix);
25+
26+
try {
27+
const bashConfigurationPath = path.join(temporaryDirectory, '.bashrc');
28+
const bashWrapperPath = path.join(temporaryDirectory, 'bash-wrapper.sh');
29+
const bashWrapperContents = [
30+
'#!/bin/sh',
31+
`HOME="${temporaryDirectory}"`,
32+
'export HOME',
33+
'exec /bin/bash "$@"',
34+
'',
35+
].join('\n');
36+
37+
await fs.writeFile(bashConfigurationPath, 'alias env="printf no"\n');
38+
await fs.writeFile(bashWrapperPath, bashWrapperContents);
39+
await fs.chmod(bashWrapperPath, 0o755);
40+
41+
const env = shellEnvSync(bashWrapperPath);
42+
t.is(env.HOME, temporaryDirectory);
43+
} finally {
44+
await fs.rm(temporaryDirectory, {recursive: true, force: true});
45+
}
46+
});
47+
2048
test('async - with custom shell', async t => {
2149
const shell = '/bin/bash';
2250
const env = await shellEnv(shell);

0 commit comments

Comments
 (0)