Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify line reader to always consume stderr and mark stderr in callback #560

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/builders/buildAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@
const buildProcess = cancelToken.adapt(
exec("./gradlew", gradleArgs, {
cwd: androidSourceDir,
env: { ...buildOptions.env, JAVA_HOME, ANDROID_HOME },

Check warning on line 117 in packages/vscode-extension/src/builders/buildAndroid.ts

View workflow job for this annotation

GitHub Actions / check

Object Literal Property name `JAVA_HOME` must match one of the following formats: camelCase

Check warning on line 117 in packages/vscode-extension/src/builders/buildAndroid.ts

View workflow job for this annotation

GitHub Actions / check

Object Literal Property name `ANDROID_HOME` must match one of the following formats: camelCase
buffer: false,
})
);
const buildAndroidProgressProcessor = new BuildAndroidProgressProcessor(progressListener);
outputChannel.clear();
lineReader(buildProcess, true).onLineRead((line) => {
lineReader(buildProcess).onLineRead((line) => {
outputChannel.appendLine(line);
buildAndroidProgressProcessor.processLine(line);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/builders/buildIOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

function buildProject(
UDID: string,

Check warning on line 40 in packages/vscode-extension/src/builders/buildIOS.ts

View workflow job for this annotation

GitHub Actions / check

Parameter name `UDID` must match one of the following formats: camelCase
xcodeProject: IOSProjectInfo,
buildDir: string,
scheme: string,
Expand All @@ -62,7 +62,7 @@
return exec("xcodebuild", xcodebuildArgs, {
env: {
...getLaunchConfiguration().env,
RCT_NO_LAUNCH_PACKAGER: "true",

Check warning on line 65 in packages/vscode-extension/src/builders/buildIOS.ts

View workflow job for this annotation

GitHub Actions / check

Object Literal Property name `RCT_NO_LAUNCH_PACKAGER` must match one of the following formats: camelCase
},
cwd: buildDir,
buffer: false,
Expand Down Expand Up @@ -112,7 +112,7 @@
Logger.debug(`Xcode build will use "${scheme}" scheme`);

let platformName: string | undefined;
const buildProcess = withTemporarySimulator(deviceInfo, (UDID) => {

Check warning on line 115 in packages/vscode-extension/src/builders/buildIOS.ts

View workflow job for this annotation

GitHub Actions / check

Parameter name `UDID` must match one of the following formats: camelCase
const process = cancelToken.adapt(
buildProject(
UDID,
Expand All @@ -126,7 +126,7 @@

const buildIOSProgressProcessor = new BuildIOSProgressProcessor(progressListener);
outputChannel.clear();
lineReader(process, true).onLineRead((line) => {
lineReader(process).onLineRead((line) => {
outputChannel.appendLine(line);
buildIOSProgressProcessor.processLine(line);
// Xcode can sometimes escape `=` with a backslash or put the value in quotes
Expand Down Expand Up @@ -228,7 +228,7 @@
const simulators = await listSimulators(SimulatorDeviceSet.Default);
const removedSimulators = simulators
.filter(({ name }) => name.startsWith(TEMP_SIMULATOR_NAME_PREFIX))
.map(({ UDID }) => removeIosSimulator(UDID, SimulatorDeviceSet.Default));

Check warning on line 231 in packages/vscode-extension/src/builders/buildIOS.ts

View workflow job for this annotation

GitHub Actions / check

Parameter name `UDID` must match one of the following formats: camelCase

await Promise.allSettled(removedSimulators);
}
9 changes: 7 additions & 2 deletions packages/vscode-extension/src/devices/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export class Preview implements Disposable {

const streamURLRegex = /(http:\/\/[^ ]*stream\.mjpeg)/;

lineReader(subprocess).onLineRead((line) => {
lineReader(subprocess).onLineRead((line, stderr) => {
if (stderr) {
Logger.warn("sim-server err:", line);
return;
}
balins marked this conversation as resolved.
Show resolved Hide resolved

const match = line.match(streamURLRegex);

if (match) {
Expand All @@ -46,7 +51,7 @@ export class Preview implements Disposable {
this.streamURL = match[1];
resolve(this.streamURL);
}
Logger.debug("Preview server:", line);
Logger.debug("sim-server out:", line);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/project/metro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class Metro implements Disposable {
reject(new Error("Metro exited but did not start server successfully."));
});

lineReader(bundlerProcess, true).onLineRead((line) => {
lineReader(bundlerProcess).onLineRead((line) => {
try {
const event = JSON.parse(line) as MetroEvent;
if (event.type === "bundle_transform_progressed") {
Expand Down
20 changes: 10 additions & 10 deletions packages/vscode-extension/src/utilities/subprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Logger } from "../Logger";
import execa, { ExecaChildProcess } from "execa";
import readline from "readline";
import { Platform } from "./platform";
import { inc } from "semver";

export type ChildProcess = ExecaChildProcess<string>;

Expand Down Expand Up @@ -62,26 +63,25 @@ function overrideEnv<T extends execa.Options>(options?: T): T | undefined {
* When using this methid, the subprocess should be started with buffer: false option
* as there's no need for allocating memory for the output that's going to be very long.
*/
export function lineReader(childProcess: ExecaChildProcess<string>, includeStderr = false) {
const input = childProcess.stdout;
if (!input) {
throw new Error("Child process has no stdout");
export function lineReader(childProcess: ExecaChildProcess<string>) {
if (!childProcess.stdout) {
throw new Error("Child process doesn't have stdout");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible for the process to not have stdout but have stderr? If so and if we decide that stderr should be passed on by default, maybe it would be useful to allow line reader to exist without stdout?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only possible if the process is configured to pipe stdout. We typically use lineReader to read from stdout hence it crashes if stdout is not present. If we ever need to reac stderr and not stdout we may need to change this function to support such usecase.

}
const stdoutReader = readline.createInterface({
input,
input: childProcess.stdout,
terminal: false,
});
let stderrReader = null;
if (includeStderr && childProcess.stderr) {
let stderrReader: readline.Interface | null = null;
if (childProcess.stderr) {
stderrReader = readline.createInterface({
input: childProcess.stderr!,
input: childProcess.stderr,
terminal: false,
});
}
return {
onLineRead: (callback: (line: string) => void) => {
onLineRead: (callback: (line: string, stderr?: boolean) => void) => {
stdoutReader.on("line", callback);
stderrReader?.on("line", callback);
stderrReader?.on("line", (line) => callback(line, true));
},
};
}
Expand Down
Loading