Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap emulator Node runtime discovery to favor local cache #2740

Merged
merged 12 commits into from
Oct 28, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes Cloud Function inspection when using standalone binary release (#2740)
36 changes: 26 additions & 10 deletions src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ export class FunctionsEmulator implements EmulatorInstance {
disabled_features: this.args.disabledRuntimeFeatures,
};
}
/**
* Returns a node major version ("10", "8") or null
* @param frb the current Functions Runtime Bundle
*/
getRequestedNodeRuntimeVersion(frb: FunctionsRuntimeBundle): string | undefined {
const pkg = require(path.join(frb.cwd, "package.json"));
return frb.nodeMajorVersion || (pkg.engines && pkg.engines.node);
}
/**
* Returns the path to a "node" executable to use.
* @param cwd the directory to checkout for a package.json file.
Expand Down Expand Up @@ -707,24 +715,24 @@ export class FunctionsEmulator implements EmulatorInstance {
// Will happen if we haven't asked about local version yet
}

// If the requested version is the same as the host, let's use that
if (requestedMajorVersion === hostMajorVersion) {
// If the requested version is already locally available, let's use that
if (requestedMajorVersion === localMajorVersion) {
this.logger.logLabeled(
"SUCCESS",
"functions",
`Using node@${requestedMajorVersion} from host.`
`Using node@${requestedMajorVersion} from local cache.`
);
return process.execPath;
return localNodePath;
}

// If the requested version is already locally available, let's use that
if (localMajorVersion === requestedMajorVersion) {
// If the requested version is the same as the host, let's use that
if (requestedMajorVersion === hostMajorVersion) {
this.logger.logLabeled(
"SUCCESS",
"functions",
`Using node@${requestedMajorVersion} from local cache.`
`Using node@${requestedMajorVersion} from host.`
);
return localNodePath;
return process.execPath;
}

// Otherwise we'll begin the conversational flow to install the correct version locally
Expand All @@ -750,8 +758,16 @@ export class FunctionsEmulator implements EmulatorInstance {
}

if (this.args.debugPort) {
const { host } = this.getInfo();
args.unshift(`--inspect=${host}:${this.args.debugPort}`);
if (process.env.FIREPIT_VERSION && process.execPath == opts.nodeBinary) {
const requestedMajorNodeVersion = this.getRequestedNodeRuntimeVersion(frb);
this.logger.log(
"WARN",
`To enable function inspection, please run "${process.execPath} is:npm i node@${requestedMajorNodeVersion} --save-dev" in your functions directory`
);
} else {
const { host } = this.getInfo();
args.unshift(`--inspect=${host}:${this.args.debugPort}`);
}
}

const childProcess = spawn(opts.nodeBinary, args, {
Expand Down