• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkgxdev / pkgx / 10854498834

13 Sep 2024 06:47PM UTC coverage: 91.505% (-1.2%) from 92.709%
10854498834

push

github

mxcl
fix regression in construct-env.ts

closes #1036

373 of 407 branches covered (91.65%)

4 of 4 new or added lines in 1 file covered. (100.0%)

20 existing lines in 4 files now uncovered.

1469 of 1606 relevant lines covered (91.47%)

81.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.78
/src/utils/execve.ts
1
import { ProgrammerError } from "./error.ts"
3✔
2
import { utils, Path, PkgxError } from "pkgx"
3✔
3
const { host } = utils
3✔
4

5
export default function({cmd: args, env}: {cmd: string[], env: Record<string, string>}): never {
3✔
6
  /// we need to forward any other env, some vars like HOME are super important
12✔
7
  /// but otherwise the user has set stuff so we should use it
12✔
8
  for (const [key, value] of Object.entries(Deno.env.toObject())) {
12✔
9
    env[key] ??= value
1,101✔
10
  }
1,101✔
11

12
  find_in_PATH(args, env.PATH, Path.abs(env.HOME))
12✔
13

14
  const path = cstr(args[0])
12✔
15
  const argv = new CStringArray(args)
12✔
16
  const envp = new CStringArray(Object.entries(env).map(([key, value]) => `${key}=${value}`))
12✔
17

18
  const errno = (() => {
12✔
19
    let tries = 10;
21✔
20
    while (true) {
21✔
21
      const errno = _internals.execve(
31✔
22
        Deno.UnsafePointer.of(path),
31✔
23
        Deno.UnsafePointer.of(argv),
31✔
24
        Deno.UnsafePointer.of(envp)
31✔
25
      );
26
      if (!tries--) return errno;
×
27
      // 11 = EAGAIN = Try again
31✔
28
      if (errno == 11) continue;
31✔
29
      return errno;
40✔
30
    }
40✔
31
  })();
12✔
32

33
  switch (errno) {
12✔
34
    case 2:   //ENOENT:
12✔
35
    case 316: //FIXME ALERT! ALERT! BUG! SOMETHING IS WRONG WITH OUR USE OR ERRNO AND THIS SOMETIMES RESULTS, USUALLY ON MACOS :/
12✔
36
      // yes: strange behavior from execve here indeed
16✔
37
      if (parse_Path(args[0])?.exists()) {
16✔
38
        throw new Deno.errors.PermissionDenied()
18✔
39
      } else {
18✔
40
        throw new Deno.errors.NotFound()
18✔
41
      }
18✔
42
    case 13:
12✔
43
      throw new Deno.errors.PermissionDenied()
13✔
UNCOV
44
    case 63: //ENAMETOOLONG:
×
UNCOV
45
    case 7:  //E2BIG:
×
UNCOV
46
    case 14: //EFAULT:
×
UNCOV
47
    case 5:  //EIO:
×
UNCOV
48
    case 62: //ELOOP:
×
UNCOV
49
    case 8:  //ENOEXEC:
×
UNCOV
50
    case 12: //ENOMEM:
×
UNCOV
51
    case 20: //ENOTDIR:
×
UNCOV
52
    case 26: //ETXTBSY:
×
UNCOV
53
      throw new PkgxError(`execve (${errno})`)
×
54
  }
12✔
55

56
  throw new ProgrammerError(`execve (${errno})`)
16✔
57
}
12✔
58

59
export function parse_Path(input: string) {
3✔
60
  const p = Path.abs(input)
7✔
61
  if (p) return p
7✔
62
  try {
9✔
63
    return _internals.getcwd().join(input)
9✔
64
  } catch {
7✔
65
    return
8✔
66
  }
8✔
67
}
7✔
68

