-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of having a `runTSC.mts` script that calls into `tsc`, have ninja call `tsc` directly (or at least have node call the `tsc` entry point) and then redirect output to `parseOutput.mts`. This was done mainly due to ease of debugging. Previously when something went wrong it was non-trivial to find out the actual command line arguments that `runTSC.mts` used to call `tsc`. After this, it is much easier to see the arguments passed to `tsc` and to copy and paste the raw command into a terminal for additional debugging. Additionally, because we `cd` into the correct directory inside `runTSC.mts` it meant that `tsc` would refuse to write files outside of the `rootDir` and we couldn't use the `$builddir` for writing temporary timestamp files. The down side of this change is that we need to write the return code into the end of the stream and then parse it in `parseOutput.mts` to return the same code. This was quite tricky on Windows but it seems to be working now.
- Loading branch information
1 parent
572f6c3
commit 4a3ab47
Showing
7 changed files
with
214 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { createInterface } from "node:readline"; | ||
import { isAbsolute, relative } from "node:path"; | ||
import { realpath } from "node:fs/promises"; | ||
import { writeFileSync } from "node:fs"; | ||
import { parseArgs } from "node:util"; | ||
|
||
const cwd = process.cwd(); | ||
|
||
async function convertToPath(line: string): Promise<string> { | ||
// Look at the canonical path to resolve symlinks and find the original | ||
// path. This will allow us to handle dependencies across monorepos. | ||
let path = await realpath(line); | ||
|
||
// Relative paths can be returned immediately | ||
if (!isAbsolute(path)) { | ||
return path.replaceAll(" ", "\\_").replaceAll("$", "\\$"); | ||
} | ||
|
||
// Absolute paths are most likely references to things inside `node_modules`, | ||
// but could be absolute paths given by the user to something else. If the | ||
// path is within the current working directory, replace with the relative | ||
// path, otherwise keep it as absolute. | ||
{ | ||
const relativeAttempt = relative(cwd, path); | ||
if ( | ||
relativeAttempt && | ||
!relativeAttempt.startsWith("..") && | ||
!isAbsolute(relativeAttempt) | ||
) { | ||
path = relativeAttempt; | ||
} | ||
} | ||
return path | ||
.replaceAll("\\", "/") | ||
.replaceAll(" ", "\\ ") | ||
.replaceAll("$", "\\$"); | ||
} | ||
|
||
async function main() { | ||
const { | ||
positionals: [out], | ||
values: { touch }, | ||
} = parseArgs({ | ||
allowPositionals: true, | ||
options: { touch: { type: "boolean" } }, | ||
}); | ||
|
||
const lines: string[] = []; | ||
for await (const line of createInterface({ | ||
input: process.stdin, | ||
})) { | ||
lines.push(line); | ||
} | ||
|
||
// Assume the last line passes in the return code of `tsc` | ||
const rc = Number.parseInt(lines.pop() ?? "1"); | ||
if (rc === 0) { | ||
const paths = await Promise.all( | ||
lines.filter((l) => l !== "").map(convertToPath), | ||
); | ||
|
||
// The ".depfile" suffix must match what's used in `node.ts` | ||
writeFileSync(out + ".depfile", out + ": " + paths.join(" ")); | ||
if (touch) { | ||
writeFileSync(out, ""); | ||
} | ||
} else { | ||
// Drop the `--listFiles` content printed at the end until we get to the | ||
// error messages. Assume that all the paths end in `ts` (e.g. `.d.ts` | ||
// or `.d.mts`) and keep searching until we find something that doesn't. | ||
let i = lines.length - 1; | ||
for (; i >= 0; --i) { | ||
if (!lines[i].endsWith("ts")) { | ||
break; | ||
} | ||
} | ||
console.log(lines.slice(0, i + 1).join("\n")); | ||
} | ||
process.exit(rc); | ||
} | ||
|
||
await main(); |
Oops, something went wrong.