From 556098ed50310965600aedd3053d35dc04aa2811 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Mon, 18 Oct 2021 13:13:57 -0400 Subject: [PATCH] Avoid hard-wired build-tree paths Instead, search for stuff up the directory tree, with the main functionality being to look for `Gulpfile.js` and assume the resulting directory is the root. (Unfortunatley, this is implemented twice, one in `scripts` and another in `src`. It's not possible to use a single implementation for both since that would require assuming a directory structure which this is intended to avoid.) Also, in `scripts/build/projects.js`, abstracdt common exec functionality into a local helper, and use full paths based on the above search instead of assuming relative paths assuming CWD being in the project root. --- scripts/build/findUpDir.js | 21 +++++++++++++++++++++ scripts/build/projects.js | 14 +++++++++++--- scripts/build/tests.js | 5 +++-- src/harness/findUpDir.ts | 21 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 scripts/build/findUpDir.js create mode 100644 src/harness/findUpDir.ts diff --git a/scripts/build/findUpDir.js b/scripts/build/findUpDir.js new file mode 100644 index 00000000000..fab301c6dcd --- /dev/null +++ b/scripts/build/findUpDir.js @@ -0,0 +1,21 @@ +const { join, resolve, dirname } = require("path"); +const { existsSync } = require("fs"); + +// search directories upward to avoid hard-wired paths based on the +// build tree (same as src/harness/findUpDir.ts) + +function findUpFile(name) { + let dir = __dirname; + while (true) { + const fullPath = join(dir, name); + if (existsSync(fullPath)) return fullPath; + const up = resolve(dir, ".."); + if (up === dir) return name; // it'll fail anyway + dir = up; + } +} +exports.findUpFile = findUpFile; + +const findUpRoot = () => + findUpRoot.cached || (findUpRoot.cached = dirname(findUpFile("Gulpfile.js"))); +exports.findUpRoot = findUpRoot; diff --git a/scripts/build/projects.js b/scripts/build/projects.js index 954f37af84e..38db816e23b 100644 --- a/scripts/build/projects.js +++ b/scripts/build/projects.js @@ -1,5 +1,7 @@ // @ts-check const { exec, Debouncer } = require("./utils"); +const { resolve } = require("path"); +const { findUpRoot } = require("./findUpDir"); class ProjectQueue { /** @@ -33,7 +35,13 @@ class ProjectQueue { } } -const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", ...(force ? ["--force"] : []), ...projects], { hidePrompt: true })); +const execTsc = (lkg, ...args) => + exec(process.execPath, + [resolve(findUpRoot(), lkg ? "./lib/tsc" : "./built/local/tsc"), + "-b", ...args], + { hidePrompt: true }) + +const projectBuilder = new ProjectQueue((projects, lkg, force) => execTsc(lkg, ...(force ? ["--force"] : []), ...projects)); /** * @param {string} project @@ -43,14 +51,14 @@ const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.e */ exports.buildProject = (project, { lkg, force } = {}) => projectBuilder.enqueue(project, { lkg, force }); -const projectCleaner = new ProjectQueue((projects, lkg) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", "--clean", ...projects], { hidePrompt: true })); +const projectCleaner = new ProjectQueue((projects, lkg) => execTsc(lkg, "--clean", ...projects)); /** * @param {string} project */ exports.cleanProject = (project) => projectCleaner.enqueue(project); -const projectWatcher = new ProjectQueue((projects) => exec(process.execPath, ["./lib/tsc", "-b", "--watch", ...projects], { hidePrompt: true })); +const projectWatcher = new ProjectQueue((projects) => execTsc(true, "--watch", ...projects)); /** * @param {string} project diff --git a/scripts/build/tests.js b/scripts/build/tests.js index 7fee32566dd..10c6db79cf1 100644 --- a/scripts/build/tests.js +++ b/scripts/build/tests.js @@ -9,6 +9,7 @@ const log = require("fancy-log"); const cmdLineOptions = require("./options"); const { CancellationToken } = require("prex"); const { exec } = require("./utils"); +const { findUpFile } = require("./findUpDir"); const mochaJs = require.resolve("mocha/bin/_mocha"); exports.localBaseline = "tests/baselines/local/"; @@ -73,11 +74,11 @@ async function runConsoleTests(runJs, defaultReporter, runInParallel, watchMode, /** @type {string[]} */ let args = []; - // timeout normally isn"t necessary but Travis-CI has been timing out on compiler baselines occasionally + // timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally // default timeout is 2sec which really should be enough, but maybe we just need a small amount longer if (!runInParallel) { args.push(mochaJs); - args.push("-R", "scripts/failed-tests"); + args.push("-R", findUpFile("scripts/failed-tests.js")); args.push("-O", '"reporter=' + reporter + (keepFailed ? ",keepFailed=true" : "") + '"'); if (tests) { args.push("-g", `"${tests}"`); diff --git a/src/harness/findUpDir.ts b/src/harness/findUpDir.ts new file mode 100644 index 00000000000..03b5ebdf285 --- /dev/null +++ b/src/harness/findUpDir.ts @@ -0,0 +1,21 @@ +namespace findUpDir { + import { join, resolve, dirname } from "path"; + import { existsSync } from "fs"; + + // search directories upward to avoid hard-wired paths based on the + // build tree (same as scripts/build/findUpDir.js) + + export function findUpFile(name: string) { + let dir = __dirname; + while (true) { + const fullPath = join(dir, name); + if (existsSync(fullPath)) return fullPath; + const up = resolve(dir, ".."); + if (up === dir) return name; // it'll fail anyway + dir = up; + } + } + + export const findUpRoot: { (): string; cached?: string; } = () => + findUpRoot.cached ||= dirname(findUpFile("Gulpfile.js")); +}