diff --git a/ci/build.ts b/ci/build.ts index 0628f492..fa7f01ed 100644 --- a/ci/build.ts +++ b/ci/build.ts @@ -260,35 +260,64 @@ class Builder { * Bundles the built code into a binary. */ private async binary(binaryName: string): Promise { - // Prepend code to the target which enables finding files within the binary. - const prependLoader = async (relativeFilePath: string): Promise => { + const prependCode = async (code: string, relativeFilePath: string): Promise => { const filePath = path.join(this.buildPath, relativeFilePath) - const shim = ` - if (!global.NBIN_LOADED) { - try { - const nbin = require("nbin"); - nbin.shimNativeFs("${this.buildPath}"); - global.NBIN_LOADED = true; - const path = require("path"); - const rg = require("vscode-ripgrep"); - rg.binaryRgPath = rg.rgPath; - rg.rgPath = path.join(require("os").tmpdir(), "code-server", path.basename(rg.binaryRgPath)); - } catch (error) { /* Not in the binary. */ } - } - ` const content = await fs.readFile(filePath, "utf8") - if (!content.startsWith(shim)) { - await fs.writeFile(filePath, shim + content) + if (!content.startsWith(code)) { + await fs.writeFile(filePath, code + content) } } + // Unpack binaries since we can't run them within the binary. + const unpack = ` + if (global.NBIN_LOADED) { + try { + const fs = require("fs-extra") + const rg = require("vscode-ripgrep") + const path = require("path") + const { logger, field } = require("@coder/logger") + + const unpackExecutables = async (filePath, destination) => { + logger.debug("unpacking executable", field("src", filePath), field("dest", destination)) + await fs.mkdirp(path.dirname(destination)) + if (filePath && !(await fs.pathExists(destination))) { + await fs.writeFile(destination, await fs.readFile(filePath)) + await fs.chmod(destination, "755") + } + } + + unpackExecutables(rg.binaryRgPath, rg.rgPath).catch((error) => console.warn(error)) + } catch (error) { + console.warn(error) + } + } + ` + + // Enable finding files within the binary. + const loader = ` + if (!global.NBIN_LOADED) { + try { + const nbin = require("nbin") + nbin.shimNativeFs("${this.buildPath}") + global.NBIN_LOADED = true + require("@coder/logger").logger.debug("shimmed file system at ${this.buildPath}") + const path = require("path") + const rg = require("vscode-ripgrep") + rg.binaryRgPath = rg.rgPath + rg.rgPath = path.join(require("os").tmpdir(), "code-server/binaries", path.basename(rg.binaryRgPath)) + } catch (error) { + // Most likely not in the binary. + } + } + ` + await this.task("Prepending nbin loader", () => { return Promise.all([ - prependLoader("out/node/entry.js"), - prependLoader("lib/vscode/out/vs/server/entry.js"), - prependLoader("lib/vscode/out/vs/server/fork.js"), - prependLoader("lib/vscode/out/bootstrap-fork.js"), - prependLoader("lib/vscode/extensions/node_modules/typescript/lib/tsserver.js"), + prependCode(loader, "out/node/entry.js"), + prependCode(loader, "lib/vscode/out/vs/server/entry.js"), + prependCode(loader + unpack, "lib/vscode/out/vs/server/fork.js"), + prependCode(loader, "lib/vscode/out/bootstrap-fork.js"), + prependCode(loader, "lib/vscode/extensions/node_modules/typescript/lib/tsserver.js"), ]) }) diff --git a/src/node/util.ts b/src/node/util.ts index 16b3940d..44e27be0 100644 --- a/src/node/util.ts +++ b/src/node/util.ts @@ -151,19 +151,6 @@ export const open = async (url: string): Promise => { }) } -/** - * Extract a file to the temporary directory and make it executable. This is - * required since we can't execute binaries stored within our binary. - */ -export const unpackExecutables = async (filePath: string): Promise => { - const destination = path.join(tmpdir, "binaries", path.basename(filePath)) - if (filePath && !(await util.promisify(fs.exists)(destination))) { - await fs.mkdirp(tmpdir) - await fs.writeFile(destination, await fs.readFile(filePath)) - await util.promisify(fs.chmod)(destination, "755") - } -} - /** * For iterating over an enum's values. */