Skip to content

Commit

Permalink
child process should ignore files that are specified in the ignore array
Browse files Browse the repository at this point in the history
  • Loading branch information
jasong689 committed May 18, 2023
1 parent 8604c92 commit 143497d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 56 deletions.
64 changes: 24 additions & 40 deletions spec/Supervisor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChildProcess, spawn } from "child_process";
import * as path from "path";
import { range } from "lodash";
import * as path from "path";

const childExit = (child: ChildProcess) => {
return new Promise<void>((resolve) => {
Expand All @@ -9,36 +9,32 @@ const childExit = (child: ChildProcess) => {
expect(code).toEqual(0);
});
});
}
};

test("it proxies ipc messages", async () => {
const binPath = path.join(__dirname, "../pkg/wds.bin.js");
const scriptPath = path.join(__dirname, "fixtures/src/add.ts");

const child = spawn(
"node",
[binPath, scriptPath],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
env: process.env,
}
);
const child = spawn("node", [binPath, scriptPath], {
stdio: ["inherit", "inherit", "inherit", "ipc"],
env: process.env,
});

const childHasBooted = new Promise<void>((resolve) => {
const handler = () => {
resolve();
child.off("message", handler)
child.off("message", handler);
};
child.on("message", handler)
})
child.on("message", handler);
});
await childHasBooted;

const messagesToChild = range(0, 3);
const messagesFromChild: Array<number> = [];

const promise = new Promise<void>((resolve) => {
child.on("message", (message: any) => {
messagesFromChild.push(message)
messagesFromChild.push(message);

if (messagesFromChild.length === messagesToChild.length) {
resolve();
Expand All @@ -55,20 +51,16 @@ test("it proxies ipc messages", async () => {
await promise;

expect(messagesFromChild).toEqual([1, 2, 3]);
})
});

test("it doesn't setup ipc if it wasn't setup with ipc itself", async () => {
const binPath = path.join(__dirname, "../pkg/wds.bin.js");
const scriptPath = path.join(__dirname, "fixtures/src/no-ipc.ts");

const child = spawn(
"node",
[binPath, scriptPath],
{
stdio: ["inherit", "inherit", "inherit"],
env: process.env,
}
);
const child = spawn("node", [binPath, scriptPath], {
stdio: ["inherit", "inherit", "inherit"],
env: process.env,
});

await childExit(child);
});
Expand All @@ -77,13 +69,9 @@ test("it inherits stdin if WDS was started without terminal commands", async ()
const binPath = path.join(__dirname, "../pkg/wds.bin.js");
const scriptPath = path.join(__dirname, "fixtures/src/echo.ts");

const child = spawn(
"node",
[binPath, scriptPath],
{
env: process.env,
}
);
const child = spawn("node", [binPath, scriptPath], {
env: process.env,
});

let output = "";

Expand All @@ -92,7 +80,7 @@ test("it inherits stdin if WDS was started without terminal commands", async ()

child.stdout.on("data", (data) => {
output += data;
})
});

await childExit(child);
expect(output).toEqual("test");
Expand All @@ -102,13 +90,9 @@ test("it doesn't have any stdin if wds is started with terminal commands", async
const binPath = path.join(__dirname, "../pkg/wds.bin.js");
const scriptPath = path.join(__dirname, "fixtures/src/echo.ts");

const child = spawn(
"node",
[binPath, scriptPath, "--commands"],
{
env: process.env,
}
);
const child = spawn("node", [binPath, scriptPath, "--commands"], {
env: process.env,
});

let output = "";

Expand All @@ -117,9 +101,9 @@ test("it doesn't have any stdin if wds is started with terminal commands", async

child.stdout.on("data", (data) => {
output += data;
})
});

await childExit(child);

expect(output).toEqual("");
})
});
1 change: 1 addition & 0 deletions src/Supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class Supervisor extends EventEmitter {
...process.env,
WDS_SOCKET_PATH: this.socketPath,
WDS_EXTENSIONS: this.project.config.extensions.join(","),
WDS_IGNORE: JSON.stringify(this.project.config.ignore),
},
stdio: stdio,
});
Expand Down
33 changes: 22 additions & 11 deletions src/SwcCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import { log, projectConfig } from "./utils";
// https://esbuild.github.io/api/#resolve-extensions
const DefaultExtensions = [".tsx", ".ts", ".jsx", ".mjs", ".cjs", ".js"];

