fauxnix-cli 0.13.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -151,7 +151,8 @@ const cd = (args) => {
151
151
  if (wordToString(operandWords[0]) === '-') {
152
152
  return [
153
153
  "if (-not $env:FAUXNIX_OLDPWD) { [Console]::Error.WriteLine('bash: cd: OLDPWD not set'); $script:fx_exit = 1 }",
154
- 'else { try { Set-Location -LiteralPath $env:FAUXNIX_OLDPWD } catch { [Console]::Error.WriteLine("bash: cd: " + $env:FAUXNIX_OLDPWD + ": No such file or directory"); $script:fx_exit = 1 } }',
154
+ // bash echoes the new cwd for `cd -` (and CDPATH hits); $OLDPWD/$PWD agree (#223).
155
+ 'else { try { Set-Location -LiteralPath $env:FAUXNIX_OLDPWD; fx-posix((Get-Location).Path) } catch { [Console]::Error.WriteLine("bash: cd: " + $env:FAUXNIX_OLDPWD + ": No such file or directory"); $script:fx_exit = 1 } }',
155
156
  ].join('\n');
156
157
  }
157
158
  return [
@@ -168,7 +169,8 @@ const pwd = (args) => {
168
169
  if (operandWords.length > 0)
169
170
  return psErr('pwd', "extra operand '" + wordToString(operandWords[0]) + "'. Try 'pwd --help'.");
170
171
  // -L is the default. CommandSpec rejects -P because junctions make it distinct.
171
- return "(Get-Location).Path.Replace('\\', '/')";
172
+ // bash renders the cwd POSIX-form (/d/foo); $PWD and `cd -` must agree (#223).
173
+ return 'fx-posix((Get-Location).Path)';
172
174
  };
173
175
  /* ------------------------------------------------------------------ */
174
176
  /* export / unset / env / printenv */
@@ -1537,12 +1539,12 @@ const FX_ENVGET_FN = [
1537
1539
  " if (@($env:FAUXNIX_UNSETVARS -split ';' | Where-Object { $_ -ceq $n }).Count -gt 0) { return '' }",
1538
1540
  " if (@($env:FAUXNIX_SETVARS -split ';' | Where-Object { $_ -ceq $n }).Count -gt 0) { return (fx-envexplicit $n) }",
1539
1541
  " if ($n -ceq 'HOME') { return [string]$HOME }",
1540
- " if ($n -ceq 'PWD') { return [string]$PWD.Path }",
1542
+ " if ($n -ceq 'PWD') { return fx-posix([string]$PWD.Path) }",
1541
1543
  " if ($n -ceq 'USER' -or $n -ceq 'LOGNAME') { return [string]$env:USERNAME }",
1542
1544
  " if ($n -ceq 'PATH') { return [string]$env:PATH }",
1543
1545
  " if ($n -ceq 'SHELL') { return 'powershell' }",
1544
1546
  " if ($n -ceq 'TERM') { return 'xterm-256color' }",
1545
- " if ($n -ceq 'OLDPWD') { return [string]$env:FAUXNIX_OLDPWD }",
1547
+ " if ($n -ceq 'OLDPWD') { return fx-posix([string]$env:FAUXNIX_OLDPWD) }",
1546
1548
  " if ($n -ceq '?') { return [string]$fx_prev }",
1547
1549
  " if ($n -ceq '$') { return [string]$PID }",
1548
1550
  " if ($n -ceq 'HOSTNAME') { return [string]$env:COMPUTERNAME }",
@@ -2427,9 +2429,9 @@ const DIRS_STACK_PS = [
2427
2429
  "if ($env:FAUXNIX_DIRSTACK) { $fx_st = @($env:FAUXNIX_DIRSTACK -split ';' | Where-Object { $_ -ne '' }) }",
2428
2430
  ].join('\n');
2429
2431
  const DIRS_PRINT_PS = [
2430
- '$fx_l = @((Get-Location).Path)',
2431
- 'foreach ($fx_s in $fx_st) { $fx_l += $fx_s }',
2432
- "($fx_l -join ' ').Replace('\\', '/')",
2432
+ '$fx_l = @(fx-posix((Get-Location).Path))',
2433
+ 'foreach ($fx_s in $fx_st) { $fx_l += fx-posix($fx_s) }',
2434
+ "($fx_l -join ' ')",
2433
2435
  ].join('\n');
2434
2436
  const dirs = () => {
2435
2437
  return [DIRS_STACK_PS, DIRS_PRINT_PS].join('\n');
package/dist/errors.js CHANGED
@@ -122,5 +122,11 @@ export function normalizeStderr(stderr) {
122
122
  }
123
123
  return line;
124
124
  });
125
- return out.join('\n').replace(/\n{3,}/g, '\n\n').trim();
125
+ // GNU stderr lines are newline-terminated; bash guarantees a separating
126
+ // newline when 2>&1 merges stderr into a following stdout chunk (#222).
127
+ // Leading blank lines are PS boot noise; collapse runs of blanks only.
128
+ const joined = out.join('\n').replace(/\n{3,}/g, '\n\n');
129
+ if (!joined.trim())
130
+ return '';
131
+ return joined.replace(/^\n+/, '').replace(/[\r\n]+$/, '\n');
126
132
  }
@@ -0,0 +1 @@
1
+ export declare function runFacade(argv: string[]): Promise<number>;
package/dist/facade.js ADDED
@@ -0,0 +1,106 @@
1
+ import { parseCommand } from './parser.js';
2
+ import { translateCommandList, EXECUTE_TRANSLATION } from './translator.js';
3
+ import { FauxnixSession } from './executor.js';
4
+ import { packageVersion } from './version.js';
5
+ /**
6
+ * Experimental native-shell facade (RFC: docs/rfc-bash-facade.md).
7
+ *
8
+ * Implements the bash process contract so a harness can point its built-in
9
+ * Bash tool at fauxnix instead of a real bash.exe:
10
+ * - one-shot: facade -c "<script>" [name arg1 ...] ($0/$1 from operands)
11
+ * - persistent: stdin blocks wrapped in <bash-input>...</bash-input>;
12
+ * each block runs in one resident session (cwd/env persist)
13
+ * and is answered with its output plus a <bash-exit>N line.
14
+ *
15
+ * Disclosure rule from the RFC: --version always identifies fauxnix.
16
+ */
17
+ const MARKER_RE = /<bash-input>([\s\S]*?)<\/bash-input>/;
18
+ const VERSION_LINE = 'GNU bash, version 5.2.0(1)-release (fauxnix facade ' + packageVersion + ')\n';
19
+ async function runScript(session, script) {
20
+ const plans = translateCommandList(parseCommand(script), EXECUTE_TRANSLATION);
21
+ const result = await session.run(plans);
22
+ return {
23
+ stdout: result.stdout,
24
+ stderr: result.stderr,
25
+ exitCode: result.exitCode,
26
+ };
27
+ }
28
+ function bashQuote(word) {
29
+ return "'" + word.replaceAll("'", `'\\''`) + "'";
30
+ }
31
+ async function runOneShot(session, script, positionals) {
32
+ // bash: trailing operands set $0, then $1.. via `set --`.
33
+ process.env['FAUXNIX_ARG0'] = positionals[0] ?? 'bash';
34
+ if (positionals.length > 1) {
35
+ const r = await runScript(session, 'set -- ' + positionals.slice(1).map(bashQuote).join(' '));
36
+ if (r.exitCode !== 0)
37
+ return r.exitCode;
38
+ }
39
+ const r = await runScript(session, script);
40
+ if (r.stdout)
41
+ process.stdout.write(r.stdout);
42
+ if (r.stderr)
43
+ process.stderr.write(r.stderr);
44
+ return r.exitCode;
45
+ }
46
+ async function runMarkerSession(session) {
47
+ await session.prewarm();
48
+ return new Promise((resolve) => {
49
+ let buf = '';
50
+ let queue = Promise.resolve();
51
+ process.stdin.setEncoding('utf8');
52
+ process.stdin.on('data', (chunk) => {
53
+ buf += chunk;
54
+ let m;
55
+ while ((m = MARKER_RE.exec(buf))) {
56
+ buf = buf.slice(m.index + m[0].length);
57
+ const cmd = m[1];
58
+ // serialize markers: a session executes one command at a time
59
+ queue = queue.then(async () => {
60
+ try {
61
+ const r = await runScript(session, cmd);
62
+ if (r.stdout)
63
+ process.stdout.write(r.stdout);
64
+ if (r.stderr)
65
+ process.stderr.write(r.stderr);
66
+ process.stdout.write(`<bash-exit>${r.exitCode}</bash-exit>\n`);
67
+ }
68
+ catch (err) {
69
+ process.stderr.write(`fauxnix: facade: ${err.message}\n`);
70
+ process.stdout.write('<bash-exit>127</bash-exit>\n');
71
+ }
72
+ });
73
+ }
74
+ });
75
+ process.stdin.on('end', () => {
76
+ queue.then(() => resolve(0));
77
+ });
78
+ });
79
+ }
80
+ export async function runFacade(argv) {
81
+ const session = new FauxnixSession();
82
+ try {
83
+ if (argv[0] === '--version') {
84
+ process.stdout.write(VERSION_LINE);
85
+ return 0;
86
+ }
87
+ if (argv[0] === '-c' || argv[0] === '-e') {
88
+ const [script, ...positionals] = argv.slice(1);
89
+ if (!script) {
90
+ process.stderr.write('bash: -c: option requires an argument\n');
91
+ return 2;
92
+ }
93
+ return await runOneShot(session, script, positionals);
94
+ }
95
+ return await runMarkerSession(session);
96
+ }
97
+ catch (err) {
98
+ // bash -c exits 2 on syntax errors; 127 covers the not-found family
99
+ const msg = err.message ?? String(err);
100
+ process.stderr.write('bash: -c: ' + msg + '\n');
101
+ return msg.includes('not found') ? 127 : 2;
102
+ }
103
+ finally {
104
+ await session.dispose();
105
+ }
106
+ }
package/dist/ps-host.js CHANGED
@@ -16,7 +16,14 @@ class NativeStderrSpoolError extends Error {
16
16
  this.name = 'NativeStderrSpoolError';
17
17
  }
18
18
  }
