|
1 | 1 | import { describe, it } from "node:test"; |
2 | 2 | import assert from "node:assert/strict"; |
3 | | -import { readFileSync } from "node:fs"; |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 5 | +import { createRequire } from "node:module"; |
| 6 | +import { tmpdir } from "node:os"; |
4 | 7 | import { fileURLToPath } from "node:url"; |
5 | 8 | import { dirname, join } from "node:path"; |
6 | 9 |
|
@@ -71,3 +74,133 @@ describe("npm platform dispatch", () => { |
71 | 74 | } |
72 | 75 | }); |
73 | 76 | }); |
| 77 | + |
| 78 | +// Regression tests for #974: installers that ignore `libc` (yarn classic) |
| 79 | +// install both the -gnu and -musl packages, so the wrapper must pick the |
| 80 | +// one that can run on this host and must never exit silently. |
| 81 | + |
| 82 | +const wrapperPath = join(__dirname, "socket-patch"); |
| 83 | +const wrapper = createRequire(import.meta.url)(wrapperPath); |
| 84 | + |
| 85 | +describe("npm wrapper libc selection (#974)", () => { |
| 86 | + it("does not run the CLI when required as a module", () => { |
| 87 | + assert.equal(typeof wrapper.orderCandidates, "function"); |
| 88 | + assert.equal(typeof wrapper.detectLibc, "function"); |
| 89 | + assert.equal(typeof wrapper.runFirstUsable, "function"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("detects musl when the Node runtime reports no glibc", () => { |
| 93 | + const libc = wrapper.detectLibc({ |
| 94 | + platform: "linux", |
| 95 | + getReport: () => ({ header: {} }), |
| 96 | + listDir: () => ["ld-musl-x86_64.so.1", "libc.musl-x86_64.so.1"], |
| 97 | + }); |
| 98 | + assert.equal(libc, "musl"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("detects glibc from the runtime report even if a musl loader exists", () => { |
| 102 | + const libc = wrapper.detectLibc({ |
| 103 | + platform: "linux", |
| 104 | + getReport: () => ({ header: { glibcVersionRuntime: "2.36" } }), |
| 105 | + listDir: () => ["ld-musl-x86_64.so.1"], |
| 106 | + }); |
| 107 | + assert.equal(libc, "glibc"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("returns null off Linux", () => { |
| 111 | + assert.equal( |
| 112 | + wrapper.detectLibc({ |
| 113 | + platform: "darwin", |
| 114 | + getReport: () => ({ header: {} }), |
| 115 | + listDir: () => [], |
| 116 | + }), |
| 117 | + null, |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + for (const key of ["linux x64", "linux arm64", "linux arm", "linux ia32"]) { |
| 122 | + it(`prefers the musl package on a musl host (${key})`, () => { |
| 123 | + const ordered = wrapper.orderCandidates(PLATFORMS[key], "musl"); |
| 124 | + assert.match(ordered[0], /-musl$/); |
| 125 | + assert.match(ordered[1], /-gnu$/); |
| 126 | + }); |
| 127 | + |
| 128 | + it(`keeps gnu first on a glibc host (${key})`, () => { |
| 129 | + assert.deepEqual(wrapper.orderCandidates(PLATFORMS[key], "glibc"), PLATFORMS[key]); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + it("falls back to the next binary when the first cannot be spawned", () => { |
| 134 | + const calls = []; |
| 135 | + const enoent = Object.assign(new Error("spawnSync gnu ENOENT"), { code: "ENOENT" }); |
| 136 | + const status = wrapper.runFirstUsable(["/gnu", "/musl"], ["--version"], { |
| 137 | + spawn: (bin) => { |
| 138 | + calls.push(bin); |
| 139 | + return bin === "/gnu" ? { status: null, error: enoent } : { status: 0 }; |
| 140 | + }, |
| 141 | + log: () => {}, |
| 142 | + }); |
| 143 | + assert.equal(status, 0); |
| 144 | + assert.deepEqual(calls, ["/gnu", "/musl"]); |
| 145 | + }); |
| 146 | + |
| 147 | + it("prints why it failed instead of exiting silently", () => { |
| 148 | + const logs = []; |
| 149 | + const enoent = Object.assign(new Error("spawnSync /gnu ENOENT"), { code: "ENOENT" }); |
| 150 | + const status = wrapper.runFirstUsable(["/gnu"], [], { |
| 151 | + spawn: () => ({ status: null, error: enoent }), |
| 152 | + log: (msg) => logs.push(msg), |
| 153 | + }); |
| 154 | + assert.equal(status, 1); |
| 155 | + assert.equal(logs.length, 1); |
| 156 | + assert.match(logs[0], /\/gnu/); |
| 157 | + assert.match(logs[0], /ENOENT/); |
| 158 | + }); |
| 159 | + |
| 160 | + it("propagates the exit status of a binary that ran", () => { |
| 161 | + const status = wrapper.runFirstUsable(["/gnu", "/musl"], [], { |
| 162 | + spawn: () => ({ status: 3 }), |
| 163 | + log: () => {}, |
| 164 | + }); |
| 165 | + assert.equal(status, 3); |
| 166 | + }); |
| 167 | + |
| 168 | + // End to end: a node_modules tree like yarn classic leaves on Alpine, |
| 169 | + // with both platform packages installed and the gnu binary unable to |
| 170 | + // start. The wrapper must run the musl binary instead of exiting 1 |
| 171 | + // with no output. |
| 172 | + it( |
| 173 | + "runs the musl binary when the gnu one cannot start (yarn classic layout)", |
| 174 | + { skip: process.platform !== "linux" || !PLATFORMS[`linux ${process.arch}`] }, |
| 175 | + () => { |
| 176 | + const root = mkdtempSync(join(tmpdir(), "sp-wrapper-")); |
| 177 | + try { |
| 178 | + const scope = join(root, "node_modules", "@socketsecurity"); |
| 179 | + const binDir = join(scope, "socket-patch", "bin"); |
| 180 | + mkdirSync(binDir, { recursive: true }); |
| 181 | + writeFileSync(join(binDir, "socket-patch"), readFileSync(wrapperPath)); |
| 182 | + for (const pkg of PLATFORMS[`linux ${process.arch}`]) { |
| 183 | + const dir = join(root, "node_modules", pkg); |
| 184 | + mkdirSync(dir, { recursive: true }); |
| 185 | + writeFileSync(join(dir, "package.json"), JSON.stringify({ name: pkg, version: "0.0.0" })); |
| 186 | + const exe = join(dir, "socket-patch"); |
| 187 | + if (pkg.endsWith("-gnu")) { |
| 188 | + // A binary whose ELF interpreter is missing fails exactly like |
| 189 | + // a glibc binary on musl: spawn reports ENOENT. |
| 190 | + writeFileSync(exe, "#!/nonexistent/ld-linux.so.2\n"); |
| 191 | + } else { |
| 192 | + writeFileSync(exe, "#!/bin/sh\necho \"musl-binary $*\"\n"); |
| 193 | + } |
| 194 | + chmodSync(exe, 0o755); |
| 195 | + } |
| 196 | + const result = spawnSync(process.execPath, [join(binDir, "socket-patch"), "--version"], { |
| 197 | + encoding: "utf8", |
| 198 | + }); |
| 199 | + assert.equal(result.status, 0, `stderr: ${result.stderr}`); |
| 200 | + assert.equal(result.stdout.trim(), "musl-binary --version"); |
| 201 | + } finally { |
| 202 | + rmSync(root, { recursive: true, force: true }); |
| 203 | + } |
| 204 | + }, |
| 205 | + ); |
| 206 | +}); |
0 commit comments