export class MissingDestinationError extends Error {}
export class MissingDestinationError extends Error {
ignoredFile: boolean;

constructor(error: { message: string; ignoredFile?: boolean }) {
super(error.message);
this.ignoredFile = !!error.ignoredFile;
}
}

const SWC_DEFAULTS: Config = {
env: {
Expand Down Expand Up @@ -178,9 +185,7 @@ export class SwcCompiler implements Compiler {
// TODO: Use the config
const { root, fileNames, swcConfig } = await this.getModule(filename);

await this.reportErrors(async () => {
await Promise.all(fileNames.map((filename) => this.buildFile(filename, root, swcConfig)));
});
await this.reportErrors(Promise.allSettled(fileNames.map((filename) => this.buildFile(filename, root, swcConfig))));

log.debug("started build", {
root,
Expand All @@ -190,11 +195,11 @@ export class SwcCompiler implements Compiler {
});
}

private async reportErrors<T>(run: () => Promise<T>) {
try {
return await run();
} catch (error) {
log.error(error);
private async reportErrors<T>(results: Promise<PromiseSettledResult<T>[]>) {
for (const result of await results) {
if (result.status === "rejected") {
log.error(result.reason);
}
}
}

Expand All @@ -203,9 +208,15 @@ export class SwcCompiler implements Compiler {

// TODO: Understand cases in which the file destination could be missing
if (ignorePattern) {
return `File ${filename} is imported but not being built because it is explicitly ignored in the wds project config. It is being ignored by the provided glob pattern '${ignorePattern}', remove this pattern from the project config or don't import this file to fix.`;
return {
message: `File ${filename} is imported but not being built because it is explicitly ignored in the wds project config. It is being ignored by the provided glob pattern '${ignorePattern}', remove this pattern from the project config or don't import this file to fix.`,
ignoredFile: true,
};
} else {
return `Built output for file ${filename} not found. Is it outside the project directory, or has it failed to build?`;
return {
message: `Built output for file ${filename} not found. Is it outside the project directory, or has it failed to build?`,
ignoredFile: false,
};
}
}

Expand Down
1 change: 1 addition & 0 deletions src/bench/scripts/ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// this is ignored
16 changes: 12 additions & 4 deletions src/child-process-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ const notifyParentProcessOfRequire = (filename: string) => {

if (!workerData || !(workerData as SyncWorkerData).isWDSSyncWorker) {
const worker = new SyncWorker(path.join(__dirname, "child-process-ipc-worker.js"));
const paths: Record<string, string> = {};
const paths: Record<
string,
| string
| {
ignored: boolean;
}
> = {};

// Compile a given file by sending it into our async-to-sync wrapper worker js file
// The leader process returns us a list of all the files it just compiled, so that we don't have to pay the IPC boundary cost for each file after this one
Expand All @@ -63,9 +69,11 @@ if (!workerData || !(workerData as SyncWorkerData).isWDSSyncWorker) {
for (const extension of process.env["WDS_EXTENSIONS"]!.split(",")) {
require.extensions[extension] = (module: any, filename: string) => {
const compiledFilename = compile(filename);
const content = fs.readFileSync(compiledFilename, "utf8");
notifyParentProcessOfRequire(filename);
module._compile(content, filename);
if (typeof compiledFilename === "string" || !compiledFilename.ignored) {
const content = fs.readFileSync(compiledFilename, "utf8");
notifyParentProcessOfRequire(filename);
module._compile(content, filename);
}
};
}
}
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { hideBin } from "yargs/helpers";
import type { RunOptions } from "./Options";
import { Project } from "./Project";
import { Supervisor } from "./Supervisor";
import { SwcCompiler } from "./SwcCompiler";
import { MissingDestinationError, SwcCompiler } from "./SwcCompiler";
import { MiniServer } from "./mini-server";
import { log, projectConfig } from "./utils";

Expand Down Expand Up @@ -93,6 +93,14 @@ const startIPCServer = async (socketPath: string, project: Project) => {
return await project.compiler.fileGroup(filename);
} catch (error) {
log.error(`Error compiling file ${filename}:`, error);

if (error instanceof MissingDestinationError && error.ignoredFile) {
return {
[filename]: {
ignored: true,
},
};
}
}
};

Expand Down
3 changes: 3 additions & 0 deletions wds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
ignore: ["src/bench/scripts/ignored.ts"],
};

0 comments on commit 143497d

Please sign in to comment.