19
- const READY_TIMEOUT_MS = 30_000;
19
+ // First-ready-line budget. 30s covers every dev machine measured; contended CI
20
+ // runners cold-booting powershell.exe under full test parallelism have exceeded it,
21
+ // so FAUXNIX_HOST_READY_TIMEOUT_MS (milliseconds, minimum 1000) widens the budget
22
+ // without changing the default.
23
+ const READY_TIMEOUT_MS = (() => {
24
+ const raw = Number(process.env.FAUXNIX_HOST_READY_TIMEOUT_MS);
25
+ return Number.isFinite(raw) && raw >= 1000 ? raw : 30_000;
26
+ })();
20
27
  export const DEFAULT_STDOUT_LIMIT = 8_388_608;
21
28
  export const DEFAULT_STDERR_LIMIT = 1_048_576;
22
29
  export function encodeHostRequest(id, script, env, opts) {
@@ -279,7 +279,7 @@ export function varExpr(name, index, param, length = false, extra) {
279
279
  case 'HOME':
280
280
  return '$HOME';
281
281
  case 'PWD':
282
- return '$PWD.Path';
282
+ return 'fx-posix([string]$PWD.Path)';
283
283
  case 'USER':
284
284
  case 'LOGNAME':
285
285
  return '$env:USERNAME';
@@ -290,7 +290,7 @@ export function varExpr(name, index, param, length = false, extra) {
290
290
  case 'TERM':
291
291
  return "'xterm-256color'";
292
292
  case 'OLDPWD':
293
- return '$env:FAUXNIX_OLDPWD';
293
+ return 'fx-posix([string]$env:FAUXNIX_OLDPWD)';
294
294
  case '?':
295
295
  return '[string]$fx_prev';
296
296
  case '$':
@@ -1275,6 +1275,7 @@ const WRAP_HELPER_ORDER = [
1275
1275
  'fx-winargv',
1276
1276
  'fx-cmdargv',
1277
1277
  'fx-native',
1278
+ 'fx-posix',
1278
1279
  ];
1279
1280
  const WRAP_HELPER_DEPS = {
1280
1281
  'fx-readlines': [],
@@ -1301,6 +1302,7 @@ const WRAP_HELPER_DEPS = {
1301
1302
  'fx-winargv': [],
1302
1303
  'fx-cmdargv': ['fx-winargv'],
1303
1304
  'fx-native': ['fx-cmdargv'],
1305
+ 'fx-posix': [],
1304
1306
  };
1305
1307
  /** Helpers the body calls that wrapScript still has to emit (not already defined there). */
1306
1308
  function wrapHelpersNeeded(body) {
@@ -1538,12 +1540,12 @@ export function wrapScript(body, opts = {}) {
1538
1540
  ' if ($fx_pair.Substring(0, $fx_eq) -ceq $n) { return (fx-svdec $fx_pair.Substring($fx_eq + 1)) }',
1539
1541
  ' }',
1540
1542
  " if ($n -ceq 'HOME') { return [string]$HOME }",
1541
- " if ($n -ceq 'PWD') { return [string]$PWD.Path }",
1543
+ " if ($n -ceq 'PWD') { return fx-posix([string]$PWD.Path) }",
1542
1544
  " if ($n -ceq 'USER' -or $n -ceq 'LOGNAME') { return [string]$env:USERNAME }",
1543
1545
  " if ($n -ceq 'PATH') { return [string]$env:PATH }",
1544
1546
  " if ($n -ceq 'SHELL') { return 'powershell' }",
1545
1547
  " if ($n -ceq 'TERM') { return 'xterm-256color' }",
1546
- " if ($n -ceq 'OLDPWD') { return $(if ($env:FAUXNIX_OLDPWD) { [string]$env:FAUXNIX_OLDPWD } else { $null }) }",
1548
+ " if ($n -ceq 'OLDPWD') { return $(if ($env:FAUXNIX_OLDPWD) { fx-posix([string]$env:FAUXNIX_OLDPWD) } else { $null }) }",
1547
1549
  " if ($n -ceq 'HOSTNAME') { return [string]$env:COMPUTERNAME }",
1548
1550
  ' $ev = Get-ChildItem Env: | Where-Object { $_.Name -ceq $n } | Select-Object -First 1',
1549
1551
  ' if ($ev) { return [string]$ev.Value }',
@@ -1655,6 +1657,13 @@ export function wrapScript(body, opts = {}) {
1655
1657
  ' return $false',
1656
1658
  '}',
1657
1659
  ],
1660
+ 'fx-posix': [
1661
+ 'function fx-posix($p) {',
1662
+ ' $p = ([string]$p).Replace(\'\\\', \'/\')',
1663
+ " if ($p -match '^[A-Za-z]:') { return '/' + $p.Substring(0, 1).ToLower() + $p.Substring(2) }",
1664
+ ' return $p',
1665
+ '}',
1666
+ ],
1658
1667
  'fx-subst': [
1659
1668
  'function fx-subst($s, $pat, $repl, $all) {',
1660
1669
  ' if ($null -eq $s) { return \'\' }',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fauxnix-cli",
3
- "version": "0.13.0",
3
+ "version": "1.0.0",
4
4
  "description": "Fauxnix — run Linux-style commands on Windows via deterministic PowerShell translation. No VM, no WSL. MCP server + CLI for AI agents.",
5
5
  "type": "module",
6
6
  "bin": {