Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 20 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@types/ws": "8.5.4",
"@typescript-eslint/eslint-plugin": "5.48.2",
"@typescript-eslint/parser": "5.48.2",
"cross-spawn": "7.0.3",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You moved this to dependencies, not devDependencies 🙃

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

No?

Copy link
Copy Markdown

@lishaduck lishaduck Sep 25, 2024

Choose a reason for hiding this comment

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

I was tired. I meant that you moved it to devDependencies, rather than removing it, and this didn't modify any tests.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Yes, the point of the PR was to remove it from the actual package. The tests could be dealt with later (if at all, depending on what shenanigans I pulled there).

"elm-tooling": "1.12.0",
"esbuild": "0.15.18",
"esbuild-register": "3.4.2",
Expand All @@ -31,7 +32,6 @@
},
"dependencies": {
"chokidar": "^3.5.3",
"cross-spawn": "^7.0.3",
"tiny-decoders": "^7.0.1",
"ws": "^8.12.0"
}
Expand Down
52 changes: 35 additions & 17 deletions src/Spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,51 @@ export function spawn(command: Command): {
killed = true;
};

const promise = (
actualSpawn: typeof childProcess.spawn
): Promise<SpawnResult> =>
new Promise((resolve) => {
const promise = (windowsPreviousAttemptError?: Error): Promise<SpawnResult> =>
new Promise((resolve, reject) => {
// istanbul ignore if
if (killed) {
resolve({ tag: "Killed", command });
return;
}

const child = actualSpawn(command.command, command.args, {
...command.options,
cwd: command.options.cwd.absolutePath,
});
const child = childProcess.spawn(
// On Windows, executing just `elm` works for `elm.exe`, but not for
// `elm.cmd` – then we need to explicitly say `.cmd`. When installing
// Elm via npm or elm-tooling a `.cmd` file is used (pointing to the
// `.exe` somewhere else). So we try first with the original command,
// and then with `.cmd` appended.
// istanbul ignore next
windowsPreviousAttemptError === undefined
? command.command
: `${command.command}.cmd`,
command.args,
{
...command.options,
cwd: command.options.cwd.absolutePath,
}
);

const stdout: Array<Buffer> = [];
const stderr: Array<Buffer> = [];

// Having a test for `CommandNotFoundError` is good, but the rest is not
// possible to test. It’s the least messy to ignore coverage for the whole thing.
// istanbul ignore next
child.on("error", (error: Error & { code?: string }) => {
resolve(
// istanbul ignore next
error.code === "ENOENT"
? { tag: "CommandNotFoundError", command }
: { tag: "OtherSpawnError", error, command }
);
if (error.code === "ENOENT") {
if (IS_WINDOWS && windowsPreviousAttemptError === undefined) {
promise(error).then(resolve).catch(reject);
} else {
resolve({ tag: "CommandNotFoundError", command });
}
} else {
resolve({
tag: "OtherSpawnError",
error: windowsPreviousAttemptError ?? error,
command,
});
}
});

let stdinWriteError:
Expand Down Expand Up @@ -167,9 +187,7 @@ export function spawn(command: Command): {

// istanbul ignore next
return {
promise: IS_WINDOWS
? import("cross-spawn").then((crossSpawn) => promise(crossSpawn.spawn))
: promise(childProcess.spawn),
promise: promise(),
kill: () => {
kill();
},
Expand Down