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

pkgxdev / pkgx / 10740113580

06 Sep 2024 02:25PM UTC coverage: 92.174% (-0.1%) from 92.292%
10740113580

push

github

mxcl
Fix dev off when environment is not active

And also make sure to pass rm -f as some people alias rm to rm -i.

368 of 400 branches covered (92.0%)

2 of 2 new or added lines in 2 files covered. (100.0%)

1 existing line in 1 file now uncovered.

1434 of 1555 relevant lines covered (92.22%)

79.17 hits per line

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

85.47
/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(
21✔
22
        Deno.UnsafePointer.of(path),
21✔
23
        Deno.UnsafePointer.of(argv),
21✔
24
        Deno.UnsafePointer.of(envp)
21✔
25
      );
26
      if (!tries--) return errno;
×
27
      // 11 = EAGAIN = Try again
21✔
UNCOV
28
      if (errno == 11) continue;
×
29
      return errno;
21✔
30
    }
21✔
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✔
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
  }
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,148✔
107
      stringsLength += string.length * 4 + 1;
1,148✔
108
    }
1,148✔
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,148✔
121
      const result = ENCODER.encodeInto(
1,148✔
122
        string,
1,148✔
123
        stringsBuffer.subarray(start),
1,148✔
124
      );
125
      offset = start + result.written + 1; // Leave null byte
1,148✔
126
      pointerBuffer[index++] = basePointer + BigInt(start);
1,148✔
127
    }
1,148✔
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) {
9✔
135
  const filename = host().platform == 'darwin' ? '/usr/lib/libSystem.dylib' : 'libc.so.6'
×
136

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

149
  try {
9✔
150
    libc.symbols.execve(arg0, args, env)
9✔
151
    return libc.symbols.errno
9✔
152
  } finally {
9✔
153
    libc.close()
9✔
154
  }
9✔
155
}
9✔
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