Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[Full changelog](https://github.com/mozilla/glean.js/compare/v1.1.0...main)

* [#1500](https://github.com/mozilla/glean.js/pull/1500): BUGFIX: Update how we invoke CLI python script to fix `npm run glean` behavior on Windows.
* [#1457](https://github.com/mozilla/glean.js/pull/1457): Update `ts-node` to 10.8.0 to resolve ESM issues when running tests inside of `webext` sample project.
* [#1452](https://github.com/mozilla/glean.js/pull/1452): Remove `glean.restarted` trailing events from events list.
* [#1450](https://github.com/mozilla/glean.js/pull/1450): Update `ts-node` to 10.8.0 to resolve ESM issues when running tests.
Expand Down
35 changes: 33 additions & 2 deletions glean/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import * as exec from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { argv, platform } from "process";
import { promisify } from "util";

Expand Down Expand Up @@ -185,10 +186,40 @@ async function runGlean(parserArgs: string[]) {
const spinner = getStartedSpinner();
const pythonBin = path.join(getPythonVenvBinariesPath(VIRTUAL_ENVIRONMENT_DIR), getSystemPythonBinName());
const isOnlineArg = process.env.OFFLINE ? "offline" : "online";
const cmd = `${pythonBin} -c "${PYTHON_SCRIPT}" ${isOnlineArg} glean_parser ${GLEAN_PARSER_VERSION} ${parserArgs.join(" ")}`;

// Trying to pass PYTHON_SCRIPT as a string in the command line arguments
// causes issues on Windows machines. The issue exists because you cannot
// pass a multi-line script using the `-c` argument without manually formatting
// your script with `\n` characters.
//
// To workaround this, we can write the script to a file inside of the OS tmpdir
// and execute it from there. Then once we are finished with the script, we can
// remove the entire directory.
let tmpDir = "";
const appPrefix = "glean.js";
const scriptName = "script.py";
const tempDirectory = os.tmpdir();
try {
tmpDir = fs.mkdtempSync(path.join(tempDirectory, appPrefix));
fs.writeFileSync(path.join(tmpDir, scriptName), PYTHON_SCRIPT);
} catch (error) {
log(
LOG_TAG,
["Unable to write utility script to tmp directory.\n", error],
LoggingLevel.Error
);
process.exit(1);
}

const cmd = `${pythonBin} ${tmpDir}/${scriptName} ${isOnlineArg} glean_parser ${GLEAN_PARSER_VERSION} ${parserArgs.join(" ")}`;

const {err, stdout, stderr} = await new Promise<{err: exec.ExecException | null, stdout: string, stderr: string}>(resolve => {
exec.exec(cmd, (err, stdout, stderr) => resolve({err, stdout, stderr}));
exec.exec(cmd, (err, stdout, stderr) => {
resolve({err, stdout, stderr});

// Remove the temp directory now that we are finished with it.
fs.rmSync(tmpDir, { recursive: true });
});
});

stopSpinner(spinner);
Expand Down