69
function find_in_PATH(cmd: string[], PATH: string | undefined, HOME: Path | undefined) {
12✔
70
  if (cmd[0].startsWith("/")) {
12✔
71
    return
15✔
72
  }
15✔
73
  if (cmd[0].includes("/")) {
12✔
74
    cmd[0] = _internals.getcwd().join(cmd[0]).string
13✔
75
    return
13✔
76
  }
13✔
77

78
  PATH ??= "/usr/bin:/bin"  // see manpage for execvp(3)
17✔
79

80
  for (const part of PATH.split(':')) {
12✔
81
    const path = (() => {
88✔
82
      if (part == '.') return _internals.getcwd()
164✔
83
      if (part == '~') return HOME
164✔
84
      if (part.startsWith('~/')) return HOME?.join(part.slice(2))
164✔
85
      //FIXME: not handled: ~user/...
237✔
86
      return new Path(part)
237✔
87
    })()?.join(cmd[0])
88✔
88
    if (path?.isExecutableFile()) {
88✔
89
      cmd[0] = path.string
89✔
90
      return
89✔
91
    }
89✔
92
  }
88✔
93
}
12✔
94

95

96
////// COPY PASTA
1✔
97
// actually minor mods to add trailing null
1✔
98
// https://github.com/aapoalas/libclang_deno/blob/main/lib/utils.ts
1✔
99

100
const ENCODER = new TextEncoder();
3✔
101

102
export class CStringArray extends Uint8Array {
3✔
103
  constructor(strings: string[]) {
3✔
104
    let stringsLength = 0;
21✔
105
    for (const string of strings) {
21✔
106
      // maximum bytes for a utf8 string is 4×length
1,162✔
107
      stringsLength += string.length * 4 + 1;
1,162✔
108
    }
1,162✔
109
    super(8 * (strings.length + 1) + stringsLength);
21✔
110
    const pointerBuffer = new BigUint64Array(this.buffer, 0, strings.length + 1);
21✔
111
    const stringsBuffer = new Uint8Array(this.buffer).subarray(
21✔
112
      (strings.length + 1) * 8,
21✔
113
    );
114
    const basePointer = BigInt(
21✔
115
      Deno.UnsafePointer.value(Deno.UnsafePointer.of(stringsBuffer)),
21✔
116
    );
117
    let index = 0;
21✔
118
    let offset = 0;
21✔
119
    for (const string of strings) {
21✔
120
      const start = offset;
1,162✔
121
      const result = ENCODER.encodeInto(
1,162✔
122
        string,
1,162✔
123
        stringsBuffer.subarray(start),
1,162✔
124
      );
125
      offset = start + result.written + 1; // Leave null byte
1,162✔
126
      pointerBuffer[index++] = basePointer + BigInt(start);
1,162✔
127
    }
1,162✔
128
  }
21✔
129
}
3✔
130

131
export const cstr = (string: string): Uint8Array =>
3✔
132
  ENCODER.encode(`${string}\0`);
3✔
133

134
function execve(arg0: Deno.PointerValue, args: Deno.PointerValue, env: Deno.PointerValue) {
10✔
135
  const filename = host().platform == 'darwin' ? '/usr/lib/libSystem.dylib' : 'libc.so.6'
×
136

137
  const libc = Deno.dlopen(
10✔
138
    filename, {
10✔
139
      execve: {
10✔
140
        parameters: ["pointer", "pointer", "pointer"],
50✔
141
        result: "i32"
10✔
142
      },
10✔
143
      errno: {
10✔
144
        type: "i32"
10✔
145
      }
10✔
146
    }
10✔
147
  )
148

149
  try {
10✔
150
    libc.symbols.execve(arg0, args, env)
10✔
151
    return libc.symbols.errno
10✔
152
  } finally {
10✔
153
    libc.close()
10✔
154
  }
10✔
155
}
10✔
156

157
export const _internals = {
3✔
158
  execve,
3✔
159
  getcwd: Path.cwd
3✔
160
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc