Skip to content

Commit

Permalink
Swap emulator Node runtime discovery to favor local cache (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeisgoat committed Oct 28, 2020
1 parent 77a02b0 commit 6943586
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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

0 comments on commit 6943586

Please sign in to comment.