Skip to content

Commit

Permalink
Piping into Code fails if data writes delayed (fix #155341) (#156973)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero authored and aiday-mar committed Aug 3, 2022
1 parent 99e5bf9 commit d3817be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/vs/code/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export async function main(argv: string[]): Promise<any> {

// returns a file path where stdin input is written into (write in progress).
try {
readFromStdin(stdinFilePath, !!verbose); // throws error if file can not be written
await readFromStdin(stdinFilePath, !!verbose); // throws error if file can not be written

// Make sure to open tmp file
addArg(argv, stdinFilePath);
Expand Down
15 changes: 11 additions & 4 deletions src/vs/platform/environment/node/stdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,33 @@ export function getStdinFilePath(): string {
}

export async function readFromStdin(targetPath: string, verbose: boolean): Promise<void> {
let encoding = await resolveTerminalEncoding(verbose);

const iconv = await import('@vscode/iconv-lite-umd');
let [encoding, iconv] = await Promise.all([
resolveTerminalEncoding(verbose), // respect terminal encoding when piping into file
import('@vscode/iconv-lite-umd'), // lazy load encoding module for usage
Promises.appendFile(targetPath, '') // make sure file exists right away (https://github.com/microsoft/vscode/issues/155341)
]);

if (!iconv.encodingExists(encoding)) {
console.log(`Unsupported terminal encoding: ${encoding}, falling back to UTF-8.`);
encoding = 'utf8';
}

// Pipe into tmp file using terminals encoding
// Use a `Queue` to be able to use `appendFile`
// which helps file watchers to be aware of the
// changes because each append closes the underlying
// file descriptor.
// (https://github.com/microsoft/vscode/issues/148952)
const decoder = iconv.getDecoder(encoding);

const appendFileQueue = new Queue();

const decoder = iconv.getDecoder(encoding);

process.stdin.on('data', chunk => {
const chunkStr = decoder.write(chunk);
appendFileQueue.queue(() => Promises.appendFile(targetPath, chunkStr));
});

process.stdin.on('end', () => {
const end = decoder.end();
if (typeof end === 'string') {
Expand Down
9 changes: 5 additions & 4 deletions src/vs/server/node/server.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const cliRemoteAuthority = process.env['VSCODE_CLI_AUTHORITY'] as string;
const cliStdInFilePath = process.env['VSCODE_STDIN_FILE_PATH'] as string;


export function main(desc: ProductDescription, args: string[]): void {
export async function main(desc: ProductDescription, args: string[]): Promise<void> {
if (!cliPipe && !cliCommand) {
console.log('Command is only available in WSL or inside a Visual Studio Code terminal.');
return;
Expand Down Expand Up @@ -184,7 +184,7 @@ export function main(desc: ProductDescription, args: string[]): void {
let stdinFilePath = cliStdInFilePath;
if (!stdinFilePath) {
stdinFilePath = getStdinFilePath();
readFromStdin(stdinFilePath, verbose); // throws error if file can not be written
await readFromStdin(stdinFilePath, verbose); // throws error if file can not be written
}

// Make sure to open tmp file
Expand Down Expand Up @@ -460,5 +460,6 @@ function mapFileToRemoteUri(uri: string): string {
}

const [, , productName, version, commit, executableName, ...remainingArgs] = process.argv;
main({ productName, version, commit, executableName }, remainingArgs);

main({ productName, version, commit, executableName }, remainingArgs).then(null, err => {
console.error(err.message || err.stack || err);
});

0 comments on commit d3817be

Please sign in to comment.