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

pkgxdev / pkgx / 10752333607

07 Sep 2024 03:00PM UTC coverage: 91.798% (-0.9%) from 92.731%
10752333607

push

github

mxcl
Try to use deno’s setup rather than our own

* Refs https://github.com/pkgxdev/pkgx/actions/runs/10740478635/job/29788773039#step:6:35
* Our linux aarch64 runner has no unzip, sigh

393 of 433 branches covered (90.76%)

1476 of 1603 relevant lines covered (92.08%)

163.38 hits per line

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

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

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

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

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

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

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

56
  throw new ProgrammerError(`execve (${errno})`)
31✔
57
}
24✔
58

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

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

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

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

95

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

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

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

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

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

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

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

157
export const _internals = {
6✔
158
  execve,
6✔
159
  getcwd: Path.cwd
6✔
160
}
6✔
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