Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell): add $PWD and $OLDPWD #6397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .yarn/versions/b2c3eb43.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/core": minor
"@yarnpkg/shell": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
8 changes: 7 additions & 1 deletion packages/yarnpkg-shell/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ const BUILTINS = new Map<string, ShellBuiltin>([
if (!stat.isDirectory())
throw new ShellError(`cd: not a directory: ${target}`);

state.environment.PWD = npath.fromPortablePath(resolvedTarget);
state.environment.OLDPWD = npath.fromPortablePath(state.cwd);
state.cwd = resolvedTarget;

return 0;
}],

Expand Down Expand Up @@ -1065,7 +1068,10 @@ export async function execute(command: string, args: Array<string> = [], {
glob,
}, {
cwd,
environment: normalizedEnv,
environment: Object.assign({}, normalizedEnv, {
PWD: cwd,
OLDPWD: ``,
}),
exitCode: null,
procedures: {},
stdin,
Expand Down
30 changes: 25 additions & 5 deletions packages/yarnpkg-shell/tests/shell.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {xfs, ppath, PortablePath} from '@yarnpkg/fslib';
import {execute, UserOptions} from '@yarnpkg/shell';
import {PassThrough} from 'stream';
import stripAnsi from 'strip-ansi';
import {setTimeout} from 'timers/promises';
import {xfs, npath, ppath, PortablePath} from '@yarnpkg/fslib';
import {execute, UserOptions} from '@yarnpkg/shell';
import {PassThrough} from 'stream';
import stripAnsi from 'strip-ansi';
import {setTimeout} from 'timers/promises';

const isNotWin32 = process.platform !== `win32`;

Expand Down Expand Up @@ -576,6 +576,26 @@ describe(`Shell`, () => {
stdout: `""\n`,
});
});

it(`should expose the cwd via $PWD`, async () => {
await xfs.mktempPromise(async tmpDir => {
await expectResult(bufferResult(
`cd ${tmpDir}; echo $PWD`,
), {
stdout: `${npath.fromPortablePath(tmpDir)}\n`,
});
});
});

it(`should expose the previous cwd via $OLDPWD`, async () => {
await xfs.mktempPromise(async tmpDir => {
await expectResult(bufferResult(
`cd ${tmpDir}; echo $OLDPWD`,
), {
stdout: `${process.cwd()}\n`,
});
});
});
});

describe(`Setting variables`, () => {
Expand Down
Loading