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

pkgxdev / pkgx / 11956491937

23 Oct 2024 08:08PM UTC coverage: 92.492% (+0.1%) from 92.353%
11956491937

push

github

mxcl
Bump denoland/setup-deno from 1 to 2

Bumps [denoland/setup-deno](https://github.com/denoland/setup-deno) from 1 to 2.
- [Release notes](https://github.com/denoland/setup-deno/releases)
- [Commits](https://github.com/denoland/setup-deno/compare/v1...v2)

---
updated-dependencies:
- dependency-name: denoland/setup-deno
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

398 of 428 branches covered (92.99%)

1376 of 1490 relevant lines covered (92.35%)

167.09 hits per line

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

96.39
/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
7
  /// but otherwise the user has set stuff so we should use it
8
  for (const [key, value] of Object.entries(Deno.env.toObject())) {
24✔
9
    env[key] ??= value
2,202✔
10
  }
2,202✔
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(
42✔
22
        Deno.UnsafePointer.of(path),
42✔
23
        Deno.UnsafePointer.of(argv),
42✔
24
        Deno.UnsafePointer.of(envp)
42✔
25
      );
26
      if (!tries--) return errno;
×
27
      // 11 = EAGAIN = Try again
28
      if (errno == 11) continue;
×
29
      return errno;
42✔
30
    }
42✔
31
  })();
24✔
32

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

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

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

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

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

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

96

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

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

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

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

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

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

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

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